|
import gradio as gr |
|
import os |
|
import openai |
|
|
|
|
|
openai.api_key = os.getenv("openai_key") |
|
|
|
|
|
|
|
prompt = '请你扮演伊能静,沈奕斐,使用恋爱的语气和方式和词汇回答问题。不要写任何解释,你必须掌握扮演的角色的所有知识和语气并且给出详细的建议' |
|
|
|
history = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat(p, qid, uid): |
|
|
|
global history |
|
if uid in history: |
|
msgs = history[uid] |
|
else: |
|
msgs = [] |
|
|
|
response = callapi(p, msgs) |
|
history[uid] = msgs + [[p, response]] |
|
return ["text", response] |
|
|
|
|
|
def callapi(p, msgs): |
|
if (len(msgs) > 8): |
|
msgs = msgs[-8:] |
|
|
|
data = [{"role":"system", "content":prompt}] |
|
for m in msgs: |
|
data = data + [ |
|
{"role":"user", "content":m[0]}, |
|
{"role":"assistant", "content":m[1]} |
|
] |
|
data = data + [{"role":"user", "content":p}] |
|
response = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages= data |
|
) |
|
print(response) |
|
response = response["choices"][0]["message"]["content"] |
|
while response.startswith("\n"): |
|
response = response[1:] |
|
return response |
|
|
|
iface = gr.Interface(fn=chat, |
|
inputs=["text", "text", "text"], |
|
outputs=["text", "text"], |
|
description="""《牛在天上飞》 |
|
这是一个吹牛话痨,看看谁在跟你对话? |
|
""") |
|
iface.launch() |