From ce91323dc93fe11fbbe383db462dee944e1a9027 Mon Sep 17 00:00:00 2001 From: Josako Date: Sat, 25 May 2024 20:17:02 +0200 Subject: [PATCH] Enable model variables & start working on RAG task --- common/models/user.py | 9 + common/utils/celery_utils.py | 2 + config/config.py | 32 +++- config/logging_config.py | 13 ++ eveai_app/templates/user/tenant_overview.html | 13 ++ eveai_app/views/user_forms.py | 6 +- eveai_chat/__init__.py | 13 +- eveai_chat/socket_handlers/chat_handler.py | 39 ++++- eveai_chat_workers/__init__.py | 36 ++++ eveai_chat_workers/tasks.py | 158 ++++++++++++++++++ eveai_workers/celery_utils.py | 23 --- scripts/run_eveai_chat_workers.py | 4 + scripts/start_eveai_chat_workers.sh | 13 ++ scripts/start_eveai_workers.sh | 4 +- static/js/eveai-chat-widget.js | 21 +++ 15 files changed, 340 insertions(+), 46 deletions(-) create mode 100644 eveai_chat_workers/__init__.py create mode 100644 eveai_chat_workers/tasks.py delete mode 100644 eveai_workers/celery_utils.py create mode 100644 scripts/run_eveai_chat_workers.py create mode 100755 scripts/start_eveai_chat_workers.sh diff --git a/common/models/user.py b/common/models/user.py index 9eb5ffa..9b2ac43 100644 --- a/common/models/user.py +++ b/common/models/user.py @@ -2,6 +2,7 @@ from common.extensions import db from flask_security import UserMixin, RoleMixin from sqlalchemy.dialects.postgresql import ARRAY import sqlalchemy as sa +from sqlalchemy import CheckConstraint class Tenant(db.Model): @@ -33,6 +34,14 @@ class Tenant(db.Model): html_included_elements = db.Column(ARRAY(sa.String(50)), nullable=True) html_excluded_elements = db.Column(ARRAY(sa.String(50)), nullable=True) + # Embedding search variables + es_k = db.Column(db.Integer, nullable=True, default=5) + es_similarity_threshold = db.Column(db.Float, nullable=True, default=0.7) + + # Chat variables + chat_RAG_temperature = db.Column(db.Float, nullable=True, default=0.3) + chat_no_RAG_temperature = db.Column(db.Float, nullable=True, default=0.5) + # Licensing Information license_start_date = db.Column(db.Date, nullable=True) license_end_date = db.Column(db.Date, nullable=True) diff --git a/common/utils/celery_utils.py b/common/utils/celery_utils.py index bd3e9dc..8224fbb 100644 --- a/common/utils/celery_utils.py +++ b/common/utils/celery_utils.py @@ -7,6 +7,8 @@ celery_app = Celery() def init_celery(celery, app): celery_app.main = app.name + app.logger.debug(f'CELERY_BROKER_URL: {app.config["CELERY_BROKER_URL"]}') + app.logger.debug(f'CELERY_RESULT_BACKEND: {app.config["CELERY_RESULT_BACKEND"]}') celery_config = { 'broker_url': app.config.get('CELERY_BROKER_URL', 'redis://localhost:6379/0'), 'result_backend': app.config.get('CELERY_RESULT_BACKEND', 'redis://localhost:6379/0'), diff --git a/config/config.py b/config/config.py index 7fff6f5..4db4cad 100644 --- a/config/config.py +++ b/config/config.py @@ -1,5 +1,6 @@ from os import environ, path from datetime import timedelta +import redis basedir = path.abspath(path.dirname(__file__)) @@ -42,7 +43,7 @@ class Config(object): # supported LLMs SUPPORTED_EMBEDDINGS = ['openai.text-embedding-3-small', 'mistral.mistral-embed'] - SUPPORTED_LLMS = ['openai.gpt-4-turbo', 'openai.gpt-3.5-turbo', 'mistral.mistral-large-2402'] + SUPPORTED_LLMS = ['openai.gpt-4o', 'openai.gpt-4-turbo', 'openai.gpt-3.5-turbo', 'mistral.mistral-large-2402'] # Celery settings CELERY_TASK_SERIALIZER = 'json' @@ -62,6 +63,20 @@ class Config(object): GPT3_5_SUMMARY_TEMPLATE = """Write a concise summary of the text in the same language as the provided text. Text is delimited between triple backquotes. ```{text}```""" + GPT4_RAG_TEMPLATE = """Answer the question based on the following context, both delimited between triple backquotes + in the same language as question. + If the question cannot be answered using the text, say "I don't know" in the same language as the question. + Context: + ```{context}``` + Question: + ```{question}```""" + GPT3_5_RAG_TEMPLATE = """Answer the question based on the following context, both delimited between triple backquotes + in the same language as question. + If the question cannot be answered using the text, say "I don't know" in the same language as the question. + Context: + ```{context}``` + Question: + ```{question}```""" # SocketIO settings # SOCKETIO_ASYNC_MODE = 'threading' @@ -91,8 +106,13 @@ class DevConfig(Config): UPLOAD_FOLDER = '/Volumes/OWC4M2_1/Development/eveAI/file_store' # Celery settings - CELERY_BROKER_URL = 'redis://localhost:6379/0' # Default Redis configuration + # eveai_app Redis Settings + CELERY_BROKER_URL = 'redis://localhost:6379/0' CELERY_RESULT_BACKEND = 'redis://localhost:6379/0' + # eveai_chat Redis Settings + CELERY_BROKER_URL_CHAT = 'redis://localhost:6379/3' + CELERY_RESULT_BACKEND_CHAT = 'redis://localhost:6379/3' + # OpenAI API Keys OPENAI_API_KEY = 'sk-proj-8R0jWzwjL7PeoPyMhJTZT3BlbkFJLb6HfRB2Hr9cEVFWEhU7' @@ -118,12 +138,8 @@ class DevConfig(Config): JWT_SECRET_KEY = 'bsdMkmQ8ObfMD52yAFg4trrvjgjMhuIqg2fjDpD/JqvgY0ccCcmlsEnVFmR79WPiLKEA3i8a5zmejwLZKl4v9Q==' # Session settings - SESSION_REDIS = { - 'host': 'localhost', # Redis server hostname or IP address - 'port': 6379, # Redis server port - 'db': 2, # Redis database number (optional) - 'password': None # Redis password (optional) - } + SESSION_REDIS = redis.from_url('redis://localhost:6379/2') + class ProdConfig(Config): DEVELOPMENT = False diff --git a/config/logging_config.py b/config/logging_config.py index 9156dfa..0214108 100644 --- a/config/logging_config.py +++ b/config/logging_config.py @@ -26,6 +26,14 @@ LOGGING = { 'backupCount': 10, 'formatter': 'standard', }, + 'file_chat_workers': { + 'level': 'DEBUG', + 'class': 'logging.handlers.RotatingFileHandler', + 'filename': 'logs/eveai_chat_workers.log', + 'maxBytes': 1024*1024*5, # 5MB + 'backupCount': 10, + 'formatter': 'standard', + }, 'console': { 'class': 'logging.StreamHandler', 'level': 'DEBUG', @@ -53,6 +61,11 @@ LOGGING = { 'level': 'DEBUG', 'propagate': False }, + 'eveai_chat_workers': { # logger for the eveai_chat_workers + 'handlers': ['file_chat_workers', 'console'], + 'level': 'DEBUG', + 'propagate': False + }, '': { # root logger 'handlers': ['console'], 'level': 'WARNING', # Set higher level for root to minimize noise diff --git a/eveai_app/templates/user/tenant_overview.html b/eveai_app/templates/user/tenant_overview.html index 51f4e36..6fe7380 100644 --- a/eveai_app/templates/user/tenant_overview.html +++ b/eveai_app/templates/user/tenant_overview.html @@ -35,6 +35,11 @@ HTML Chunking +