可以设定结果输出为stream模式(流式输出),获得更好的使用体验开启流式输出主要就2步:在client.chat.completions.create()调用模型的时候设定参数:stream=Truefor循环response对象,并在循环内输出内容from openaiimportOpenAI from openai.types.chat.chat_completionimportChatCompletion# 获取client对象,OpenAI类对象client=OpenAI(base_url="https://llm-w51hypeilnm3yxpr.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",)# 调用模型response:ChatCompletion=client.chat.completions.create(model="qwen3.8-27b", messages=[{"role":"system","content":"你是一个python专家"},{"role":"assistant","content":"好的,我是编程专家"},{"role":"user","content":"输出1-10数字,用python代码"}], stream=True)forchunkinresponse:ifnot chunk.choices:continueprint(chunk.choices[0].delta.content,end="",flush=True)
可以设定结果输出为stream模式(流式输出),获得更好的使用体验开启流式输出主要就2步:在client.chat.completions.create()调用模型的时候设定参数:stream=Truefor循环response对象,并在循环内输出内容from openaiimportOpenAI from openai.types.chat.chat_completionimportChatCompletion# 获取client对象,OpenAI类对象client=OpenAI(base_url="https://llm-w51hypeilnm3yxpr.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",)# 调用模型response:ChatCompletion=client.chat.completions.create(model="qwen3.8-27b", messages=[{"role":"system","content":"你是一个python专家"},{"role":"assistant","content":"好的,我是编程专家"},{"role":"user","content":"输出1-10数字,用python代码"}], stream=True)forchunkinresponse:ifnot chunk.choices:continueprint(chunk.choices[0].delta.content,end="",flush=True)