找回密码
  注册[Register]
查看: 383|回复: 8

chatGPT网页版|服务器部署|支持公网访问|自定义接口

[复制链接]
发表于 2023-3-15 07:36 | 显示全部楼层 |阅读模式
禁止求评分、诱导评分、互刷评分、互刷悬赏值,违规者封号处理。
禁止发布推广、邀请码、邀请链接、二维码或者有利益相关的任何推广行为。
所有非原创软件请发布在【精品软件区】,发帖必须按照本版块版规格式发帖。

ChatGPT-lthero
github项目链接:https://github.com/lthero-big/ChatGPT-lthero

在线体验:
https://huggingface.co/spaces/lthero/ChatGPT-lthero
【内置API-KEY】,可以克隆此项目,运行速度更快

安装依赖
pip install -r requirements.txt

运行代码
python index.py
或者尝试
python3 index.py


使用
  • 必须在代码中设置userApiKey,或者在运行后的网页setting输入userApiKey
  • 支持自行动态调整topP,temperature等参数
  • 回复支持Markdown语法
  • 左侧的LastResponse部分支持markdown格式的代码显示,仅显示ChatGPT回复的最后一条信息。


部署
  • 默认只能本机访问:程序最后一条代码blocks.launch(server_name="127.0.0.1", server_port=7860, debug=False)。
  • 如果需要部署在服务器,并设置公网访问,要将上述代码127.0.0.1修改成0.0.0.0,并需要自行放开端口7860号
  • 如果没有自己服务器,blocks.launch(server_name="127.0.0.1", server_port=7860, debug=True),程序会创建一个免费公网访问的链接,但必须让程序持续运行


参数说明

openAPI

是chatGPT访问链接默认值:https://api.openai.com/v1/chat/completions如果想部署在自己国内服务器上,需要使用国内能访问的api
具体方案请查看:https://github.com/noobnooc/noobnooc/discussions/9



userApiKey
是chatGPT的api,每个账号有18美元额度默认值:NULL在openai申请api
具体查看:https://platform.openai.com/account/api-keys



temperature
温度较高,则生成的文本将更加多样化,但存在语法错误和产生无意义内容的风险更高。温度较低,意味着模型会更加保守,并坚持从其训练数据中学到的内容,导致预测结果更可预测但创造性较差。
默认值:1



topP
Top P可以生成与低温相似的准确性和正确性的文本,但具有更多变化和创造力。如果Top P值设置得太高,则也存在生成无意义或不相关文本的风险。默认值:0.5


presencePenalty
presencePenalty是一些自然语言处理模型中使用的参数,用于惩罚已经在对话中提到过的单词或短语的重复出现。这种惩罚鼓励模型生成更多样化和多变化的回应。如果presencePenalty参数变得很高,它将强烈阻止已经在对话中提到过的单词或短语的重复出现。这可能会导致模型生成更多样化和多变化的回应,但如果模型无法正确地将先前提到的信息纳入其输出中,则可能会导致不太连贯或相关的回应。
默认值:0



frequencyPenalty
用于惩罚生成的回应中过于频繁出现的单词或短语。这个惩罚的目的是鼓励模型使用更广泛的词汇并产生更多样化和有趣的回应。如果将frequencyPenalty设置得太高,则可能会导致不太连贯或相关的回应
默认值:0



maxTokens
单次对话最大token量默认值:500

效果图
200815ga4m4ztxrt616gmp.png

代码


[Python] 纯文本查看 复制代码
import openai
import gradio as gr
import requests
import markdown
 
# input your openaiAPI here
userApi = "sk-ssssss"
prompt = "The following is a conversation with an AI assistant"
LastResponse = ""
ResponseInHtml = ""
temperature = 1
topP = 0.5
presencePenalty = 0
frequencyPenalty = 0
maxTokens = 500
openAPI = 'https://api.openai.com/v1/chat/completions'
 
 
def openai_create(user_prompt):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer %s' % userApi,
    }
 
    json_data = {
        'model': 'gpt-3.5-turbo',
        'messages': [
            {
                'role': 'user',
                'content': str(user_prompt),
            },
        ],
        'max_tokens': maxTokens,
        'temperature': temperature,
        'presence_penalty': presencePenalty,
        'top_p': topP,
        'frequency_penalty': frequencyPenalty,
    }
    response = requests.post(openAPI, headers=headers, json=json_data)
    # we have to remove the first two '\n'
    return parse_text(str(response.json()["choices"][0]["message"]["content"]).strip('\n'))
 
 
def conversation_history(userInput, history):
    global LastResponse, ResponseInHtml
    history = history or []
    s = list(sum(history, ()))
    s.append(userInput)
    inp = ' '.join(s)
    output = openai_create(inp)
    history.append((userInput, output))
    LastResponse = output
    # ResponseInHtml = markdown.markdown(LastResponse, extensions=[
    #     'markdown.extensions.extra',
    #     'markdown.extensions.codehilite',
    #     'markdown.extensions.toc',
    # ])
    return history, history
 
 
def parse_text(text):
    lines = text.split("\n")
    lines = [line for line in lines if line != ""]
    count = 0
    for i, line in enumerate(lines):
        if "```" in line:
            count += 1
            items = line.split('`')
            if count % 2 == 1:
                lines[i] = f'<pre><code class="language-{items[-1]}">'
            else:
                lines[i] = f'<br></code></pre>'
        else:
            if i > 0:
                if count % 2 == 1:
                    line = line.replace("`", "\`")
                    line = line.replace("<", "<")
                    line = line.replace(">", ">")
                    line = line.replace(" ", " ")
                    line = line.replace("*", "*")
                    line = line.replace("_", "_")
                    line = line.replace("-", "-")
                    line = line.replace(".", ".")
                    line = line.replace("!", "!")
                    line = line.replace("(", "(")
                    line = line.replace(")", ")")
                    line = line.replace("$", "$")
                lines[i] = "<br>" + line
    text = "".join(lines)
    return text
 
 
def upDateMD():
    return LastResponse
 
 
def upDateHtml():
    return ResponseInHtml
 
 
def Clear():
    global LastResponse
    LastResponse = ""
    return LastResponse
 
 
def changeAPI(setuserApi):
    global userApi
    userApi = setuserApi
    openai.api_key = userApi
 
 
def changeTemper(setTemperature):
    global temperature
    temperature = setTemperature
 
 
def changeTop(setTop):
    global topP
    topP = setTop
 
 
def changeFrequencyPenalty(setFrequencyPenalty):
    global frequencyPenalty
    frequencyPenalty = setFrequencyPenalty
 
 
def changePresencePenalty(setPresencePenalty):
    global presencePenalty
    presencePenalty = setPresencePenalty
 
 
blocks = gr.Blocks()
with blocks:
    gr.Markdown(
        """
        ### Please Enter your own OpenAI API Key in the Setting to remove the 500 token limit.
        """
    )
    with gr.Tab("Chat"):
        with gr.Row():
            with gr.Column(scale=1, variant="compact"):
                gr.Markdown(
                    """
                    # LastResponse:
                    """
                )
                md = gr.Markdown(
                    """
                    """
                )
            with gr.Column(scale=1, variant="panel"):
                gr.Markdown(
                    """
                    ## ChatBot:
                    """
                )
                with gr.Row():
                    with gr.Column(scale=0.85):
                        chatbot = gr.Chatbot(elem_id="chatbot", )
                        state = gr.State()
 
                with gr.Row():
                    with gr.Column(scale=0.85):
                        message = gr.Textbox(show_label=False, placeholder=prompt).style(container=False)
                    with gr.Column(scale=0.15, min_width=0):
                        btn = gr.Button(value="ClearAll")
    with gr.Tab("Setting"):
        with gr.Row():
            api = gr.Textbox(elem_id="Input", show_label=False, placeholder="Enter your own OpenAI API Key to remove "
                                                                            "the 500 token "
                                                                            "limit.").style(container=False)
        with gr.Row():
            TemperSlider = gr.Slider(0, 1, step=0.01, label="temperature", info="If the temperature is low, the model "
                                                                                "will probably output the most correct "
                                                                                "text, but rather boring, with small "
                                                                                "variation.If the temperature is high, "
                                                                                "the generated text will be more "
                                                                                "diverse, but there is a higher "
                                                                                "possibility of grammar mistakes and "
                                                                                "generation of nonsense.",
                                     value=temperature)
        with gr.Row():
            topPSlider = gr.Slider(0, 1, step=0.01, label="Top_p", info="Top P can generate text with accuracy and "
                                                                        "correctness", value=topP)
        with gr.Row():
            presencePenaltySlider = gr.Slider(0, 1, step=0.01, label="presencePenalty", info="presencePenalty",
                                              value=presencePenalty)
        with gr.Row():
            frequencyPenaltySlider = gr.Slider(0, 1, step=0.01, label="frequencyPenaltySlider", info="frequencyPenalty",
                                               value=frequencyPenalty)
 
    message.submit(conversation_history, inputs=[message, state], outputs=[chatbot, state], show_progress=True)
    message.submit(lambda: "", None, message)
    # output response content into markdown
    chatbot.change(upDateMD, None, md)
    # output response content into Html 
    # chatbot.change(upDateHtml,None,html)
    # clear textBox
    btn.click(lambda: "", None, state)
    btn.click(Clear, None, md)
    btn.click(lambda: "", None, chatbot)
    # TemperSlider
    TemperSlider.change(changeTemper, inputs=TemperSlider)
    # topPSlider
    topPSlider.change(changeTop, inputs=topPSlider)
    # presencePenaltySlider
    presencePenaltySlider.change(changePresencePenalty, inputs=presencePenaltySlider)
    # frequencyPenaltySlider
    frequencyPenaltySlider.change(changeFrequencyPenalty, inputs=frequencyPenaltySlider)
    # api
    api.change(changeAPI, inputs=api)
 
blocks.launch(server_name="0.0.0.0", server_port=7860, debug=False)

如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2023-3-15 07:36 | 显示全部楼层

支持楼主,谢谢分享。
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2023-3-15 07:49 | 显示全部楼层
谢谢大牛
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2023-3-15 07:49 | 显示全部楼层
先收藏了,万一用到呢
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2023-3-15 07:54 | 显示全部楼层
谢谢@Thanks!
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2023-3-15 08:01 | 显示全部楼层
多谢楼主分享
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2023-3-15 08:05 | 显示全部楼层
谢谢大佬
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2023-3-15 08:13 | 显示全部楼层
感谢楼主分享
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

发表于 2023-3-15 09:06 | 显示全部楼层
6666
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心值】和【牛币】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 有用 没用

使用道具 举报

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

RSS订阅|手机版|小黑屋|大牛论坛 |我的广告

GMT+8, 2024-5-6 09:11 , Processed in 0.049070 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表