当前位置: 首页 > news >正文

AI Chat API 对接说明

AI Chat API 对接说明

我们知道,市面上一些问答 API 的对接还是相对没那么容易的,比如说 OpenAI 的 Chat Completions API,它有一个messages字段,如果要完成连续对话,需要我们把所有的上下文历史全部传递,同时还需要处理 Token 超出限制的问题。

AceDataCloud 提供的 AI 问答 API 针对上述情况进行了优化,在保证问答效果不变的情况下,对连续对话的实现进行了封装,对接时无需再关心 messages 的传递,也无需关心 Token 超出限制的问题(API 内部自动进行了处理),同时也提供了对话查询、修改等功能,使得整体的对接大大简化。

本文档会介绍下 AI 问答 API 的对接说明。

申请流程

要使用 API,需要先到 AI 问答 API 对应页面申请对应的服务,进入页面之后,点击「Acquire」按钮,如图所示:

如果你尚未登录或注册,会自动跳转到登录页面邀请您来注册和登录,登录注册之后会自动返回当前页面。

在首次申请时会有免费额度赠送,可以免费使用该 API。

基本使用

首先先了解下基本的使用方式,就是输入问题,获得回答,只需要简单地传递一个question字段,并指定相应模型即可。

比如说询问:“What’s your name?”,我们接下来就可以在界面上填写对应的内容,如图所示:

可以看到这里我们设置了 Request Headers,包括:

  • accept:想要接收怎样格式的响应结果,这里填写为application/json,即 JSON 格式。
  • authorization:调用 API 的密钥,申请之后可以直接下拉选择。

另外设置了 Request Body,包括:

  • model:模型的选择,比如主流的 GPT 3.5,GPT 4 等。
  • question:需要询问的问题,可以是任意的纯文本。

选择之后,可以发现右侧也生成了对应代码,如图所示:

点击「Try」按钮即可进行测试,如上图所示,这里我们就得到了如下结果:
{"answer":"I am an AI language model developed by OpenAI and I don't have a personal name. However, you can call me GPT or simply Chatbot. How can I assist you today?"}

可以看到,这里返回的结果中有一个answer字段,就是该问题的回答。我们可以输入任意问题,就可以得到任意的回答。

如果你不需要任何多轮对话的支持,这个 API 可以极大方便你的对接。

另外如果想生成对应的对接代码,可以直接复制生成,例如 CURL 的代码如下:

curl-XPOST'https://api.acedata.cloud/aichat/conversations'\-H'accept: application/json'\-H'authorization: Bearer {token}'\-H'content-type: application/json'\-d'{ "model": "gpt-3.5", "question": "What's your name?"}'

Python 的对接代码如下:

importrequests url="https://api.acedata.cloud/aichat/conversations"headers={"accept":"application/json","authorization":"Bearer {token}","content-type":"application/json"}payload={"model":"gpt-3.5","question":"What's your name?"}response=requests.post(url,json=payload,headers=headers)print(response.text)

多轮对话

如果您想要对接多轮对话功能,需要传递一个额外参数stateful,其值为true,后续的每次请求都要携带该参数。传递了stateful参数之后,API 会额外返回一个id参数,代表当前对话的 ID,后续我们只需要将该 ID 作为参数传递,就可以轻松实现多轮对话。

下面我们来演示下具体的操作。

第一次请求,将stateful参数设置为true,并正常传递modelquestion参数,如图所示:

对应代码如下:

curl-XPOST'https://api.acedata.cloud/aichat/conversations'\-H'accept: application/json'\-H'authorization: Bearer {token}'\-H'content-type: application/json'\-d'{ "model": "gpt-3.5", "question": "What's your name?", "stateful":true}'

可以得到如下回答:

{"answer":"I am an AI language model created by OpenAI and I don't have a personal name. You can simply call me OpenAI or ChatGPT. How can I assist you today?","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}

第二次请求,将第一次请求返回的id字段作为参数传递,同时stateful参数依然设置为true,询问「What I asked you just now?」,如图所示:

对应代码如下:

curl-XPOST'https://api.acedata.cloud/aichat/conversations'\-H'accept: application/json'\-H'authorization: Bearer {token}'\-H'content-type: application/json'\-d'{ "model": "gpt-3.5", "stateful": true, "id": "7cdb293b-2267-4979-a1ec-48d9ad149916", "question": "What I asked you just now?" }'

结果如下:

{"answer":"You asked me what my name is. As an AI language model, I do not possess a personal identity, so I don't have a specific name. However, you can refer to me as OpenAI or ChatGPT, the names used for this AI model. Is there anything else I can help you with?","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}

可以看到,就可以根据上下文回答对应的问题了。

流式响应

该接口也支持流式响应,这对网页对接十分有用,可以让网页实现逐字显示效果。

如果想流式返回响应,可以更改请求头里面的accept参数,修改为application/x-ndjson

修改如图所示,不过调用代码需要有对应的更改才能支持流式响应。

accept修改为application/x-ndjson之后,API 将逐行返回对应的 JSON 数据,在代码层面我们需要做相应的修改来获得逐行的结果。

Python 样例调用代码:

importrequests url="https://api.acedata.cloud/aichat/conversations"headers={"accept":"application/x-ndjson","authorization":"Bearer {token}","content-type":"application/json"}payload={"model":"gpt-3.5","stateful":True,"id":"7cdb293b-2267-4979-a1ec-48d9ad149916","question":"Hello"}response=requests.post(url,json=payload,headers=headers,stream=True)forlineinresponse.iter_lines():print(line.decode())

输出效果如下:

{"answer":"Hello","delta_answer":"Hello","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}{"answer":"Hello!","delta_answer":"!","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}{"answer":"Hello! How","delta_answer":" How","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}{"answer":"Hello! How can","delta_answer":" can","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}{"answer":"Hello! How can I","delta_answer":" I","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}{"answer":"Hello! How can I assist","delta_answer":" assist","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}{"answer":"Hello! How can I assist you","delta_answer":" you","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}{"answer":"Hello! How can I assist you today","delta_answer":" today","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}{"answer":"Hello! How can I assist you today?","delta_answer":"?","id":"7cdb293b-2267-4979-a1ec-48d9ad149916"}

可以看到,响应里面的answer即为最新的回答内容,delta_answer则是新增的回答内容,您可以根据结果来对接到您的系统中。

JavaScript 也是支持的,比如 Node.js 的流式调用代码如下:

constaxios=require("axios");consturl="https://api.acedata.cloud/aichat/conversations";constheaders={"Content-Type":"application/json",Accept:"application/x-ndjson",Authorization:"Bearer {token}",};constbody={question:"Hello",model:"gpt-3.5",stateful:true,};axios.post(url,body,{headers:headers,responseType:"stream"}).then((response)=>{console.log(response.status);response.data.on("data",(chunk)=>{console.log(chunk.toString());});}).catch((error)=>{console.error(error);});

Java 样例代码:

Stringurl="https://api.acedata.cloud/aichat/conversations";OkHttpClientclient=newOkHttpClient();MediaTypemediaType=MediaType.parse("application/json");RequestBodybody=RequestBody.create(mediaType,"{\"question\": \"Hello\", \"stateful\": true, \"model\": \"gpt-3.5\"}");Requestrequest=newRequest.Builder().url(url).post(body).addHeader("Content-Type","application/json").addHeader("Accept","application/x-ndjson").addHeader("Authorization","Bearer {token}").build();client.newCall(request).enqueue(newCallback(){@OverridepublicvoidonFailure(Callcall,IOExceptione){e.printStackTrace();}@OverridepublicvoidonResponse(Callcall,Responseresponse)throwsIOException{if(!response.isSuccessful())thrownewIOException("Unexpected code "+response);try(BufferedReaderbr=newBufferedReader(newInputStreamReader(response.body().byteStream(),"UTF-8"))){StringresponseLine;while((responseLine=br.readLine())!=null){System.out.println(responseLine);}}}});

其他语言可以另外自行改写,原理都是一样的。

模型预设

我们知道,OpenAI 相关的 API 有对应的system_prompt的概念,就是给整个模型设置一个预设,比如它叫什么名字等等。本 AI 问答 API 也暴露了这个参数,叫做preset,利用它我们可以给模型增加预设,我们用一个例子来体验下:

这里我们额外添加preset字段,内容为You are a professional artist,如图所示:

对应代码如下:

curl-XPOST'https://api.acedata.cloud/aichat/conversations'\-H'accept: application/json'\-H'authorization: Bearer {token}'\-H'content-type: application/json'\-d'{ "model": "gpt-3.5", "stateful": true, "question": "What can you help me?", "preset": "You are a professional artist" }'

运行结果如下:

{"answer":"As a professional artist, I can offer a range of services and assistance depending on your specific needs. Here are a few ways I can help you:\n\n1. Custom Artwork: If you have a specific vision or idea, I can create custom artwork for you. This can include paintings, drawings, digital art, or any other medium you prefer.\n\n2. Commissioned Pieces: If you have a specific subject or concept in mind, I can create commissioned art pieces tailored to your preferences. This could be for personal enjoyment or as a unique gift for someone special.\n\n3. Art Consultation: If you need guidance on art selection, interior design, or how to showcase and display art in your space, I can provide professional advice to help enhance your aesthetic sense and create a cohesive look."}

可以看到这里我们告诉 GPT 他是一个机器人,然后问它可以为我们做什么,他就可以扮演一个机器人的角色来回答问题了。

图片识别

本 AI 也能支持添加附件进行图片识别,通过references传递对应图片链接即可,比如我这里有一张苹果的图片,如图所示:

该图片的链接是 https://cdn.acedata.cloud/ht05g0.png,我们直接将其作为references参数传递即可,同时需要注意的是,模型必须要选择支持视觉识别的模型,目前支持的是gpt-4-vision,所以输入如下:

对应的代码如下:

curl-XPOST'https://api.acedata.cloud/aichat/conversations'\-H'accept: application/json'\-H'authorization: Bearer {token}'\-H'content-type: application/json'\-d'{ "model": "gpt-4-vision", "question": "How many apples in the picture?", "references": ["https://cdn.acedata.cloud/ht05g0.png"] }'

运行结果如下:

{"answer":"There are 5 apples in the picture."}

可以看到,我们就成功得到了对应图片的回答结果。

联网问答

本 API 还支持联网模型,包括 GPT-3.5、GPT-4 均能支持,在 API 背后有一个自动搜索互联网并总结的过程,我们可以选择模型为gpt-3.5-browsing来体验下,如图所示:

代码如下:

curl-XPOST'https://api.acedata.cloud/aichat/conversations'\-H'accept: application/json'\-H'authorization: Bearer {token}'\-H'content-type: application/json'\-d'{ "model": "gpt-3.5-browsing", "question": "What's the weather of New York today?"}'

运行结果如下:

{"answer":"The weather in New York today is as follows:\n- Current Temperature: 16°C (60°F)\n- High: 16°C (60°F)\n- Low: 10°C (50°F)\n- Humidity: 47%\n- UV Index: 6 of 11\n- Sunrise: 5:42 am\n- Sunset: 8:02 pm\n\nIt's overcast with a chance of occasional showers overnight, and the chance of rain is 50%.\nFor more details, you can visit [The Weather Channel](https://weather.com/weather/tenday/l/96f2f84af9a5f5d452eb0574d4e4d8a840c71b05e22264ebdc0056433a642c84).\n\nIs there anything else you'd like to know?"}

可以看到,这里它自动联网搜索了 The Weather Channel 网站,并获得了里面的信息,然后进一步返回了实时结果。

cast with a chance of occasional showers overnight, and the chance of rain is 50%.\nFor more details, you can visit The Weather Channel.\n\nIs there anything else you’d like to know?"
}

可以看到,这里它自动联网搜索了 The Weather Channel 网站,并获得了里面的信息,然后进一步返回了实时结果。 > 如果对模型回答质量有更高要求,可以将模型更换为 `gpt-4-browsing`,回答效果会更好。
http://www.jsqmd.com/news/465836/

相关文章:

  • NVIC相关寄存器
  • AI写教材新玩法!掌握低查重技巧,高效完成教材编写任务
  • 直接上干货!今天咱们聊聊怎么用Matlab实现基于动态窗口法(DWA)的机器人避障。这个算法特别适合处理突发障碍物,先扔个可以直接运行的代码框架
  • 网站被挂马、后台被入侵处理全流程
  • OpenClaw好部署吗?2026年OpenClaw(Clawdbot)新手1分钟搭建超简单指南
  • 如何快速将天虹提货券回收变现?一文带你了解操作流程! - 团团收购物卡回收
  • 腾讯云OpenClaw(Clawdbot)2026年小白2分钟保姆级集成教程
  • 为什么 AutoClaw 能成为 OpenClaw 的 “最优平替”?
  • 模板整理
  • 网站被黑跳转广告、恶意链接清理方法
  • 阿里云OpenClaw(Clawdbot)2026年10分钟萌新安装保姆级方法
  • Python基于flask+uniapp微信小程序的茶益游 茶叶茶友圈文化分享交流 Android的茶文化宣传平台
  • 2026年Q1防火电缆加工厂五强深度解析 - 2026年企业推荐榜
  • ThinkPHP和PHP的区别
  • Python基于flask+uniapp微信小程序的高校学科竞赛参赛申请管理系统
  • 华为云OpenClaw(Clawdbot)2026年4分钟喂奶级安装流程指南
  • 京东云怎么部署OpenClaw?OpenClaw(Clawdbot)2026年8分钟保姆级安装流程指南
  • Python基于flask+uniapp微信小程序的的酒店预订系统的设计与实现
  • 基于深度学习框架基于YOLOV8中草药识别检测系统 YOLOV8模型如何训练中草药检测数据集
  • 轻量化GE-GRU-VAE:多维时间序列异常检测的工业级突破与前沿探索
  • Python基于flask+uniapp微信小程序的的饮食健康管理系统
  • 射频定向耦合器:原理、设计与工程实践
  • 重庆火锅新选择:本地人亲测好吃的火锅店TOP榜,特色美食/社区火锅/火锅店/火锅/美食,火锅品牌排行 - 品牌推荐师
  • 网站Class think\XXX not found(类找不到)错误怎么办|已解决
  • Python基于flask+uniapp微信小程序的福建畲族文创商城文化交流与交易平台
  • 微波放大器核心参数详解:功率、增益、匹配与稳定性
  • Python基于flask+uniapp微信小程序的的计算机精品课程在线学习互动系统演示 聊天
  • 2026年离子交换设备源头厂家盘点,优质选择看这里,反渗透设备/离子交换设备/净水机,离子交换设备实力厂家推荐 - 品牌推荐师
  • 产业园区如何降低科技服务平台建设成本?
  • 微波射频中的非线性效应:分析、应用与抑制