介绍

openai库提供了各种语言调用llm的基本操作,封装了网络请求,消息序列化等中间操作
LangChain 或 OpenAI SDK 在底层调用模型时,使用的是 HTTP + JSON 的方式进行通信。
请求会被序列化为 JSON 格式,通过 HTTP POST 发送到模型服务端;服务端返回的结果同样是 JSON,再由 SDK 解析成 Python 对象。
在流式输出场景下(stream=True),则采用 Server-Sent Events(SSE)协议,将模型生成的内容按 token 分块传输,每一块依然是 JSON 格式。
这种“HTTP + JSON”的组合已经成为大模型 API 的事实标准,原因在于其简单、通用、易调试,并且方便不同厂商之间做兼容实现。

基本调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import os  
from openai import OpenAI
from pprint import pprint

try:
client = OpenAI(
# 若没有配置环境变量,请用阿里云百炼API Key将下行替换为: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
model="qwen3-max", # 模型列表: https://help.aliyun.com/model-studio/getting-started/models
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'assistant', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '你是谁?'}
]
)

pprint(completion)
# print(completion.choices[0].message.content)
except Exception as e:
print(f"错误信息:{e}")
print("请参考文档:https://help.aliyun.com/model-studio/developer-reference/error-code")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"service_id": "chatcmpl-df5ec6da-9726-9ae4-b4f5-2c22eb15114f",
"object_name": "chat.completion",
"system_fingerprint": "",
"service_tier": "",
"prompt_tokens": 38,
"completion_tokens": 98,
"created": 1773806514,
"created_at": "",
"metadata": {
"content": "我是通义千问,阿里巴巴集团旗下的超大规模语言模型。",
"refinery_status": "正在开发中",
"status": "开发中",
"role": "assistant",
"created_at": "2023-10-01T14:00:00Z"
},
"audio": "",
"function_call": "",
"tool_calls": "",
"refinery_status": "",
"status": "",
"role": ""
}

流式输出

让模型一边输出一边生成,优化下效果,不导致一开始没生产完一致等待

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os  
from openai import OpenAI
from pprint import pprint

try:
client = OpenAI(
# 若没有配置环境变量,请用阿里云百炼API Key将下行替换为: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

response = client.chat.completions.create(
model="qwen3-max", # 模型列表: https://help.aliyun.com/model-studio/getting-started/models
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'assistant', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '你是谁?'}
],
stream=True
)

for ck in response:
print(
ck.choices[0].delta.content,
end=" ", # 每个chunk以什么分割
flush=True # 是否立刻刷新缓冲区
)
except Exception as e:
print(f"错误信息:{e}")
print("请参考文档:https://help.aliyun.com/model-studio/developer-reference/error-code")

每个消息的格式段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"id": "chatcmpl-076493bf-9b45-92e9-8762-05a379b115a4",
"choices": [
{
"delta": {
"content": "",
"function_call": null,
"refusal": null,
"role": "assistant",
"tool_calls": null
},
"finish_reason": null,
"index": 0,
"logprobs": null
}
],
"created": 1773806937,
"model": "qwen3-max",
"object": "chat.completion.chunk",
"service_tier": null,
"system_fingerprint": null,
"usage": null
}

可以在message中存入历史消息,transform的开始是续写功能,要让模型有对应更好的效果,要让上文更加准确,可以在message中做一些操作,后来的各种agent应该就是在这里操作?

function call

即固定模型输出的数据格式
例如做一个股票系统,需要有股票的时间,名字,当前在那个点,投入了多少钱

1
2
3
4
5
6
{
"name": "xxx",
"in_time": "YYYY-xx-zz",
"cur_status": "up + 1.0% / down + [点]",
"money": 3, // 投入了多少钱,单位是RMB
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json  
import os
from openai import OpenAI
from pprint import pprint

schema = ['名字', '时间', '涨停', '投入钱']
metadata = [
{
"name": "好股票",
"in_time": "2026-10-02",
"cur_status": "up + 1.0%",
"money": 1000, # 投入了多少钱,单位是RMB
}
]
pre_message = {'role': 'system',
'content': '你是一个金融股票助手,给出的消息请抽取出{schema}的数据,并以metadata返回为一个单独的数据包,之后再返回用户相关一些消息,记录为option, '
'如果原理句子查询不到,请标记为\'查询不到\''}

query = '我是xxx,我昨天投递了10w到了x股票,想知道今天这个股票的点数'

try:
client = OpenAI(
# 若没有配置环境变量,请用阿里云百炼API Key将下行替换为: api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

response = client.chat.completions.create(
model="qwen3-max", # 模型列表: https://help.aliyun.com/model-studio/getting-started/models
messages=[
{'role': 'system', 'content': '直击要害回答答案'},
{'role': 'user', 'content': '讲讲transform续写和角色分类和ai在这个基础上做的操作'},
],
stream=True
)

def call(msg: str) -> bool:
response = client.chat.completions.create(
model="qwen3-max",
messages=[
pre_message,
{'role': 'user', 'content': msg}
],
stream=True
)
for x in response:
print(
x.choices[0].delta.content,
end="",
flush=True,
)


ok = call(query)
except Exception as e:
print(f"错误信息:{e}")
print("请参考文档:https://help.aliyun.com/model-studio/developer-reference/error-code")