AI Chat API Integration Guide
As we know, integrating some Q&A APIs on the market is still relatively difficult. For example, OpenAI's Chat Completions API has a messages field, and to maintain a continuous conversation, we need to pass the entire context history and also handle issues with exceeding the Token limit.
The AI Q&A API provided by AceDataCloud has been optimized for the above situation. While maintaining the same Q&A performance, it encapsulates the implementation of continuous conversations. During integration, there is no need to worry about passing messages or exceeding the Token limit (the API handles this automatically internally). It also provides features such as querying and modifying conversations, greatly simplifying the overall integration.
This document introduces how to integrate the AI Q&A API.
¶ Application Process
To use the AI Q&A API, first go to the Ace Data Cloud Console to obtain your API Token for later use.

If you have not logged in or registered, you will be automatically redirected to the login page to register and log in. Once completed, you will automatically return to the current page.
The API Key for a general application can be used for services enabled for the account; the Key for a dedicated application can be used within that application's service scope. Please check the Key's permissions, validity period, and limits, and confirm that the application balance is sufficient to pay for this request. Available trial credits are subject to what is displayed in the account. If the balance is insufficient, click the "Recharge" button.
📘 Full documentation: AI Q&A API →
¶ Basic Usage
First, let's look at the basic usage: enter a question and receive an answer. You only need to pass a question field and specify the corresponding model.
For example, to ask "What's your name?", we can fill in the corresponding content on the interface, as shown below:

As you can see, we have set the Request Headers here, including:
accept: The format in which you want to receive the response. Here it is set toapplication/json, meaning JSON format.authorization: The key used to call the API. After obtaining it, you can select it directly from the drop-down menu.
We have also set the Request Body, including:
model: The choice of model, such as the popular GPT 3.5, GPT 4, etc.question: The question to ask, which can be any plain text.
After making the selections, you can see that the corresponding code has also been generated on the right, as shown below:

Click the "Try" button to test it. As shown above, we get the following result:
{
"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?"
}
As you can see, the returned result contains an answer field, which is the answer to the question. We can enter any question and receive an answer.
If you do not need support for multi-turn conversations, this API can greatly simplify your integration.
If you want to generate the corresponding integration code, you can also copy the generated code directly. For example, the CURL code is as follows:
curl -X POST '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?"
}'
The Python integration code is as follows:
import requests
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)
¶ Multi-Turn Conversations
If you want to integrate multi-turn conversation functionality, you need to pass an additional parameter, stateful, with a value of true. Every subsequent request must also include this parameter. After the stateful parameter is passed, the API will additionally return an id parameter representing the ID of the current conversation. For subsequent requests, we only need to pass this ID as a parameter to easily implement multi-turn conversations.
Let's demonstrate the specific steps below.
For the first request, set the stateful parameter to true and pass the model and question parameters as usual, as shown below:

The corresponding code is as follows:
curl -X POST '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
}'
You can get the following response:
{
"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"
}
For the second request, pass the id field returned by the first request as a parameter, keep the stateful parameter set to true, and ask "What did I ask you just now?", as shown below:

The corresponding code is as follows:
curl -X POST '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?"
}'
The result is as follows:
{
"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"
}
As you can see, it can now answer the corresponding question based on the context.
¶ Conversation Management and Billing
When action is omitted from POST /aichat/conversations or "action": "chat" is passed, question and model must be provided, and billing is based on the selected model. Each of the following four conversation management operations costs 0 Credits and does not require question or model.
Using the request URL and authorization header above, replace <id> in the examples with the id returned by your own multi-turn conversation.
| Operation | JSON Request Body Example | Result |
|---|---|---|
| Retrieve conversation | {"action":"retrieve","id":"<id>"} |
Returns the saved conversation, including its message history. |
| Batch retrieve | {"action":"retrieve_batch","ids":["<id>"]} |
items contains the retrieved conversations, and count is the number of results. |
| Update conversation | {"action":"update","id":"<id>","title":"Project Discussion"} |
Sets the title. Passing a messages array can replace the entire message history, and both can also be updated at the same time. |
| Delete conversation | {"action":"delete","id":"<id>"} |
Returns id and deleted: true. |
¶ Streaming Response
This API also supports streaming responses, which is very useful for web integration and allows web pages to display text character by character.
To return a streaming response, change the accept parameter in the request header to application/x-ndjson.
Make the change as shown in the image, although the calling code also needs corresponding changes to support streaming responses.

After changing accept to application/x-ndjson, the API will return the corresponding JSON data line by line, and we need to make corresponding changes in the code to obtain the results line by line.
Python sample calling code:
import requests
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)
for line in response.iter_lines():
print(line.decode())
The output is as follows:
{"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"}
As you can see, answer in the response is the latest answer content, while delta_answer is the newly added answer content, and you can use the results to integrate it into your system.
JavaScript is also supported; for example, the streaming call code for Node.js is as follows:
const axios = require("axios");
const url = "https://api.acedata.cloud/aichat/conversations";
const headers = {
"Content-Type": "application/json",
Accept: "application/x-ndjson",
Authorization: "Bearer {token}",
};
const body = {
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 sample code:
String url = "https://api.acedata.cloud/aichat/conversations";
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"question\": \"Hello\", \"stateful\": true, \"model\": \"gpt-3.5\"}");
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/x-ndjson")
.addHeader("Authorization", "Bearer {token}")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
try (BufferedReader br = new BufferedReader(
new InputStreamReader(response.body().byteStream(), "UTF-8"))) {
String responseLine;
while ((responseLine = br.readLine()) != null) {
System.out.println(responseLine);
}
}
}
});
You can adapt the code for other languages yourself; the principle is the same.
¶ Model Preset
As we know, OpenAI-related APIs have the concept of a system_prompt, which sets a preset for the entire model, such as what its name is and so on. This AI Q&A API also exposes this parameter, called preset, which we can use to add a preset to the model. Let's use an example to try it out:
Here we add the preset field, with the content You are a professional artist, as shown in the image:

The corresponding code is as follows:
curl -X POST '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"
}'
The result is as follows:
{
"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."
}
As you can see, here we told GPT that it is a robot and then asked it what it could do for us, and it was able to answer the question by playing the role of a robot.
¶ Image Recognition
This AI also supports adding attachments for image recognition by passing the corresponding image link through references. For example, I have an image of an apple here, as shown:
The link to the image is https://cdn.acedata.cloud/ht05g0.png. We can pass it directly as the references parameter. At the same time, note that the selected model must support visual recognition. The currently supported model is gpt-4-vision, so the input is as follows:

The corresponding code is as follows:
curl -X POST '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"]
}'
The result is as follows:
{
"answer": "There are 5 apples in the picture."
}
As you can see, we successfully obtained an answer for the corresponding image.
¶ Online Q&A
This API also supports models with internet access, including both GPT-3.5 and GPT-4. Behind the API, there is a process that automatically searches the internet and summarizes the results. We can select gpt-3.5-browsing as the model to try it out, as shown below:

The code is as follows:
curl -X POST '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?"
}'
The result is as follows:
{
"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?"
}
As you can see, it automatically searched The Weather Channel website, obtained the information there, and then returned real-time results.
If you have higher requirements for the quality of the model's answers, you can switch the model to
gpt-4-browsingfor better results.
