便携AI聚合API支持OpenAI的GPT全模型,包括GPT-3.5(最新的gpt-3.5-turbo模型)、GPT-4.0(gpt-4、gpt-4-32k、gpt-4-turbo、gpt-4-vision-preview、gpt-4o等),下面分享下GPT模型接入教程。
一、前言
本文参考OpenAI官方API文档:
- API reference:https://platform.openai.com/docs/api-reference
- Docs:https://platform.openai.com/docs/overview
文中使用的API Key均为便携AI聚合API后台生成的令牌,以sk-开头的一串随机字符,获取方法:《便携AI聚合API新建令牌(API key)教程》。
便携AI聚合API有三个接入地址(即URL),一般推荐选择第一个或者第二个:
- 中转API调用地址①(中国香港服务器,直连线路,带宽大):
https://api.bianxie.ai
- 中转API调用地址②(国内上海服务器,带宽稍小):
https://api.bianxieai.com
- 中转API调用地址③(国外服务器,也可以直连,备用):
https://api.a8.hk
(三个网站都可以登录账号,数据同步)
模型支持各种语言接入,包括python、PHP、C#、C、Ruby、Java、Go、JavaScript等,本文主要分享官方的CURL调用方法,以及基于python的调用方法,如果你是用其他语言调用API的,则直接问ChatGPT或者其他语言模型怎么改写就行了,如下图:
二、GPT模型接入教程
1、列出模型种类
OpenAI官方分享的调用方法:
curl https://api.bianxie.ai/v1/models \
-H "Authorization: Bearer $OPENAI_API_KEY" \
curl https://api.bianxie.ai/v1/models \
-H "Authorization: Bearer $OPENAI_API_KEY" \
curl https://api.bianxie.ai/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY" \
改写成python:
import requests
url = "https://api.bianxie.ai/v1/models"
headers = {
"Authorization": "Bearer sk-Xy3WuCpTTvY",
}
response = requests.get(url, headers=headers)
print(response.json())
import requests
url = "https://api.bianxie.ai/v1/models"
headers = {
"Authorization": "Bearer sk-Xy3WuCpTTvY",
}
response = requests.get(url, headers=headers)
print(response.json())
import requests url = "https://api.bianxie.ai/v1/models" headers = { "Authorization": "Bearer sk-Xy3WuCpTTvY", } response = requests.get(url, headers=headers) print(response.json())
返回示例:
{
'data': [{
'id': 'gpt-3.5-turbo',
'object': 'model',
'created': 1626777600,
'owned_by': 'openai',
'permission': [{
'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ',
'object': 'model_permission',
'created': 1626777600,
'allow_create_engine': True,
'allow_sampling': True,
'allow_logprobs': True,
'allow_search_indices': False,
'allow_view': True,
'allow_fine_tuning': False,
'organization': '*',
'group': None,
'is_blocking': False
}],
'root': 'gpt-3.5-turbo',
'parent': None
}, {
'id': 'gpt-3.5-turbo-0125',
'object': 'model',
'created': 1626777600,
'owned_by': 'openai',
'permission': [{
'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ',
'object': 'model_permission',
'created': 1626777600,
'allow_create_engine': True,
'allow_sampling': True,
'allow_logprobs': True,
'allow_search_indices': False,
'allow_view': True,
'allow_fine_tuning': False,
'organization': '*',
'group': None,
'is_blocking': False
}],
'root': 'gpt-3.5-turbo-0125',
'parent': None
}, {
'id': 'gpt-4',
'object': 'model',
'created': 1626777600,
'owned_by': 'openai',
'permission': [{
'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ',
'object': 'model_permission',
'created': 1626777600,
'allow_create_engine': True,
'allow_sampling': True,
'allow_logprobs': True,
'allow_search_indices': False,
'allow_view': True,
'allow_fine_tuning': False,
'organization': '*',
'group': None,
'is_blocking': False
}],
'root': 'gpt-4',
'parent': None
}, {
'id': 'gpt-4-0125-preview',
'object': 'model',
'created': 1626777600,
'owned_by': 'openai',
'permission': [{
'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ',
'object': 'model_permission',
'created': 1626777600,
'allow_create_engine': True,
'allow_sampling': True,
'allow_logprobs': True,
'allow_search_indices': False,
'allow_view': True,
'allow_fine_tuning': False,
'organization': '*',
'group': None,
'is_blocking': False
}],
'root': 'gpt-4-0125-preview',
'parent': None
}]
}
{
'data': [{
'id': 'gpt-3.5-turbo',
'object': 'model',
'created': 1626777600,
'owned_by': 'openai',
'permission': [{
'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ',
'object': 'model_permission',
'created': 1626777600,
'allow_create_engine': True,
'allow_sampling': True,
'allow_logprobs': True,
'allow_search_indices': False,
'allow_view': True,
'allow_fine_tuning': False,
'organization': '*',
'group': None,
'is_blocking': False
}],
'root': 'gpt-3.5-turbo',
'parent': None
}, {
'id': 'gpt-3.5-turbo-0125',
'object': 'model',
'created': 1626777600,
'owned_by': 'openai',
'permission': [{
'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ',
'object': 'model_permission',
'created': 1626777600,
'allow_create_engine': True,
'allow_sampling': True,
'allow_logprobs': True,
'allow_search_indices': False,
'allow_view': True,
'allow_fine_tuning': False,
'organization': '*',
'group': None,
'is_blocking': False
}],
'root': 'gpt-3.5-turbo-0125',
'parent': None
}, {
'id': 'gpt-4',
'object': 'model',
'created': 1626777600,
'owned_by': 'openai',
'permission': [{
'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ',
'object': 'model_permission',
'created': 1626777600,
'allow_create_engine': True,
'allow_sampling': True,
'allow_logprobs': True,
'allow_search_indices': False,
'allow_view': True,
'allow_fine_tuning': False,
'organization': '*',
'group': None,
'is_blocking': False
}],
'root': 'gpt-4',
'parent': None
}, {
'id': 'gpt-4-0125-preview',
'object': 'model',
'created': 1626777600,
'owned_by': 'openai',
'permission': [{
'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ',
'object': 'model_permission',
'created': 1626777600,
'allow_create_engine': True,
'allow_sampling': True,
'allow_logprobs': True,
'allow_search_indices': False,
'allow_view': True,
'allow_fine_tuning': False,
'organization': '*',
'group': None,
'is_blocking': False
}],
'root': 'gpt-4-0125-preview',
'parent': None
}]
}
{ 'data': [{ 'id': 'gpt-3.5-turbo', 'object': 'model', 'created': 1626777600, 'owned_by': 'openai', 'permission': [{ 'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ', 'object': 'model_permission', 'created': 1626777600, 'allow_create_engine': True, 'allow_sampling': True, 'allow_logprobs': True, 'allow_search_indices': False, 'allow_view': True, 'allow_fine_tuning': False, 'organization': '*', 'group': None, 'is_blocking': False }], 'root': 'gpt-3.5-turbo', 'parent': None }, { 'id': 'gpt-3.5-turbo-0125', 'object': 'model', 'created': 1626777600, 'owned_by': 'openai', 'permission': [{ 'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ', 'object': 'model_permission', 'created': 1626777600, 'allow_create_engine': True, 'allow_sampling': True, 'allow_logprobs': True, 'allow_search_indices': False, 'allow_view': True, 'allow_fine_tuning': False, 'organization': '*', 'group': None, 'is_blocking': False }], 'root': 'gpt-3.5-turbo-0125', 'parent': None }, { 'id': 'gpt-4', 'object': 'model', 'created': 1626777600, 'owned_by': 'openai', 'permission': [{ 'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ', 'object': 'model_permission', 'created': 1626777600, 'allow_create_engine': True, 'allow_sampling': True, 'allow_logprobs': True, 'allow_search_indices': False, 'allow_view': True, 'allow_fine_tuning': False, 'organization': '*', 'group': None, 'is_blocking': False }], 'root': 'gpt-4', 'parent': None }, { 'id': 'gpt-4-0125-preview', 'object': 'model', 'created': 1626777600, 'owned_by': 'openai', 'permission': [{ 'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ', 'object': 'model_permission', 'created': 1626777600, 'allow_create_engine': True, 'allow_sampling': True, 'allow_logprobs': True, 'allow_search_indices': False, 'allow_view': True, 'allow_fine_tuning': False, 'organization': '*', 'group': None, 'is_blocking': False }], 'root': 'gpt-4-0125-preview', 'parent': None }] }
2、开始聊天对话
chat接口支持多种参数,必须的就是model
和messages
,其他参数如temperature
、max_tokens
可以让你自定义这次请求的设置,具体每个参数的意思大家可以参考官网。
OpenAI官方分享的调用方法:
curl https://api.bianxie.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}'
curl https://api.bianxie.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}'
curl https://api.bianxie.ai/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-4o", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7 }'
改写成python:
import requests
api_key = 'sk-xxxxx'
url = 'https://api.bianxie.ai/v1/chat/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
data = {
'model': 'gpt-4o',
'messages': [{'role': 'user', 'content': 'Say this is a test!'}],
'temperature': 0.7
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
import requests
api_key = 'sk-xxxxx'
url = 'https://api.bianxie.ai/v1/chat/completions'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
data = {
'model': 'gpt-4o',
'messages': [{'role': 'user', 'content': 'Say this is a test!'}],
'temperature': 0.7
}
response = requests.post(url, headers=headers, json=data)
print(response.json())
import requests api_key = 'sk-xxxxx' url = 'https://api.bianxie.ai/v1/chat/completions' headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}' } data = { 'model': 'gpt-4o', 'messages': [{'role': 'user', 'content': 'Say this is a test!'}], 'temperature': 0.7 } response = requests.post(url, headers=headers, json=data) print(response.json())
返回示例:
{
'id': 'chatcmpl-6FFeIRDxm9wh2zDhPpajrlU7CDGzI',
'model': 'gpt-4o',
'object': 'chat.completion',
'created': 1717544390,
'choices': [{
'index': 0,
'message': {
'role': 'assistant',
'content': 'This is a test!'
},
'finish_reason': 'stop'
}],
'usage': {
'prompt_tokens': 21,
'completion_tokens': 5,
'total_tokens': 26
}
}
{
'id': 'chatcmpl-6FFeIRDxm9wh2zDhPpajrlU7CDGzI',
'model': 'gpt-4o',
'object': 'chat.completion',
'created': 1717544390,
'choices': [{
'index': 0,
'message': {
'role': 'assistant',
'content': 'This is a test!'
},
'finish_reason': 'stop'
}],
'usage': {
'prompt_tokens': 21,
'completion_tokens': 5,
'total_tokens': 26
}
}
{ 'id': 'chatcmpl-6FFeIRDxm9wh2zDhPpajrlU7CDGzI', 'model': 'gpt-4o', 'object': 'chat.completion', 'created': 1717544390, 'choices': [{ 'index': 0, 'message': { 'role': 'assistant', 'content': 'This is a test!' }, 'finish_reason': 'stop' }], 'usage': { 'prompt_tokens': 21, 'completion_tokens': 5, 'total_tokens': 26 } }