Skip to content

Commit 47bce79

Browse files
committed
Add prompt, tool to include nearby locations in google search query
1 parent eca926b commit 47bce79

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/fastapi_app/llm_tools.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def build_google_search_function() -> list[ChatCompletionToolParam]:
1111
{
1212
"type": "function",
1313
"function": {
14-
"name": "search_database",
14+
"name": "search_google",
1515
"description": "Search for relevant products based on user query",
1616
"parameters": {
1717
"type": "object",
@@ -20,6 +20,17 @@ def build_google_search_function() -> list[ChatCompletionToolParam]:
2020
"type": "string",
2121
"description": "Query string to use for full text search, e.g. 'ตรวจสุขภาพ'",
2222
},
23+
"locations": {
24+
"type": "array",
25+
"items": {
26+
"type": "string",
27+
},
28+
"description": """
29+
A list of nearby districts(Amphoes) from what the user provides.
30+
For example, if the user says `รังสิต`, the locations should be
31+
[`ธัญบุรี`, `เมืองปทุมธานี`, `คลองหลวง`, `ลำลูกกา`].
32+
"""
33+
}
2334
},
2435
"required": ["search_query"],
2536
},
@@ -30,19 +41,19 @@ def build_google_search_function() -> list[ChatCompletionToolParam]:
3041
def extract_search_arguments(chat_completion: ChatCompletion):
3142
response_message = chat_completion.choices[0].message
3243
search_query = None
44+
3345

3446
if response_message.tool_calls:
3547
for tool in response_message.tool_calls:
3648
if tool.type != "function":
3749
continue
3850
function = tool.function
39-
if function.name == "search_database":
51+
if function.name == "search_google":
4052
arg = json.loads(function.arguments)
4153
search_query = arg.get("search_query")
42-
elif query_text := response_message.content:
43-
search_query = query_text.strip()
54+
locations = arg.get("locations", [])
4455

45-
return search_query
56+
return search_query, locations
4657

4758

4859
def build_specify_package_function() -> list[ChatCompletionToolParam]:

src/fastapi_app/rag_advanced.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,21 @@ async def google_search(self, messages):
6565
max_tokens=query_response_token_limit,
6666
n=1,
6767
tools=build_google_search_function(),
68-
tool_choice="auto",
68+
tool_choice={"type": "function", "function": {"name": "search_google"}},
6969
)
7070

71-
query_text = extract_search_arguments(query_chat_completion)
71+
search_query, locations = extract_search_arguments(query_chat_completion)
72+
locations = [f'"{location}"' for location in locations] if locations else []
73+
74+
query_text = f"{search_query} {' OR '.join(locations)}" if locations else search_query
7275

7376
results = await self.searcher.google_search(query_text, top=3)
7477

7578
sources_content = [f"[{(package.url)}]:{package.to_str_for_broad_rag()}\n\n" for package in results]
7679

7780
thought_steps = [
78-
ThoughtStep(title="Prompt to generate search arguments", description=query_text, props={}),
81+
ThoughtStep(title="Prompt to generate search arguments", description=messages, props={}),
82+
ThoughtStep(title="Google Search query", description=query_text, props={}),
7983
ThoughtStep(title="Google Search results", description=[result.to_dict() for result in results], props={}),
8084
]
8185
return sources_content, thought_steps

0 commit comments

Comments
 (0)