From 1d0aecb47369175b9b7fafb6a65be5e0db2e5ef2 Mon Sep 17 00:00:00 2001 From: John Aziz Date: Sun, 15 Sep 2024 05:22:22 +0300 Subject: [PATCH 1/4] add created_at and updated_at to the cosmos users collection This PR adds two new fields to the users collection the created_at field UTC time that gets created when a user starts a new coneversation, and the updated_at time UTC time for the last update that happened to the conversation --- src/quartapp/config_base.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/quartapp/config_base.py b/src/quartapp/config_base.py index fb04245..3891d5e 100644 --- a/src/quartapp/config_base.py +++ b/src/quartapp/config_base.py @@ -1,6 +1,7 @@ import json import os from abc import ABC, abstractmethod +from datetime import datetime, timezone from urllib.parse import quote_plus from langchain_core.documents import Document @@ -63,7 +64,12 @@ async def add_to_cosmos( raise IndexError old_messages.append(new_message) self.setup._database_setup._users_collection.insert_one( - {"_id": new_session_state, "messages": old_messages} + { + "_id": new_session_state, + "messages": old_messages, + "created_at": datetime.now(timezone.utc), # noqa: UP017 + "updated_at": datetime.now(timezone.utc), # noqa: UP017 + } ) return True except (AttributeError, ConfigurationError, InvalidName, InvalidOperation, OperationFailure, IndexError): @@ -73,10 +79,12 @@ async def add_to_cosmos( if len(old_messages) == 0 or len(new_message) == 0 or len(new_session_state) == 0: raise IndexError self.setup._database_setup._users_collection.update_one( - {"_id": new_session_state}, {"$push": {"messages": old_messages[-1]}} + {"_id": new_session_state}, + {"$push": {"messages": {"$each": [old_messages[-1], new_message]}}}, # noqa: UP017 ) self.setup._database_setup._users_collection.update_one( - {"_id": new_session_state}, {"$push": {"messages": new_message}} + {"_id": new_session_state}, + {"$set": {"updated_at": datetime.now(timezone.utc)}}, # noqa: UP017 ) return True except (AttributeError, ConfigurationError, InvalidName, InvalidOperation, OperationFailure, IndexError): From 13ddbeb33a957e74dacd34b649029850c5c32148 Mon Sep 17 00:00:00 2001 From: John Aziz Date: Sun, 15 Sep 2024 05:25:32 +0300 Subject: [PATCH 2/4] fix imports --- rag-azure-openai-cosmosdb-langchain-notebook.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rag-azure-openai-cosmosdb-langchain-notebook.ipynb b/rag-azure-openai-cosmosdb-langchain-notebook.ipynb index d255053..51562fe 100644 --- a/rag-azure-openai-cosmosdb-langchain-notebook.ipynb +++ b/rag-azure-openai-cosmosdb-langchain-notebook.ipynb @@ -110,9 +110,10 @@ } ], "source": [ - "from pymongo import MongoClient\n", "from urllib.parse import quote_plus\n", "\n", + "from pymongo import MongoClient\n", + "\n", "# Read and Store Environment variables\n", "mongo_connection_string = os.getenv(\"AZURE_COSMOS_CONNECTION_STRING\", \"\")\n", "mongo_username = quote_plus(os.getenv(\"AZURE_COSMOS_USERNAME\"))\n", @@ -433,7 +434,6 @@ "source": [ "from langchain.prompts import ChatPromptTemplate\n", "\n", - "\n", "REPHRASE_PROMPT = \"\"\"\\\n", "Given the following conversation and a follow up question, rephrase the follow up \\\n", "question to be a standalone question.\n", From 1441c5b8cd6046ca3f7fbc0ef79ea149468ca747 Mon Sep 17 00:00:00 2001 From: John Aziz Date: Sun, 15 Sep 2024 05:27:39 +0300 Subject: [PATCH 3/4] fix name of workflows --- .github/workflows/python-lint-format.yaml | 2 +- .github/workflows/python-static-analysis.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-lint-format.yaml b/.github/workflows/python-lint-format.yaml index f99f3ed..7dc0b52 100644 --- a/.github/workflows/python-lint-format.yaml +++ b/.github/workflows/python-lint-format.yaml @@ -17,7 +17,7 @@ permissions: contents: read jobs: - checks-format: + check-format-lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/python-static-analysis.yaml b/.github/workflows/python-static-analysis.yaml index de29063..55940c9 100644 --- a/.github/workflows/python-static-analysis.yaml +++ b/.github/workflows/python-static-analysis.yaml @@ -17,7 +17,7 @@ permissions: contents: read jobs: - checks-format: + check-static-analysis: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 From 866bba41519743f418f99b86766f4741aae4b413 Mon Sep 17 00:00:00 2001 From: John Aziz Date: Sun, 15 Sep 2024 05:29:51 +0300 Subject: [PATCH 4/4] fix types --- src/quartapp/approaches/setup.py | 2 +- src/quartapp/approaches/utils.py | 2 +- src/quartapp/config_base.py | 2 +- tests/conftest.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/quartapp/approaches/setup.py b/src/quartapp/approaches/setup.py index 5fc5978..4582fd7 100644 --- a/src/quartapp/approaches/setup.py +++ b/src/quartapp/approaches/setup.py @@ -2,7 +2,7 @@ from langchain_community.vectorstores import AzureCosmosDBVectorSearch from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings -from pydantic.v1 import SecretStr +from pydantic.types import SecretStr from pymongo.collection import Collection from quartapp.approaches.keyword import KeyWord diff --git a/src/quartapp/approaches/utils.py b/src/quartapp/approaches/utils.py index 8d2e226..032fd0c 100644 --- a/src/quartapp/approaches/utils.py +++ b/src/quartapp/approaches/utils.py @@ -1,6 +1,6 @@ from langchain_community.vectorstores import AzureCosmosDBVectorSearch from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings -from pydantic.v1 import SecretStr +from pydantic.types import SecretStr from pymongo import MongoClient from pymongo.collection import Collection from pymongo.errors import ServerSelectionTimeoutError diff --git a/src/quartapp/config_base.py b/src/quartapp/config_base.py index 3891d5e..47fdaf2 100644 --- a/src/quartapp/config_base.py +++ b/src/quartapp/config_base.py @@ -5,7 +5,7 @@ from urllib.parse import quote_plus from langchain_core.documents import Document -from pydantic.v1 import SecretStr +from pydantic.types import SecretStr from pymongo.errors import ( ConfigurationError, InvalidName, diff --git a/tests/conftest.py b/tests/conftest.py index 4dc8944..6b28082 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,7 @@ Choice, ) from openai.types.create_embedding_response import Usage -from pydantic.v1 import SecretStr +from pydantic.types import SecretStr import quartapp from quartapp.app import create_app