Skip to content

Commit 3efe47c

Browse files
committed
Retriever provided at agent creation
1 parent ec804d6 commit 3efe47c

File tree

7 files changed

+72
-140
lines changed

7 files changed

+72
-140
lines changed

services/chatbot/src/chatbot/chat_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from uuid import uuid4
22
from langgraph.graph.message import Messages
3-
from .vectordb import update_collection
3+
from .retriever import update_collection
44
from .extensions import db
55
from .langgraph_agent import execute_langgraph_agent
66

services/chatbot/src/chatbot/langgraph_agent.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from langgraph.prebuilt import create_react_agent
55
from .extensions import postgresdb
66
from .mcp_client import get_mcp_client
7-
7+
from .retriever import get_retriever_tool
88

99
async def build_langgraph_agent(api_key, model_name, user_jwt):
1010
system_prompt = textwrap.dedent(
@@ -49,7 +49,8 @@ async def build_langgraph_agent(api_key, model_name, user_jwt):
4949
mcp_client = get_mcp_client(user_jwt)
5050
mcp_tools = await mcp_client.get_tools()
5151
db_tools = toolkit.get_tools()
52-
tools = mcp_tools + db_tools
52+
retriever_tool = get_retriever_tool(api_key)
53+
tools = mcp_tools + db_tools + [retriever_tool]
5354
agent_node = create_react_agent(model=llm, tools=tools, prompt=system_prompt)
5455
return agent_node
5556

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from langchain.agents.agent_toolkits import create_retriever_tool
2+
from .config import Config
3+
import chromadb
4+
from uuid import uuid4
5+
from langchain_core.documents import Document
6+
from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction
7+
from langchain_openai import OpenAIEmbeddings
8+
from langchain_chroma import Chroma
9+
10+
async def get_collection(api_key):
11+
chroma_client = await chromadb.AsyncHttpClient(
12+
host=Config.CHROMA_HOST,
13+
port=Config.CHROMA_PORT,
14+
)
15+
embeddings = OpenAIEmbeddingFunction(api_key=api_key, model_name="text-embedding-3-large")
16+
collection = await chroma_client.get_or_create_collection(
17+
name="chats",
18+
embedding_function=embeddings,
19+
)
20+
return collection
21+
22+
async def update_collection(api_key, session_id, new_messages):
23+
docs = []
24+
ids = []
25+
for role, content in new_messages.items():
26+
if content:
27+
doc_id = str(uuid4())
28+
doc = Document(
29+
page_content=content,
30+
metadata={"session_id": session_id, "role": role}
31+
)
32+
docs.append(doc)
33+
ids.append(doc_id)
34+
35+
if docs:
36+
collection = await get_collection(api_key)
37+
await collection.add(
38+
documents=[d.page_content for d in docs],
39+
metadatas=[d.metadata for d in docs],
40+
ids=ids
41+
)
42+
43+
def get_retriever_tool(api_key):
44+
embeddings = OpenAIEmbeddings(api_key=api_key, model="text-embedding-3-large")
45+
chroma_client = chromadb.HttpClient(
46+
host=Config.CHROMA_HOST,
47+
port=Config.CHROMA_PORT,
48+
)
49+
collection = Chroma(
50+
collection_name="chats",
51+
embedding_function=embeddings,
52+
client=chroma_client,
53+
)
54+
retriever = collection.as_retriever(search_type="similarity", search_kwargs={"k": 5})
55+
return create_retriever_tool(
56+
retriever=retriever,
57+
name="retriever_tool",
58+
description="""
59+
Answer questions based on user chat history (summarized and semantically indexed).
60+
Use this when the user asks about prior chats, what they asked earlier, or wants a summary of past conversations.
61+
Answer questions based on the user's prior chat history.
62+
63+
Use this tool when the user refers to anything mentioned before, asks for a summary of previous messages or sessions,
64+
or references phrases like 'what I said earlier', 'things we discussed', 'my earlier question', 'until now', 'till date', 'all my conversations' or 'previously mentioned'.
65+
The chat history is semantically indexed and summarized using vector search.
66+
""",
67+
)

services/chatbot/src/chatbot/vectordb.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

services/chatbot/src/mcpserver/config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@ class Config:
1010
CHROMA_PERSIST_DIRECTORY = os.getenv("CHROMA_PERSIST_DIRECTORY", "/app/vectorstore")
1111
OPENAPI_SPEC = os.getenv("OPENAPI_SPEC", "/app/resources/crapi-openapi-spec.json")
1212
API_USER = os.getenv("API_USER", "[email protected]")
13-
API_PASSWORD = os.getenv("API_PASSWORD", "Admin!123")
14-
CHROMA_HOST = os.environ.get("CHROMA_HOST", "chromadb")
15-
CHROMA_PORT = os.environ.get("CHROMA_PORT", "8000")
13+
API_PASSWORD = os.getenv("API_PASSWORD", "Admin!123")

services/chatbot/src/mcpserver/server.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
from .config import Config
66
import logging
77
import time
8-
from .tool_helpers import (
9-
get_any_api_key,
10-
build_retrieverQA,
11-
)
128

139
# Configure logging
1410
logging.basicConfig(
@@ -78,27 +74,6 @@ def get_http_client():
7874
openapi_spec=openapi_spec, client=get_http_client(), name="My crAPI MCP Server"
7975
)
8076

81-
@mcp.tool(tags={"history", "search", "summary", "context"},)
82-
async def retriever_tool(question: str) -> str:
83-
"""Answer questions based on user chat history (summarized and semantically indexed).
84-
Use this when the user asks about prior chats, what they asked earlier, or wants a summary of past conversations.
85-
Answer questions based on the user's prior chat history.
86-
87-
Use this tool when the user refers to anything mentioned before, asks for a summary of previous messages or sessions,
88-
or references phrases like 'what I said earlier', 'things we discussed', 'my earlier question', 'until now', 'till date', 'all my conversations' or 'previously mentioned'.
89-
The chat history is semantically indexed and summarized using vector search."""
90-
91-
logger.info(f"search_chat_history called with: {question}")
92-
api_key=await get_any_api_key()
93-
if not api_key:
94-
logger.error("API key is not available. Cannot search chat history.")
95-
return "OpenAI API key is not available. Cannot search chat history."
96-
retrieverQA = build_retrieverQA(api_key=api_key)
97-
response = await retrieverQA.ainvoke({"query": question})
98-
result = response["result"]
99-
logger.info(f"RESULT: {result}")
100-
return result
101-
10277
if __name__ == "__main__":
10378
mcp_server_port = int(os.environ.get("MCP_SERVER_PORT", 5500))
10479
mcp.run(

services/chatbot/src/mcpserver/tool_helpers.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)