Skip to content

Commit ab26c9d

Browse files
committed
add tools scripts for agents
Signed-off-by: cheehook <[email protected]>
1 parent 86727eb commit ab26c9d

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
search_knowledge_base:
5+
description: Search a knowledge base for a given query. Returns text related to the query.
6+
callable_api: tools.py:search_knowledge_base
7+
args_schema:
8+
query:
9+
type: str
10+
description: query
11+
return_output: retrieved_data
12+
13+
search_sql_database:
14+
description: Search a SQL database with a natural language query. Returns text related to the query.
15+
callable_api: tools.py:search_sql_database
16+
args_schema:
17+
query:
18+
type: str
19+
description: natural language query
20+
return_output: retrieved_data

app-backend/templates/tools/tools.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import os
5+
import requests
6+
7+
8+
def search_knowledge_base(query: str) -> str:
9+
"""Search a knowledge base about music and singers for a given query.
10+
11+
Returns text related to the query.
12+
"""
13+
url = os.environ.get("WORKER_AGENT_URL")
14+
print(url)
15+
proxies = {"http": ""}
16+
payload = {
17+
"messages": query,
18+
}
19+
response = requests.post(url, json=payload, proxies=proxies)
20+
return response.json()["text"]
21+
22+
23+
def search_sql_database(query: str) -> str:
24+
"""Search a SQL database on artists and their music with a natural language query.
25+
26+
Returns text related to the query.
27+
"""
28+
url = os.environ.get("SQL_AGENT_URL")
29+
print(url)
30+
proxies = {"http": ""}
31+
payload = {
32+
"messages": query,
33+
}
34+
response = requests.post(url, json=payload, proxies=proxies)
35+
return response.json()["text"]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import os
5+
import requests
6+
7+
def search_knowledge_base(query: str) -> str:
8+
"""Search the knowledge base for a specific query."""
9+
url = os.environ.get("RETRIEVAL_TOOL_URL")
10+
print(url)
11+
proxies = {"http": ""}
12+
payload = {
13+
"text": query,
14+
}
15+
response = requests.post(url, json=payload, proxies=proxies)
16+
print(response)
17+
if "documents" in response.json():
18+
docs = response.json()["documents"]
19+
context = ""
20+
for i, doc in enumerate(docs):
21+
if i == 0:
22+
context = doc
23+
else:
24+
context += "\n" + doc
25+
# print(context)
26+
return context
27+
elif "text" in response.json():
28+
return response.json()["text"]
29+
elif "reranked_docs" in response.json():
30+
docs = response.json()["reranked_docs"]
31+
context = ""
32+
for i, doc in enumerate(docs):
33+
if i == 0:
34+
context = doc["text"]
35+
else:
36+
context += "\n" + doc["text"]
37+
# print(context)
38+
return context
39+
else:
40+
return "Error parsing response from the knowledge base."
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
search_knowledge_base:
5+
description: Search knowledge base for a given query. Returns text related to the query.
6+
callable_api: worker_agent_tools.py:search_knowledge_base
7+
args_schema:
8+
query:
9+
type: str
10+
description: query
11+
return_output: retrieved_data

0 commit comments

Comments
 (0)