Skip to content

Commit

Permalink
Merge pull request #41 from anevjes/openai-pii-stripping
Browse files Browse the repository at this point in the history
Openai-pii-stripping
  • Loading branch information
anevjes authored Aug 9, 2024
2 parents f2beccd + de979eb commit e903f6d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
19 changes: 15 additions & 4 deletions aisentry/facade/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

\
import logging
import uuid
from datetime import datetime
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions tests/http/adapter_test-ai-sentry.http
Original file line number Diff line number Diff line change
@@ -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

{
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tests/http/adapter_test-apim.http
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
5 changes: 5 additions & 0 deletions tests/http/get-thread.http
Original file line number Diff line number Diff line change
@@ -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: []
2 changes: 1 addition & 1 deletion tests/http/list_assistants.http
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/http/non_streaming_embedding.http
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit e903f6d

Please sign in to comment.