You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

5 months ago
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)