Skip to content

Add chromadb #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions deploy/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,16 @@ services:
- API_PASSWORD=Admin!123
- OPENAPI_SPEC=/app/resources/crapi-openapi-spec.json
- DEFAULT_MODEL=gpt-4o-mini
- CHROMA_PERSIST_DIRECTORY=/app/vectorstore
- CHROMA_HOST=chromadb
- CHROMA_PORT=8000
# - CHATBOT_OPENAI_API_KEY=
volumes:
- chatbot-vectors:/app/vectorstore
depends_on:
mongodb:
condition: service_healthy
crapi-identity:
condition: service_healthy
chromadb:
condition: service_healthy
# ports:
# - "${LISTEN_IP:-127.0.0.1}:5002:5002"

Expand Down Expand Up @@ -262,6 +263,22 @@ services:
cpus: '0.3'
memory: 128M

chromadb:
container_name: chromadb
image: 'chromadb/chroma:latest'
environment:
IS_PERSISTENT: 'TRUE'
healthcheck:
test: [ "CMD", "/bin/bash", "-c", "cat < /dev/null > /dev/tcp/localhost/8000" ]
interval: 15s
timeout: 15s
retries: 15
start_period: 20s
volumes:
- chromadb-data:/data
# ports:
# - "${LISTEN_IP:-127.0.0.1}:8000:8000"

mailhog:
user: root
container_name: mailhog
Expand Down Expand Up @@ -303,4 +320,4 @@ services:
volumes:
mongodb-data:
postgresql-data:
chatbot-vectors:
chromadb-data:
3 changes: 2 additions & 1 deletion deploy/helm/templates/chatbot/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ data:
MONGO_DB_NAME: {{ .Values.mongodb.config.mongoDbName }}
CHATBOT_OPENAI_API_KEY: {{ .Values.openAIApiKey }}
DEFAULT_MODEL: {{ .Values.chatbot.config.defaultModel | quote }}
CHROMA_PERSIST_DIRECTORY: {{ .Values.chatbot.config.chromaPersistDirectory | quote }}
CHROMA_HOST: {{ .Values.chromadb.service.name }}
CHROMA_PORT: {{ .Values.chromadb.port | quote }}
API_USER: {{ .Values.chatbot.config.apiUser | quote }}
API_PASSWORD: {{ .Values.chatbot.config.apiPassword | quote }}
OPENAPI_SPEC: {{ .Values.chatbot.config.openapiSpec | quote }}
7 changes: 0 additions & 7 deletions deploy/helm/templates/chatbot/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,3 @@ spec:
port: {{ .Values.chatbot.port }}
initialDelaySeconds: 15
periodSeconds: 10
volumeMounts:
- name: chatbot-vectors
mountPath: {{ .Values.chatbot.config.chromaPersistDirectory | quote }}
volumes:
- name: chatbot-vectors
persistentVolumeClaim:
claimName: {{ .Values.chatbot.storage.pvc.name }}
33 changes: 0 additions & 33 deletions deploy/helm/templates/chatbot/storage.yaml

This file was deleted.

11 changes: 11 additions & 0 deletions deploy/helm/templates/chromadb/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.chromadb.config.name }}
labels:
release: {{ .Release.Name }}
{{- with .Values.chromadb.config.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
data:
IS_PERSISTENT: {{ .Values.chromadb.config.isPersistent | quote }}
15 changes: 15 additions & 0 deletions deploy/helm/templates/chromadb/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.chromadb.service.name }}
labels:
release: {{ .Release.Name }}
{{- with .Values.chromadb.service.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
ports:
- port: {{ .Values.chromadb.port }}
name: chromadb
selector:
{{- toYaml .Values.chromadb.serviceSelectorLabels | nindent 4 }}
38 changes: 38 additions & 0 deletions deploy/helm/templates/chromadb/statefulset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ .Values.chromadb.name }}
labels:
release: {{ .Release.Name }}
{{- with .Values.chromadb.statefulsetLabels }}
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
serviceName: {{ .Values.chromadb.service.name }}
replicas: {{ .Values.chromadb.replicaCount }}
selector:
matchLabels:
{{- toYaml .Values.chromadb.statefulsetSelectorMatchLabels | nindent 6 }}
template:
metadata:
labels:
release: {{ .Release.Name }}
{{- toYaml .Values.chromadb.podLabels | nindent 8 }}
spec:
containers:
- name: {{ .Values.chromadb.name }}
image: {{ .Values.chromadb.image }}:{{ .Values.chromadb.version }}
imagePullPolicy: {{ .Values.chromadb.imagePullPolicy }}
ports:
- containerPort: {{ .Values.chromadb.port }}
envFrom:
- configMapRef:
name: {{ .Values.chromadb.config.name }}
volumeMounts:
- mountPath: /data
name: chromadb-data
volumes:
- name: chromadb-data
persistentVolumeClaim:
claimName: {{ .Values.chromadb.storage.pvc.name }}

34 changes: 34 additions & 0 deletions deploy/helm/templates/chromadb/storage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{- if eq .Values.chromadb.storage.type "manual" }}
apiVersion: v1
kind: PersistentVolume
metadata:
name: {{ .Values.chromadb.storage.pv.name }}
labels:
release: {{ .Release.Name }}
{{- toYaml .Values.chromadb.storage.pv.labels | nindent 4 }}
spec:
storageClassName: {{ .Values.chromadb.storage.type }}
capacity:
storage: {{ .Values.chromadb.storage.pv.resources.storage }}
accessModes:
- ReadWriteOnce
hostPath:
path: {{ .Values.chromadb.storage.pv.hostPath }}
---
{{- end }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ .Values.chromadb.storage.pvc.name }}
labels:
release: {{ .Release.Name }}
{{- toYaml .Values.chromadb.storage.pvc.labels | nindent 4 }}
spec:
{{- if ne .Values.chromadb.storage.type "default" }}
storageClassName: {{ .Values.chromadb.storage.type }}
{{- end }}
accessModes:
- ReadWriteOnce
resources:
{{- toYaml .Values.chromadb.storage.pvc.resources | nindent 4 }}

