快速接入
目前OpenAI-CODE主要提供GPT全模型服务,详细支持的模型可查看支持模型
调用方式
- 先通过 openai-core注册一个帐号;
- 登录后,点击:【令牌】即可获得你的令牌;
- 复制令牌 将原请求头中的api_key 替换成本站的令牌
例如:
原来: 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
现在: 本站的令牌
原请求地址 api.openai.com的后面加上-core,即 api.openai-core.com
例如:
原来: https://api.openai.com/v1/chat/completions
现在: https://api.openai-core.com/v1/chat/completions
发票及对公业务等,请联系客服
🔥对话接口实例
- 实例是以对话(v1/chat/completions) 接口 作为样例;官方还有很多类型的结果
- 更多对话接口参数,请参考官网文档 https://platform.openai.com/docs/api-reference/chat/create
- 下面为各位准备好多实例
CURL 实例
curl --request POST \
--url https://api.openai-core.com/v1/chat/completions \
--header 'Authorization: Bearer 替换为你的令牌' \
-H "Content-Type: application/json" \
--data '{
"max_tokens": 1200,
"model": "gpt-3.5-turbo",
"temperature": 0.8,
"top_p": 1,
"presence_penalty": 1,
"messages": [
{
"role": "system",
"content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible."
},
{
"role": "user",
"content": "你是chatGPT多少?"
}
]
}'
curl --request POST \
--url https://api.openai-core.com/v1/chat/completions \
--header 'Authorization: Bearer 替换为你的令牌' \
-H "Content-Type: application/json" \
--data '{
"max_tokens": 1200,
"model": "gpt-3.5-turbo",
"temperature": 0.8,
"top_p": 1,
"presence_penalty": 1,
"messages": [
{
"role": "system",
"content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible."
},
{
"role": "user",
"content": "你是chatGPT多少?"
}
]
}'
如需调用4.0,可将上面modle中 gpt-3.5-turbo改为gpt-4或gpt-4-0613即可。
PHP 实例
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai-core.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer 替换为你的令牌';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = array(
'max_tokens' => 1200,
'model' => 'gpt-3.5-turbo',
'temperature' => 0.8,
'top_p' => 1,
'presence_penalty' => 1,
'messages' => array(
array(
'role' => 'system',
'content' => 'You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.'
),
array(
'role' => 'user',
'content' => '你是chatGPT多少?'
)
)
);
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai-core.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer 替换为你的令牌';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = array(
'max_tokens' => 1200,
'model' => 'gpt-3.5-turbo',
'temperature' => 0.8,
'top_p' => 1,
'presence_penalty' => 1,
'messages' => array(
array(
'role' => 'system',
'content' => 'You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.'
),
array(
'role' => 'user',
'content' => '你是chatGPT多少?'
)
)
);
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result;
如需调用4.0,可将上面modle中 gpt-3.5-turbo改为gpt-4或gpt-4-0613即可。
Python 实例
import requests
import json
url = "https://api.openai-core.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer 替换为你的令牌"
}
data = {
"max_tokens": 1200,
"model": "gpt-3.5-turbo",
"temperature": 0.8,
"top_p": 1,
"presence_penalty": 1,
"messages": [
{
"role": "system",
"content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible."
},
{
"role": "user",
"content": "你是chatGPT多少?"
}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data).encode('utf-8') )
result = response.content.decode("utf-8")
print(result)
import requests
import json
url = "https://api.openai-core.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer 替换为你的令牌"
}
data = {
"max_tokens": 1200,
"model": "gpt-3.5-turbo",
"temperature": 0.8,
"top_p": 1,
"presence_penalty": 1,
"messages": [
{
"role": "system",
"content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible."
},
{
"role": "user",
"content": "你是chatGPT多少?"
}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data).encode('utf-8') )
result = response.content.decode("utf-8")
print(result)
如需调用4.0,可将上面modle中 gpt-3.5-turbo改为gpt-4或gpt-4-0613即可。
javascript 实例
const axios = require('axios');
const url = 'https://api.openai-core.com/v1/chat/completions';
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer 替换为你的令牌'
};
const data = {
max_tokens: 1200,
model: 'gpt-3.5-turbo',
temperature: 0.8,
top_p: 1,
presence_penalty: 1,
messages: [
{
role: 'system',
content: 'You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.'
},
{
role: 'user',
content: '你是chatGPT多少?'
}
]
};
axios.post(url, data, { headers })
.then(response => {
const result = response.data;
console.log(result);
})
.catch(error => {
console.error(error);
});
const axios = require('axios');
const url = 'https://api.openai-core.com/v1/chat/completions';
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer 替换为你的令牌'
};
const data = {
max_tokens: 1200,
model: 'gpt-3.5-turbo',
temperature: 0.8,
top_p: 1,
presence_penalty: 1,
messages: [
{
role: 'system',
content: 'You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.'
},
{
role: 'user',
content: '你是chatGPT多少?'
}
]
};
axios.post(url, data, { headers })
.then(response => {
const result = response.data;
console.log(result);
})
.catch(error => {
console.error(error);
});
如需调用4.0,可将上面modle中 gpt-3.5-turbo改为gpt-4或gpt-4-0613即可。
typescript 实例
import axios from 'axios';
const url = 'https://api.openai-core.com/v1/chat/completions';
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer 替换为你的令牌'
};
const data = {
max_tokens: 1200,
model: 'gpt-3.5-turbo',
temperature: 0.8,
top_p: 1,
presence_penalty: 1,
messages: [
{
role: 'system',
content: 'You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.'
},
{
role: 'user',
content: '你是chatGPT多少?'
}
]
};
axios.post(url, data, { headers })
.then(response => {
const result = response.data;
console.log(result);
})
.catch(error => {
console.error(error);
});
import axios from 'axios';
const url = 'https://api.openai-core.com/v1/chat/completions';
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer 替换为你的令牌'
};
const data = {
max_tokens: 1200,
model: 'gpt-3.5-turbo',
temperature: 0.8,
top_p: 1,
presence_penalty: 1,
messages: [
{
role: 'system',
content: 'You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.'
},
{
role: 'user',
content: '你是chatGPT多少?'
}
]
};
axios.post(url, data, { headers })
.then(response => {
const result = response.data;
console.log(result);
})
.catch(error => {
console.error(error);
});
如需调用4.0,可将上面modle中 gpt-3.5-turbo改为gpt-4或gpt-4-0613即可。
java 实例
import okhttp3.*;
import java.io.IOException;
public class OpenAIChat {
public static void main(String[] args) throws IOException {
String url = "https://api.openai-core.com/v1/chat/completions";
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
String json = "{\n" +
" \"max_tokens\": 1200,\n" +
" \"model\": \"gpt-3.5-turbo\",\n" +
" \"temperature\": 0.8,\n" +
" \"top_p\": 1,\n" +
" \"presence_penalty\": 1,\n" +
" \"messages\": [\n" +
" {\n" +
" \"role\": \"system\",\n" +
" \"content\": \"You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.\"\n" +
" },\n" +
" {\n" +
" \"role\": \"user\",\n" +
" \"content\": \"你是chatGPT多少?\"\n" +
" }\n" +
" ]\n" +
"}";
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer 替换为你的令牌")
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
System.out.println(result);
}
}
import okhttp3.*;
import java.io.IOException;
public class OpenAIChat {
public static void main(String[] args) throws IOException {
String url = "https://api.openai-core.com/v1/chat/completions";
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
String json = "{\n" +
" \"max_tokens\": 1200,\n" +
" \"model\": \"gpt-3.5-turbo\",\n" +
" \"temperature\": 0.8,\n" +
" \"top_p\": 1,\n" +
" \"presence_penalty\": 1,\n" +
" \"messages\": [\n" +
" {\n" +
" \"role\": \"system\",\n" +
" \"content\": \"You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.\"\n" +
" },\n" +
" {\n" +
" \"role\": \"user\",\n" +
" \"content\": \"你是chatGPT多少?\"\n" +
" }\n" +
" ]\n" +
"}";
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer 替换为你的令牌")
.build();
Call call = client.newCall(request);
Response response = call.execute();
String result = response.body().string();
System.out.println(result);
}
}
如需调用4.0,可将上面modle中 gpt-3.5-turbo改为gpt-4或gpt-4-0613即可。
go 实例
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
url := "https://api.openai-core.com/v1/chat/completions"
apiKey := "替换为你的令牌"
payload := map[string]interface{}{
"max_tokens": 1200,
"model": "gpt-3.5-turbo",
"temperature": 0.8,
"top_p": 1,
"presence_penalty": 1,
"messages": []map[string]string{
{
"role": "system",
"content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.",
},
{
"role": "user",
"content": "你是chatGPT多少?",
},
},
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
fmt.Println("Error encoding JSON payload:", err)
return
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
if err != nil {
fmt.Println("Error creating HTTP request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error making API request:", err)
return
}
defer resp.Body.Close()
// 处理响应
// 请根据实际需求解析和处理响应数据
fmt.Println("Response HTTP Status:", resp.StatusCode)
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
url := "https://api.openai-core.com/v1/chat/completions"
apiKey := "替换为你的令牌"
payload := map[string]interface{}{
"max_tokens": 1200,
"model": "gpt-3.5-turbo",
"temperature": 0.8,
"top_p": 1,
"presence_penalty": 1,
"messages": []map[string]string{
{
"role": "system",
"content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.",
},
{
"role": "user",
"content": "你是chatGPT多少?",
},
},
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
fmt.Println("Error encoding JSON payload:", err)
return
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
if err != nil {
fmt.Println("Error creating HTTP request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error making API request:", err)
return
}
defer resp.Body.Close()
// 处理响应
// 请根据实际需求解析和处理响应数据
fmt.Println("Response HTTP Status:", resp.StatusCode)
}
如需调用4.0,可将上面modle中 gpt-3.5-turbo改为gpt-4或gpt-4-0613即可。
🚀js实现sse打字效果
注意
可node.js 后端运行;也支持浏览器模式;需要注意的是如果是前端 注意保护好你的令牌。
如何保护好令牌?可以跟nginx 配合 将header 部分的 'Authorization': 'Bearer
你的令牌'
放到 nginx当中
//记得引入 `axios`
const chatGPT=( msg, opt )=>{
let content='';
const dataPar=(data)=>{
let rz = {};
let dz= data.split('data:',2);
const str = dz[1].trim();
if(str=='[DONE]') rz={ finish:true,text:''};
else{
rz=JSON.parse(str);
rz.text= rz.choices[0].delta.content;
}
return rz ;
}
const dd= ( data )=>{
let arr = data.trim().split("\n\n");
let rz={text:'',arr:[]};
const atext= arr.map(v=>{
const aa= dataPar(v);
return aa.text;
});
rz.arr= atext;
rz.text= atext.join("");
if( opt.onMessage) opt.onMessage(rz);
return rz ;
}
return new Promise((resolve, reject) => {
axios({
method: 'post',
url: 'https://api.openai-core.com/v1/chat/completions',
data: {
"max_tokens": 1200,
"model": "gpt-3.5-turbo", //模型替换 如需调用4.0,改为gpt-4或gpt-4-0613即可
"temperature": 0.8,
"top_p": 1,
"presence_penalty": 1,
"messages": [
{
"role": "system",
"content": opt.system??"You are ChatGPT"
},
{
"role": "user",
"content": msg
}
],
"stream": true //数据流方式输出
},
headers:{
'Content-Type': 'application/json',
'Authorization': 'Bearer 你的令牌'
},
onDownloadProgress: e=>dd( e.target.responseText)
})
.then(d=> resolve(dd(d.data) ))
.catch(e=> reject(e ) );
})
}
//调用
chatGPT( '你是谁?'
,{
//system:'', //角色定义
onMessage: d=> console.log('过程性结果:',d.text )
}
).then( d=> console.log('✅最终结果:', d ) ).catch( e=> console.log('❎错误:', e ) );
//记得引入 `axios`
const chatGPT=( msg, opt )=>{
let content='';
const dataPar=(data)=>{
let rz = {};
let dz= data.split('data:',2);
const str = dz[1].trim();
if(str=='[DONE]') rz={ finish:true,text:''};
else{
rz=JSON.parse(str);
rz.text= rz.choices[0].delta.content;
}
return rz ;
}
const dd= ( data )=>{
let arr = data.trim().split("\n\n");
let rz={text:'',arr:[]};
const atext= arr.map(v=>{
const aa= dataPar(v);
return aa.text;
});
rz.arr= atext;
rz.text= atext.join("");
if( opt.onMessage) opt.onMessage(rz);
return rz ;
}
return new Promise((resolve, reject) => {
axios({
method: 'post',
url: 'https://api.openai-core.com/v1/chat/completions',
data: {
"max_tokens": 1200,
"model": "gpt-3.5-turbo", //模型替换 如需调用4.0,改为gpt-4或gpt-4-0613即可
"temperature": 0.8,
"top_p": 1,
"presence_penalty": 1,
"messages": [
{
"role": "system",
"content": opt.system??"You are ChatGPT"
},
{
"role": "user",
"content": msg
}
],
"stream": true //数据流方式输出
},
headers:{
'Content-Type': 'application/json',
'Authorization': 'Bearer 你的令牌'
},
onDownloadProgress: e=>dd( e.target.responseText)
})
.then(d=> resolve(dd(d.data) ))
.catch(e=> reject(e ) );
})
}
//调用
chatGPT( '你是谁?'
,{
//system:'', //角色定义
onMessage: d=> console.log('过程性结果:',d.text )
}
).then( d=> console.log('✅最终结果:', d ) ).catch( e=> console.log('❎错误:', e ) );
😁embeddings 接口
请求地址: POST https://api.openai-core.com/v1/embeddings
node.js 请求实例
const fetch = require("node-fetch");
fetch("https://api.openai-core.com/v1/embeddings", {
method: "POST",
headers: {
Authorization: "Bearer 替换为你的令牌",
"Content-Type": "application/json",
},
body: JSON.stringify({
input: "一起来使用ChatGPT",
model: "text-embedding-ada-002",
}),
});
const fetch = require("node-fetch");
fetch("https://api.openai-core.com/v1/embeddings", {
method: "POST",
headers: {
Authorization: "Bearer 替换为你的令牌",
"Content-Type": "application/json",
},
body: JSON.stringify({
input: "一起来使用ChatGPT",
model: "text-embedding-ada-002",
}),
});
😁moderations 接口
请求地址: POST https://api.openai-core.com/v1/moderations
node.js 请求实例
const fetch = require("node-fetch");
fetch("https://api.openai-core.com/v1/moderations", {
method: "POST",
headers: {
Authorization: "Bearer 替换为你的令牌",
"Content-Type": "application/json",
},
body: JSON.stringify({ input: "有人砍我" }),
});
const fetch = require("node-fetch");
fetch("https://api.openai-core.com/v1/moderations", {
method: "POST",
headers: {
Authorization: "Bearer 替换为你的令牌",
"Content-Type": "application/json",
},
body: JSON.stringify({ input: "有人砍我" }),
});
😁各种应用
现实中有好多应用,可以选择一种你喜欢的
chatgpt-web
docker 启动 默认模型是gpt-3.5
docker run --name chatgpt-web -d -p 6011:3002 \
--env OPENAI_API_KEY=替换为你的令牌 \
--env TIMEOUT_MS=600000 --env OPENAI_MAX_TOKEN=1000 \
--env OPENAI_API_BASE_URL=https://api.openai-core.com chenzhaoyu94/chatgpt-web
docker run --name chatgpt-web -d -p 6011:3002 \
--env OPENAI_API_KEY=替换为你的令牌 \
--env TIMEOUT_MS=600000 --env OPENAI_MAX_TOKEN=1000 \
--env OPENAI_API_BASE_URL=https://api.openai-core.com chenzhaoyu94/chatgpt-web
chatgpt-web gpt-4
默认模型是gpt-3.5 如何起一个默认模型 gpt-4.0 呢? 使用环境变量
OPENAI_API_MODEL
docker run --name chatgpt-web -d -p 6040:3002 \
--env OPENAI_API_KEY=替换为你的令牌 \
--env TIMEOUT_MS=600000 --env OPENAI_MAX_TOKEN=1000 \
--env OPENAI_API_MODEL=gpt-4-0613 \
--env OPENAI_API_BASE_URL=https://api.openai-core.com chenzhaoyu94/chatgpt-web
docker run --name chatgpt-web -d -p 6040:3002 \
--env OPENAI_API_KEY=替换为你的令牌 \
--env TIMEOUT_MS=600000 --env OPENAI_MAX_TOKEN=1000 \
--env OPENAI_API_MODEL=gpt-4-0613 \
--env OPENAI_API_BASE_URL=https://api.openai-core.com chenzhaoyu94/chatgpt-web
可选模型有哪些
模型 | 说明 |
---|---|
gpt-4o-2024-08-06 | |
gpt-4 | |
gpt-4-0314 | |
gpt-4-1106-preview | |
gpt-4-0125-preview | |
gpt-4-turbo-preview | |
gpt-4-turbo | |
gpt-4-turbo-2024-04-09 | |
gpt-4o | |
gpt-4o-2024-05-13 | |
text-embedding-ada-002 | |
text-embedding-3-small | |
text-embedding-3-large | |
text-davinci-003 | |
text-davinci-002 | |
text-curie-001 | |
text-babbage-001 | |
text-ada-001 | |
text-davinci-edit-001 | |
code-davinci-edit-001 | |
dall-e-2 | |
dall-e-3 | |
tts-1 | |
tts-1-1106 | |
tts-1-hd | |
tts-1-hd-1106 | |
gpt-3.5-turbo | |
gpt-3.5-turbo-16k | |
gpt-3.5-turbo-1106 | |
gpt-3.5-turbo-0125 | |
gpt-3.5-turbo-instruct | |
gpt-4-0613 | |
text-moderation-latest | |
text-moderation-stable | |
text-moderation-007 |
看效果
chatgpt-next-web
docker run --name chatgpt-next-web -d -p 6013:3000 \
-e OPENAI_API_KEY="替换为你的令牌" \
-e BASE_URL=https://api.openai-core.com yidadaa/chatgpt-next-web
docker run --name chatgpt-next-web -d -p 6013:3000 \
-e OPENAI_API_KEY="替换为你的令牌" \
-e BASE_URL=https://api.openai-core.com yidadaa/chatgpt-next-web
看效果
其他
如果还有其应用 请联系我们客服协助