近期有个很火的论题啊,那就是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": "\n\n§ Code\n\nimport turtle\n\nturtle.setup(800, 600)\nturtle.penup()\nturtle.goto(0, -200)\nturtle.pendown()\nturtle.pensize(2)\nturtle.pencolor('red')\n\n# 画花瓣\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"
}
]
}