本文演示基於白菜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…