Update src/agent.py
This commit is contained in:
+29
-30
@@ -1,32 +1,35 @@
|
|||||||
"""Agent that uses the RAG tools.
|
"""Agent construction for the RAG system.
|
||||||
|
|
||||||
The agent is built with ``create_agent`` from ``langchain.agents`` and is
|
This module builds a LangChain agent that uses a local knowledge base.
|
||||||
configured to use the local ``ChatOllama`` model (``llama3``). It has two
|
The agent is created using :func:`langchain.agents.create_agent`.
|
||||||
tools: ``search_knowledge_base`` and ``add_to_knowledge_base``.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from langchain_ollama import ChatOllama
|
from langchain_ollama import ChatOllama
|
||||||
from langchain.agents import create_agent
|
from langchain.agents import create_agent
|
||||||
|
|
||||||
# Import the tools from the package.
|
|
||||||
from .tools import search_knowledge_base, add_to_knowledge_base
|
from .tools import search_knowledge_base, add_to_knowledge_base
|
||||||
|
|
||||||
# Instantiate the LLM.
|
# LLM configuration
|
||||||
llm = ChatOllama(model="llama3", temperature=0.7)
|
LLM_MODEL = "llama3"
|
||||||
|
SYSTEM_PROMPT = (
|
||||||
# Create the agent.
|
"You are an assistant that uses a local knowledge base. "
|
||||||
agent = create_agent(
|
"When a user asks a question, first search the knowledge base "
|
||||||
model=llm,
|
"with the tool 'search_knowledge_base'. If the information is not "
|
||||||
tools=[search_knowledge_base, add_to_knowledge_base],
|
"sufficient, ask clarifying questions. You can also add new "
|
||||||
verbose=True,
|
"information to the knowledge base using the tool 'add_to_knowledge_base'."
|
||||||
)
|
)
|
||||||
|
|
||||||
# Helper function to run a user query.
|
# Create the LLM with system prompt
|
||||||
|
llm = ChatOllama(model=LLM_MODEL, system=SYSTEM_PROMPT)
|
||||||
|
|
||||||
|
# Build the agent
|
||||||
|
_tools = [search_knowledge_base, add_to_knowledge_base]
|
||||||
|
agent = create_agent(llm=llm, tools=_tools, verbose=True)
|
||||||
|
|
||||||
|
# Helper function to run a query through the agent
|
||||||
|
|
||||||
def run_query(user_input: str) -> str:
|
def run_query(user_input: str) -> str:
|
||||||
"""Invoke the agent with a user message and return the response.
|
"""Run a user query through the agent and return the response.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
@@ -36,18 +39,14 @@ def run_query(user_input: str) -> str:
|
|||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
str
|
str
|
||||||
The agent's reply.
|
The agent's response.
|
||||||
"""
|
"""
|
||||||
result = agent.invoke({"messages": [{"role": "user", "content": user_input}]})
|
# The agent expects a dict with an "input" key
|
||||||
# The result is a dict with a list of messages. The last message is the assistant's reply.
|
result = agent.run({"input": user_input})
|
||||||
messages = result.get("messages", [])
|
# The result may be a string or a dict; convert to string
|
||||||
if not messages:
|
if isinstance(result, dict):
|
||||||
return "No response."
|
return str(result)
|
||||||
# Find the last assistant message.
|
return result
|
||||||
for msg in reversed(messages):
|
|
||||||
if msg.get("role") == "assistant":
|
|
||||||
return msg.get("content", "")
|
|
||||||
# Fallback to the first message.
|
|
||||||
return messages[-1].get("content", "")
|
|
||||||
|
|
||||||
# End of src/agent.py
|
# Expose the agent for external use
|
||||||
|
__all__ = ["agent", "run_query"]
|
||||||
Reference in New Issue
Block a user