Function Calling for Knowledge Retrieval
Build an agent with two functions: arXiv article search using embeddings, and PDF summarization. The agent decides which function to call based on the user's query.
Add a third function list_downloaded_papers that returns the list of already downloaded papers from the CSV library. Verify the agent uses it when a user asks 'what papers do we already have'.
Copy and adapt to your context. Text in angle brackets should be replaced.
tools = [
{
"type": "function",
"function": {
"name": "get_articles",
"description": "Search arXiv for papers on a topic.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"}
},
"required": ["query"],
},
},
},
]
messages = [
{"role": "system", "content": "Use functions to answer research questions."},
{"role": "user", "content": user_question},
]
response = client.chat.completions.create(
model="gpt-4o-mini", messages=messages, tools=tools
)
# Loop while model wants to call tools
while response.choices[0].finish_reason == "tool_calls":
call = response.choices[0].message.tool_calls[0]
result = dispatch(call) # your dispatcher
messages += [response.choices[0].message,
{"role": "tool", "tool_call_id": call.id,
"name": call.function.name, "content": str(result)}]
response = client.chat.completions.create(
model="gpt-4o-mini", messages=messages, tools=tools
)