Text Q&A answers queries by retrieving content from a knowledge base. It uses a session ID to maintain context in multi-turn conversations. You can also select different Large Language Models (LLMs) to generate answers and customize the responses.
Prerequisites
-
Obtain an API key to authenticate calls to OpenSearch - LLM Intelligent Q&A Edition. For more information, see Manage API keys.
-
Obtain a service endpoint to make calls to OpenSearch - LLM Intelligent Q&A Edition. For more information, see Obtain service endpoints.
This API is designed for server-side use and must not be called directly from a front-end environment such as a browser, mini program, or mobile app. There are two reasons for this. First, the API uses the Authorization: Bearer <API key> header for authentication. An API key is a long-term credential; embedding it in front-end code would expose it to theft. Second, the API does not support Cross-Origin Resource Sharing (CORS), so browsers will block any direct calls. You must store the API key on your server, make calls from the server, and then return the results to the front end.
API
|
Request method |
Protocol |
Request format |
|
POST |
HTTP |
JSON |
Request URL
{host}/v3/openapi/apps/{app_group_identity}/actions/knowledge-search
-
{host}: The service endpoint, accessible over the Internet or a virtual private cloud (VPC). For more information, see Obtain endpoints. -
{app_group_identity}: The application name. Sign in to the OpenSearch LLM-Based Conversational Search Edition console and find this name on the Instance Management page.
Request parameters
Header parameters
|
Parameter |
Type |
Required |
Description |
Example |
|
Content-Type |
string |
Yes |
Specifies the media type of the request body. Must be "application/json". |
application/json |
|
Authorization |
string |
Yes |
Your API key, prefixed with "Bearer ". |
Bearer OS-d1**2a |
|
accept |
string |
No |
Set to "text/event-stream" to receive streaming responses using Server-Sent Events (SSE). |
text/event-stream |
Body parameters
|
Parameter |
Type |
Required |
Description |
Example value |
|
question |
map |
Yes |
The input question. |
{ "text":"user question", "type": "TEXT", "session" : "" } |
|
question.text |
string |
Yes |
The text content of the user's question. |
user question |
|
question.session |
string |
No |
A session ID that identifies the context of a multi-turn conversation. Valid values:
|
1725530408586 |
|
question.type |
string |
No |
The type of the input question. Set this to |
TEXT |
|
options |
map |
No |
Optional parameters to control settings such as retrieval, model selection, and prompts. |
|
|
options.chat |
map |
No |
Parameters for accessing a Large Language Model (LLM). |
|
|
options.chat.disable |
boolean |
No |
Specifies whether to disable access to the LLM.
|
false |
|
options.chat.stream |
boolean |
No |
Specifies whether to enable a streaming response.
|
true |
|
options.chat.model |
string |
No |
The Large Language Model (LLM) to use. Valid values: Asia Pacific SE 1 (Singapore)
|
opensearch-llama2-13b |
|
options.chat.enable_deep_search |
boolean |
No |
Specifies whether to enable deep search.
|
false |
|
options.chat.model_generation |
integer |
No |
Specifies the model version to use for a custom product model. If you do not specify this parameter, the system uses the oldest version by default. |
20 |
|
options.chat.prompt_template |
string |
No |
The name of a custom prompt template. If this parameter is empty, the system uses its built-in prompt template by default. |
user_defined_prompt_name |
|
options.chat.prompt_config |
object |
No |
The key-value pairs for configuring a custom prompt. The parameter must be in the following format:
|
|
|
options.chat.prompt_config.attitude |
string |
No |
A parameter for the built-in prompt template that controls the tone of the response. The default value is
|
normal |
|
options.chat.prompt_config.rule |
string |
No |
Controls the level of detail in the response. The default value is
|
detailed |
|
options.chat.prompt_config.noanswer |
string |
No |
The response to return when the system cannot answer the question. The default value is
|
sorry |
|
options.chat.prompt_config.language |
string |
No |
The language to use for the response. The default value is
|
Chinese |
|
options.chat.prompt_config.role |
boolean |
No |
Specifies whether to enable a custom persona for the response. If you set this to |
false |
|
options.chat.prompt_config.role_name |
string |
No |
The name of the custom persona. For example: AI Assistant. |
AI Assistant |
|
options.chat.prompt_config.out_format |
string |
No |
The format of the output content. The default value is
|
text |
|
options.chat.generate_config.repetition_penalty |
float |
No |
Controls the penalty for repetition in generated token sequences. A higher value reduces the likelihood of repetition. A value of |
1.01 |
|
options.chat.generate_config.top_k |
integer |
No |
The number of highest-probability tokens to consider for sampling. For example, a value of |
50 |
|
options.chat.generate_config.top_p |
float |
No |
The cumulative probability threshold for nucleus sampling. For example, a value of |
0.5 |
|
options.chat.generate_config.temperature |
float |
No |
Controls the randomness and diversity of generated text. A higher temperature flattens the probability distribution of candidate tokens, allowing the model to choose less likely tokens and making the output more diverse. A lower value sharpens the distribution, making the model more likely to select high-probability tokens and the output more deterministic. Value range: [0, 2). Do not set this parameter to 0. python version >=1.10.1 java version >= 2.5.1 |
0.7 |
|
options.chat.history_max |
integer |
No |
The maximum number of turns to retain in the history of a multi-turn conversation. The maximum value is 20. The default value is 1. |
20 |
|
options.chat.link |
boolean |
No |
Specifies whether to include source citations in the response. Valid values:
The following is an example of a response that contains citations:
The numbers enclosed in |
false |
|
options.chat.rich_text_strategy |
string |
No |
The post-processing strategy for output from a rich-text LLM. If this parameter is empty or not specified, rich text processing is disabled by default.
|
inside_response |
|
options.chat.agent |
map |
No |
Specifies options for the RAG agent's tool-use capabilities. When enabled, the model decides whether to execute a tool based on the available content. The LLMs that currently support this feature are:
|
|
|
options.chat.agent.think_process |
boolean |
No |
Specifies whether to return the agent's thought process. |
true |
|
options.chat.agent.max_think_round |
integer |
No |
The maximum number of thought rounds for the agent. The value cannot exceed 20. |
10 |
|
options.chat.agent.language |
string |
No |
The language for the thought process and the final answer.
|
AUTO |
|
options.chat.agent.tools |
list of string |
No |
The RAG tools that the agent can use. The following tools are available:
|
["knowledge_search"] |
|
options.retrieve |
map |
No |
Specifies parameters that control the retrieval process. |
|
|
options.retrieve.web_search.enable |
boolean |
No |
Specifies whether to enable web search.
|
false |
|
doc |
map |
No |
Specifies parameters related to document retrieval. |
|
|
options.retrieve.doc.disable |
boolean |
No |
Specifies whether to disable retrieval from the knowledge base.
|
false |
|
options.retrieve.doc.filter |
string |
No |
A filter expression to select data for retrieval from the knowledge base. If not specified, no filter is applied. For syntax examples, see filter parameter. Supported fields:
Example format:
|
category=\"value1\" |
|
options.retrieve.doc.sf |
float |
No |
The score threshold for vector-based retrieval.
|
0.35 |
|
options.retrieve.doc.top_n |
integer |
No |
The number of documents to retrieve. The value must be in the range (0, 50]. The default value is 5. |
5 |
|
options.retrieve.doc.formula |
string |
No |
A custom ranking formula for retrieved documents. Note
For syntax details, refer to Business Sorting Functions. The Algorithmic Relevance and Geographic Relevance features are not supported. |
-timestamp: Sorts documents in descending order by the timestamp field. |
|
options.retrieve.doc.rerank_size |
integer |
No |
The number of documents to rerank when the rerank feature is enabled. The value must be in the range (0, 100]. The default value is 30. |
30 |
|
options.retrieve.doc.operator |
string |
No |
The relationship between the terms that result from tokenizing
|
AND |
|
options.retrieve.doc.dense_weight |
float |
No |
When sparse vectors are enabled, this parameter controls the weight of the dense vector during document retrieval. The value must be in the range (0.0, 1.0). The default value is 0.7. |
0.7 |
|
options.retrieve.entry |
map |
No |
Specifies parameters for retrieving results from manual intervention data. |
|
|
options.retrieve.entry.disable |
boolean |
No |
Specifies whether to disable retrieval from manual intervention data.
|
false |
|
options.retrieve.entry.sf |
float |
No |
The vector score threshold for retrieving manual intervention data. The value range is |
0.3 |
|
options.retrieve.image |
map |
No |
Specifies parameters for retrieving image results from the knowledge base. |
|
|
options.retrieve.image.disable |
boolean |
No |
Specifies whether to disable image retrieval. The default value is
|
false |
|
options.retrieve.image.sf |
float |
No |
The score threshold for image vector retrieval.
|
1.0 |
|
options.retrieve.image.dense_weight |
float |
No |
When sparse vectors are enabled, this parameter controls the weight of the dense vector during image retrieval. The value must be in the range (0.0, 1.0). The default value is 0.7. |
0.7 |
|
options.retrieve.qp |
map |
No |
Specifies options for query rewriting. |
|
|
options.retrieve.qp.query_extend |
boolean |
No |
Specifies whether to expand the user query. The search engine uses the expanded query to retrieve document chunks.
|
false |
|
options.retrieve.qp.query_extend_num |
integer |
No |
When query expansion is enabled, this parameter specifies the maximum number of additional queries to generate. The default value is 5. |
5 |
|
options.retrieve.rerank |
map |
No |
Specifies options for reranking retrieved documents. |
|
|
options.retrieve.rerank.enable |
boolean |
No |
Specifies whether to use a model to rerank search results for relevance. Valid values:
|
true |
|
options.retrieve.rerank.model |
string |
No |
The name of the large model to use for reranking.
|
ops-bge-reranker-larger |
|
options.retrieve.return_hits |
boolean |
No |
Specifies whether to return the document retrieval results, which correspond to the |
false |
Request body
{
"question": {
"text": "What is Alibaba Cloud OpenSearch?",
"session": "session_001",
"type": "TEXT"
},
"options": {
"chat": {
"disable": false,
"stream": false,
"model": "Qwen",
"history_max": 20,
"link": false,
"agent": {
"tools": ["knowledge_search"]
}
},
"retrieve": {
"doc": {
"disable": false,
"filter": "category=\"type\"",
"sf": 0.35,
"top_n": 5,
"operator": "OR"
},
"web_search": { "enable": false },
"entry": { "disable": false, "sf": 0.3 },
"image": { "disable": false, "sf": 1.0 },
"rerank": {
"enable": true,
"model": "ops-bge-reranker-larger"
},
"return_hits": false
}
}
}
Key parameters:
-
Use
question.sessionto enable multi-turn conversation context. -
Set
options.chat.disabletotrueto skip the LLM and return retrieval results. -
Use
options.retrieve.doc.top_nto specify the number of retrieved documents. The default is 5. -
Set
options.retrieve.return_hitstotrueto return the detailed content ofsearch_hits.
Response parameters
|
Parameter |
Type |
Description |
|
request_id |
string |
Unique request identifier. Include this value in support requests. |
|
status |
string |
The processing status of the request.
|
|
latency |
float |
Server processing time in milliseconds. Returned only when the request succeeds. |
|
id |
integer |
The primary key. |
|
title |
string |
Document title. |
|
category |
string |
Category name. |
|
url |
string |
Document URL. |
|
answer |
string |
The generated answer. |
|
type |
string |
Result type. |
|
scores |
array |
Document relevance scores. |
|
event |
string |
The type of streaming event. A thinking round includes |
|
event_status |
string |
Indicates whether the event is complete.
|
|
code |
string |
The error code. Returned only when an error occurs. |
|
message |
string |
The error message. Returned only when an error occurs. |
Sample response body
Successful response
{
"request_id": "6859E98D-D885-4AEF-B61C-9683A0184744",
"status": "OK",
"latency": 6684.41,
"result": {
"data": [
{
"answer": "Alibaba Cloud OpenSearch is a managed service for searching structured data...",
"type": "TEXT",
"reference": [
{"url": "https://www.alibabacloud.com/help/document_detail/463469.html", "title": "OpenSearch Product Introduction"}
]
}
],
"search_hits": [
{
"fields": {"content": "Content of OpenSearch-related documents...", "title": "OpenSearch Introduction"},
"scores": ["0.9778"],
"type": "DOC"
}
]
}
}
Error response
{
"request_id": "e579a090bf99dc787d29d878b40c8367",
"status": "FAIL",
"errors": [
{"code": 3005, "message": "topN[51] is not in (0, 50]"}
]
}