OpenAIDemo / app.py
mik-chinaamc's picture
Update app.py
182360b
raw
history blame
1.79 kB
import os
import openai
import gradio as gr
openai.api_key = os.environ['OPENAI_API_KEY']
prompt = "这个小程序是为华夏基金制作的OpenAI连通性概念验证。"
def openai_create(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0,
max_tokens=500,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
return response.choices[0].text
SysIns = [{"role": "system", "content": "You are a professional stock analyst."}]
SysIns.append({"role": "system", "content": "让我们讲中文吧。"})
messages = SysIns
def openai_chatcreate(prompt):
messages.append({"role":"user","content":prompt})
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=messages
)
messages.append(response.choices[0]["message"])
return response.choices[0]["message"]["content"]
def chatgpt_chatclone(input, history):
history = history or []
s = list(sum(history,()))
s.append(input)
output = openai_chatcreate(input)
history.append((input, output))
return history, history
def chatgpt_clear(input, history):
messages = []
messages = SysIns
return "", [], []
blocks = gr.Blocks()
with blocks:
chatbot = gr.Chatbot(label="OpenAI 分析师")
message = gr.Textbox(placeholder=prompt, label="输入您的查询。OpenAI已被指示扮演专业股票分析师的角色做出回应。您每次输入的上下文都会影响OpenAI的回应。按Clear可以重设对话。")
state = gr.State()
submit = gr.Button("Send")
submit.click(fn=chatgpt_chatclone, inputs=[message, state], outputs=[chatbot, state])
clear = gr.Button("Clear")
clear.click(fn=chatgpt_clear, inputs=[message, state], outputs=[message, chatbot, state])
blocks.launch(debug=False)