以千问API调用为例
1. 在阿里云工作台注册账号
https://billing-cost.console.aliyun.com/home
新人会有免费的一部分额度调用,有效时间是3个月
2. 申请API key
3. 在Python环境中可以直接编写代码调用(记得提前导入包依赖)
import os from openai import OpenAItry:client = OpenAI(api_key="",base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",)completion = client.chat.completions.create(model="qwen-plus", # 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/modelsmessages=[{'role': 'system', 'content': 'You are a helpful assistant.'},{'role': 'user', 'content': '你是谁?'}])print(completion.choices[0].message.content) except Exception as e:print(f"错误信息:{e}")print("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
4. 运行结果
