Skip to content

Commit 028b23b

Browse files
authored
Update agentic-search.md
1 parent 4609760 commit 028b23b

File tree

1 file changed

+287
-1
lines changed

1 file changed

+287
-1
lines changed

docs/llama-nexus/mcp/agentic-search.md

Lines changed: 287 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,290 @@ sidebar_position: 2
44

55
# Agentic Search
66

7-
To be add
7+
With its support for MCP servers, we can support "agentic search" from llama nexus. That is to support transparent knowledge base search via the `/chat/completions` API. There is no need for the llama nexus client to handle any tool call or MCP. It works like this:
8+
9+
A `search()` tool is registered with llama nexus from an MCP server. When the user asks a question via the `/chat/completions` API, llama nexus knows to use the `search()` tool internally with its downstream LLM, and respond based on the search result. The user automagically gets a correct response to its `/chat/completions` request without needing to know anything about MCP tool calls or the knowledge base!
10+
11+
In this tutorial, you will learn how to set two databases from the same source materials. One is a Qdrant vector database to support semantic search, and the other is a TiDB relational database to support keyword search. We could improve the search accuracy by searching both databases. We will cover
12+
13+
* How to set up the databases
14+
* How to add data and index to the databases
15+
* How to set up the agentic search MCP server that provides the `search()` tool
16+
* How to add the agentic search MCP server to llama nexus
17+
* How to test the fina llama nexus set up
18+
19+
## Prepare databases
20+
21+
We use Qdrant and TiDB to store the knowledge content in vector and full text formats respectively. They enable semantic and keywords searches of the knowledge base.
22+
23+
### Qdrant cloud
24+
25+
Create a Qdrant cloud account here: https://cloud.qdrant.io/
26+
27+
Create a new cluster in any region.
28+
29+
Click open the cluster name, go to the API Keys tab to get an API key.
30+
31+
![Qdrant database](agentic-search-qdrant.png)
32+
33+
Open the Dashboard or Cluster UI for this cluster by clicking on the `...` button next to the cluster name. The base URL of the Dashboard is the base URL for connecting to the cluster.
34+
35+
```
36+
https://bb8ab5cf-eae2-4c7c-9493-5f7a183c89b8.us-east4-0.gcp.cloud.qdrant.io:6333
37+
```
38+
39+
#### Optional: create your own collection in Qdrant
40+
41+
> This is optional because the system will automatically create this vector collection if it does not exist. If the collection already exists, the system will write new data into the collection.
42+
43+
In the Console tab to the left of the Dashboard page, type in the following API request to create a new vector collection named `myPoints`.
44+
45+
```
46+
PUT collections/myPoints
47+
{
48+
"vectors": {
49+
"size": 1536,
50+
"distance": "Cosine"
51+
}
52+
}
53+
```
54+
55+
### TiDB cloud
56+
57+
Create an TiDB cloud account here: https://tidbcloud.com/
58+
59+
Create a new cluster in the Singapore (`ap-southeast-1`) region.
60+
61+
Click open the "Overview" tab, and click on the "Connect" button. Write down the following database connection string.
62+
63+
```
64+
mysql://42eZLKLSueg7p8K.root:<PASSWORD>@gateway01.ap-southeast-1.prod.aws.tidbcloud.com:4000/test
65+
```
66+
67+
The `<PASSWORD>` can be revealed once with the Reset Password link above the connection string display.
68+
69+
#### Optional: create your own database table in TiDB
70+
71+
> This is optional because the system will automatically create this table if it does not exist. If the table already exists, the system will write new data into the table.
72+
73+
Click open the "SQL Editor" tab. Enter a SQL command and hit the "Run" button to create a new database.
74+
75+
```
76+
CREATE DATABASE myDB;
77+
```
78+
79+
Then, create a table in `myDB` based on our schema.
80+
81+
```
82+
USE myDB;
83+
84+
CREATE TABLE myItems (
85+
id INT NOT NULL AUTO_INCREMENT,
86+
title TEXT NOT NULL,
87+
content TEXT NOT NULL,
88+
FULLTEXT INDEX (content) WITH PARSER MULTILINGUAL
89+
);
90+
```
91+
92+
Click open the "Overview" tab, and click on the "Connect" button. Write down the following database connection string.
93+
94+
```
95+
mysql://42eZLKLSueg7p8K.root:<PASSWORD>@gateway01.ap-southeast-1.prod.aws.tidbcloud.com:4000/myDB
96+
```
97+
98+
## Set up knowledge base
99+
100+
Go to https://kb.cardea.cc/ to login from your GitHUb account. Then, click on Settings to configure your knowledge processing pipeline.
101+
102+
![Configure the knowledge base](agentic-search-kb.png)
103+
104+
### Qdrant
105+
106+
It stores the vectorized knowledge base for semantic search.
107+
108+
| Name | Value |
109+
| -------- | -------- |
110+
| Database URL | Your Qdrant cloud cluster's dashboard URL: `https://bb8ab5cf-eae2-4c7c-9493-5f7a183c89b8.us-east4-0.gcp.cloud.qdrant.io:6333/` |
111+
| API Key | Get it from your Qdrant cloud cluster's API Key tab |
112+
| Collection Name | `myPoints` |
113+
114+
115+
### TiDB
116+
117+
It stores the full text knowledge base for keywords search.
118+
119+
| Name | Value |
120+
| -------- | -------- |
121+
| Database URL | The TiDB database connection string from the TiDB dashboard. |
122+
| Table Name | `myItems` |
123+
124+
### Embedding Service
125+
126+
The API service that creates vector embeddings for the knowledge content.
127+
128+
| Name | Value |
129+
| -------- | -------- |
130+
| Base URL | `https://0x448f0405310a9258cd5eab5f25f15679808c5db2.gaia.domains/v1` |
131+
| Model Name | `gte-qwen2-7b` |
132+
| API Key | `1234` |
133+
| Vector Size | `1536` |
134+
135+
> The Vector Size is determined by the embedding model we use.
136+
137+
### LLM Service
138+
139+
The API service that generates summaries and QAs for search indexes for the knowledge content.
140+
141+
| Name | Value |
142+
| -------- | -------- |
143+
| Base URL | `https://0x9fcf7888963793472bfcb8c14f4b6b47a7462f17.gaia.domains/v1` |
144+
| Model Name | `gemma3-27b` |
145+
| API Key | `1234` |
146+
147+
## Add knowledge
148+
149+
After Save Configuration, you can now enter and upload knowledge content from the web site.
150+
151+
### Add a set of QA pairs
152+
153+
If your knowledge is in the form of QA pairs, you can select the "QA Input" tab.
154+
155+
Copy and paste your QA pairs into the text area. The content must be formatted like this:
156+
157+
```
158+
Q: This is a Q
159+
A: This is an A
160+
161+
Q: This is a second Q
162+
A: This is a second A
163+
```
164+
165+
![Add QA to the knowledge base](agentic-search-add-qa.png)
166+
167+
Click on "Process and Create Embeddings" at the bottom of the page.
168+
169+
![Job progress](agentic-search-process.png)
170+
171+
You will see that both the Qdrant vector collection and TiDB table are populated.
172+
173+
## Start the MCP server
174+
175+
Install the agentic search MCP server for Linux on x86.
176+
177+
```
178+
curl -LO https://github.com/cardea-mcp/cardea-mcp-servers/releases/download/0.7.0/cardea-mcp-servers-unknown-linux-gnu-x86_64.tar.gz
179+
180+
gunzip cardea-mcp-servers-unknown-linux-gnu-x86_64.tar.gz
181+
tar xvf cardea-mcp-servers-unknown-linux-gnu-x86_64.tar
182+
```
183+
184+
> Download for your platform: https://github.com/cardea-mcp/cardea-mcp-servers/releases/tag/0.7.0/
185+
186+
Set the environment variables. The `TIDB_CONNECTION` is the TiDB connection URL from above, which includes the username and password. The `QDRANT_BASE_URL` is the Qdrant cluster connection URL, and it defaults to `http://localhost:6333`.
187+
188+
```
189+
export RUST_LOG=debug
190+
export LLAMA_LOG=debug
191+
export TIDB_CONNECTION=mysql://42eZLKLSueg7p8K.root:<PASSWORD>@gateway01.ap-southeast-1.prod.aws.tidbcloud.com:4000/test
192+
export QDRANT_BASE_URL=https://bb8ab5cf-eae2-4c7c-9493-5f7a183c89b8.us-east4-0.gcp.cloud.qdrant.io:6333
193+
export QDRANT_API_KEY=xxxxxxxx
194+
```
195+
196+
Start the agentic search MCP server with database connection parameters. Make sure that you adjust the `--search-tool-desc` and `--search-tool-param-desc` to describe the search queries that can be performed for this database.
197+
198+
```
199+
nohup ./cardea-agentic-search-mcp-server \
200+
--socket-addr 127.0.0.1:9096 \
201+
--transport stream-http \
202+
--search-tool-desc "You MUST call the search() tool before you answer any factual question. Create a question from the user query and relevant context, and pass the question as a string to the tool call." \
203+
--search-tool-param-desc "The question to search for answers." \
204+
search \
205+
--qdrant-collection myPoints \
206+
--qdrant-payload-field "full_text" \
207+
--tidb-ssl-ca /etc/ssl/certs/ca-certificates.crt \
208+
--tidb-table-name myItems \
209+
--chat-service https://0xb2962131564bc854ece7b0f7c8c9a8345847abfb.gaia.domains \
210+
--embedding-service https://0x448f0405310a9258cd5eab5f25f15679808c5db2.gaia.domains \
211+
--limit 5 \
212+
--score-threshold 0.8 &
213+
```
214+
215+
The MCP server is started on port `9096`.
216+
217+
## Create an inference server
218+
219+
### Install llama-nexus
220+
221+
The following command installs the Linux on x86 version of llama-nexus.
222+
223+
```
224+
curl -LO https://github.com/LlamaEdge/llama-nexus/releases/download/0.5.0/llama-nexus-unknown-linux-gnu-x86_64.tar.gz
225+
226+
gunzip llama-nexus-unknown-linux-gnu-x86_64.tar.gz
227+
tar xvf llama-nexus-unknown-linux-gnu-x86_64.tar
228+
```
229+
230+
> Download for your platfrom here: https://github.com/LlamaEdge/llama-nexus/releases/tag/0.5.0/
231+
232+
### Configure llama-nexus
233+
234+
Edit the `config.toml` file to specify a port (`9095`) for the gateway seerver to listen to.
235+
236+
```
237+
[server]
238+
host = "0.0.0.0" # The host to listen on.
239+
port = 9095 # The port to listen on.
240+
```
241+
242+
Enable the previously started TiDB search MCP server on port `9096`.
243+
244+
```
245+
[[mcp.server.tool]]
246+
name = "cardea-agentic-search"
247+
transport = "stream-http"
248+
url = "http://127.0.0.1:9096/mcp"
249+
enable = true
250+
```
251+
252+
253+
### Start llama-nexus
254+
255+
```
256+
nohup ./llama-nexus --config config.toml &
257+
```
258+
259+
### Register downstream API servers
260+
261+
Register an LLM chat API server for the `/chat/completions` endpoint.
262+
263+
```
264+
curl --location 'http://localhost:9095/admin/servers/register' \
265+
--header 'Content-Type: application/json' \
266+
--data '{
267+
"url": "https://0xb2962131564bc854ece7b0f7c8c9a8345847abfb.gaia.domains",
268+
"kind": "chat"
269+
}'
270+
```
271+
272+
Register an embedding API server for the `/embeddings` endpoint.
273+
274+
```
275+
curl --location 'http://localhost:9095/admin/servers/register' \
276+
--header 'Content-Type: application/json' \
277+
--data '{
278+
"url": "https://0x448f0405310a9258cd5eab5f25f15679808c5db2.gaia.domains",
279+
"kind": "embeddings"
280+
}'
281+
```
282+
283+
## Test the inference server
284+
285+
Use the command line to request the `/chat/completions` API endpoint directly.
286+
287+
```
288+
curl -X POST http://localhost:9095/v1/chat/completions \
289+
-H 'accept: application/json' \
290+
-H 'Content-Type: application/json' \
291+
-d '{"messages":[{"role":"system", "content": "If the user asks a question, you MUST use the search() tool call and pass in a list of search keywords to search for relevant information and then form your response based on the search results."},{"role":"user", "content": "What prevents cancer?"}]}'
292+
```
293+

0 commit comments

Comments
 (0)