From bae41e25f80078e446e516018f49f80f6559f469 Mon Sep 17 00:00:00 2001 From: Atomie CHEN Date: Sat, 29 Jul 2023 14:05:23 +0800 Subject: [PATCH 1/4] Minor improvement --- src/handyllm/prompt_converter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/handyllm/prompt_converter.py b/src/handyllm/prompt_converter.py index 3e2aef1..21e76fa 100644 --- a/src/handyllm/prompt_converter.py +++ b/src/handyllm/prompt_converter.py @@ -64,8 +64,8 @@ def chat_replace_variables(self, chat, variable_map: dict, inplace=False): for message in chat: new_message = {"role": message['role'], "content": message['content']} for var, value in variable_map.items(): - if var in message['content']: - new_message = {"role": new_message['role'], "content": new_message['content'].replace(var, value)} + if var in new_message['content']: + new_message['content'] = new_message['content'].replace(var, value) new_chat.append(new_message) return new_chat From 8f03898b55f3ca966160adcc72bf37be3670992f Mon Sep 17 00:00:00 2001 From: Atomie CHEN Date: Sat, 29 Jul 2023 14:08:55 +0800 Subject: [PATCH 2/4] Update PromptConverter example --- README.md | 11 +++++++++-- tests/prompt.txt | 3 ++- tests/test_prompt.py | 16 ++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 13b7822..b5d2a21 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,8 @@ $user$ "item2": "Indeed." } %output_format% -%misc% +%misc1% +%misc2% ``` ```python @@ -144,7 +145,13 @@ converter = PromptConverter() chat = converter.rawfile2chat('prompt.txt') # variables wrapped in %s can be replaced at runtime -new_chat = converter.chat_replace_variables(chat, {r'%misc%': 'Note: do not use any bad word.'}) +new_chat = converter.chat_replace_variables( + chat, + { + r'%misc1%': 'Note1: do not use any bad word.', + r'%misc2%': 'Note2: be optimistic.', + } +) ``` ### Substitute diff --git a/tests/prompt.txt b/tests/prompt.txt index 8660379..29d8593 100644 --- a/tests/prompt.txt +++ b/tests/prompt.txt @@ -15,4 +15,5 @@ $user$ "item2": "Indeed." } %output_format% -%misc% \ No newline at end of file +%misc1% +%misc2% \ No newline at end of file diff --git a/tests/test_prompt.py b/tests/test_prompt.py index a361bba..9796013 100644 --- a/tests/test_prompt.py +++ b/tests/test_prompt.py @@ -6,9 +6,17 @@ # chat can be used as the message parameter for OpenAI API chat = converter.rawfile2chat('prompt.txt') # variables are substituted according to map -print(json.dumps(chat, indent=2)) -print() +# print(json.dumps(chat, indent=2)) +print(converter.chat2raw(chat)) +print('-----') # variables wrapped in %s can be replaced at runtime -new_chat = converter.chat_replace_variables(chat, {r'%misc%': 'Note: do not use any bad word.'}) -print(json.dumps(new_chat, indent=2)) +new_chat = converter.chat_replace_variables( + chat, + { + r'%misc1%': 'Note1: do not use any bad word.', + r'%misc2%': 'Note2: be optimistic.', + } +) +# print(json.dumps(new_chat, indent=2)) +print(converter.chat2raw(new_chat)) From 3f76e177f0436beda4ef66d75130a92347b014be Mon Sep 17 00:00:00 2001 From: Atomie CHEN Date: Sat, 29 Jul 2023 15:06:23 +0800 Subject: [PATCH 3/4] Support non-iterable log_marks --- README.md | 6 +++++- src/handyllm/openai_api.py | 13 +++++++++++-- src/handyllm/utils.py | 6 ++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b5d2a21..d47349a 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,13 @@ Example scripts are placed in [tests](./tests) folder. ## OpenAI API Request +### Logs + +You can pass custom `logger` and `log_marks` (a string or a collection of strings) to `chat`/`completions` to get input and output logging. + ### Timeout control -This toolkit supports client-side `timeout` control, which OpenAI's official python package does not support yet: +This toolkit supports client-side `timeout` control: ```python from handyllm import OpenAIAPI diff --git a/src/handyllm/openai_api.py b/src/handyllm/openai_api.py index 9da69ec..f64f8db 100644 --- a/src/handyllm/openai_api.py +++ b/src/handyllm/openai_api.py @@ -6,6 +6,7 @@ import copy from .prompt_converter import PromptConverter +from . import utils module_logger = logging.getLogger(__name__) @@ -145,7 +146,11 @@ def chat(model, messages, timeout=None, endpoint_manager=None, logger=None, log_ if logger is not None and 'messages' in kwargs: arguments = copy.deepcopy(kwargs) arguments.pop('messages', None) - input_lines = [str(item) for item in log_marks] + # check if log_marks is iterable + if utils.isiterable(log_marks): + input_lines = [str(item) for item in log_marks] + else: + input_lines = [str(log_marks)] input_lines.append(json.dumps(arguments, indent=2, ensure_ascii=False)) input_lines.append(" INPUT START ".center(50, '-')) input_lines.append(OpenAIAPI.converter.chat2raw(kwargs['messages'])) @@ -199,7 +204,11 @@ def completions(model, prompt, timeout=None, endpoint_manager=None, logger=None, if logger is not None and 'prompt' in kwargs: arguments = copy.deepcopy(kwargs) arguments.pop('prompt', None) - input_lines = [str(item) for item in log_marks] + # check if log_marks is iterable + if utils.isiterable(log_marks): + input_lines = [str(item) for item in log_marks] + else: + input_lines = [str(log_marks)] input_lines.append(json.dumps(arguments, indent=2, ensure_ascii=False)) input_lines.append(" INPUT START ".center(50, '-')) input_lines.append(kwargs['prompt']) diff --git a/src/handyllm/utils.py b/src/handyllm/utils.py index 846df69..c2f01d0 100644 --- a/src/handyllm/utils.py +++ b/src/handyllm/utils.py @@ -2,6 +2,7 @@ from urllib.parse import urlparse import os import time +import collections.abc def get_filename_from_url(download_url): @@ -23,3 +24,8 @@ def download_binary(download_url, file_path=None, dir='.'): file.write(response.content) return file_path +def isiterable(arg): + return ( + isinstance(arg, collections.abc.Iterable) + and not isinstance(arg, str) + ) From cb659684f583171cb2b7b96ef337f725ee430b39 Mon Sep 17 00:00:00 2001 From: Atomie CHEN Date: Sat, 29 Jul 2023 15:07:39 +0800 Subject: [PATCH 4/4] Bump version to 0.3.1 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 36a4c29..d686554 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "HandyLLM" -version = "0.3.0" +version = "0.3.1" authors = [ { name="Atomie CHEN", email="atomic_cwh@163.com" }, ]