Function calling для поиска знаний
Строим агента с двумя функциями: поиск статей arXiv по эмбеддингам и суммаризация PDF. Агент сам решает какую функцию вызвать по запросу пользователя.
Добавьте третью функцию list_downloaded_papers которая возвращает список уже скачанных статей из CSV-библиотеки. Убедитесь что агент использует её когда пользователь спрашивает «что у нас уже есть».
Скопируйте и адаптируйте под свой контекст. Текст в треугольных скобках — то, что нужно заменить.
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) # ваш диспатчер
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
)