Skip to content

Commit

Permalink
Making calls to proxy completion models work better
Browse files Browse the repository at this point in the history
  • Loading branch information
nagkumar91 committed Apr 29, 2024
1 parent b52c81a commit 3770661
Showing 1 changed file with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# ---------------------------------------------------------
import asyncio
import copy
import json
import logging
import time
import uuid
Expand All @@ -14,6 +15,22 @@
from .models import AsyncHTTPClientWithRetry, OpenAIChatCompletionsModel


class SimulationRequestDTO:
def __init__(self, url, headers, payload, params, templatekey, template_parameters):
self.url = url
self.headers = headers
self.json = json.dumps(payload)
self.params = params
self.templatekey = templatekey
self.templateParameters = template_parameters

def to_dict(self):
return self.__dict__

def to_json(self):
return json.dumps(self.__dict__)


class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
def __init__(self, name, template_key, template_parameters, *args, **kwargs):
self.tkey = template_key
Expand Down Expand Up @@ -80,7 +97,7 @@ async def request_api(

self._log_request(request_data)

token = await self.token_manager.get_token()
token = self.token_manager.get_token()

proxy_headers = {
"Authorization": f"Bearer {token}",
Expand All @@ -99,19 +116,21 @@ async def request_api(
if self.api_version:
params["api-version"] = self.api_version

sim_request_dict = {
"url": self.endpoint_url,
"headers": headers,
"payload": request_data,
"params": params,
"templatekey": self.tkey,
"template_parameters": self.tparam,
}
sim_request_dto = SimulationRequestDTO(
url=self.endpoint_url,
headers=headers,
payload=request_data,
params=params,
templatekey=self.tkey,
template_parameters=self.tparam,
)

time_start = time.time()
full_response = None

async with session.post(url=self.endpoint_url, headers=proxy_headers, json=sim_request_dict) as response:
async with session.post(
url=self.endpoint_url, headers=proxy_headers, json=sim_request_dto.to_dict()
) as response:
if response.status == 202:
response = await response.json()
self.result_url = response["location"]
Expand Down

0 comments on commit 3770661

Please sign in to comment.