# API 密钥 API_KEY="【你的 API 密钥】" # 基础 URL BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1" # 模型 Code MODEL="qwen3-max"
编写测试代码
fromopenaiimportOpenAIfromdotenvimportload_dotenvimportos load_dotenv()api_key=os.getenv("API_KEY")base_url=os.getenv("BASE_URL")model=os.getenv("MODEL")client=OpenAI(api_key=api_key,base_url=base_url)completion=client.chat.completions.create(model=model,messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"你是谁?"}],stream=True)forchunkincompletion:print(chunk.choices[0].delta.content,end="",flush=True)
completion=client.chat.completions.create(model=model,messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"你是谁?"}],stream=True)
fromopenaiimportOpenAIfromdotenvimportload_dotenvimportos load_dotenv()api_key=os.getenv("API_KEY")base_url=os.getenv("BASE_URL")model=os.getenv("MODEL")client=OpenAI(api_key=api_key,base_url=base_url)completion=client.chat.completions.create(model=model,messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"你是谁?"}])response=completion.choices[0].message.contentprint(response)
2、print 函数参数
参数
说明
end
指定 print 函数结束后要添加的字符
flush
指定是否强制立即刷新输出缓冲区
默认情况,print 函数后自动换行
print("Hello")print("World")
# 输出结果 Hello World
# 等同于print("Hello",end="\n")print("World")
# 输出结果 Hello World
换行 2 次
print("Hello",end="\n\n")print("World")
# 输出结果 Hello World
不换行
print("Hello",end="")print("World")
# 输出结果 HelloWorld
自定义分隔符
print("Hello",end=" ")print("World")
# 输出结果 Hello World
print("Hello",end="---")print("World")
# 输出结果 Hello---World
关于 messages
1、基本介绍
messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"你是谁?"}]
messages=[{"role":"system","content":"你是一个英语老师,纠正我的语法错误"},{"role":"user","content":"I goes to school yesterday."}]
# 输出结果 Great effort! However, there's a small grammar mistake in your sentence. The correct sentence should be: **"I went to school yesterday."** ### Explanation: - The verb **"go"** is irregular. - Since you're talking about something that happened **yesterday** (past time), you need the **past tense** form of "go," which is **"went."** - "I goes" is incorrect because "goes" is used with third-person singular subjects like **he, she,** or **it** (e.g., "She goes to school every day"). Let me know if you'd like more examples! 😊
AI工程宝典:产品运营的智能升级指南 【免费下载链接】aie-book [WIP] Resources for AI engineers. Also contains supporting materials for the book AI Engineering (Chip Huyen, 2025) 项目地址: https://gitcode.com/GitHub_Trending/ai/aie-book
还在为…
GeoJSON.io 终极指南:快速创建和编辑地理数据的免费在线工具 【免费下载链接】geojson.io A quick, simple tool for creating, viewing, and sharing spatial data 项目地址: https://gitcode.com/gh_mirrors/ge/geojson.io
GeoJSON.io 是一款功能强大的免费…