import os from pathlib import Path from openai import OpenAI # pip install -U openai API_KEY = "sk-01d13a39e09844038322108ecdbd1bbc" client = OpenAI( api_key=API_KEY, # 如果您没有配置环境变量,请在此处替换您的API-KEY base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填写DashScope服务base_url ) # 修改文件路径为原始字符串(注意路径前的r) file_object = client.files.create(file=Path(r"D:\dsWork\QingLong\音频文本.txt"), purpose="file-extract") print(file_object.id) file_id = file_object.id # 初始化messages列表 completion = client.chat.completions.create( model="qwen-long", messages=[ {'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'system', 'content': 'fileid://' + file_id}, {'role': 'user', 'content': '不要使用markdown格式输出,只输出原文文字,你的任务是按语义进行分段。'} ], stream=True, stream_options={"include_usage": True} ) full_content = "" for chunk in completion: if chunk.choices and chunk.choices[0].delta.content: # 拼接输出内容 full_content += chunk.choices[0].delta.content #print(chunk.model_dump()) # 修改最后的打印语句(去掉大括号) print(full_content)