调用开源api
POSTMAN
这里我用的是postman,主要看请求头、body 、其它工具语言都是一致的
参数说明
地址 : https://api.openai.com/v1/chat/completions
请求方式 :post
请求头 :
application/json
Content-Type:application/json
Authorization: Bearer 你的API_KEY
请求示例
// body
{"model":"gpt-3.5-turbo","messages":[{"role":"system","content":"You are a helpful assistant"},{"content":"将一个笑话","role":"user"}]
}//上下文版本 就是把你的问答和他的回答加入messages参数即可{"model":"gpt-3.5-turbo","messages":[{"role":"system","content":"You are a helpful assistant"},{"content":"将一个笑话","role":"user"},{"role": "assistant","content": "好的,给你讲一个笑话吧:\n\n有一天,一个男人去医生那里看病,医生让他躺在床上,然后拿出一根大棒子,对他说:“现在我要敲你的膝盖了,你就说‘啊’。”然后医生就开始用力敲他的膝盖,男人却一声不吭。医生奇怪地问他:“你怎么不说‘啊’呢?”男人回答说:“我已经把我的喉咙准备好了,可是我的膝盖没听到你的棒子声啊!”"},{"content":"可以解释一下吗,我没太听懂","role":"user"}]
}
请求截图及代码
请求Java代码
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\r\n \"model\":\"gpt-3.5-turbo\",\r\n \"messages\":[\r\n {\r\n \"role\":\"system\",\"content\":\"You are a helpful assistant\"\r\n },\r\n {\r\n \"content\":\"将一个笑话\",\r\n \"role\":\"user\"\r\n },\r\n {\r\n \"role\": \"assistant\",\r\n \"content\": \"好的,给你讲一个笑话吧:\\n\\n有一天,一个男人去医生那里看病,医生让他躺在床上,然后拿出一根大棒子,对他说:“现在我要敲你的膝盖了,你就说‘啊’。”然后医生就开始用力敲他的膝盖,男人却一声不吭。医生奇怪地问他:“你怎么不说‘啊’呢?”男人回答说:“我已经把我的喉咙准备好了,可是我的膝盖没听到你的棒子声啊!”\"\r\n },\r\n {\r\n \"content\":\"可以解释一下吗,我没太听懂\",\r\n \"role\":\"user\"\r\n }\r\n ]\r\n}");
Request request = new Request.Builder().url("https://api.openai.com/v1/chat/completions").method("POST", body).addHeader("Authorization", "Bearer APIKEY").addHeader("Content-Type", "application/json").build();
Response response = client.newCall(request).execute();
python
import http.client
import jsonconn = http.client.HTTPSConnection("api.openai.com")
payload = json.dumps({"model": "gpt-3.5-turbo","messages": [{"role": "system","content": "You are a helpful assistant"},{"content": "将一个笑话","role": "user"},{"role": "assistant","content": "好的,给你讲一个笑话吧:\n\n有一天,一个男人去医生那里看病,医生让他躺在床上,然后拿出一根大棒子,对他说:“现在我要敲你的膝盖了,你就说‘啊’。”然后医生就开始用力敲他的膝盖,男人却一声不吭。医生奇怪地问他:“你怎么不说‘啊’呢?”男人回答说:“我已经把我的喉咙准备好了,可是我的膝盖没听到你的棒子声啊!”"},{"content": "可以解释一下吗,我没太听懂","role": "user"}]
})
headers = {'Authorization': 'Bearer APIKEY','Content-Type': 'application/json'
}
conn.request("POST", "/v1/chat/completions", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
JS jQuery
var settings = {"url": "https://api.openai.com/v1/chat/completions","method": "POST","timeout": 0,"headers": {"Authorization": "Bearer APIKEY","Content-Type": "application/json"},"data": JSON.stringify({"model": "gpt-3.5-turbo","messages": [{"role": "system","content": "You are a helpful assistant"},{"content": "将一个笑话","role": "user"},{"role": "assistant","content": "好的,给你讲一个笑话吧:\n\n有一天,一个男人去医生那里看病,医生让他躺在床上,然后拿出一根大棒子,对他说:“现在我要敲你的膝盖了,你就说‘啊’。”然后医生就开始用力敲他的膝盖,男人却一声不吭。医生奇怪地问他:“你怎么不说‘啊’呢?”男人回答说:“我已经把我的喉咙准备好了,可是我的膝盖没听到你的棒子声啊!”"},{"content": "可以解释一下吗,我没太听懂","role": "user"}]}),
};$.ajax(settings).done(function (response) {console.log(response);
});