|
import sys |
|
import os |
|
import io, asyncio |
|
|
|
|
|
|
|
sys.path.insert(0, os.path.abspath("../..")) |
|
|
|
from litellm import completion |
|
import litellm |
|
|
|
litellm.num_retries = 3 |
|
|
|
import time, random |
|
import pytest |
|
|
|
|
|
def pre_request(): |
|
file_name = f"dynamo.log" |
|
log_file = open(file_name, "a+") |
|
|
|
|
|
log_file.truncate(0) |
|
|
|
|
|
original_stdout = sys.stdout |
|
|
|
sys.stdout = log_file |
|
|
|
return original_stdout, log_file, file_name |
|
|
|
|
|
import re |
|
|
|
|
|
def verify_log_file(log_file_path): |
|
with open(log_file_path, "r") as log_file: |
|
log_content = log_file.read() |
|
print( |
|
f"\nVerifying DynamoDB file = {log_file_path}. File content=", log_content |
|
) |
|
|
|
|
|
pattern = r"Response from DynamoDB:{.*?}" |
|
|
|
|
|
matches = re.findall(pattern, log_content) |
|
|
|
|
|
print("DynamoDB Success Log Matches:") |
|
for match in matches: |
|
print(match) |
|
|
|
|
|
print(f"Total occurrences of specified response: {len(matches)}") |
|
|
|
|
|
success_count = sum( |
|
1 |
|
for match in matches |
|
if "'HTTPStatusCode': 200" in match or "'HTTPStatusCode': 201" in match |
|
) |
|
|
|
|
|
print(f"Count of successful responses from DynamoDB: {success_count}") |
|
assert success_count == 3 |
|
|
|
|
|
def test_dynamo_logging(): |
|
|
|
|
|
try: |
|
|
|
|
|
|
|
litellm.success_callback = ["dynamodb"] |
|
litellm.dynamodb_table_name = "litellm-logs-1" |
|
litellm.set_verbose = True |
|
original_stdout, log_file, file_name = pre_request() |
|
|
|
print("Testing async dynamoDB logging") |
|
|
|
async def _test(): |
|
return await litellm.acompletion( |
|
model="gpt-3.5-turbo", |
|
messages=[{"role": "user", "content": "This is a test"}], |
|
max_tokens=100, |
|
temperature=0.7, |
|
user="ishaan-2", |
|
) |
|
|
|
response = asyncio.run(_test()) |
|
print(f"response: {response}") |
|
|
|
|
|
async def _test2(): |
|
response = await litellm.acompletion( |
|
model="gpt-3.5-turbo", |
|
messages=[{"role": "user", "content": "This is a test"}], |
|
max_tokens=10, |
|
temperature=0.7, |
|
user="ishaan-2", |
|
stream=True, |
|
) |
|
async for chunk in response: |
|
pass |
|
|
|
asyncio.run(_test2()) |
|
|
|
|
|
async def _test3(): |
|
return await litellm.aembedding( |
|
model="text-embedding-ada-002", input=["hi"], user="ishaan-2" |
|
) |
|
|
|
response = asyncio.run(_test3()) |
|
time.sleep(1) |
|
except Exception as e: |
|
pytest.fail(f"An exception occurred - {e}") |
|
finally: |
|
|
|
|
|
sys.stdout = original_stdout |
|
|
|
log_file.close() |
|
verify_log_file(file_name) |
|
print("Passed! Testing async dynamoDB logging") |
|
|
|
|
|
|
|
|