mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 18:54:30 +00:00
refactor: add black formatting
This commit is contained in:
parent
b87d630b0a
commit
4905929de3
156 changed files with 19723 additions and 10869 deletions
|
@ -9,7 +9,7 @@ import os, io, asyncio
|
|||
|
||||
sys.path.insert(
|
||||
0, os.path.abspath("../..")
|
||||
) # Adds the parent directory to the system path
|
||||
) # Adds the parent directory to the system path
|
||||
import pytest
|
||||
import litellm
|
||||
from litellm import embedding, completion, completion_cost, Timeout
|
||||
|
@ -19,16 +19,22 @@ import importlib, inspect
|
|||
# test /chat/completion request to the proxy
|
||||
from fastapi.testclient import TestClient
|
||||
from fastapi import FastAPI
|
||||
from litellm.proxy.proxy_server import router, save_worker_config, initialize # Replace with the actual module where your FastAPI router is defined
|
||||
from litellm.proxy.proxy_server import (
|
||||
router,
|
||||
save_worker_config,
|
||||
initialize,
|
||||
) # Replace with the actual module where your FastAPI router is defined
|
||||
|
||||
filepath = os.path.dirname(os.path.abspath(__file__))
|
||||
python_file_path = f"{filepath}/test_configs/custom_callbacks.py"
|
||||
|
||||
# @app.on_event("startup")
|
||||
# async def wrapper_startup_event():
|
||||
# initialize(config=config_fp)
|
||||
# initialize(config=config_fp)
|
||||
|
||||
# Use the app fixture in your client fixture
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
filepath = os.path.dirname(os.path.abspath(__file__))
|
||||
|
@ -38,25 +44,23 @@ def client():
|
|||
app.include_router(router) # Include your router in the test app
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
|
||||
# Your bearer token
|
||||
token = os.getenv("PROXY_MASTER_KEY")
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}"
|
||||
}
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
print("Testing proxy custom logger")
|
||||
|
||||
|
||||
def test_embedding(client):
|
||||
try:
|
||||
litellm.set_verbose=False
|
||||
litellm.set_verbose = False
|
||||
from litellm.proxy.utils import get_instance_fn
|
||||
|
||||
my_custom_logger = get_instance_fn(
|
||||
value = "custom_callbacks.my_custom_logger",
|
||||
config_file_path=python_file_path
|
||||
value="custom_callbacks.my_custom_logger", config_file_path=python_file_path
|
||||
)
|
||||
print("id of initialized custom logger", id(my_custom_logger))
|
||||
litellm.callbacks = [my_custom_logger]
|
||||
|
@ -69,26 +73,50 @@ def test_embedding(client):
|
|||
print("my_custom_logger", my_custom_logger)
|
||||
assert my_custom_logger.async_success_embedding == False
|
||||
|
||||
test_data = {
|
||||
"model": "azure-embedding-model",
|
||||
"input": ["hello"]
|
||||
}
|
||||
test_data = {"model": "azure-embedding-model", "input": ["hello"]}
|
||||
response = client.post("/embeddings", json=test_data, headers=headers)
|
||||
print("made request", response.status_code, response.text)
|
||||
print("vars my custom logger /embeddings", vars(my_custom_logger), "id", id(my_custom_logger))
|
||||
assert my_custom_logger.async_success_embedding == True # checks if the status of async_success is True, only the async_log_success_event can set this to true
|
||||
assert my_custom_logger.async_embedding_kwargs["model"] == "azure-embedding-model" # checks if kwargs passed to async_log_success_event are correct
|
||||
print(
|
||||
"vars my custom logger /embeddings",
|
||||
vars(my_custom_logger),
|
||||
"id",
|
||||
id(my_custom_logger),
|
||||
)
|
||||
assert (
|
||||
my_custom_logger.async_success_embedding == True
|
||||
) # checks if the status of async_success is True, only the async_log_success_event can set this to true
|
||||
assert (
|
||||
my_custom_logger.async_embedding_kwargs["model"] == "azure-embedding-model"
|
||||
) # checks if kwargs passed to async_log_success_event are correct
|
||||
kwargs = my_custom_logger.async_embedding_kwargs
|
||||
litellm_params = kwargs.get("litellm_params")
|
||||
metadata = litellm_params.get("metadata", None)
|
||||
print("\n\n Metadata in custom logger kwargs", litellm_params.get("metadata"))
|
||||
assert metadata is not None
|
||||
assert "user_api_key" in metadata
|
||||
assert "headers" in metadata
|
||||
assert "headers" in metadata
|
||||
proxy_server_request = litellm_params.get("proxy_server_request")
|
||||
model_info = litellm_params.get("model_info")
|
||||
assert proxy_server_request == {'url': 'http://testserver/embeddings', 'method': 'POST', 'headers': {'host': 'testserver', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'testclient', 'authorization': 'Bearer sk-1234', 'content-length': '54', 'content-type': 'application/json'}, 'body': {'model': 'azure-embedding-model', 'input': ['hello']}}
|
||||
assert model_info == {'input_cost_per_token': 0.002, 'mode': 'embedding', 'id': 'hello'}
|
||||
assert proxy_server_request == {
|
||||
"url": "http://testserver/embeddings",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"host": "testserver",
|
||||
"accept": "*/*",
|
||||
"accept-encoding": "gzip, deflate",
|
||||
"connection": "keep-alive",
|
||||
"user-agent": "testclient",
|
||||
"authorization": "Bearer sk-1234",
|
||||
"content-length": "54",
|
||||
"content-type": "application/json",
|
||||
},
|
||||
"body": {"model": "azure-embedding-model", "input": ["hello"]},
|
||||
}
|
||||
assert model_info == {
|
||||
"input_cost_per_token": 0.002,
|
||||
"mode": "embedding",
|
||||
"id": "hello",
|
||||
}
|
||||
result = response.json()
|
||||
print(f"Received response: {result}")
|
||||
print("Passed Embedding custom logger on proxy!")
|
||||
|
@ -98,12 +126,12 @@ def test_embedding(client):
|
|||
|
||||
def test_chat_completion(client):
|
||||
try:
|
||||
# Your test data
|
||||
litellm.set_verbose=False
|
||||
# Your test data
|
||||
litellm.set_verbose = False
|
||||
from litellm.proxy.utils import get_instance_fn
|
||||
|
||||
my_custom_logger = get_instance_fn(
|
||||
value = "custom_callbacks.my_custom_logger",
|
||||
config_file_path=python_file_path
|
||||
value="custom_callbacks.my_custom_logger", config_file_path=python_file_path
|
||||
)
|
||||
|
||||
print("id of initialized custom logger", id(my_custom_logger))
|
||||
|
@ -121,36 +149,66 @@ def test_chat_completion(client):
|
|||
test_data = {
|
||||
"model": "Azure OpenAI GPT-4 Canada",
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "write a litellm poem"
|
||||
},
|
||||
{"role": "user", "content": "write a litellm poem"},
|
||||
],
|
||||
"max_tokens": 10,
|
||||
}
|
||||
|
||||
|
||||
response = client.post("/chat/completions", json=test_data, headers=headers)
|
||||
print("made request", response.status_code, response.text)
|
||||
print("LiteLLM Callbacks", litellm.callbacks)
|
||||
asyncio.sleep(1) # sleep while waiting for callback to run
|
||||
asyncio.sleep(1) # sleep while waiting for callback to run
|
||||
|
||||
print("my_custom_logger in /chat/completions", my_custom_logger, "id", id(my_custom_logger))
|
||||
print(
|
||||
"my_custom_logger in /chat/completions",
|
||||
my_custom_logger,
|
||||
"id",
|
||||
id(my_custom_logger),
|
||||
)
|
||||
print("vars my custom logger, ", vars(my_custom_logger))
|
||||
assert my_custom_logger.async_success == True # checks if the status of async_success is True, only the async_log_success_event can set this to true
|
||||
assert my_custom_logger.async_completion_kwargs["model"] == "chatgpt-v-2" # checks if kwargs passed to async_log_success_event are correct
|
||||
print("\n\n Custom Logger Async Completion args", my_custom_logger.async_completion_kwargs)
|
||||
assert (
|
||||
my_custom_logger.async_success == True
|
||||
) # checks if the status of async_success is True, only the async_log_success_event can set this to true
|
||||
assert (
|
||||
my_custom_logger.async_completion_kwargs["model"] == "chatgpt-v-2"
|
||||
) # checks if kwargs passed to async_log_success_event are correct
|
||||
print(
|
||||
"\n\n Custom Logger Async Completion args",
|
||||
my_custom_logger.async_completion_kwargs,
|
||||
)
|
||||
litellm_params = my_custom_logger.async_completion_kwargs.get("litellm_params")
|
||||
metadata = litellm_params.get("metadata", None)
|
||||
print("\n\n Metadata in custom logger kwargs", litellm_params.get("metadata"))
|
||||
assert metadata is not None
|
||||
assert "user_api_key" in metadata
|
||||
assert "headers" in metadata
|
||||
assert "headers" in metadata
|
||||
config_model_info = litellm_params.get("model_info")
|
||||
proxy_server_request_object = litellm_params.get("proxy_server_request")
|
||||
|
||||
assert config_model_info == {'id': 'gm', 'input_cost_per_token': 0.0002, 'mode': 'chat'}
|
||||
assert proxy_server_request_object == {'url': 'http://testserver/chat/completions', 'method': 'POST', 'headers': {'host': 'testserver', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'testclient', 'authorization': 'Bearer sk-1234', 'content-length': '123', 'content-type': 'application/json'}, 'body': {'model': 'Azure OpenAI GPT-4 Canada', 'messages': [{'role': 'user', 'content': 'write a litellm poem'}], 'max_tokens': 10}}
|
||||
assert config_model_info == {
|
||||
"id": "gm",
|
||||
"input_cost_per_token": 0.0002,
|
||||
"mode": "chat",
|
||||
}
|
||||
assert proxy_server_request_object == {
|
||||
"url": "http://testserver/chat/completions",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"host": "testserver",
|
||||
"accept": "*/*",
|
||||
"accept-encoding": "gzip, deflate",
|
||||
"connection": "keep-alive",
|
||||
"user-agent": "testclient",
|
||||
"authorization": "Bearer sk-1234",
|
||||
"content-length": "123",
|
||||
"content-type": "application/json",
|
||||
},
|
||||
"body": {
|
||||
"model": "Azure OpenAI GPT-4 Canada",
|
||||
"messages": [{"role": "user", "content": "write a litellm poem"}],
|
||||
"max_tokens": 10,
|
||||
},
|
||||
}
|
||||
result = response.json()
|
||||
print(f"Received response: {result}")
|
||||
print("\nPassed /chat/completions with Custom Logger!")
|
||||
|
@ -161,40 +219,38 @@ def test_chat_completion(client):
|
|||
def test_chat_completion_stream(client):
|
||||
try:
|
||||
# Your test data
|
||||
litellm.set_verbose=False
|
||||
litellm.set_verbose = False
|
||||
from litellm.proxy.utils import get_instance_fn
|
||||
|
||||
my_custom_logger = get_instance_fn(
|
||||
value = "custom_callbacks.my_custom_logger",
|
||||
config_file_path=python_file_path
|
||||
value="custom_callbacks.my_custom_logger", config_file_path=python_file_path
|
||||
)
|
||||
|
||||
print("id of initialized custom logger", id(my_custom_logger))
|
||||
|
||||
litellm.callbacks = [my_custom_logger]
|
||||
import json
|
||||
|
||||
print("initialized proxy")
|
||||
# import the initialized custom logger
|
||||
print(litellm.callbacks)
|
||||
|
||||
|
||||
print("LiteLLM Callbacks", litellm.callbacks)
|
||||
print("my_custom_logger", my_custom_logger)
|
||||
|
||||
assert my_custom_logger.streaming_response_obj == None # no streaming response obj is set pre call
|
||||
assert (
|
||||
my_custom_logger.streaming_response_obj == None
|
||||
) # no streaming response obj is set pre call
|
||||
|
||||
test_data = {
|
||||
"model": "Azure OpenAI GPT-4 Canada",
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "write 1 line poem about LiteLLM"
|
||||
},
|
||||
{"role": "user", "content": "write 1 line poem about LiteLLM"},
|
||||
],
|
||||
"max_tokens": 40,
|
||||
"stream": True # streaming call
|
||||
"stream": True, # streaming call
|
||||
}
|
||||
|
||||
|
||||
response = client.post("/chat/completions", json=test_data, headers=headers)
|
||||
print("made request", response.status_code, response.text)
|
||||
complete_response = ""
|
||||
|
@ -205,7 +261,7 @@ def test_chat_completion_stream(client):
|
|||
print(line)
|
||||
line = str(line)
|
||||
|
||||
json_data = line.replace('data: ', '')
|
||||
json_data = line.replace("data: ", "")
|
||||
|
||||
# Parse the JSON string
|
||||
data = json.loads(json_data)
|
||||
|
@ -213,22 +269,24 @@ def test_chat_completion_stream(client):
|
|||
print("\n\n decode_data", data)
|
||||
|
||||
# Access the content of choices[0]['message']['content']
|
||||
content = data['choices'][0]['delta']['content'] or ""
|
||||
content = data["choices"][0]["delta"]["content"] or ""
|
||||
|
||||
# Process the content as needed
|
||||
print("Content:", content)
|
||||
|
||||
complete_response+= content
|
||||
complete_response += content
|
||||
|
||||
print("\n\nHERE is the complete streaming response string", complete_response)
|
||||
print("\n\nHERE IS the streaming Response from callback\n\n")
|
||||
print(my_custom_logger.streaming_response_obj)
|
||||
import time
|
||||
|
||||
time.sleep(0.5)
|
||||
|
||||
streamed_response = my_custom_logger.streaming_response_obj
|
||||
assert complete_response == streamed_response["choices"][0]["message"]["content"]
|
||||
assert (
|
||||
complete_response == streamed_response["choices"][0]["message"]["content"]
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
pytest.fail(f"LiteLLM Proxy test failed. Exception {str(e)}")
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue