diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b899d9296..890c1e543 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -77,16 +77,16 @@ jobs: fi echo "MIN_COVERAGE=$MIN_COVERAGE" >> "$GITHUB_OUTPUT" - # - name: Run Python Tests - # run: make python-test optional_args="--junitxml=coverage-junit.xml --cov=. --cov-report xml:coverage.xml --cov-fail-under ${{ steps.coverage-value.outputs.MIN_COVERAGE }}" - # - uses: actions/upload-artifact@v4 - # if: ${{ !cancelled() }} - # with: - # name: coverage - # path: | - # coverage-junit.xml - # coverage.xml - # if-no-files-found: error + - name: Run Python Tests + run: make python-test optional_args="--junitxml=coverage-junit.xml --cov=. --cov-report xml:coverage.xml --cov-fail-under ${{ steps.coverage-value.outputs.MIN_COVERAGE }}" + - uses: actions/upload-artifact@v4 + if: ${{ !cancelled() }} + with: + name: coverage + path: | + coverage-junit.xml + coverage.xml + if-no-files-found: error - name: Setup node uses: actions/setup-node@v4 with: @@ -95,5 +95,5 @@ jobs: cache-dependency-path: "code/frontend/package-lock.json" - name: Run frontend unit tests run: make unittest-frontend - # - name: Lint - # run: make lint + - name: Lint + run: make lint diff --git a/code/backend/batch/utilities/chat_history/auth_utils.py b/code/backend/batch/utilities/chat_history/auth_utils.py index aab6191cc..8a5708b76 100644 --- a/code/backend/batch/utilities/chat_history/auth_utils.py +++ b/code/backend/batch/utilities/chat_history/auth_utils.py @@ -2,39 +2,42 @@ import json import logging + def get_authenticated_user_details(request_headers): user_object = {} - ## check the headers for the Principal-Id (the guid of the signed in user) + # check the headers for the Principal-Id (the guid of the signed in user) if "X-Ms-Client-Principal-Id" not in request_headers.keys(): - ## if it's not, assume we're in development mode and return a default user + # if it's not, assume we're in development mode and return a default user from . import sample_user + raw_user_object = sample_user.sample_user else: - ## if it is, get the user details from the EasyAuth headers - raw_user_object = {k:v for k,v in request_headers.items()} + # if it is, get the user details from the EasyAuth headers + raw_user_object = {k: v for k, v in request_headers.items()} - user_object['user_principal_id'] = raw_user_object.get('X-Ms-Client-Principal-Id') - user_object['user_name'] = raw_user_object.get('X-Ms-Client-Principal-Name') - user_object['auth_provider'] = raw_user_object.get('X-Ms-Client-Principal-Idp') - user_object['auth_token'] = raw_user_object.get('X-Ms-Token-Aad-Id-Token') - user_object['client_principal_b64'] = raw_user_object.get('X-Ms-Client-Principal') - user_object['aad_id_token'] = raw_user_object.get('X-Ms-Token-Aad-Id-Token') + user_object["user_principal_id"] = raw_user_object.get("X-Ms-Client-Principal-Id") + user_object["user_name"] = raw_user_object.get("X-Ms-Client-Principal-Name") + user_object["auth_provider"] = raw_user_object.get("X-Ms-Client-Principal-Idp") + user_object["auth_token"] = raw_user_object.get("X-Ms-Token-Aad-Id-Token") + user_object["client_principal_b64"] = raw_user_object.get("X-Ms-Client-Principal") + user_object["aad_id_token"] = raw_user_object.get("X-Ms-Token-Aad-Id-Token") return user_object + def get_tenantid(client_principal_b64): logger = logging.getLogger(__name__) - tenant_id = '' + tenant_id = "" if client_principal_b64: try: # Decode the base64 header to get the JSON string decoded_bytes = base64.b64decode(client_principal_b64) - decoded_string = decoded_bytes.decode('utf-8') + decoded_string = decoded_bytes.decode("utf-8") # Convert the JSON string1into a Python dictionary user_info = json.loads(decoded_string) # Extract the tenant ID - tenant_id = user_info.get('tid') # 'tid' typically holds the tenant ID + tenant_id = user_info.get("tid") # 'tid' typically holds the tenant ID except Exception as ex: - logger.exception(ex) + logger.exception(ex) return tenant_id diff --git a/code/backend/batch/utilities/chat_history/cosmosdb.py b/code/backend/batch/utilities/chat_history/cosmosdb.py index 213125d61..7c3bb70c8 100644 --- a/code/backend/batch/utilities/chat_history/cosmosdb.py +++ b/code/backend/batch/utilities/chat_history/cosmosdb.py @@ -1,18 +1,27 @@ -import uuid from datetime import datetime from azure.cosmos.aio import CosmosClient from azure.cosmos import exceptions -class CosmosConversationClient(): - def __init__(self, cosmosdb_endpoint: str, credential: any, database_name: str, container_name: str, enable_message_feedback: bool = False): +class CosmosConversationClient: + + def __init__( + self, + cosmosdb_endpoint: str, + credential: any, + database_name: str, + container_name: str, + enable_message_feedback: bool = False, + ): self.cosmosdb_endpoint = cosmosdb_endpoint self.credential = credential self.database_name = database_name self.container_name = container_name self.enable_message_feedback = enable_message_feedback try: - self.cosmosdb_client = CosmosClient(self.cosmosdb_endpoint, credential=credential) + self.cosmosdb_client = CosmosClient( + self.cosmosdb_endpoint, credential=credential + ) except exceptions.CosmosHttpResponseError as e: if e.status_code == 401: raise ValueError("Invalid credentials") from e @@ -20,27 +29,37 @@ def __init__(self, cosmosdb_endpoint: str, credential: any, database_name: str, raise ValueError("Invalid CosmosDB endpoint") from e try: - self.database_client = self.cosmosdb_client.get_database_client(database_name) + self.database_client = self.cosmosdb_client.get_database_client( + database_name + ) except exceptions.CosmosResourceNotFoundError: raise ValueError("Invalid CosmosDB database name") try: - self.container_client = self.database_client.get_container_client(container_name) + self.container_client = self.database_client.get_container_client( + container_name + ) except exceptions.CosmosResourceNotFoundError: raise ValueError("Invalid CosmosDB container name") - async def ensure(self): - if not self.cosmosdb_client or not self.database_client or not self.container_client: + if ( + not self.cosmosdb_client + or not self.database_client + or not self.container_client + ): return False, "CosmosDB client not initialized correctly" try: - database_info = await self.database_client.read() - except: - return False, f"CosmosDB database {self.database_name} on account {self.cosmosdb_endpoint} not found" + await self.database_client.read() + except Exception: + return ( + False, + f"CosmosDB database {self.database_name} on account {self.cosmosdb_endpoint} not found", + ) try: - container_info = await self.container_client.read() - except: + await self.container_client.read() + except Exception: return False, f"CosmosDB container {self.container_name} not found" return True, "CosmosDB client initialized successfully" @@ -55,7 +74,7 @@ async def create_conversation(self, user_id, conversation_id, title=""): "title": title, "conversationId": conversation_id, } - ## TODO: add some error handling based on the output of the upsert_item call + # TODO: add some error handling based on the output of the upsert_item call resp = await self.container_client.upsert_item(conversation) if resp: return resp @@ -70,59 +89,56 @@ async def upsert_conversation(self, conversation): return False async def delete_conversation(self, user_id, conversation_id): - conversation = await self.container_client.read_item(item=conversation_id, partition_key=user_id) + conversation = await self.container_client.read_item( + item=conversation_id, partition_key=user_id + ) if conversation: - resp = await self.container_client.delete_item(item=conversation_id, partition_key=user_id) + resp = await self.container_client.delete_item( + item=conversation_id, partition_key=user_id + ) return resp else: return True - async def delete_messages(self, conversation_id, user_id): - ## get a list of all the messages in the conversation + # get a list of all the messages in the conversation messages = await self.get_messages(user_id, conversation_id) response_list = [] if messages: for message in messages: - resp = await self.container_client.delete_item(item=message['id'], partition_key=user_id) + resp = await self.container_client.delete_item( + item=message["id"], partition_key=user_id + ) response_list.append(resp) return response_list - - async def get_conversations(self, user_id, limit, sort_order = 'DESC', offset = 0): - parameters = [ - { - 'name': '@userId', - 'value': user_id - } - ] + async def get_conversations(self, user_id, limit, sort_order="DESC", offset=0): + parameters = [{"name": "@userId", "value": user_id}] query = f"SELECT * FROM c where c.userId = @userId and c.type='conversation' order by c.updatedAt {sort_order}" if limit is not None: query += f" offset {offset} limit {limit}" conversations = [] - async for item in self.container_client.query_items(query=query, parameters=parameters): + async for item in self.container_client.query_items( + query=query, parameters=parameters + ): conversations.append(item) return conversations async def get_conversation(self, user_id, conversation_id): parameters = [ - { - 'name': '@conversationId', - 'value': conversation_id - }, - { - 'name': '@userId', - 'value': user_id - } + {"name": "@conversationId", "value": conversation_id}, + {"name": "@userId", "value": user_id}, ] - query = f"SELECT * FROM c where c.id = @conversationId and c.type='conversation' and c.userId = @userId" + query = "SELECT * FROM c where c.id = @conversationId and c.type='conversation' and c.userId = @userId" conversations = [] - async for item in self.container_client.query_items(query=query, parameters=parameters): + async for item in self.container_client.query_items( + query=query, parameters=parameters + ): conversations.append(item) - ## if no conversations are found, return None + # if no conversations are found, return None if len(conversations) == 0: return None else: @@ -130,35 +146,37 @@ async def get_conversation(self, user_id, conversation_id): async def create_message(self, uuid, conversation_id, user_id, input_message: dict): message = { - 'id': uuid, - 'type': 'message', - 'userId' : user_id, - 'createdAt': datetime.utcnow().isoformat(), - 'updatedAt': datetime.utcnow().isoformat(), - 'conversationId' : conversation_id, - 'role': input_message['role'], - 'content': input_message['content'] + "id": uuid, + "type": "message", + "userId": user_id, + "createdAt": datetime.utcnow().isoformat(), + "updatedAt": datetime.utcnow().isoformat(), + "conversationId": conversation_id, + "role": input_message["role"], + "content": input_message["content"], } if self.enable_message_feedback: - message['feedback'] = '' + message["feedback"] = "" resp = await self.container_client.upsert_item(message) if resp: - ## update the parent conversations's updatedAt field with the current message's createdAt datetime value + # update the parent conversations's updatedAt field with the current message's createdAt datetime value conversation = await self.get_conversation(user_id, conversation_id) if not conversation: return "Conversation not found" - conversation['updatedAt'] = message['createdAt'] + conversation["updatedAt"] = message["createdAt"] await self.upsert_conversation(conversation) return resp else: return False async def update_message_feedback(self, user_id, message_id, feedback): - message = await self.container_client.read_item(item=message_id, partition_key=user_id) + message = await self.container_client.read_item( + item=message_id, partition_key=user_id + ) if message: - message['feedback'] = feedback + message["feedback"] = feedback resp = await self.container_client.upsert_item(message) return resp else: @@ -166,18 +184,14 @@ async def update_message_feedback(self, user_id, message_id, feedback): async def get_messages(self, user_id, conversation_id): parameters = [ - { - 'name': '@conversationId', - 'value': conversation_id - }, - { - 'name': '@userId', - 'value': user_id - } + {"name": "@conversationId", "value": conversation_id}, + {"name": "@userId", "value": user_id}, ] - query = f"SELECT * FROM c WHERE c.conversationId = @conversationId AND c.type='message' AND c.userId = @userId ORDER BY c.timestamp ASC" + query = "SELECT * FROM c WHERE c.conversationId = @conversationId AND c.type='message' AND c.userId = @userId ORDER BY c.timestamp ASC" messages = [] - async for item in self.container_client.query_items(query=query, parameters=parameters): + async for item in self.container_client.query_items( + query=query, parameters=parameters + ): messages.append(item) return messages diff --git a/code/backend/batch/utilities/chat_history/sample_user.py b/code/backend/batch/utilities/chat_history/sample_user.py index 0b10d9ab5..9353bcc1b 100644 --- a/code/backend/batch/utilities/chat_history/sample_user.py +++ b/code/backend/batch/utilities/chat_history/sample_user.py @@ -1,39 +1,39 @@ sample_user = { - "Accept": "*/*", - "Accept-Encoding": "gzip, deflate, br", - "Accept-Language": "en", - "Client-Ip": "22.222.222.2222:64379", - "Content-Length": "192", - "Content-Type": "application/json", - "Cookie": "AppServiceAuthSession=/AuR5ENU+pmpoN3jnymP8fzpmVBgphx9uPQrYLEWGcxjIITIeh8NZW7r3ePkG8yBcMaItlh1pX4nzg5TFD9o2mxC/5BNDRe/uuu0iDlLEdKecROZcVRY7QsFdHLjn9KB90Z3d9ZeLwfVIf0sZowWJt03BO5zKGB7vZgL+ofv3QY3AaYn1k1GtxSE9HQWJpWar7mOA64b7Lsy62eY3nxwg3AWDsP3/rAta+MnDCzpdlZMFXcJLj+rsCppW+w9OqGhKQ7uCs03BPeon3qZOdmE8cOJW3+i96iYlhneNQDItHyQqEi1CHbBTSkqwpeOwWP4vcwGM22ynxPp7YFyiRw/X361DGYy+YkgYBkXq1AEIDZ44BCBz9EEaEi0NU+m6yUOpNjEaUtrJKhQywcM2odojdT4XAY+HfTEfSqp0WiAkgAuE/ueCu2JDOfvxGjCgJ4DGWCoYdOdXAN1c+MenT4OSvkMO41YuPeah9qk9ixkJI5s80lv8rUu1J26QF6pstdDkYkAJAEra3RQiiO1eAH7UEb3xHXn0HW5lX8ZDX3LWiAFGOt5DIKxBKFymBKJGzbPFPYjfczegu0FD8/NQPLl2exAX3mI9oy/tFnATSyLO2E8DxwP5wnYVminZOQMjB/I4g3Go14betm0MlNXlUbU1fyS6Q6JxoCNLDZywCoU9Y65UzimWZbseKsXlOwYukCEpuQ5QPT55LuEAWhtYier8LSh+fvVUsrkqKS+bg0hzuoX53X6aqUr7YB31t0Z2zt5TT/V3qXpdyD8Xyd884PqysSkJYa553sYx93ETDKSsfDguanVfn2si9nvDpvUWf6/R02FmQgXiaaaykMgYyIuEmE77ptsivjH3hj/MN4VlePFWokcchF4ciqqzonmICmjEHEx5zpjU2Kwa+0y7J5ROzVVygcnO1jH6ZKDy9bGGYL547bXx/iiYBYqSIQzleOAkCeULrGN2KEHwckX5MpuRaqTpoxdZH9RJv0mIWxbDA0kwGsbMICQd0ZODBkPUnE84qhzvXInC+TL7MbutPEnGbzgxBAS1c2Ct4vxkkjykOeOxTPxqAhxoefwUfIwZZax6A9LbeYX2bsBpay0lScHcA==", - "Disguised-Host": "your_app_service.azurewebsites.net", - "Host": "your_app_service.azurewebsites.net", - "Max-Forwards": "10", - "Origin": "https://your_app_service.azurewebsites.net", - "Referer": "https://your_app_service.azurewebsites.net/", - "Sec-Ch-Ua": "\"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"", - "Sec-Ch-Ua-Mobile": "?0", - "Sec-Ch-Ua-Platform": "\"Windows\"", - "Sec-Fetch-Dest": "empty", - "Sec-Fetch-Mode": "cors", - "Sec-Fetch-Site": "same-origin", - "Traceparent": "00-24e9a8d1b06f233a3f1714845ef971a9-3fac69f81ca5175c-00", - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42", - "Was-Default-Hostname": "your_app_service.azurewebsites.net", - "X-Appservice-Proto": "https", - "X-Arr-Log-Id": "4102b832-6c88-4c7c-8996-0edad9e4358f", - "X-Arr-Ssl": "2048|256|CN=Microsoft Azure TLS Issuing CA 02, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US", - "X-Client-Ip": "22.222.222.222", - "X-Client-Port": "64379", - "X-Forwarded-For": "22.222.222.22:64379", - "X-Forwarded-Proto": "https", - "X-Forwarded-Tlsversion": "1.2", - "X-Ms-Client-Principal": "your_base_64_encoded_token", - "X-Ms-Client-Principal-Id": "00000000-0000-0000-0000-000000000000", - "X-Ms-Client-Principal-Idp": "aad", - "X-Ms-Client-Principal-Name": "testusername@constoso.com", - "X-Ms-Token-Aad-Id-Token": "your_aad_id_token", - "X-Original-Url": "/chatgpt", - "X-Site-Deployment-Id": "your_app_service", - "X-Waws-Unencoded-Url": "/chatgpt" + "Accept": "*/*", + "Accept-Encoding": "gzip, deflate, br", + "Accept-Language": "en", + "Client-Ip": "22.222.222.2222:64379", + "Content-Length": "192", + "Content-Type": "application/json", + "Cookie": "AppServiceAuthSession=/AuR5ENU+pmpoN3jnymP8fzpmVBgphx9uPQrYLEWGcxjIITIeh8NZW7r3ePkG8yBcMaItlh1pX4nzg5TFD9o2mxC/5BNDRe/uuu0iDlLEdKecROZcVRY7QsFdHLjn9KB90Z3d9ZeLwfVIf0sZowWJt03BO5zKGB7vZgL+ofv3QY3AaYn1k1GtxSE9HQWJpWar7mOA64b7Lsy62eY3nxwg3AWDsP3/rAta+MnDCzpdlZMFXcJLj+rsCppW+w9OqGhKQ7uCs03BPeon3qZOdmE8cOJW3+i96iYlhneNQDItHyQqEi1CHbBTSkqwpeOwWP4vcwGM22ynxPp7YFyiRw/X361DGYy+YkgYBkXq1AEIDZ44BCBz9EEaEi0NU+m6yUOpNjEaUtrJKhQywcM2odojdT4XAY+HfTEfSqp0WiAkgAuE/ueCu2JDOfvxGjCgJ4DGWCoYdOdXAN1c+MenT4OSvkMO41YuPeah9qk9ixkJI5s80lv8rUu1J26QF6pstdDkYkAJAEra3RQiiO1eAH7UEb3xHXn0HW5lX8ZDX3LWiAFGOt5DIKxBKFymBKJGzbPFPYjfczegu0FD8/NQPLl2exAX3mI9oy/tFnATSyLO2E8DxwP5wnYVminZOQMjB/I4g3Go14betm0MlNXlUbU1fyS6Q6JxoCNLDZywCoU9Y65UzimWZbseKsXlOwYukCEpuQ5QPT55LuEAWhtYier8LSh+fvVUsrkqKS+bg0hzuoX53X6aqUr7YB31t0Z2zt5TT/V3qXpdyD8Xyd884PqysSkJYa553sYx93ETDKSsfDguanVfn2si9nvDpvUWf6/R02FmQgXiaaaykMgYyIuEmE77ptsivjH3hj/MN4VlePFWokcchF4ciqqzonmICmjEHEx5zpjU2Kwa+0y7J5ROzVVygcnO1jH6ZKDy9bGGYL547bXx/iiYBYqSIQzleOAkCeULrGN2KEHwckX5MpuRaqTpoxdZH9RJv0mIWxbDA0kwGsbMICQd0ZODBkPUnE84qhzvXInC+TL7MbutPEnGbzgxBAS1c2Ct4vxkkjykOeOxTPxqAhxoefwUfIwZZax6A9LbeYX2bsBpay0lScHcA==", + "Disguised-Host": "your_app_service.azurewebsites.net", + "Host": "your_app_service.azurewebsites.net", + "Max-Forwards": "10", + "Origin": "https://your_app_service.azurewebsites.net", + "Referer": "https://your_app_service.azurewebsites.net/", + "Sec-Ch-Ua": '"Microsoft Edge";v="113", "Chromium";v="113", "Not-A.Brand";v="24"', + "Sec-Ch-Ua-Mobile": "?0", + "Sec-Ch-Ua-Platform": '"Windows"', + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "same-origin", + "Traceparent": "00-24e9a8d1b06f233a3f1714845ef971a9-3fac69f81ca5175c-00", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42", + "Was-Default-Hostname": "your_app_service.azurewebsites.net", + "X-Appservice-Proto": "https", + "X-Arr-Log-Id": "4102b832-6c88-4c7c-8996-0edad9e4358f", + "X-Arr-Ssl": "2048|256|CN=Microsoft Azure TLS Issuing CA 02, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US", + "X-Client-Ip": "22.222.222.222", + "X-Client-Port": "64379", + "X-Forwarded-For": "22.222.222.22:64379", + "X-Forwarded-Proto": "https", + "X-Forwarded-Tlsversion": "1.2", + "X-Ms-Client-Principal": "your_base_64_encoded_token", + "X-Ms-Client-Principal-Id": "00000000-0000-0000-0000-000000000000", + "X-Ms-Client-Principal-Idp": "aad", + "X-Ms-Client-Principal-Name": "testusername@constoso.com", + "X-Ms-Token-Aad-Id-Token": "your_aad_id_token", + "X-Original-Url": "/chatgpt", + "X-Site-Deployment-Id": "your_app_service", + "X-Waws-Unencoded-Url": "/chatgpt", } diff --git a/code/backend/batch/utilities/helpers/azure_search_helper.py b/code/backend/batch/utilities/helpers/azure_search_helper.py index 0c438a3bb..e6a7e94dd 100644 --- a/code/backend/batch/utilities/helpers/azure_search_helper.py +++ b/code/backend/batch/utilities/helpers/azure_search_helper.py @@ -155,7 +155,11 @@ def create_index(self): name=self.env_helper.AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG, prioritized_fields=SemanticPrioritizedFields( title_field=None, - content_fields=[SemanticField(field_name=self.env_helper.AZURE_SEARCH_CONTENT_COLUMN)], + content_fields=[ + SemanticField( + field_name=self.env_helper.AZURE_SEARCH_CONTENT_COLUMN + ) + ], ), ) ] diff --git a/code/tests/functional/tests/backend_api/default/test_advanced_image_processing.py b/code/tests/functional/tests/backend_api/default/test_advanced_image_processing.py index 9f5e1dde7..375fff101 100644 --- a/code/tests/functional/tests/backend_api/default/test_advanced_image_processing.py +++ b/code/tests/functional/tests/backend_api/default/test_advanced_image_processing.py @@ -245,7 +245,7 @@ def test_image_urls_included_in_call_to_openai( "role": "system", }, { - "content": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nuser question', + "content": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nUse the Retrieved Documents to answer the question: user question', "name": "example_user", "role": "system", }, @@ -264,7 +264,7 @@ def test_image_urls_included_in_call_to_openai( "content": [ { "type": "text", - "text": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nWhat is the meaning of life?', + "text": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nUse the Retrieved Documents to answer the question: What is the meaning of life?', }, {"type": "image_url", "image_url": {"url": ANY}}, ], diff --git a/code/tests/functional/tests/backend_api/default/test_conversation.py b/code/tests/functional/tests/backend_api/default/test_conversation.py index bdae6a389..8d7106f8c 100644 --- a/code/tests/functional/tests/backend_api/default/test_conversation.py +++ b/code/tests/functional/tests/backend_api/default/test_conversation.py @@ -566,7 +566,7 @@ def test_post_makes_correct_call_to_openai_chat_completions_with_documents( "role": "system", }, { - "content": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nuser question', + "content": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nUse the Retrieved Documents to answer the question: user question', "name": "example_user", "role": "system", }, @@ -585,7 +585,7 @@ def test_post_makes_correct_call_to_openai_chat_completions_with_documents( "content": [ { "type": "text", - "text": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nWhat is the meaning of life?', + "text": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nUse the Retrieved Documents to answer the question: What is the meaning of life?', } ], "role": "user", diff --git a/code/tests/functional/tests/backend_api/default/test_speech_token.py b/code/tests/functional/tests/backend_api/default/test_speech_token.py index 401f92191..ba02365b2 100644 --- a/code/tests/functional/tests/backend_api/default/test_speech_token.py +++ b/code/tests/functional/tests/backend_api/default/test_speech_token.py @@ -20,7 +20,7 @@ def test_speech_token_returned(app_url: str, app_config: AppConfig): "token": "speech-token", "region": app_config.get("AZURE_SPEECH_SERVICE_REGION"), "languages": app_config.get("AZURE_SPEECH_RECOGNIZER_LANGUAGES").split(","), - "key": "some-azure-speech-service-key" + "key": "some-azure-speech-service-key", } assert response.headers["Content-Type"] == "application/json" diff --git a/code/tests/functional/tests/backend_api/integrated_vectorization_custom_conversation/test_iv_question_answer_tool.py b/code/tests/functional/tests/backend_api/integrated_vectorization_custom_conversation/test_iv_question_answer_tool.py index a3a2034fb..9d1eb152b 100644 --- a/code/tests/functional/tests/backend_api/integrated_vectorization_custom_conversation/test_iv_question_answer_tool.py +++ b/code/tests/functional/tests/backend_api/integrated_vectorization_custom_conversation/test_iv_question_answer_tool.py @@ -212,7 +212,7 @@ def test_post_makes_correct_call_to_openai_chat_completions_in_question_answer_t "role": "system", }, { - "content": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"Dual Transformer Encoder (DTE) DTE (https://dev.azure.com/TScience/TSciencePublic/_wiki/wikis/TSciencePublic.wiki/82/Dual-Transformer-Encoder) DTE is a general pair-oriented sentence representation learning framework based on transformers. It provides training, inference and evaluation for sentence similarity models. Model Details DTE can be used to train a model for sentence similarity with the following features: - Build upon existing transformer-based text representations (e.g.TNLR, BERT, RoBERTa, BAG-NLR) - Apply smoothness inducing technology to improve the representation robustness - SMART (https://arxiv.org/abs/1911.03437) SMART - Apply NCE (Noise Contrastive Estimation) based similarity learning to speed up training of 100M pairs We use pretrained DTE model"}},{"[doc2]":{"content":"trained on internal data. You can find more details here - Models.md (https://dev.azure.com/TScience/_git/TSciencePublic?path=%2FDualTransformerEncoder%2FMODELS.md&version=GBmaster&_a=preview) Models.md DTE-pretrained for In-context Learning Research suggests that finetuned transformers can be used to retrieve semantically similar exemplars for e.g. KATE (https://arxiv.org/pdf/2101.06804.pdf) KATE . They show that finetuned models esp. tuned on related tasks give the maximum boost to GPT-3 in-context performance. DTE have lot of pretrained models that are trained on intent classification tasks. We can use these model embedding to find natural language utterances which are similar to our test utterances at test time. The steps are: 1. Embed"}},{"[doc3]":{"content":"train and test utterances using DTE model 2. For each test embedding, find K-nearest neighbors. 3. Prefix the prompt with nearest embeddings. The following diagram from the above paper (https://arxiv.org/pdf/2101.06804.pdf) the above paper visualizes this process: DTE-Finetuned This is an extension of DTE-pretrained method where we further finetune the embedding models for prompt crafting task. In summary, we sample random prompts from our training data and use them for GPT-3 inference for the another part of training data. Some prompts work better and lead to right results whereas other prompts lead"}},{"[doc4]":{"content":"to wrong completions. We finetune the model on the downstream task of whether a prompt is good or not based on whether it leads to right or wrong completion. This approach is similar to this paper: Learning To Retrieve Prompts for In-Context Learning (https://arxiv.org/pdf/2112.08633.pdf) this paper: Learning To Retrieve Prompts for In-Context Learning . This method is very general but it may require a lot of data to actually finetune a model to learn how to retrieve examples suitable for the downstream inference model like GPT-3."}}]}\n\n## User Question\nWhat features does the Dual Transformer Encoder (DTE) provide for sentence similarity models and in-context learning?', + "content": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"Dual Transformer Encoder (DTE) DTE (https://dev.azure.com/TScience/TSciencePublic/_wiki/wikis/TSciencePublic.wiki/82/Dual-Transformer-Encoder) DTE is a general pair-oriented sentence representation learning framework based on transformers. It provides training, inference and evaluation for sentence similarity models. Model Details DTE can be used to train a model for sentence similarity with the following features: - Build upon existing transformer-based text representations (e.g.TNLR, BERT, RoBERTa, BAG-NLR) - Apply smoothness inducing technology to improve the representation robustness - SMART (https://arxiv.org/abs/1911.03437) SMART - Apply NCE (Noise Contrastive Estimation) based similarity learning to speed up training of 100M pairs We use pretrained DTE model"}},{"[doc2]":{"content":"trained on internal data. You can find more details here - Models.md (https://dev.azure.com/TScience/_git/TSciencePublic?path=%2FDualTransformerEncoder%2FMODELS.md&version=GBmaster&_a=preview) Models.md DTE-pretrained for In-context Learning Research suggests that finetuned transformers can be used to retrieve semantically similar exemplars for e.g. KATE (https://arxiv.org/pdf/2101.06804.pdf) KATE . They show that finetuned models esp. tuned on related tasks give the maximum boost to GPT-3 in-context performance. DTE have lot of pretrained models that are trained on intent classification tasks. We can use these model embedding to find natural language utterances which are similar to our test utterances at test time. The steps are: 1. Embed"}},{"[doc3]":{"content":"train and test utterances using DTE model 2. For each test embedding, find K-nearest neighbors. 3. Prefix the prompt with nearest embeddings. The following diagram from the above paper (https://arxiv.org/pdf/2101.06804.pdf) the above paper visualizes this process: DTE-Finetuned This is an extension of DTE-pretrained method where we further finetune the embedding models for prompt crafting task. In summary, we sample random prompts from our training data and use them for GPT-3 inference for the another part of training data. Some prompts work better and lead to right results whereas other prompts lead"}},{"[doc4]":{"content":"to wrong completions. We finetune the model on the downstream task of whether a prompt is good or not based on whether it leads to right or wrong completion. This approach is similar to this paper: Learning To Retrieve Prompts for In-Context Learning (https://arxiv.org/pdf/2112.08633.pdf) this paper: Learning To Retrieve Prompts for In-Context Learning . This method is very general but it may require a lot of data to actually finetune a model to learn how to retrieve examples suitable for the downstream inference model like GPT-3."}}]}\n\n## User Question\nUse the Retrieved Documents to answer the question: What features does the Dual Transformer Encoder (DTE) provide for sentence similarity models and in-context learning?', "name": "example_user", "role": "system", }, @@ -231,7 +231,7 @@ def test_post_makes_correct_call_to_openai_chat_completions_in_question_answer_t "content": [ { "type": "text", - "text": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nWhat is the meaning of life?', + "text": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nUse the Retrieved Documents to answer the question: What is the meaning of life?', } ], "role": "user", diff --git a/code/tests/functional/tests/backend_api/sk_orchestrator/test_response_with_search_documents_tool.py b/code/tests/functional/tests/backend_api/sk_orchestrator/test_response_with_search_documents_tool.py index c9556097e..43d7b7f9f 100644 --- a/code/tests/functional/tests/backend_api/sk_orchestrator/test_response_with_search_documents_tool.py +++ b/code/tests/functional/tests/backend_api/sk_orchestrator/test_response_with_search_documents_tool.py @@ -227,7 +227,7 @@ def test_post_makes_correct_call_to_openai_chat_completions_in_question_answer_t "role": "system", }, { - "content": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"Dual Transformer Encoder (DTE) DTE (https://dev.azure.com/TScience/TSciencePublic/_wiki/wikis/TSciencePublic.wiki/82/Dual-Transformer-Encoder) DTE is a general pair-oriented sentence representation learning framework based on transformers. It provides training, inference and evaluation for sentence similarity models. Model Details DTE can be used to train a model for sentence similarity with the following features: - Build upon existing transformer-based text representations (e.g.TNLR, BERT, RoBERTa, BAG-NLR) - Apply smoothness inducing technology to improve the representation robustness - SMART (https://arxiv.org/abs/1911.03437) SMART - Apply NCE (Noise Contrastive Estimation) based similarity learning to speed up training of 100M pairs We use pretrained DTE model"}},{"[doc2]":{"content":"trained on internal data. You can find more details here - Models.md (https://dev.azure.com/TScience/_git/TSciencePublic?path=%2FDualTransformerEncoder%2FMODELS.md&version=GBmaster&_a=preview) Models.md DTE-pretrained for In-context Learning Research suggests that finetuned transformers can be used to retrieve semantically similar exemplars for e.g. KATE (https://arxiv.org/pdf/2101.06804.pdf) KATE . They show that finetuned models esp. tuned on related tasks give the maximum boost to GPT-3 in-context performance. DTE have lot of pretrained models that are trained on intent classification tasks. We can use these model embedding to find natural language utterances which are similar to our test utterances at test time. The steps are: 1. Embed"}},{"[doc3]":{"content":"train and test utterances using DTE model 2. For each test embedding, find K-nearest neighbors. 3. Prefix the prompt with nearest embeddings. The following diagram from the above paper (https://arxiv.org/pdf/2101.06804.pdf) the above paper visualizes this process: DTE-Finetuned This is an extension of DTE-pretrained method where we further finetune the embedding models for prompt crafting task. In summary, we sample random prompts from our training data and use them for GPT-3 inference for the another part of training data. Some prompts work better and lead to right results whereas other prompts lead"}},{"[doc4]":{"content":"to wrong completions. We finetune the model on the downstream task of whether a prompt is good or not based on whether it leads to right or wrong completion. This approach is similar to this paper: Learning To Retrieve Prompts for In-Context Learning (https://arxiv.org/pdf/2112.08633.pdf) this paper: Learning To Retrieve Prompts for In-Context Learning . This method is very general but it may require a lot of data to actually finetune a model to learn how to retrieve examples suitable for the downstream inference model like GPT-3."}}]}\n\n## User Question\nWhat features does the Dual Transformer Encoder (DTE) provide for sentence similarity models and in-context learning?', + "content": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"Dual Transformer Encoder (DTE) DTE (https://dev.azure.com/TScience/TSciencePublic/_wiki/wikis/TSciencePublic.wiki/82/Dual-Transformer-Encoder) DTE is a general pair-oriented sentence representation learning framework based on transformers. It provides training, inference and evaluation for sentence similarity models. Model Details DTE can be used to train a model for sentence similarity with the following features: - Build upon existing transformer-based text representations (e.g.TNLR, BERT, RoBERTa, BAG-NLR) - Apply smoothness inducing technology to improve the representation robustness - SMART (https://arxiv.org/abs/1911.03437) SMART - Apply NCE (Noise Contrastive Estimation) based similarity learning to speed up training of 100M pairs We use pretrained DTE model"}},{"[doc2]":{"content":"trained on internal data. You can find more details here - Models.md (https://dev.azure.com/TScience/_git/TSciencePublic?path=%2FDualTransformerEncoder%2FMODELS.md&version=GBmaster&_a=preview) Models.md DTE-pretrained for In-context Learning Research suggests that finetuned transformers can be used to retrieve semantically similar exemplars for e.g. KATE (https://arxiv.org/pdf/2101.06804.pdf) KATE . They show that finetuned models esp. tuned on related tasks give the maximum boost to GPT-3 in-context performance. DTE have lot of pretrained models that are trained on intent classification tasks. We can use these model embedding to find natural language utterances which are similar to our test utterances at test time. The steps are: 1. Embed"}},{"[doc3]":{"content":"train and test utterances using DTE model 2. For each test embedding, find K-nearest neighbors. 3. Prefix the prompt with nearest embeddings. The following diagram from the above paper (https://arxiv.org/pdf/2101.06804.pdf) the above paper visualizes this process: DTE-Finetuned This is an extension of DTE-pretrained method where we further finetune the embedding models for prompt crafting task. In summary, we sample random prompts from our training data and use them for GPT-3 inference for the another part of training data. Some prompts work better and lead to right results whereas other prompts lead"}},{"[doc4]":{"content":"to wrong completions. We finetune the model on the downstream task of whether a prompt is good or not based on whether it leads to right or wrong completion. This approach is similar to this paper: Learning To Retrieve Prompts for In-Context Learning (https://arxiv.org/pdf/2112.08633.pdf) this paper: Learning To Retrieve Prompts for In-Context Learning . This method is very general but it may require a lot of data to actually finetune a model to learn how to retrieve examples suitable for the downstream inference model like GPT-3."}}]}\n\n## User Question\nUse the Retrieved Documents to answer the question: What features does the Dual Transformer Encoder (DTE) provide for sentence similarity models and in-context learning?', "name": "example_user", "role": "system", }, @@ -246,7 +246,7 @@ def test_post_makes_correct_call_to_openai_chat_completions_in_question_answer_t "content": [ { "type": "text", - "text": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nWhat is the meaning of life?', + "text": '## Retrieved Documents\n{"retrieved_documents":[{"[doc1]":{"content":"content"}}]}\n\n## User Question\nUse the Retrieved Documents to answer the question: What is the meaning of life?', } ], "role": "user", diff --git a/code/tests/utilities/helpers/test_azure_search_helper.py b/code/tests/utilities/helpers/test_azure_search_helper.py index 922f3e2e4..4d246c021 100644 --- a/code/tests/utilities/helpers/test_azure_search_helper.py +++ b/code/tests/utilities/helpers/test_azure_search_helper.py @@ -72,7 +72,9 @@ def env_helper_mock(): env_helper.AZURE_SEARCH_USE_SEMANTIC_SEARCH = AZURE_SEARCH_USE_SEMANTIC_SEARCH env_helper.AZURE_SEARCH_FIELDS_ID = AZURE_SEARCH_FIELDS_ID env_helper.AZURE_SEARCH_CONTENT_COLUMN = AZURE_SEARCH_CONTENT_COLUMN - env_helper.AZURE_SEARCH_CONTENT_VECTOR_COLUMN = AZURE_SEARCH_CONTENT_VECTOR_COLUMN + env_helper.AZURE_SEARCH_CONTENT_VECTOR_COLUMN = ( + AZURE_SEARCH_CONTENT_VECTOR_COLUMN + ) env_helper.AZURE_SEARCH_TITLE_COLUMN = AZURE_SEARCH_TITLE_COLUMN env_helper.AZURE_SEARCH_FIELDS_METADATA = AZURE_SEARCH_FIELDS_METADATA env_helper.AZURE_SEARCH_SOURCE_COLUMN = AZURE_SEARCH_SOURCE_COLUMN @@ -242,7 +244,9 @@ def test_creates_search_index_if_not_exists( name=AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG, prioritized_fields=SemanticPrioritizedFields( title_field=None, - content_fields=[SemanticField(field_name=AZURE_SEARCH_CONTENT_COLUMN)], + content_fields=[ + SemanticField(field_name=AZURE_SEARCH_CONTENT_COLUMN) + ], ), ) ] diff --git a/code/tests/utilities/helpers/test_config_helper.py b/code/tests/utilities/helpers/test_config_helper.py index 573fa54f8..214d5ef16 100644 --- a/code/tests/utilities/helpers/test_config_helper.py +++ b/code/tests/utilities/helpers/test_config_helper.py @@ -372,11 +372,10 @@ def test_get_default_employee_assistant(): def test_get_document_processors(config_dict: dict): # given config_dict["document_processors"] = [ - {"document_type": "jpg", "use_advanced_image_processing": True}, { "document_type": "png", - "chunking": {"strategy": None, "size": None, "overlap": None}, - "loading": {"strategy": None}, + "chunking": {"strategy": "layout", "size": 500, "overlap": 100}, + "loading": {"strategy": "read"}, "use_advanced_image_processing": True, }, { @@ -396,16 +395,12 @@ def test_get_document_processors(config_dict: dict): # then assert config.document_processors == [ - EmbeddingConfig( - document_type="jpg", - chunking=None, - loading=None, - use_advanced_image_processing=True, - ), EmbeddingConfig( document_type="png", - chunking=None, - loading=None, + chunking=ChunkingSettings( + {"strategy": "layout", "size": 500, "overlap": 100} + ), + loading=LoadingSettings({"strategy": "read"}), use_advanced_image_processing=True, ), EmbeddingConfig(