Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzhe-log10 committed Feb 22, 2024
1 parent 994ff2e commit b2f2fb9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
21 changes: 10 additions & 11 deletions log10/_httpx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import httpx
from dotenv import load_dotenv
from httpx import Request, Response
from load import global_tags, sessionID

from log10.load import global_tags, sessionID


load_dotenv()
Expand All @@ -17,8 +18,9 @@
token = os.environ.get("LOG10_TOKEN")
org_id = os.environ.get("LOG10_ORG_ID")

transport = httpx.Transport(retries=5)
httpx_client = httpx.Client(transport=transport)
httpx_client = httpx.Client()
httpx_logger = logging.getLogger("httpx")
httpx_logger.setLevel(logging.DEBUG)


def _try_post_request(url: str, payload: dict = {}) -> httpx.Response:
Expand All @@ -30,6 +32,7 @@ def _try_post_request(url: str, payload: dict = {}) -> httpx.Response:
}
res = None
try:
logger.debug(f"POST {url} with headers={headers} payload: {payload}")
res = httpx_client.post(url, headers=headers, json=payload)
res.raise_for_status()
except httpx.HTTPError as http_err:
Expand All @@ -39,18 +42,19 @@ def _try_post_request(url: str, payload: dict = {}) -> httpx.Response:
+ "\nSee https://github.com/log10-io/log10#%EF%B8%8F-setup for details"
)
else:
logger.error(f"Log10: failed with error: {http_err}")
logger.error(f"Failed with error: {http_err}")
except Exception as err:
logger.error(f"Log10: failed to insert in log10: {payload} with error {err}")
logger.error(f"Failed to insert in log10: {payload} with error {err}")


async def get_completion_id(request: Request):
completion_url = "/api/completions"
res = _try_post_request(url=f"{base_url}{completion_url}")
import ipdb; ipdb.set_trace()
try:
completion_id = res.json().get("completionID")
except Exception as e:
logger.error(f"LOG10: failed to get completion ID. Error: {e}. Skipping completion recording.")
logger.error(f"Failed to get completion ID. Error: {e}. Skipping completion recording.")
else:
request.headers["x-log10-completion-id"] = completion_id

Expand Down Expand Up @@ -179,8 +183,3 @@ async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
extensions=response.extensions,
request=request,
)

async def



18 changes: 18 additions & 0 deletions log10/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,24 @@ def log10(module, DEBUG_=False, USE_ASYNC_=True):
attr = module.resources.chat.completions.Completions
method = getattr(attr, "create")
setattr(attr, "create", intercepting_decorator(method))

# support for async completions
# patch module.AsyncOpenAI.__init__ to new_init
origin_init = module.AsyncOpenAI.__init__
def new_init(self, *args, **kwargs):
logger.debug("LOG10: patching AsyncOpenAI.__init__")
import httpx

from log10._httpx_utils import get_completion_id
event_hooks = {
"request": [get_completion_id],
}
async_httpx_client = httpx.AsyncClient(
event_hooks=event_hooks)
kwargs["http_client"] = async_httpx_client
origin_init(self, *args, **kwargs)
module.AsyncOpenAI.__init__ = new_init

else:
attr = module.api_resources.completion.Completion
method = getattr(attr, "create")
Expand Down

0 comments on commit b2f2fb9

Please sign in to comment.