OpenAIDemo / app.py
mik-chinaamc's picture
Update app.py
710f3d3
raw
history blame
1.61 kB
import os
import openai
import gradio as gr
openai.api_key = os.environ['OPENAI_API_KEY']
prompt = "This is a concept verification of applying GPT-3 model for China AMC"
def openai_create(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0,
max_tokens=200,
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})
print(messages)
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))
print(history)
return history, history
def chatgpt_clear(input, history):
messages = SysIns
return "", [], []
blocks = gr.Blocks()
with blocks:
chatbot = gr.Chatbot()
message = gr.Textbox(placeholder=prompt)
state = gr.State()
clear = gr.Button("Clear")
clear.click(fn=chatgpt_clear, inputs=[message, state], outputs=[message, chatbot, state])
submit = gr.Button("Send")
submit.click(fn=chatgpt_chatclone, inputs=[message, state], outputs=[chatbot, state])
blocks.launch(share=True, debug=False)