diff --git a/routstr/payment/helpers.py b/routstr/payment/helpers.py index c621a55e..0ec24636 100644 --- a/routstr/payment/helpers.py +++ b/routstr/payment/helpers.py @@ -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 diff --git a/routstr/payment/x_cashu.py b/routstr/payment/x_cashu.py index 551dce4a..7d2a7c57 100644 --- a/routstr/payment/x_cashu.py +++ b/routstr/payment/x_cashu.py @@ -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, ) diff --git a/routstr/proxy.py b/routstr/proxy.py index 5ebe7c6f..e3da5abc 100644 --- a/routstr/proxy.py +++ b/routstr/proxy.py @@ -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), ), )