From 48bccba0ecc575e5afcfcee54c01288eb2026ed6 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Tue, 7 May 2024 14:32:16 +0800 Subject: [PATCH 1/2] Use response_format to request JSON result. --- devchat/llm/openai.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/devchat/llm/openai.py b/devchat/llm/openai.py index d6664be0..e2204ea5 100644 --- a/devchat/llm/openai.py +++ b/devchat/llm/openai.py @@ -114,9 +114,7 @@ def chunks_call(chunks): def content_to_json(content): try: - # json will format as ```json ... ``` in 1106 model - response_content = _try_remove_markdown_block_flag(content) - response_obj = json.loads(response_content) + response_obj = json.loads(content) return response_obj except json.JSONDecodeError as err: raise RetryException(err) from err @@ -148,7 +146,7 @@ def to_dict_content_and_call(content, tool_calls=None): pipeline(chat_completion_stream_commit, retry_timeout, chunks_call), times=3 ) -chat_completion_no_stream_return_json = exception_handle( +chat_completion_no_stream_return_json_with_retry = exception_handle( retry( pipeline(chat_completion_stream_commit, retry_timeout, chunks_content, content_to_json), times=3, @@ -156,6 +154,15 @@ def to_dict_content_and_call(content, tool_calls=None): exception_output_handle(lambda err: None), ) +def chat_completion_no_stream_return_json( + messages: List[Dict], llm_config: Dict): + """call llm without stream, return json object""" + llm_config["response_format"]={"type": "json_object"} + return chat_completion_no_stream_return_json_with_retry( + messages=messages, + llm_config=llm_config) + + chat_completion_stream = exception_handle( retry( pipeline( From ff2eb0c875e782aa3b3f9ac1e61d46bf063f6389 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Tue, 7 May 2024 14:34:28 +0800 Subject: [PATCH 2/2] Add Tiktoken cache directory to CLI initialization --- devchat/_cli/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/devchat/_cli/__init__.py b/devchat/_cli/__init__.py index e93def6a..1cce4ad4 100644 --- a/devchat/_cli/__init__.py +++ b/devchat/_cli/__init__.py @@ -1,3 +1,4 @@ +import os from .log import log from .prompt import prompt from .run import run @@ -5,6 +6,9 @@ from .route import route from .command import commands, command, Command +script_dir = os.path.dirname(os.path.realpath(__file__)) +os.environ['TIKTOKEN_CACHE_DIR'] = os.path.join(script_dir, '..', 'tiktoken_cache') + __all__ = [ 'log', 'prompt',