From 181aca9324ce929a0b3e58d06abaa35e9b2dc824 Mon Sep 17 00:00:00 2001 From: Vinicius Mesel <4984147+vmesel@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:26:59 -0300 Subject: [PATCH 1/3] Adds session management for openweb ui --- docker-compose.dev-container.yml | 18 ++++++++++++++++++ src/dialog/routers/openai.py | 22 ++++++++++++++++------ src/dialog/settings.py | 9 ++++++++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/docker-compose.dev-container.yml b/docker-compose.dev-container.yml index ff38611..8843c28 100644 --- a/docker-compose.dev-container.yml +++ b/docker-compose.dev-container.yml @@ -39,3 +39,21 @@ services: - PGPASSWORD=talkdai env_file: - .env + openwebui: + image: ghcr.io/open-webui/open-webui:main + ports: + - '3000:8080' + environment: + - OPENAI_API_KEYS=FAKE-KEY; + - OPENAI_API_BASE_URLS=http://dialog:8000/openai; + - ENABLE_OPENAI_API=true + volumes: + - open-webui:/app/backend/data + depends_on: + db: + condition: service_healthy + dialog: + condition: service_started + +volumes: + open-webui: \ No newline at end of file diff --git a/src/dialog/routers/openai.py b/src/dialog/routers/openai.py index cee66d4..727bdab 100644 --- a/src/dialog/routers/openai.py +++ b/src/dialog/routers/openai.py @@ -1,7 +1,8 @@ # *-* coding: utf-8 *-* -from uuid import uuid4 -import datetime +import os import logging +import datetime +from uuid import uuid4 from dialog.db import engine, get_session from dialog_lib.db.models import Chat as ChatEntity, ChatMessages @@ -44,11 +45,20 @@ async def ask_question_to_llm(message: OpenAIChat, session: Session = Depends(ge """ This posts a message to the LLM and returns the response in the OpenAI format. """ + start_time = datetime.datetime.now() - new_chat = ChatEntity( - session_id = f"openai-{str(uuid4())}", - ) - session.add(new_chat) + chat_entity = session.query(ChatEntity).filter(ChatEntity.session_id == Settings().OPENWEB_UI_SESSION).first() + + if not chat_entity: + logging.info("Creating new chat entity") + new_chat = ChatEntity( + session_id = Settings().OPENWEB_UI_SESSION, + ) + session.add(new_chat) + session.flush() + else: + logging.info("Using old chat entity") + new_chat = chat_entity non_empty_messages = [] diff --git a/src/dialog/settings.py b/src/dialog/settings.py index 43fe4c1..340ed45 100644 --- a/src/dialog/settings.py +++ b/src/dialog/settings.py @@ -1,5 +1,6 @@ import tomllib import logging +from uuid import uuid4 from pathlib import Path from decouple import Csv, Config @@ -7,6 +8,8 @@ logger = logging.getLogger(__name__) +openweb_ui_session_id_fallback = uuid4() + class Settings: @property @@ -113,4 +116,8 @@ def CORS_ALLOW_METHODS(self): @property def CORS_ALLOW_HEADERS(self): - return config.get("CORS_ALLOW_HEADERS", cast=Csv(), default="*") \ No newline at end of file + return config.get("CORS_ALLOW_HEADERS", cast=Csv(), default="*") + + @property + def OPENWEB_UI_SESSION(self): + return config.get("OPENWEB_UI_SESSION", f"dialog-openweb-ui") \ No newline at end of file From a6e8ebd585151db828f67575918ff114ff7586c0 Mon Sep 17 00:00:00 2001 From: Vinicius Mesel <4984147+vmesel@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:28:50 -0300 Subject: [PATCH 2/3] Adds docs on OPENWEB_UI_SESSION --- docs/settings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/settings.md b/docs/settings.md index 1c27e7e..b32c475 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -28,7 +28,7 @@ Here is a brief explanation of the environment variables: - `DIALOG_LOADCSV_CLEARDB`: if set to `true`, the script `load_csv.py` will delete all previously imported vectors and reimport everything again. - `COSINE_SIMILARITY_THRESHOLD`: the cosine similarity threshold used to filter the results from the database's similarity query. The default is `0.5`. - `PLUGINS`: the path to the plugins that will be loaded into the application comma-separated. An example is: `dialog-whatsapp,plugins.my-custom-plugin`. - + - `OPENWEB_UI_SESSION`: the session ID used to connect to the OpenWeb UI API. By default, it is `dialog-openweb-ui` and it can be changed. ## CSV format From 2240ada7ddf20b94b9c6d29b91322be75bcf1da8 Mon Sep 17 00:00:00 2001 From: Vinicius Mesel <4984147+vmesel@users.noreply.github.com> Date: Thu, 11 Jul 2024 13:39:01 -0300 Subject: [PATCH 3/3] Adds proper history handling for prompting --- docs/quick-start.md | 4 +++- sample_data/prompt.toml | 4 ++++ src/dialog/llm/agents/default.py | 32 ++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/docs/quick-start.md b/docs/quick-start.md index 29ba7cc..540542a 100644 --- a/docs/quick-start.md +++ b/docs/quick-start.md @@ -98,7 +98,7 @@ See [our documentation](https://dialog.talkd.ai/settings#csv-knowledge-base) for #### `.toml` prompt configuration -The `[prompt.header]`, `[prompt.suggested]`, and `[fallback.prompt]` fields are mandatory fields used for processing the conversation and connecting to the LLM. +The `[prompt.header]`, `[prompt.history_header]`, `[prompt.suggested]`, and `[fallback.prompt]` fields are mandatory fields used for processing the conversation and connecting to the LLM. The `[prompt.fallback]` field is used when the LLM does not find a compatible embedding in the database; that is, the `[prompt.header]` **is ignored** and the `[prompt.fallback]` is used. Without it, there could be hallucinations about possible answers to questions outside the scope of the embeddings. @@ -117,6 +117,8 @@ qualified service to high-end customers. Be brief in your answers, without being and objective in your responses. Never say that you are a model (AI), always answer as Avelino. Be polite and friendly!""" +history_header = """This is the history of the conversation with the user:""" + suggested = "Here is some possible content that could help the user in a better way." diff --git a/sample_data/prompt.toml b/sample_data/prompt.toml index 63b0a08..4a1a7ba 100644 --- a/sample_data/prompt.toml +++ b/sample_data/prompt.toml @@ -16,6 +16,10 @@ suggested = """ Here are some contents that you need to use in order to help you with the user's question: """ +history_header = """ +This is the history of the conversation: +""" + [fallback] prompt = """ Answer the following message to the user. diff --git a/src/dialog/llm/agents/default.py b/src/dialog/llm/agents/default.py index 5c859da..549d0d9 100644 --- a/src/dialog/llm/agents/default.py +++ b/src/dialog/llm/agents/default.py @@ -17,6 +17,7 @@ from dialog.settings import Settings from dialog.db import get_session + class DialogLLM(AbstractRAG): def __init__(self, *args, **kwargs): kwargs["dbsession"] = next(get_session()) @@ -47,24 +48,35 @@ def generate_prompt(self, text): self.config.get("fallback").get("prompt") # maintaining compatibility with the previous configuration header = prompt_config.get("header") suggested = prompt_config.get("suggested") + history_header = prompt_config.get("history_header") messages = [] + + messages.append(SystemMessagePromptTemplate.from_template(header)) if len(self.relevant_contents) > 0: context = "Context: \n".join( [f"{c.question}\n{c.content}\n" for c in self.relevant_contents] ) - messages.append(SystemMessagePromptTemplate.from_template(header)) - messages.append(SystemMessagePromptTemplate.from_template( - f"{suggested}. {context}")) messages.append( - MessagesPlaceholder( - variable_name="chat_history", optional=True)) - messages.append( - HumanMessagePromptTemplate.from_template("{user_message}")) + SystemMessagePromptTemplate.from_template(f"{suggested}. {context}") + ) else: messages.append( - SystemMessagePromptTemplate.from_template(fallback)) - messages.append( - HumanMessagePromptTemplate.from_template("{user_message}")) + SystemMessagePromptTemplate.from_template(fallback) + ) + + messages.append( + SystemMessagePromptTemplate.from_template(history_header) + ) + + messages.append( + MessagesPlaceholder( + variable_name="chat_history" + ) + ) + + messages.append( + HumanMessagePromptTemplate.from_template("{user_message}") + ) self.prompt = ChatPromptTemplate.from_messages(messages) if Settings().VERBOSE_LLM: