ふぁd
Browse files- __pycache__/proxy_server.cpython-39.pyc +0 -0
- __pycache__/utils.cpython-39.pyc +0 -0
- proxy_server.py +61 -1
- utils.py +21 -0
__pycache__/proxy_server.cpython-39.pyc
CHANGED
|
Binary files a/__pycache__/proxy_server.cpython-39.pyc and b/__pycache__/proxy_server.cpython-39.pyc differ
|
|
|
__pycache__/utils.cpython-39.pyc
ADDED
|
Binary file (623 Bytes). View file
|
|
|
proxy_server.py
CHANGED
|
@@ -63,6 +63,9 @@ from litellm.caching import DualCache
|
|
| 63 |
from litellm.proxy.health_check import perform_health_check
|
| 64 |
from litellm._logging import verbose_router_logger, verbose_proxy_logger
|
| 65 |
|
|
|
|
|
|
|
|
|
|
| 66 |
litellm.suppress_debug_info = True
|
| 67 |
from fastapi import (
|
| 68 |
FastAPI,
|
|
@@ -1414,11 +1417,68 @@ async def completion(
|
|
| 1414 |
raise HTTPException(status_code=status, detail=error_msg)
|
| 1415 |
|
| 1416 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1417 |
@routers.post(
|
| 1418 |
-
"/v1/chat/
|
| 1419 |
dependencies=[Depends(user_api_key_auth)],
|
| 1420 |
tags=["chat/completions"],
|
| 1421 |
)
|
|
|
|
|
|
|
| 1422 |
|
| 1423 |
|
| 1424 |
@router.post(
|
|
|
|
| 63 |
from litellm.proxy.health_check import perform_health_check
|
| 64 |
from litellm._logging import verbose_router_logger, verbose_proxy_logger
|
| 65 |
|
| 66 |
+
from utils import getenv, set_env_variables
|
| 67 |
+
import litellm as llm
|
| 68 |
+
|
| 69 |
litellm.suppress_debug_info = True
|
| 70 |
from fastapi import (
|
| 71 |
FastAPI,
|
|
|
|
| 1417 |
raise HTTPException(status_code=status, detail=error_msg)
|
| 1418 |
|
| 1419 |
|
| 1420 |
+
def completion_request(user_api_key):
|
| 1421 |
+
resp = requests.post(
|
| 1422 |
+
f"{FASTREPL_PROXY_URL_BASE}/chat/completions",
|
| 1423 |
+
headers={
|
| 1424 |
+
"Content-Type": "application/json",
|
| 1425 |
+
"Authorization": f"Bearer {user_api_key}",
|
| 1426 |
+
},
|
| 1427 |
+
json={
|
| 1428 |
+
"model": "gpt-3.5-turbo",
|
| 1429 |
+
"messages": [
|
| 1430 |
+
{
|
| 1431 |
+
"content": "what is YC?",
|
| 1432 |
+
"role": "user"
|
| 1433 |
+
}
|
| 1434 |
+
]
|
| 1435 |
+
|
| 1436 |
+
}
|
| 1437 |
+
)
|
| 1438 |
+
return resp.json()
|
| 1439 |
+
|
| 1440 |
+
|
| 1441 |
+
if __name__ == "__main__":
|
| 1442 |
+
key = new_user(100)["api_key"]
|
| 1443 |
+
print("new key", key)
|
| 1444 |
+
|
| 1445 |
+
for _ in range(20):
|
| 1446 |
+
print(cost_current(key))
|
| 1447 |
+
|
| 1448 |
+
response = completion_request(key)
|
| 1449 |
+
print(response)
|
| 1450 |
+
|
| 1451 |
+
|
| 1452 |
+
# for completion
|
| 1453 |
+
@routers.post("/v1/chat/completions", dependencies=[Depends(user_api_key_auth)])
|
| 1454 |
+
async def completion(request: Request):
|
| 1455 |
+
key = request.headers.get("Authorization").replace("Bearer ", "") # type: ignore
|
| 1456 |
+
data = await request.json()
|
| 1457 |
+
print(f"received request data: {data}")
|
| 1458 |
+
data["user_key"] = key
|
| 1459 |
+
data["budget_manager"] = budget_manager
|
| 1460 |
+
data["master_key"] = master_key
|
| 1461 |
+
set_env_variables(data)
|
| 1462 |
+
# handle how users send streaming
|
| 1463 |
+
if 'stream' in data:
|
| 1464 |
+
if type(data['stream']) == str: # if users send stream as str convert to bool
|
| 1465 |
+
# convert to bool
|
| 1466 |
+
if data['stream'].lower() == "true":
|
| 1467 |
+
data['stream'] = True # convert to boolean
|
| 1468 |
+
|
| 1469 |
+
response = llm.completion(**data)
|
| 1470 |
+
if 'stream' in data and data['stream'] == True: # use generate_responses to stream responses
|
| 1471 |
+
return StreamingResponse(data_generator(response), media_type='text/event-stream')
|
| 1472 |
+
return response
|
| 1473 |
+
|
| 1474 |
+
|
| 1475 |
@routers.post(
|
| 1476 |
+
"/v1/chat/completionsssssssssss",
|
| 1477 |
dependencies=[Depends(user_api_key_auth)],
|
| 1478 |
tags=["chat/completions"],
|
| 1479 |
)
|
| 1480 |
+
def test(aaaa,bbbb):
|
| 1481 |
+
print(aaa)
|
| 1482 |
|
| 1483 |
|
| 1484 |
@router.post(
|
utils.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import functools
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@functools.lru_cache(maxsize=None)
|
| 9 |
+
def getenv(key, default=0):
|
| 10 |
+
return type(default)(os.getenv(key, default))
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def set_env_variables(data):
|
| 14 |
+
try:
|
| 15 |
+
if "env_variables" in data:
|
| 16 |
+
env_variables = data["env_variables"]
|
| 17 |
+
for key in env_variables:
|
| 18 |
+
os.environ[key] = env_variables[key]
|
| 19 |
+
data.pop("env_variables")
|
| 20 |
+
except:
|
| 21 |
+
pass
|