多轮对话
SpAk /chat/completions API 是一个“无状态” API,即服务端不记录用户请求的上下文,用户在每次请求时,需将之前所有对话历史拼接好后,传递给对话 API。
from openai import OpenAI
client = OpenAI(api_key="<SpAk API Key>", base_url="https://yuansuo.top")
# Round 1
messages = [{"role": "user", "content": "What's the highest mountain in the world?"}]
response = client.chat.completions.create(
model="SpAk-Aster-V1-MOE",
messages=messages
)
messages.append(response.choices[0].message)
print(f"Messages Round 1: {messages}")
# Round 2
messages.append({"role": "user", "content": "What is the second?"})
response = client.chat.completions.create(
model="SpAk-Aster-V1-MOE",
messages=messages
)
messages.append(response.choices[0].message)
print(f"Messages Round 2: {messages}")
[
{"role": "user", "content": "What's the highest mountain in the world?"},
{"role": "assistant", "content": "The highest mountain in the world is Mount Everest."},
{"role": "user", "content": "What is the second?"}
]