目前对客的AI产品基本上都是流式输出的,下面是一个使用python流式输出的样例
from openai import OpenAI
client = OpenAI(
api_key = "xxxxxxx",
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"
)
def chat_stream(user_prompt, system_prompt):
response = client.chat.completions.create(
model = "qwen-max",
messages = [
{"role":"system", "content": system_prompt},
{"role":"user", "content": user_prompt}
],
stream = True # 指定流式输出
)
for chunk in response:
yield chunk.choices[0].delta.content # 等待被唤起的时候输出
response = chat_stream(user_prompt="我们公司项目管理应该用什么工具", system_prompt="你负责教育内容开发公司的答疑,你的名字叫公司小蜜,你要回答同事们的问题。")
for chunk in response:
print(chunk, end="")这样就是根据大模型的反馈呈现流式输出的效果。