前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >chatgpt api基础使用示例

chatgpt api基础使用示例

原创
作者头像
Alan_1
发布2023-05-05 21:41:45
4670
发布2023-05-05 21:41:45
举报
文章被收录于专栏:Alan的blogAlan的blog

环境

代码语言:shell
复制
pip install openai

api

Completions

url: POST https://api.openai.com/v1/completions

参数:

  • model: (string) text-davinci-003, text-davinci-002, text-curie-001, text-babbage-001, text-ada-001
  • prompt: (string/array) (<|endoftext|>)
  • suffix:(string) (null) 文本完成后缀
  • max_tokens:(int) (16) 生成令牌的最大数量,len(prompt+max_tokens) < old_max:2048(new_max:4096)
  • temperature: (float) (1) 值范围:0.0~2.0,越大生成内容越随机,越小生成内容越固定
    • 建议与top_p仅修改一个
  • top_p: (float) (1) 核采样,模型考虑具有top_p概率质量的标记结果-->0.1: 只考虑构成前 10% 概率质量的标记(mass)
  • n: (int) (1) 为每个prompt生成多少completions
  • stream: (bool) (false)是否将部分进度流式返回。如果设置,则令牌将作为仅数据的服务器推送事件在其可用时发送,流将以data:[DONE]消息终止。
  • logprobs: (int) (null) 在返回的logprobs最有可能的令牌中包含对数概率和所选择的令牌。果logprobs是5,API将返回一个最有可能的令牌列表。API将始终返回所采样令牌的对数概率,因此响应中可能有logprobs + 1个元素。logprobs的最大值为5
  • echo: (bool) (false)回显prompt
  • stop: (string/array) (null) 最多4个序列,其中API将停止生成进一步的令牌。返回的文本将不包含停止序列。
  • presence_penalty: (int) (0) -2.0~2.0 正值会根据到目前为止文本中它们的现有频率对新令牌进行惩罚,降低模型按原样重复相同行的可能性。
  • best_of: (int) (1) 在服务器端生成best_of完成,并返回“best”(每个令牌的对数概率最高的完成)。结果无法流式传输。当与n一起使用时,best_of控制候选完成的数量,并且n指定要返回的数量 - best_of必须大于n。
    • 注意:由于此参数生成许多完成,因此可能会快速消耗您的令牌配额。请谨慎使用,并确保您对max_tokens和stop设置合理的设置。
  • logit_bias: (map) (null) 用于修改特定令牌出现在完成中的概率。它接受一个json对象,将令牌(由GPT令牌化器中的其令牌ID指定)映射到从-100到100的相关偏差值
    • 在数学上,该偏差被添加到模型生成的logits中进行采样之前。确切的影响因模型而异,但是-1到1之间的值应该减少或增加选择的可能性;像-100或100这样的值应该导致禁止或专门选择相关令牌
  • user: (string) 表示您的最终用户的唯一标识符,可帮助OpenAI监视和检测滥用
chat

url: POST https://api.openai.com/v1/chat/completions

参数

  • model: (string) gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301
  • messages: (array) - role: (string) One of system, user, or assistant - content: (string) 角色消息内容 - name: (string) max_len=64 a-z,A-Z,0-9和下划线
  • temperature: (float) (1) 0~2
  • top_p: (float) (1)
  • n: (int) (1)
  • stream: (bool) (false)
  • stop: (bool) (flase)
  • max_tokens: (int) (inf)
  • presence_penalty: (float) (0)
  • frequency_penalty: (float) (0)
  • logit_bias: (map) (null)
  • user: (string)

示例:

  • 检索模型列表import os import openai openai.organization = "YOUR_ORG_ID" openai.api_key = os.getenv("OPENAI_API_KEY") openai.Model.list()
  • 基础使用
代码语言:javascript
复制
```python
import osimport openai
  openai.organization = "YOUR_ORG_ID"
  openai.api_key = "OPENAI_API_KEY"
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
    model=model,
    messages=messages,
    temperature=0, # this is the degree of randomness of the model's output
	)
return response.choices[0].message["content"]

Principle 1: Write clear and specific instructions

代码语言:javascript
复制
  text_1 = f"""
  Making a cup of tea is easy! First, you need to get some \ 
  water boiling. While that's happening, \ 
  grab a cup and put a tea bag in it. Once the water is \ 
  hot enough, just pour it over the tea bag. \ 
  Let it sit for a bit so the tea can steep. After a \ 
  few minutes, take out the tea bag. If you \ 
  like, you can add some sugar or milk to taste. \ 
  And that's it! You've got yourself a delicious \ 
  cup of tea to enjoy.
  """
  prompt = f"""
  You will be provided with text delimited by triple quotes. 
代码语言:javascript
复制
If it contains a sequence of instructions, \ 
  re-write those instructions in the following format:
  Step 1 - ...
  Step 2 - …
…
  Step N - …
If the text does not contain a sequence of instructions, \ 
  then simply write \"No steps provided.\"
  \"\"\"{text_1}\"\"\"
  """
  response = get_completion(prompt)
  print("Completion for Text 1:")
print(response)
```

其他模型

Edits

Images

Create image edit

Create image variation

Embedding

Audio

Create transcription
Create translation

Files

Upload file

Delete file

Retrieve file

Fine-tunes

Moderations

Engines

Parameter details

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境
  • api
    • Completions
      • chat
      • 示例:
      • Principle 1: Write clear and specific instructions
      • 其他模型
        • Edits
          • Images
            • Create image edit
              • Create image variation
                • Embedding
                  • Audio
                    • Create transcription
                    • Create translation
                  • Files
                    • Upload file
                      • Delete file
                        • Retrieve file
                          • Fine-tunes
                            • Moderations
                              • Engines
                                • Parameter details
                                领券
                                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档