Skip to content

Commit ef5f603

Browse files
authored
Implemented vector index and MCP tool for semantic search (#311)
* Implemented vector index for chat history context and MCP tool for semantic search & summarization * Persisted storage of vectors using Chroma
1 parent ee8652c commit ef5f603

File tree

13 files changed

+188
-4
lines changed

13 files changed

+188
-4
lines changed

deploy/docker/docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ services:
167167
- MONGO_DB_PASSWORD=crapisecretpassword
168168
- MONGO_DB_NAME=crapi
169169
- DEFAULT_MODEL=gpt-4o-mini
170+
- CHROMA_PERSIST_DIRECTORY=/app/vectorstore
170171
# - CHATBOT_OPENAI_API_KEY=
172+
volumes:
173+
- chatbot-vectors:/app/vectorstore
171174
depends_on:
172175
mongodb:
173176
condition: service_healthy
@@ -295,3 +298,4 @@ services:
295298
volumes:
296299
mongodb-data:
297300
postgresql-data:
301+
chatbot-vectors:

deploy/helm/templates/chatbot/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ data:
2222
MONGO_DB_NAME: {{ .Values.mongodb.config.mongoDbName }}
2323
CHATBOT_OPENAI_API_KEY: {{ .Values.openAIApiKey }}
2424
DEFAULT_MODEL: {{ .Values.chatbot.config.defaultModel | quote }}
25+
CHROMA_PERSIST_DIRECTORY: {{ .Values.chatbot.config.chromaPersistDirectory | quote }}

deploy/helm/templates/chatbot/deployment.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ spec:
5757
port: {{ .Values.chatbot.port }}
5858
initialDelaySeconds: 15
5959
periodSeconds: 10
60+
volumeMounts:
61+
- name: chatbot-vectors
62+
mountPath: {{ .Values.chatbot.config.chromaPersistDirectory | quote }}
63+
volumes:
64+
- name: chatbot-vectors
65+
persistentVolumeClaim:
66+
claimName: {{ .Values.chatbot.storage.pvc.name }}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{{- if eq .Values.chatbot.storage.type "manual" }}
2+
apiVersion: v1
3+
kind: PersistentVolume
4+
metadata:
5+
name: {{ .Values.chatbot.storage.pv.name }}
6+
labels:
7+
release: {{ .Release.Name }}
8+
{{- toYaml .Values.chatbot.storage.pv.labels | nindent 4 }}
9+
spec:
10+
storageClassName: {{ .Values.chatbot.storage.type }}
11+
capacity:
12+
storage: {{ .Values.chatbot.storage.pv.resources.storage }}
13+
accessModes:
14+
- ReadWriteOnce
15+
hostPath:
16+
path: {{ .Values.chatbot.storage.pv.hostPath }}
17+
---
18+
{{- end }}
19+
apiVersion: v1
20+
kind: PersistentVolumeClaim
21+
metadata:
22+
name: {{ .Values.chatbot.storage.pvc.name }}
23+
labels:
24+
release: {{ .Release.Name }}
25+
{{- toYaml .Values.chatbot.storage.pvc.labels | nindent 4 }}
26+
spec:
27+
{{- if ne .Values.chatbot.storage.type "default" }}
28+
storageClassName: {{ .Values.chatbot.storage.type }}
29+
{{- end }}
30+
accessModes:
31+
- ReadWriteOnce
32+
resources:
33+
{{- toYaml .Values.chatbot.storage.pvc.resources | nindent 4 }}

deploy/helm/values-pv.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,22 @@ postgresdb:
3535
resources:
3636
requests:
3737
storage: 2Gi
38+
39+
chatbot:
40+
storage:
41+
type: "manual"
42+
pv:
43+
name: chatbot-vectors-pv
44+
labels:
45+
app: crapi-chatbot
46+
resources:
47+
storage: 1Gi
48+
hostPath: /mnt/vectorstore
49+
type: "default"
50+
pvc:
51+
name: chatbot-vectors-pv-claim
52+
labels:
53+
app: crapi-chatbot
54+
resources:
55+
requests:
56+
storage: 1Gi

deploy/helm/values.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ chatbot:
152152
mongoDbDriver: mongodb
153153
secretKey: crapi
154154
defaultModel: gpt-4o-mini
155+
chromaPersistDirectory: /app/vectorstore
156+
storage:
157+
# type: "manual"
158+
# pv:
159+
# name: chatbot-vectors-pv
160+
# labels:
161+
# app: crapi-chatbot
162+
# resources:
163+
# storage: 1Gi
164+
# hostPath: /mnt/vectorstore
165+
type: "default"
166+
pvc:
167+
name: chatbot-vectors-pv-claim
168+
labels:
169+
app: crapi-chatbot
170+
resources:
171+
requests:
172+
storage: 1Gi
155173
deploymentLabels:
156174
app: crapi-chatbot
157175
podLabels:

services/chatbot/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ RUN pip install --no-cache-dir -r requirements.txt
1616
COPY src /app
1717
COPY certs /app/certs
1818
COPY retrieval /app/retrieval
19+
RUN mkdir -p /app/vectorstore
1920
RUN mkdir -p /app/resources
2021
COPY src/resources/crapi-openapi-spec.json /app/resources/crapi-openapi-spec.json
2122
ENV PYTHONPATH="/app"

services/chatbot/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ langgraph==0.5.1
1818
faiss-cpu==1.11.0
1919
psycopg2-binary
2020
uvicorn==0.35.0
21-
fastmcp==2.10.2
21+
fastmcp==2.10.2
22+
chromadb==1.0.15

services/chatbot/src/chatbot/chat_service.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from uuid import uuid4
2-
32
from langgraph.graph.message import Messages
4-
3+
from .vector_index import update_vector_index
54
from .extensions import db
6-
from .langgraph_agent import build_langgraph_agent, execute_langgraph_agent
5+
from .langgraph_agent import execute_langgraph_agent
76

87

98
async def get_chat_history(session_id):
@@ -39,4 +38,8 @@ async def process_user_message(session_id, user_message, api_key, model_name, us
3938
# Limit chat history to last 20 messages
4039
history = history[-20:]
4140
await update_chat_history(session_id, history)
41+
# if not os.path.exists(retrieval_index_path):
42+
# await build_vector_index_from_chat_history(api_key)
43+
# else:
44+
await update_vector_index(api_key, session_id, {"user": user_message, "assistant": reply.content})
4245
return reply.content, response_message_id

services/chatbot/src/chatbot/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ class Config:
1111
SECRET_KEY = os.getenv("SECRET_KEY", "super-secret")
1212
MONGO_URI = MONGO_CONNECTION_URI
1313
DEFAULT_MODEL_NAME = os.getenv("DEFAULT_MODEL", "gpt-4o-mini")
14+
CHROMA_PERSIST_DIRECTORY = os.getenv("CHROMA_PERSIST_DIRECTORY", "/app/vectorstore")

0 commit comments

Comments
 (0)