chatgpt API 使用指南
OpenAI API简介
“OpenAI 的 API 可用于解释或生成自然语言、代码或图像的任何涉及任务,我们提供范围广泛的模型,为各种任务提供不同层次的性能,还可以微调您自己的定制模型,从而利用这些模型进行内容生成、语义搜索和分类。”
核心概念
- Prompts
- 提示词
- Tokens
- 将文本分解为token,通过token来理解用户的意思。
- Models
- 有多个model,分别适合不同的任务。
通过API来使用chatgpt可以查看官网例子:API demo
python教程
import openai
import os
# get key from : https://platform.openai.com/account/api-keys
openai.api_key = 'sk-VujkGgw43****************KHffRxdBzOKPZKK0NS'
openai.organization = 'org-dipVTUm************7Rp64'
# get all model that openai supported list : https://platform.openai.com/docs/models
all_model = openai.Model.list()
print(all_model)
# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)
print(response['choices'][0]['message']['content'])
print("============")
print(response)
官网教程
API调用教程: API reference
比较详细的教程: Guide