首页 > 其他分享 >[初级]原生代码请求completions接口,完成gpt3.5 /4 接口调用

[初级]原生代码请求completions接口,完成gpt3.5 /4 接口调用

时间:2024-03-08 18:01:00浏览次数:31  
标签:completions gpt3.5 system 接口 content 3.5 gpt null data

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

 

gpt 4流式请求不在演示

查看 白菜GPT 会员中心 消费明细 核实无误

 

 白菜GPT 免费GPT3.5/4服务入口[二维码说明:白菜GPT会员服务官网]

标签:completions,gpt3.5,system,接口,content,3.5,gpt,null,data
From: https://www.cnblogs.com/MyXQ/p/18061561

相关文章

  • 使用go写了一个简单的http接口
    使用go写了一个简单的http接口packagemainimport( "fmt" "net/http" "os/exec" "sync" "time")var( requestCountint lastTimestamptime.Time musync.Mutex)const( maxRequests=30 resetDu......
  • .net api接口接收字符串或者xml
    a///<summary>///接收字符串或者xml///</summary>///<returns></returns>[HttpPost]publicasyncTask<JsonResult>AcceptXMLOrString(){varlen=Convert.ToInt32(HttpContext.Request.ContentLength);byte[]byt=newbyte......
  • API电商接口大数据分析与数据挖掘 (商品详情店铺)
    API接口、数据分析以及数据挖掘在商品详情和店铺相关的应用中,各自扮演着重要的角色。以下是关于它们各自的功能以及如何在商品详情和店铺分析中协同工作的简要说明。API接口API(应用程序接口)是一种允许不同软件应用程序相互通信的方式。在商品详情和店铺分析的上下文中,API接口可......
  • c# RestClient 请求接口
    引用RestSharp.dll//人员信息RestClientclient=newRestClient("url");client.Timeout=-1;varrequest=newRestRequest(Method.POST);request.AddHeader("appId",appId);......
  • 自动化之接口测试基础
    1.接口是啥?==>传递数据的通道接口测试:是测试系统组件间接口的一种测试接口测试主要包括:1.同一个系统中模块与模块间的接口(内部接口)2.跨系统平台与平台间的对接(外部接口)接口类型:1.HTTP接口:通过get或post等来获取数据,在数据处理上效率比较高  2.Webservice接口:通过soap协议来......
  • 多态实际例子,接口实际例子
    //业务:订单需要支持多种支付方式。换句话:订单需要支持多种支付【方法】//订单里面需要通过【调用同一方法】但是要求有【不同表现形式】————怎么才能【调用同一方法】能够有【不同表现形式】?记住通过【不同的对象调用同一方法】就可以实现【不同表现形式】,这个对象是传进......
  • Swagger-自动生成接口文档
    Swagger官网:https://swagger.io/Swagger3是接口文档生成工具依赖pom.xml<dependency> <groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi2-spring-boot-starter</artifactId><version>4.4.0</version></......
  • 安装ML.NET命令行接口(CLI)工具遇到的坑
    根据微软官方文档(https://learn.microsoft.com/zh-cn/dotnet/machine-learning/how-to-guides/install-ml-net-cli?tabs=windows)安装ML.NET命令行接口(CLI)工具时执行如下命令:dotnettoolinstall--globalmlnet-win-x64始终报从指定的Nuget源中找不到对应的工具: 以为是......
  • SpringBoot 支付宝付款接口类、支付异步回调函数模板
    1.付款接口类1.1.引入Maven依赖<dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><version>4.38.221.ALL</version></dependency>1.2.将下面代码保存为AlipayTemplate.java@Config......
  • 高德接口-前端本地开发调用代理
    请求中api:exportfunctionjPAmapApi(data){returnrequest({url:'/v4/grasproad/driving?key=b03c9b62740951fd7fb9bba7f5be1c4f',method:'post',data:data,headers:{'content-type':'applicatio......