commit 77b6fcd15e6821b2c07392c5a5be01b3d449f8e2 Author: Ринар Мирзагитов Date: Thu Jun 11 09:16:07 2026 +0000 Решение готово к публикации: add agent.py diff --git a/agent.py b/agent.py new file mode 100644 index 0000000..9b2f790 --- /dev/null +++ b/agent.py @@ -0,0 +1,32 @@ +""" +Create a LangChain agent that chooses between local KB and web search. +""" + +from typing import List, Dict + +from langchain.agents import create_agent +from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder +from langchain.schema.document import Document + +# Import tools +from tools import search_local_kb, web_search + +SYSTEM_PROMPT = """ +You are an assistant that can answer questions using either a local knowledge base or the web. Use the tool `search_local_kb` when the question is about content already in your documents. Use `web_search` for up‑to‑date facts. +When you provide an answer, include the source: either "chromadb" or "tavily". +""" + +# Prompt template with tool messages +prompt = ChatPromptTemplate.from_messages([ + ("system", SYSTEM_PROMPT), + MessagesPlaceholder("history"), + ("human", "{input}"), +]) + +# Create agent harness +agent = create_agent( + model="ollama:llama3", + tools=[search_local_kb, web_search], + system_prompt=SYSTEM_PROMPT, + prompt_template=prompt, +)