如何只定义一次ChatGPT API的系统角色,使后续对话始终基于该角色
就像代码里那样,我还有一份类似问题的列表。每次我都需要给系统传递一个描述,来说明它的角色和上下文。但这样做非常耗费资源,有没有办法在问问题之前,或者第一次问问题的时候就定义好系统的角色,然后后面的所有问题都基于这个角色呢?
for question in questions:
completion = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[
{"role": "system", "content": "You are a professional lawyer, please help me with professional legal questions."},
{"role": "user", "content": question},
],
# max_tokens=1000,
# temperature=0.7
)
message = completion.choices[0].message
answer = message.get("content")
print(answer.encode('utf-8').decode("utf-8"))
我在openAI的网站上看过相关内容,但没有找到解决办法,所以任何帮助都非常感谢!
2 个回答
0
是的,你可以在提问之前先定义系统的角色,然后在后续的问题中继续使用这个角色。
# Define the system's role outside of the loop
system_role = {"role": "system", "content": "You are a professional lawyer, please help me with professional legal questions."}
for question in questions:
completion = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[
system_role, # Reuse the system's role
{"role": "user", "content": question},
],
# max_tokens=1000,
# temperature=0.7
)
message = completion.choices[0].message
answer = message.get("content")
print(answer.encode('utf-8').decode("utf-8"))
0
你正在使用已经过时的聊天完成API。建议你使用新的助手API(虽然还在测试阶段,但几周后就会正式发布)。助手API能够理解上下文,也就是说,当你设置初始指令(就像在聊天API中设置系统提示)时,你只需要关注接下来的问题。当你创建一个助手时,它会记住整个对话的上下文(也就是系统提示和后续的问题/回答),直到它过期或者你把它删除。