近期有个很火的话题啊,那就是CahtGPT,这玩意是美国OpenAI研发的聊天机器人,于2022年11月30日发布。
ChatGPT是人工智能技术驱动的自然语言处理工具,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,真正像人类一样来聊天交流,甚至能完成撰写邮件、视频脚本、文案、翻译、代码等任务。
接下来带你一分钟学会开发自己的OpenAI的应用
如果您需要获取ApiFox或者postman类接口可以联系我哦。点击联系我
先体验下ChatGPT的魅力
生成API密钥
在我们开始使用 OpenAI API 之前,我们需要登录我们的 OpenAI 帐户并生成我们的API 密钥。
注册OpenAI账号需要科学上网,这里推荐一个中国区注册OpenAI账号试用ChatGPT指南,可自行百度,链接贴不上来😂
账号注册完毕后,点击右上角,如下图
可以看到如下,就可以创建APIKEY了,记住它,接下来会用到
OpenAI的Restful接口
API接口是不需要翻墙的
列举可用模型
请求
GET https://api.openai.com/v1/models
Accept: application/json
Authorization: Bearer 上面注册的APIKEY
User-Agent: Apifox/1.0.0 (https://www.apifox.cn)
Accept: */*
Host: api.openai.com
Connection: keep-alive
响应
这个响应有点长啊,就不贴了
检索模型
请求
GET https://api.openai.com/v1/models/text-davinci-003 HTTP/1.1
Host: api.openai.com
Authorization: Bearer 上面注册的APIKEY
User-Agent: Apifox/1.0.0 (https://www.apifox.cn)
Accept: */*
Host: api.openai.com
Connection: keep-alive
响应
{
"id": "text-davinci-003",
"object": "model",
"created": 1669599635,
"owned_by": "openai-internal",
"permission": [
{
"id": "modelperm-YuSlMLid7lQpVIhxhBfL6aqV",
"object": "model_permission",
"created": 1675988524,
"allow_create_engine": false,
"allow_sampling": true,
"allow_logprobs": true,
"allow_search_indices": false,
"allow_view": true,
"allow_fine_tuning": false,
"organization": "*",
"group": null,
"is_blocking": false
}
],
"root": "text-davinci-003",
"parent": null
}
创建补全
请求
POST https://api.openai.com/v1/completions HTTP/1.1
Host: api.openai.com
Authorization: Bearer 上面注册的APIKEY
User-Agent: Apifox/1.0.0 (https://www.apifox.cn)
Content-Type: application/json
Accept: */*
Host: api.openai.com
Connection: keep-alive
{
"model": "text-davinci-003",
"prompt": "用python画一朵玫瑰花",
"max_tokens": 100,
"temperature": 0
}
参数说明
- model :要使用的模型的 ID(在这里你可以看到所有可用的模型)
- Prompt:生成结果的触发指令
- max_token:完成时生成的最大token数量(这里可以看到OpenAI使用的tokenizer)
- temperature:要使用的采样策略。 接近 1 的值会给模型带来更多风险/创造力,而接近 0 的值会生成明确定义的答案。
响应
{
"id": "cmpl-6iCq1CxxOmkD6CnV9cIXL4p8uyqoc",
"object": "text_completion",
"created": 1675993801,
"model": "text-davinci-003",
"choices": [
{
"text": "nn§ Codennimport turtlennturtle.setup(800, 600)nturtle.penup()nturtle.goto(0, -200)nturtle.pendown()nturtle.pensize(2)nturtle.pencolor('red')nn# 画花瓣nfor i in range(36):n turtle.left(10)n turtle.forward(50)n",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 16,
"completion_tokens": 100,
"total_tokens": 116
}
}
创建编辑
请求
POST https://api.openai.com/v1/edits HTTP/1.1
Host: api.openai.com
Authorization: Bearer 上面注册的APIKEY
User-Agent: Apifox/1.0.0 (https://www.apifox.cn)
Content-Type: application/json
Accept: */*
Host: api.openai.com
Connection: keep-alive
{
"model": "text-davinci-edit-001",
"input": "What day of the wek is it?",
"instruction": "Fix the spelling mistakes"
}
响应
{
"object": "edit",
"created": 1675993904,
"choices": [
{
"text": "What day of the week is it?n",
"index": 0
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 28,
"total_tokens": 53
}
}
创建图像
请求
POST https://api.openai.com/v1/images/generations HTTP/1.1
Host: api.openai.com
Authorization: Bearer 上面注册的APIKEY
User-Agent: Apifox/1.0.0 (https://www.apifox.cn)
Content-Type: application/json
Accept: */*
Host: api.openai.com
Connection: keep-alive
{
"prompt": "女朋友",
"n": 2,
"size": "1024x1024"
}
响应
{
"created": 1675993861,
"data": [
{
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-C6Ra2fYSAd37i0yVVenErs3k/user-QTxrrIrYc4e03H17ivLspji9/img-SSRanN8pAn5pYkjwFDthhSve.png?st=2023-02-10T00%3A51%3A01Z&se=2023-02-10T02%3A51%3A01Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-02-09T21%3A37%3A21Z&ske=2023-02-10T21%3A37%3A21Z&sks=b&skv=2021-08-06&sig=8fMSqkWqOy%2BsN4kT%2B6ys1vjDvmwnQ3l4x40Ihlj/FKI%3D"
},
{
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-C6Ra2fYSAd37i0yVVenErs3k/user-QTxrrIrYc4e03H17ivLspji9/img-O4LyFgAHMAElCaw1AfI1vstU.png?st=2023-02-10T00%3A51%3A01Z&se=2023-02-10T02%3A51%3A01Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-02-09T21%3A37%3A21Z&ske=2023-02-10T21%3A37%3A21Z&sks=b&skv=2021-08-06&sig=63PByqIMYZK9SiwNvhAZh6Ew7rEDswKCwxPUjIqsSLM%3D"
}
]
}