本文演示基于白菜GPT转发APIKEY实现gpt-3.5、gpt-4对话请求及流式请求
复制如下代码,替换baicai-xxxxxxxx部分为你的 白菜API KEY
发起gpt-3.5请求
import http.client import json conn = http.client.HTTPSConnection("api.baicaigpt.com") payload = json.dumps({ "model": "gpt-3.5-turbo", "messages": [ { "role": "system", "content": "你是小红书写作助理." }, { "role": "user", "content": "写一篇口红的种草文案!" } ] }) headers = { 'Authorization': 'Bearer bacai-XXXXXX', 'Content-Type': 'application/json' } conn.request("POST", "/v1/chat/completions", payload, headers) res = conn.getresponse() data = res.read() print(data.decode("utf-8"))
gpt-3.5返回结果如下
{ "id": "chatcmpl-8xvUcih3W8kb20uMgwA3aTuSUaPGn", "object": "chat.completion", "created": 1709293046, "model": "gpt-3.5-turbo-0125", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "标题:让你的嘴唇闪耀光芒!小红书推荐口红试色~\n\n正文:\n\n有人说,口红是一个女人的法宝,可以瞬间改变你的气质和形象。在选择口红时,不仅要考虑颜色的质感和持久度,还要考虑到适合自己的肤色和个人风格。今天小红书为大家推荐几款性价比超高的口红,让你的嘴唇闪耀光芒!\n\n1. 娇韵诗唇膏\n\n娇韵诗的唇膏质地柔滑,色彩饱满,丰盈润泽,让双唇更具魅力。不论是哑光、珠光还是光泽,都能满足你不同场合和心情的需求。尤其推荐他们家的经典红色系列,绝对是必备单品!\n\n2. 雅诗兰黛无痕哑光唇膏\n\n这款唇膏的持久度非常出色,一次涂抹便能持续整天,不会褪色或掉妆。而且哑光质地让双唇看起来更加饱满丰润,宛若丝绒般柔软。无论是日常妆容还是晚宴妆,都能轻松驾驭。\n\n3. 迪奥烈艳蓝金唇膏\n\n迪奥家的唇膏一直备受追捧,尤其是烈艳蓝金系列,不仅包装精美奢华,而且色彩艳丽持久。每一款都带有迪奥独有的花香,给人一种舒适的感觉。如果你喜欢高贵优雅的风格,不妨试试这系列口红!\n\n以上是小红书为大家推荐的几款口红,每一款都有其独特的魅力,相信总有一款适合你。赶紧入手,让你的双唇闪耀光芒,展现自信迷人的魅力!" }, "logprobs": null, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 37, "completion_tokens": 672, "total_tokens": 709 }, "system_fingerprint": "fp_86156a94a0" }
发起gpt-4请求
import http.client import json conn = http.client.HTTPSConnection("api.baicaigpt.com") payload = json.dumps({ "model": "gpt-4-turbo-preview", "messages": [ { "role": "system", "content": "你是小红书写作助理." }, { "role": "user", "content": "写一篇口红的种草文案!" } ] }) headers = { 'Authorization': 'Bearer bacai-XXXXXX', 'Content-Type': 'application/json' } conn.request("POST", "/v1/chat/completions", payload, headers) res = conn.getresponse() data = res.read() print(data.decode("utf-8"))
gpt-4返回结果如下
{ "id": "chatcmpl-8xvSaFy2pyluqEOUrYcGznsW2wApL", "object": "chat.completion", "created": 1709292920, "model": "gpt-4-0125-preview", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "标题:不可错过的春季唇色:令你瞬间提升气质的口红种草\n\n正文:\n春天,是一年中最适合谈恋爱的季节,也是更新换代口红色号的绝佳时机。今天,我要为大家种草一款无论是日常出街还是约会必备,都能令你瞬间提升气质,吸引所有目光的神仙口红。\n\n首先,让我们揭晓这款神仙口红的名字——纯情玫瑰豆沙色。就像它的名字一样,这款口红含有浓厚的玫瑰韵味,加上豆沙色的自然柔和,无论涂抹一层还是叠加数层,都能展现出不同的风情,适合各种场合与肤色。\n\n接下来是关于它的质地。这款口红采用了滋润不干燥的配方,即便是干燥的春风也挡不住它的润泽度。轻轻一抹,不仅能改善唇纹,还能为你的双唇带来长时间的保湿效果,让双唇时刻保持饱满和光泽感。\n\n最令人惊喜的,还是它的持久度。很多小伙伴都有这样的烦恼,吃东西或是长时间不补妆就会让口红色号脱落。但这款口红,能够确保你从早晨到晚上,无需频繁补妆,保持完美妆容。\n\n不得不提的是,它的包装设计也极具特色。采用简约而不失优雅的设计,无论是放在化妆包里还是随身携带,都极具时尚感。每次拿出来补妆,都能成为一道亮丽的风景线。\n\n最后,说到这款口红的性价比,可以说是物超所值。相较于同级别的大品牌口红,它不但拥有卓越的品质,更是价格亲民,绝对是追求高性价比的小仙女们的最佳选择。\n\n春日的温暖阳光下,搭配一款纯情玫瑰豆沙色口红,无疑是提升你气质和魅力的最佳方式。它不仅能让你在人群中脱颖而出,更能让你的春日生活充满色彩和活力。快来尝试一下,让它成为你这个春天不可或缺的美妆好物吧!" }, "logprobs": null, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 37, "completion_tokens": 827, "total_tokens": 864 }, "system_fingerprint": "fp_00ceb2df5b" }
发起gpt3.5流式请求
import http.client import json conn = http.client.HTTPSConnection("api.baicaigpt.com") payload = json.dumps({ "model": "gpt-3.5-turbo", "stream":True, "messages": [ { "role": "system", "content": "你是小红书写作助理." }, { "role": "user", "content": "写一篇口红的种草文案!" } ] }) headers = { 'Authorization': 'Bearer bacai-XXXXXX', 'Content-Type': 'application/json' } conn.request("POST", "/v1/chat/completions", payload, headers) res = conn.getresponse() data = res.read() print(data.decode("utf-8"))
gpt3.5流式请求返回结果如下
data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"当"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"你"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"看"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"到"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"这"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"支"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"口"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"红"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":","},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"是"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"不"},"logprobs":null,"finish_reason":null}]} data: {"id":"chatcmpl-8xvW2acIVJRXQ1vZUFSTdWQ4XX2MF","object":"chat.completion.chunk","created":1709293134,"model":"gpt-3.5-turbo-0125","system_fingerprint":"fp_2b778c6b35","choices":[{"index":0,"delta":{"content":"是"},"logprobs":null,"finish_reason":null}]} … data: [DONE] Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings…