千问模型
import openai
# 配置阿里云千问 API
openai.api_key = "sk-4a5c557b5d25436fa088b9b1917b01c1" # 替换为你的千问API密钥
openai.api_base = "https://dashscope.aliyuncs.com/compatible-mode/v1" # 千问API的兼容模式地址
# 获取用户输入
user_input = input("请输入你想问的问题: ")
# 调用千问大模型
response = openai.ChatCompletion.create(
model="qwen-plus", # 可选模型:qwen-turbo, qwen-max, qwen-plus, etc.
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": user_input} # 使用用户输入的内容
],
)
# 打印返回结果
print("AI回答:", response['choices'][0]['message']['content'])
input("按任意键结束程序...")
deepseek模型
import openai
openai.api_key="sk-z4R3nSZOZphMEbN0q9Zjr3yrg7tOw6N0uF0CFoFZvLK28o22"
openai.api_base="https://api.lkeap.cloud.tencent.com/v1"
s_value = True
# 请求
chat_completion = openai.ChatCompletion.create(
model="deepseek-r1",
messages=[
{
"role": "user",
"content": "今日热点",
}
],
stream=s_value,
)
if s_value:
for chunk in chat_completion:
# 打印思维链内容
if hasattr(chunk.choices[0].delta, 'reasoning_content'):
print(f"{chunk.choices[0].delta.reasoning_content}", end="")
# 打印模型最终返回的content
if hasattr(chunk.choices[0].delta, 'content'):
if chunk.choices[0].delta.content != None and len(chunk.choices[0].delta.content) != 0:
print(chunk.choices[0].delta.content, end="")
else:
result = chat_completion.choices[0].message.content