Skip to content

Commit

Permalink
Merge pull request #389 from devchat-ai/log_llm_calls
Browse files Browse the repository at this point in the history
chore: Add logging to IDEService in OpenAI and pipeline modules
  • Loading branch information
yangbobo2021 committed May 18, 2024
2 parents df25107 + 6d5ca1a commit f6590bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions devchat/llm/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import httpx
import openai

from devchat.ide import IDEService

from .pipeline import (
RetryException,
exception_handle,
Expand Down Expand Up @@ -83,6 +85,7 @@ def retry_timeout(chunks):
for chunk in chunks:
yield chunk
except (openai.APIConnectionError, openai.APITimeoutError) as err:
IDEService().ide_logging("info", f"in retry_timeout: err: {err}")
raise RetryException(err) from err


Expand Down Expand Up @@ -127,8 +130,10 @@ def content_to_json(content):
response_obj = json.loads(content_no_block)
return response_obj
except json.JSONDecodeError as err:
IDEService().ide_logging("info", f"in content_to_json: json decode error: {err}")
raise RetryException(err) from err
except Exception as err:
IDEService().ide_logging("info", f"in content_to_json: other error: {err}")
raise err


Expand Down
8 changes: 8 additions & 0 deletions devchat/llm/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import sys
import time
from typing import Dict

import openai

from devchat.ide import IDEService


class RetryException(Exception):
def __init__(self, err):
Expand All @@ -17,8 +20,10 @@ def wrapper(*args, **kwargs):
except RetryException as err:
if index + 1 == times:
raise err.error
IDEService().ide_logging("debug", f"has retries: {index + 1}")
continue
except Exception as err:
IDEService().ide_logging("info", f"exception: {err}")
raise err.error

return wrapper
Expand Down Expand Up @@ -59,6 +64,7 @@ def wrapper(*args, **kwargs):

def pipeline(*funcs):
def wrapper(*args, **kwargs):
start_time = time.time()
for index, func in enumerate(funcs):
if index > 0:
if isinstance(args, Dict) and args.get("__type__", None) == "parallel":
Expand All @@ -67,6 +73,8 @@ def wrapper(*args, **kwargs):
args = func(args)
else:
args = func(*args, **kwargs)
end_time = time.time()
IDEService().ide_logging("debug", f"time on pipeline: {end_time-start_time}")
return args

return wrapper
Expand Down

0 comments on commit f6590bf

Please sign in to comment.