mirror of
https://github.com/Routstr/routstr-core.git
synced 2026-08-09 02:54:37 +00:00
azure openai compatibility
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
import os
|
||||
from typing import Mapping
|
||||
|
||||
from fastapi import HTTPException, Response
|
||||
from fastapi.requests import Request
|
||||
@@ -14,6 +15,7 @@ logger = get_logger(__name__)
|
||||
|
||||
UPSTREAM_BASE_URL = os.environ.get("UPSTREAM_BASE_URL", "")
|
||||
UPSTREAM_API_KEY = os.environ.get("UPSTREAM_API_KEY", "")
|
||||
CHAT_COMPLETIONS_API_VERSION = os.environ.get("CHAT_COMPLETIONS_API_VERSION", "")
|
||||
|
||||
if not UPSTREAM_BASE_URL:
|
||||
raise ValueError("Please set the UPSTREAM_BASE_URL environment variable")
|
||||
@@ -201,3 +203,13 @@ def prepare_upstream_headers(request_headers: dict) -> dict:
|
||||
)
|
||||
|
||||
return headers
|
||||
|
||||
|
||||
def prepare_upstream_params(
|
||||
path: str, query_params: Mapping[str, str] | None
|
||||
) -> dict[str, str]:
|
||||
"""Prepare query params for upstream request, optionally adding api-version for chat/completions."""
|
||||
params: dict[str, str] = dict(query_params or {})
|
||||
if path.endswith("chat/completions") and CHAT_COMPLETIONS_API_VERSION:
|
||||
params["api-version"] = CHAT_COMPLETIONS_API_VERSION
|
||||
return params
|
||||
|
||||
@@ -9,7 +9,12 @@ from fastapi.responses import Response, StreamingResponse
|
||||
from ..core import get_logger
|
||||
from ..wallet import recieve_token, send_token
|
||||
from .cost_caculation import CostData, CostDataError, MaxCostData, calculate_cost
|
||||
from .helpers import UPSTREAM_BASE_URL, create_error_response, prepare_upstream_headers
|
||||
from .helpers import (
|
||||
UPSTREAM_BASE_URL,
|
||||
create_error_response,
|
||||
prepare_upstream_headers,
|
||||
prepare_upstream_params,
|
||||
)
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -128,7 +133,7 @@ async def forward_to_upstream(
|
||||
url,
|
||||
headers=headers,
|
||||
content=request.stream(),
|
||||
params=request.query_params,
|
||||
params=prepare_upstream_params(path, request.query_params),
|
||||
),
|
||||
stream=True,
|
||||
)
|
||||
|
||||
+4
-3
@@ -21,6 +21,7 @@ from .payment.helpers import (
|
||||
create_error_response,
|
||||
get_max_cost_for_model,
|
||||
prepare_upstream_headers,
|
||||
prepare_upstream_params,
|
||||
)
|
||||
from .payment.x_cashu import x_cashu_handler
|
||||
|
||||
@@ -283,7 +284,7 @@ async def forward_to_upstream(
|
||||
url,
|
||||
headers=headers,
|
||||
content=request_body,
|
||||
params=request.query_params,
|
||||
params=prepare_upstream_params(path, request.query_params),
|
||||
),
|
||||
stream=True,
|
||||
)
|
||||
@@ -294,7 +295,7 @@ async def forward_to_upstream(
|
||||
url,
|
||||
headers=headers,
|
||||
content=request.stream(),
|
||||
params=request.query_params,
|
||||
params=prepare_upstream_params(path, request.query_params),
|
||||
),
|
||||
stream=True,
|
||||
)
|
||||
@@ -722,7 +723,7 @@ async def forward_get_to_upstream(
|
||||
url,
|
||||
headers=headers,
|
||||
content=request.stream(),
|
||||
params=request.query_params,
|
||||
params=prepare_upstream_params(path, request.query_params),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user