聊天模型接受一系列消息作为输入,并返回模型生成的消息作为输出。
1 导入openai 的api key
from openai import OpenAI
client = OpenAI(
api_key='***')
2 调用聊天API
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system",
"content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
{"role": "user",
"content": "Compose a short poem that explains the concept of recursion in programming."}
]
)
print(completion.choices[0].message.content)
'''
In the realm of code, recursion shines bright,
A concept of looping with a mystical light.
A function calling itself, over and over,
A dance of logic, a captivating rover.
Like a mirror reflecting into infinity,
Recursion dives deep with elegant affinity.
Breaking down problems into smaller parts,
It conquers complexity with creative arts.
Through recursive calls, layers unfold,
Solving mysteries, making connections bold.
A looping dream, a mesmerizing show,
In the world of coding, recursion's glow.
'''
- 主要输入是 messages 参数。
- 消息必须是一个消息对象数组,每个对象都有一个角色(“system”、“user”或“assistant”)和内容。
- 对话可以短至一条消息,或者多次来回交换。
- 通常,对话以“system”消息开始,然后是“user”和“assistant”消息的交替。
- 系统消息有助于设定助手的行为。
- 例如,可以修改助手的个性或提供关于助手在整个对话中应如何表现的具体指示
- 系统消息是可选的,没有系统消息的模型行为可能类似于使用通用消息,如“你是一个有帮助的助手。”
- 用户消息提供了助手需要回应的请求或评论
- 助手消息存储了之前助手的回应,但也可以由用户自行编写,以展示期望的行为。
3 聊天完成 API响应结果分析
id |
| ||
choices |
| ||
finish_reason | 每个响应都会包括一个
| ||
index |
| ||
message |
| ||
content |
| ||
tool_calls |
| ||
role |
| ||
logprobs |
| ||
created |
| ||
model |
| ||
object |
| ||
usage |
| ||
completion_tokens |
| ||
prompt_tokens |
| ||
total_tokens |
|
4 seed & temperature
要获得(大多数情况下)跨 API 调用的确定性输出,可以:
- 设置种子参数seed
- 确保所有其他参数(如提示messages或温度temperature)在请求间完全相同。
- 较低的温度值会导致输出更加一致,而较高的值会产生更多样化和创造性的结果
- temperature的范围在0~2之间
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user",
"content": "Compose a short sentence about Singapore."}
],
)
print(completion.choices[0].message.content)
'''
Singapore is a vibrant and modern city-state known for its impressive skyline and diverse cultural attractions.
'''
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user",
"content": "Compose a short sentence about Singapore."}
],
seed=1
)
print(completion.choices[0].message.content)
'''
Singapore is a vibrant and bustling city-state known for its modernity and unique blend of cultures.
'''
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user",
"content": "Compose a short sentence about Singapore."}
],
seed=1,
temperature=2
)
print(completion.choices[0].message.content)
'''
Singapore is praised for its advanced infrastructure, efficient public transport, and rich multicultural environment.
'''
5 token(令牌)数量
- API 调用中的令牌总数影响:
- API 调用成本,因为openai是按令牌付费
- API 调用时间,因为写入更多令牌需要更多时间
- API 调用是否能够成功,因为总令牌必须低于模型的最大限制(对于 gpt-3.5-turbo 是 4097 )
- 输入和输出令牌都计入这些数量。
- 例如,如果API 调用在消息输入中使用了 10 个令牌,在消息输出中收到了 20 个令牌,则将被收取 30 个令牌
- 查看调用了多少令牌:
completion.usage
#CompletionUsage(completion_tokens=17, prompt_tokens=14, total_tokens=31)
6 插入文本
text='hello world'
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user",
"content": f'Translate the following English text to French: \"{text}\"'}
],
)
print(completion.choices[0].message.content)
#"Bonjour le monde"
7 其他主要的参数
| 频率惩罚
|
| 日志偏置
|
| 对数概率
|
| 最大令牌数
|
| 数量
|