From b727cd3baa1b800519b80a25fa1c5e7792c3ac95 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Sat, 18 May 2024 14:59:05 +0800 Subject: [PATCH 1/2] chore: Add IDEService logging to OpenAI and pipeline modules --- devchat/llm/openai.py | 5 +++++ devchat/llm/pipeline.py | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/devchat/llm/openai.py b/devchat/llm/openai.py index f6dfd03f..e279a313 100644 --- a/devchat/llm/openai.py +++ b/devchat/llm/openai.py @@ -17,6 +17,8 @@ ) +from devchat.ide import IDEService + def _try_remove_markdown_block_flag(content): """ 如果content是一个markdown块,则删除它的头部```xxx和尾部``` @@ -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 @@ -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 diff --git a/devchat/llm/pipeline.py b/devchat/llm/pipeline.py index 7917f0b4..1cf263c8 100644 --- a/devchat/llm/pipeline.py +++ b/devchat/llm/pipeline.py @@ -1,8 +1,9 @@ import sys +import time from typing import Dict import openai - +from devchat.ide import IDEService class RetryException(Exception): def __init__(self, err): @@ -17,8 +18,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 @@ -59,6 +62,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": @@ -67,6 +71,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 From 6d5ca1a4e223c623828bc31d195cace78bf2ba16 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Sat, 18 May 2024 15:04:01 +0800 Subject: [PATCH 2/2] chore: Refactor OpenAI and pipeline modules The code changes refactor the OpenAI and pipeline modules by adding logging for the IDEService. --- devchat/llm/openai.py | 4 ++-- devchat/llm/pipeline.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/devchat/llm/openai.py b/devchat/llm/openai.py index e279a313..ce882596 100644 --- a/devchat/llm/openai.py +++ b/devchat/llm/openai.py @@ -7,6 +7,8 @@ import httpx import openai +from devchat.ide import IDEService + from .pipeline import ( RetryException, exception_handle, @@ -17,8 +19,6 @@ ) -from devchat.ide import IDEService - def _try_remove_markdown_block_flag(content): """ 如果content是一个markdown块,则删除它的头部```xxx和尾部``` diff --git a/devchat/llm/pipeline.py b/devchat/llm/pipeline.py index 1cf263c8..6a2bcf32 100644 --- a/devchat/llm/pipeline.py +++ b/devchat/llm/pipeline.py @@ -3,8 +3,10 @@ from typing import Dict import openai + from devchat.ide import IDEService + class RetryException(Exception): def __init__(self, err): self.error = err