自学内容网 自学内容网

OpenAI 笔记:chat API

聊天模型接受一系列消息作为输入,并返回模型生成的消息作为输出。

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

  • 类型: 数组
  • 描述: 聊天完成选择的列表。如果 n 大于 1,则可以有多个选择。

finish_reason

每个响应都会包括一个 finish_reasonfinish_reason 的可能值包括:

  • stop:API 返回完整消息,或者由 stop 参数提供的一个停止序列终止的消息
  • length:由于 max_tokens 参数或令牌限制,模型输出不完整
  • tool_calls:模型决定调用一个工具
  • content_filter:由于我们的内容过滤器的标记,省略了内容
  • null:API 响应仍在进行中或不完整

index

  • 类型: 整数
  • 描述: 在选择列表中的选择索引。

message

  • 类型: 对象
  • 描述: 模型生成的聊天完成消息。

content

  • 类型: 字符串或空
  • 描述: 消息的内容。

tool_calls

  • 类型: 数组
  • 描述: 模型生成的工具调用,例如函数调用。

role

  • 类型: 字符串
  • 描述: 该消息作者的角色。

logprobs

  • 类型: 对象或空
  • 描述: 选项的对数概率信息。

created

  • 类型: 整数
  • 描述: 聊天完成创建的 Unix 时间戳(秒)。

model

  • 类型: 字符串
  • 描述: 用于聊天完成的模型。

object

  • 类型: 字符串
  • 描述: 对象类型,始终为 chat.completion。

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  其他主要的参数

frequency_penalty

频率惩罚

  • 类型: 数值或空
  • 可选
  • 默认值: 0
  • 范围: -2.0 到 2.0。正值根据文本中已存在的令牌的频率惩罚新令牌,减少模型重复相同文本的可能性。

logit_bias

日志偏置

  • 类型: 映射
  • 可选
  • 默认值: 空
  • 描述: 修改指定令牌在完成中出现的可能性。接受一个 JSON 对象,映射令牌(通过令牌ID指定)到一个从 -100 到 100 的偏置值。在抽样前,偏置会被加到模型生成的 logits 上。具体效果因模型而异,但-1到1之间的值应该会降低或增加选择的可能性;如 -100 或 100 的值应该会导致禁用或独占选择相关令牌。

logprobs

对数概率

  • 类型: 布尔值或空
  • 可选
  • 默认值: 假
  • 描述: 是否返回输出令牌的对数概率。如果为真,返回每个输出令牌的对数概率。

max_tokens

最大令牌数

  • 类型: 整数或空
  • 可选
  • 描述: 在聊天完成中可以生成的最大令牌数。输入令牌和生成令牌的总长度受模型的上下文长度限制。

n

数量

  • 类型: 整数或空
  • 可选
  • 默认值: 1
  • 描述: 为每个输入消息生成聊天完成选择的数量。


原文地址:https://blog.csdn.net/qq_40206371/article/details/138156741

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!