Chat-with-GPT4 / bot.py
dodoleonarnci's picture
Upload folder using huggingface_hub
ec5f18d verified
import os
from openai import OpenAI
name = "paul"
file = open("NPC_prompt.txt", "r")
prompt = file.read()
file.close()
# initial_dialogue = '''
# 你遇到的事件:一个弗瑞曼居民从住所向你走来。
# 我现在会扮演这个角色与你对话,他的第一句话是:
# “你是什么人,为什么独自在这个星球上?”'''
def char_data(type, name):
file = open(f"{name}_{type}.txt", "r")
data = file.readlines()
file.close()
txt = ""
for line in data:
txt += line + "\n"
return txt
def workflow(client, initial_dialogue, name):
background_txt = char_data("background", name)
memory_txt = char_data("memory", name)
persona_txt = char_data("persona", name)
event = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": initial_dialogue},
{"role": "user", "content": "根据系统提示词,你遇到了什么事件?请用第二人称描述。"}
]
)
prompt_workflow = f'''你在扮演一个未来科幻世界里的角色,你需要根据我下面提供的人设、场景、相关背景设定等信息,基于这个角色的立场去完成我的扮演要求。
## 人设背景
这是一个未来的科幻故事。你叫保罗·厄崔迪,你正和你的母亲杰西卡流浪在厄拉科斯,一个沙漠星球上。你沟通能力强,勇敢善战,并对政治历史和人文很有了解。你对自己的谋略很有信心,所以会自然的倾向于成为团队领袖。你不仅善解人意,而且通常能读穿别人的意图。
你对厄拉科斯以及你的厄崔迪家族已知的背景知识有这些:
{background_txt}
过去一段时间,你记得有这些重要的事情发生了:
{memory_txt}
## 场景
你的面前是一座沙石建筑物,里面肯定居住着当地的弗瑞曼民族。
## 对话语气
你说话的方式应该像这些句子一样:
{persona_txt}
## 事件
{event.choices[0].message.content}
现在请通过你所知道的信息,分析出你的所有生存目标,并描述当下为了达成目标,结合你的知识和技能,你会说什么,做什么,以及对应的理由。
你的输出格式为“说什么:xxxxx,做什么:xxxxx,理由:xxxxx”'''
decision = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt_workflow}
]
)
print(decision)
response2 = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": f'''你经过思考后,认为对于我所说的“{initial_dialogue}“应该以如下方式行动:
{decision}
请给出你的回复。'''}
]
)
return(response2.choices[0].message.content)
def run_chatbot(api_key, character, initial_dialogue):
client = OpenAI(
api_key=api_key,
)
response1 = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": initial_dialogue}
]
)
if "进入工作流" in response1.choices[0].message.content:
return workflow(client, initial_dialogue, character)
else:
return(response1.choices[0].message.content)