From 77b6fcd15e6821b2c07392c5a5be01b3d449f8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B8=D0=BD=D0=B0=D1=80=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0=D0=B3=D0=B8=D1=82=D0=BE=D0=B2?= Date: Thu, 11 Jun 2026 09:16:07 +0000 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE=20=D0=BA=20=D0=BF=D1=83?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D0=B8:=20add=20agent?= =?UTF-8?q?.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 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, +)