Skip to content

Commit

Permalink
Merge pull request #381 from openchatai/feat/analytics
Browse files Browse the repository at this point in the history
Feat/analytics
  • Loading branch information
codebanesr authored Dec 8, 2023
2 parents 17fc73f + a6bc7ec commit 7a87a30
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion llm-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM python:3.9-slim AS common
WORKDIR /app
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install -r requirements.txt
COPY . /app/

# Development stage
Expand Down
31 changes: 31 additions & 0 deletions llm-server/routes/analytics/analytics_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from shared.models.opencopilot_db.analytics import Analytics
from sqlalchemy.orm import sessionmaker
from shared.models.opencopilot_db import engine

def upsert_analytics_record(chatbot_id: str, successful_operations: int, total_operations: int, logs: str = ""):
Session = sessionmaker(bind=engine)
session = Session()

# Fetch the existing record, if any
existing_record = session.query(Analytics).filter_by(chatbot_id=chatbot_id).first()

if existing_record:
# Increment existing values
existing_record.successful_operations += successful_operations
existing_record.total_operations += total_operations
if logs:
existing_record.logs = logs
print("Analytics record updated for chatbot_id:", chatbot_id)
else:
# Create a new record
record_to_upsert = Analytics(
chatbot_id=chatbot_id,
successful_operations=successful_operations,
total_operations=total_operations,
logs=logs
)
session.add(record_to_upsert)
print("Analytics record inserted for chatbot_id:", chatbot_id)

session.commit()
session.close()
6 changes: 5 additions & 1 deletion llm-server/routes/chat/chat_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import cast

from flask import jsonify, Blueprint, request, Response, abort, Request
from routes.analytics.analytics_service import upsert_analytics_record
from routes.chat.chat_dto import ChatInput
from utils.get_logger import CustomLogger
from utils.llm_consts import X_App_Name
Expand Down Expand Up @@ -160,13 +161,16 @@ async def send_chat():
)

if response_data["response"]:
upsert_analytics_record(chatbot_id=str(bot.id), successful_operations=1, total_operations=1)
create_chat_history(str(bot.id), session_id, True, message)
create_chat_history(
str(bot.id),
session_id,
False,
response_data["response"] or response_data["error"],
response_data["response"] or response_data["error"] or "",
)
elif response_data["error"]:
upsert_analytics_record(chatbot_id=str(bot.id), successful_operations=0, total_operations=1, logs=response_data["error"])

return jsonify(
{"type": "text", "response": {"text": response_data["response"]}}
Expand Down
20 changes: 20 additions & 0 deletions llm-server/shared/models/opencopilot_db/analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from sqlalchemy import Column, Integer, String, ForeignKey
from .get_declarative_base import Base
from .database_setup import engine
from .chatbot import Chatbot
class Analytics(Base):
__tablename__ = 'analytics'

chatbot_id = Column(String(36), primary_key=True)
chatbot_id_fk = Column(String(36), ForeignKey(Chatbot.id), nullable=True)
successful_operations = Column(Integer)
total_operations = Column(Integer)
logs= Column(String(4096), nullable=True)

def __init__(self, chatbot_id, successful_operations, total_operations, logs=None):
self.chatbot_id = chatbot_id
self.successful_operations = successful_operations
self.total_operations = total_operations
self.logs = logs

Base.metadata.create_all(engine)
2 changes: 1 addition & 1 deletion llm-server/shared/models/opencopilot_db/database_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def connection_creator():
# Create an SQLAlchemy engine with the connection pool
engine = create_engine(db_url, poolclass=QueuePool, pool_logging_name="worker_pool")

pool = QueuePool(creator=connection_creator, pool_size=5)
pool = QueuePool(creator=connection_creator, pool_size=20)

def create_database_schema():
Base.metadata.create_all(engine)

0 comments on commit 7a87a30

Please sign in to comment.