41 changes: 41 additions & 0 deletions deploy/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,44 @@ apiGatewayService:
app: gateway-service
serviceSelectorLabels:
app: gateway-service

chromadb:
name: chromadb
image: chromadb/chroma
version: latest
imagePullPolicy: IfNotPresent
port: 8000
replicaCount: 1
service:
name: chromadb
labels:
app: chromadb
config:
name: chromadb-config
labels:
app: chromadb
storage:
# type: "manual"
# pv:
# name: chromadb-pv
# labels:
# app: chromadb
# resources:
# storage: 1Gi
# hostPath: /mnt/chromadb
type: "default"
pvc:
name: chromadb-pv-claim
labels:
app: chromadb
resources:
requests:
storage: 2Gi
serviceSelectorLabels:
app: chromadb
podLabels:
app: chromadb
statefulsetLabels:
app: chromadb
statefulsetSelectorMatchLabels:
app: chromadb
3 changes: 2 additions & 1 deletion services/chatbot/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ faiss-cpu==1.11.0
psycopg2-binary
uvicorn==0.35.0
fastmcp==2.10.2
chromadb==1.0.15
chromadb-client==1.0.15
langchain-chroma==0.2.4
8 changes: 6 additions & 2 deletions services/chatbot/src/chatbot/chat_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
get_or_create_session_id,
store_api_key,
store_model_name,
get_user_jwt
get_user_jwt,
)

chat_bp = Blueprint("chat", __name__, url_prefix="/genai")
Expand Down Expand Up @@ -38,6 +38,7 @@ async def init():
await store_api_key(session_id, openai_api_key)
return jsonify({"message": "Initialized"}), 200


@chat_bp.route("/model", methods=["POST"])
async def model():
session_id = await get_or_create_session_id()
Expand All @@ -49,6 +50,7 @@ async def model():
await store_model_name(session_id, model_name)
return jsonify({"model_used": model_name}), 200


@chat_bp.route("/ask", methods=["POST"])
async def chat():
session_id = await get_or_create_session_id()
Expand All @@ -62,7 +64,9 @@ async def chat():
id = data.get("id", uuid4().int & (1 << 63) - 1)
if not message:
return jsonify({"message": "Message is required", "id": id}), 400
reply, response_id = await process_user_message(session_id, message, openai_api_key, model_name, user_jwt)
reply, response_id = await process_user_message(
session_id, message, openai_api_key, model_name, user_jwt
)
return jsonify({"id": response_id, "message": reply}), 200


Expand Down
11 changes: 5 additions & 6 deletions services/chatbot/src/chatbot/chat_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from uuid import uuid4
from langgraph.graph.message import Messages
from .vector_index import update_vector_index
from .retriever import update_collection
from .extensions import db
from .langgraph_agent import execute_langgraph_agent

Expand All @@ -27,19 +27,18 @@ async def process_user_message(session_id, user_message, api_key, model_name, us
source_message_id = uuid4().int & (1 << 63) - 1
history.append({"id": source_message_id, "role": "user", "content": user_message})
# Run LangGraph agent
response = await execute_langgraph_agent(api_key, model_name, history, user_jwt, session_id)
response = await execute_langgraph_agent(
api_key, model_name, history, user_jwt, session_id
)
print("Response", response)
reply: Messages = response.get("messages", [{}])[-1]
print("Reply", reply.content)
response_message_id = uuid4().int & (1 << 63) - 1
history.append(
{"id": response_message_id, "role": "assistant", "content": reply.content}
)
await update_collection(api_key, session_id, {"user": user_message, "assistant": reply.content})
# Limit chat history to last 20 messages
history = history[-20:]
await update_chat_history(session_id, history)
# if not os.path.exists(retrieval_index_path):
# await build_vector_index_from_chat_history(api_key)
# else:
await update_vector_index(api_key, session_id, {"user": user_message, "assistant": reply.content})
return reply.content, response_message_id
5 changes: 3 additions & 2 deletions services/chatbot/src/chatbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dotenv import load_dotenv

from .dbconnections import MONGO_CONNECTION_URI
from .dbconnections import MONGO_CONNECTION_URI, CHROMA_HOST, CHROMA_PORT

load_dotenv()

Expand All @@ -11,4 +11,5 @@ class Config:
SECRET_KEY = os.getenv("SECRET_KEY", "super-secret")
MONGO_URI = MONGO_CONNECTION_URI
DEFAULT_MODEL_NAME = os.getenv("DEFAULT_MODEL", "gpt-4o-mini")
CHROMA_PERSIST_DIRECTORY = os.getenv("CHROMA_PERSIST_DIRECTORY", "/app/vectorstore")
CHROMA_HOST = CHROMA_HOST
CHROMA_PORT = CHROMA_PORT
3 changes: 3 additions & 0 deletions services/chatbot/src/chatbot/dbconnections.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@
POSTGRES_PORT,
POSTGRES_DB,
)

CHROMA_HOST = os.environ.get("CHROMA_HOST", "chromadb")
CHROMA_PORT = os.environ.get("CHROMA_PORT", "8000")
Loading
Loading