diff --git a/aisentry/facade/app.py b/aisentry/facade/app.py index 8bae94e..735a9db 100644 --- a/aisentry/facade/app.py +++ b/aisentry/facade/app.py @@ -1,4 +1,4 @@ - +\ import logging import uuid from datetime import datetime @@ -90,6 +90,7 @@ async def catch_all(path): method = request.method original_headers = request.headers params = request.args + body = None body = await request.get_data() # Request Processed variable @@ -141,7 +142,18 @@ async def catch_all(path): token = credential.get_token("https://cognitiveservices.azure.com/.default") openAI_request_headers['Authorization'] = f"Bearer {token.token}" - json_body = json.loads(body) + + decoded_body = body.decode('UTF-8').strip() + json_body = None + + if not decoded_body: + logger.info("Received an empty or None body") + # Handle the empty or None body case here + json_body = {} + else: + json_body = json.loads(decoded_body) + + object_value = json_body.get("object") if object_value == "assistant": @@ -373,8 +385,7 @@ async def stream_response(response): endpoint_info["x-ratelimit-remaining-tokens"]=0 utc_now = datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z') - - request_body = json.loads(body) + request_body = json_body response_json = json.loads(response_body) #Extract the token count from the response diff --git a/tests/http/adapter_test-ai-sentry.http b/tests/http/adapter_test-ai-sentry.http index 970ec80..45e9ada 100644 --- a/tests/http/adapter_test-ai-sentry.http +++ b/tests/http/adapter_test-ai-sentry.http @@ -1,8 +1,9 @@ -POST http://4.147.31.58/openai/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview HTTP/1.1 -ai-sentry-consumer: Product-car-review -ai-sentry-log-level: PII_STRIPPING_ENABLED -ai-sentry-backend-pool: pool1 -ai-sentry-adapters: ["SampleApiRequestTransformer"] +POST http://20.167.97.191/openai/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview HTTP/1.1 +Ai-Sentry-Consumer: Product-car-review +Ai-Sentry-Log-Level: COMPLETE +Ai-Sentry-Backend-Pool: pool1 +Ai-Sentry-Adapters: [] +ArianWasHere: aaaa Content-Type: application/json { @@ -13,7 +14,7 @@ Content-Type: application/json }, { "role":"user", - "content":"Write a review on toyota yaris gr" + "content":"Write a 3 sentence review on honda integra type r DC2" } ], "stream":true, diff --git a/tests/http/adapter_test-apim.http b/tests/http/adapter_test-apim.http index 2140776..196eb60 100644 --- a/tests/http/adapter_test-apim.http +++ b/tests/http/adapter_test-apim.http @@ -1,5 +1,6 @@ -POST https://anevjes-apim-001.azure-api.net/openai-2024-02-01/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview HTTP/1.1 +POST https://anevjes-apim-dev.azure-api.net/openai/deployments/gpt-4o/chat/completions?api-version=2024-02-15-preview HTTP/1.1 Content-Type: application/json +api-key: 13737537357353 { "messages": [ diff --git a/tests/http/get-thread.http b/tests/http/get-thread.http new file mode 100644 index 0000000..2807cae --- /dev/null +++ b/tests/http/get-thread.http @@ -0,0 +1,5 @@ +GET http://20.167.97.191/openai/threads/thread_58vrUKZ2LYSoGdVYk6WfXzxQ?api-version=2024-05-01-preview +Ai-Sentry-Consumer: test-assistant +Ai-Sentry-Log-Level: DISABLED +Ai-Sentry-Backend-Pool: pool1 +Ai-Sentry-Adapters: [] \ No newline at end of file diff --git a/tests/http/list_assistants.http b/tests/http/list_assistants.http index 77150af..ba4f900 100644 --- a/tests/http/list_assistants.http +++ b/tests/http/list_assistants.http @@ -1,4 +1,4 @@ -POST http://4.198.191.229/openai/assistants?api-version=2024-05-01-preview +POST http://20.167.97.191/openai/assistants?api-version=2024-05-01-preview Content-Type: application/json Ai-Sentry-Consumer: Product-car-review Ai-Sentry-Log-Level: PII_STRIPPING_ENABLED diff --git a/tests/http/non_streaming_embedding.http b/tests/http/non_streaming_embedding.http index bd62531..d717975 100644 --- a/tests/http/non_streaming_embedding.http +++ b/tests/http/non_streaming_embedding.http @@ -1,11 +1,11 @@ -@resource_name = 4.200.52.148 +@resource_name = 20.167.97.191 @deployment_name = text-embedding-ada-002 -@api_key = {{$dotenv AOAI_API_KEY}} +@api_key = {{$dotenv AOAI_API_KEY}}ct ###‰ POST http://{{resource_name}}/openai/deployments/{{deployment_name}}/embeddings?api-version=2024-06-01 HTTP/1.1 Content-Type: application/json -api-key: {{api_key}} +#api-key: {{api_key}} ai-sentry-backend-pool:pool1 ai-sentry-consumer:embedding-automated-test1 ai-sentry-log-level:PII_STRIPPING_ENABLED