本文以Python SDK为例,为您介绍如何通过OpenAI SDK调用大模型回答服务。
请求示例
from openai import OpenAI
def get_response():
client = OpenAI(
api_key="OS_API_KEY", # OS_API_KEY需要替换为您在平台上创建的API Key
base_url="http://xxxx-hangzhou.opensearch.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="ops-qwen-turbo",
messages=[
{"role": "system", "content": "你是一个机器人助手"},
{"role": "user", "content": "河南的省会是哪里"},
{"role": "assistant", "content": "郑州"},
{"role": "user", "content": "那里有什么好玩的"}]
)
print(completion.model_dump_json())
if __name__ == '__main__':
get_response()
返回示例
{
"id":"48657EBF-2B81-4E3E-96B4-27FE7D6E780A",
"choices":[
{
"finish_reason":"stop",
"index":0,
"logprobs":null,
"message":{
"content":"郑州市有很多旅游景点和文化活动供游客选择,例如郑州黄帝故里拜祖大典、郑州市博物馆、郑州科技馆、郑州市动物园等。此外,郑州还有美丽的碧沙岗公园和紫荆山公园等休闲的好去处。总的来说,郑州不仅有深厚的文化底蕴,还有现代化的设施和公园,是一个适合旅游和娱乐的城市。",
"role":"assistant",
"function_call":null,
"tool_calls":null
}
}
],
"created":1719567872,
"model":"ops-qwen-turbo",
"object":"chat.completion",
"service_tier":null,
"system_fingerprint":null,
"usage":{
"completion_tokens":77,
"prompt_tokens":38,
"total_tokens":115
}
}
文档内容是否对您有帮助?