From 4894cc1dcfb4acd18215b0dc542e1311acf318bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Thu, 4 Jun 2026 22:57:18 +0000 Subject: [PATCH] Add src/agent.py --- src/agent.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/agent.py diff --git a/src/agent.py b/src/agent.py new file mode 100644 index 0000000..c4939bb --- /dev/null +++ b/src/agent.py @@ -0,0 +1,54 @@ +"""Agent implementation using LangChain tool‑calling. + +The agent is built with the modern LangChain interface. It uses a system prompt that +encourages the assistant to search the knowledge base before answering. +""" + +from typing import List + +from langchain_core.prompts import ChatPromptTemplate +from langchain.agents import AgentExecutor, create_tool_calling_agent +from langchain_ollama import ChatOllama + +from .tools import search_knowledge_base, add_to_knowledge_base + +# Create the LLM that will power the agent. We use Ollama's local llama3 model. +llm = ChatOllama(model="llama3", temperature=0.7) + +# Register tools +TOOLS: List = [search_knowledge_base, add_to_knowledge_base] + +# Prompt template that tells the agent to use the tools when needed. +prompt = ChatPromptTemplate.from_messages([ + ("system", "You are a helpful assistant. Use the provided tools to answer the user."), + ("placeholder", "{chat_history}"), + ("human", "{input}"), + ("placeholder", "{agent_scratchpad}"), +]) + +# Create the tool‑calling agent +agent = create_tool_calling_agent(llm, TOOLS, prompt) + +# Wrap it in an executor that will handle the conversation loop +agent_executor = AgentExecutor(agent=agent, tools=TOOLS, verbose=True) + +# Expose a simple function that can be called from a CLI or other entry point + +def run_agent(messages: List[dict]): + """Run the agent on a list of chat messages. + + Parameters + ---------- + messages: List[dict] + Each message is a dict with keys ``role`` ("user" or "assistant") and ``content``. + + Returns + ------- + str + The assistant's reply. + """ + # Convert the list of messages into the format expected by the executor + chat_history = [(msg["role"], msg["content"]) for msg in messages] + input_text = messages[-1]["content"] if messages else "" + result = agent_executor.invoke({"input": input_text, "chat_history": chat_history}) + return result["output"] \ No newline at end of file