From 9cde8a6bc0ae0097383d553523674b47af4680a6 Mon Sep 17 00:00:00 2001 From: DavdGao Date: Thu, 24 Oct 2024 15:57:02 +0800 Subject: [PATCH] [HOTFIX] Fix the error in format function by adding system message (if provided) (#472) --- src/agentscope/models/dashscope_model.py | 6 ++-- src/agentscope/models/litellm_model.py | 6 ++-- src/agentscope/models/model.py | 17 +++++------ src/agentscope/models/ollama_model.py | 30 ++++++++++++-------- src/agentscope/models/openai_model.py | 15 +++------- src/agentscope/models/post_model.py | 5 ++-- src/agentscope/models/yi_model.py | 6 ++-- src/agentscope/models/zhipu_model.py | 6 ++-- tests/format_test.py | 36 ++++++++++++++++-------- 9 files changed, 75 insertions(+), 52 deletions(-) diff --git a/src/agentscope/models/dashscope_model.py b/src/agentscope/models/dashscope_model.py index 5d53f4d55..a3ac23613 100644 --- a/src/agentscope/models/dashscope_model.py +++ b/src/agentscope/models/dashscope_model.py @@ -356,11 +356,13 @@ def format( # prompt1 [ + { + "role": "system", + "content": "You're a helpful assistant" + }, { "role": "user", "content": ( - "You're a helpful assistant\\n" - "\\n" "## Conversation History\\n" "Bob: Hi, how can I help you?\\n" "user: What's the date today?" diff --git a/src/agentscope/models/litellm_model.py b/src/agentscope/models/litellm_model.py index 948481ae2..24399ad58 100644 --- a/src/agentscope/models/litellm_model.py +++ b/src/agentscope/models/litellm_model.py @@ -322,11 +322,13 @@ def format( # prompt1 [ + { + "role": "system", + "content": "You're a helpful assistant" + }, { "role": "user", "content": ( - "You're a helpful assistant\\n" - "\\n" "## Conversation History\\n" "Bob: Hi, how can I help you?\\n" "user: What's the date today?" diff --git a/src/agentscope/models/model.py b/src/agentscope/models/model.py index 429d34d7a..0586d4c94 100644 --- a/src/agentscope/models/model.py +++ b/src/agentscope/models/model.py @@ -243,7 +243,7 @@ def format_for_common_chat_models( *args: Union[Msg, Sequence[Msg]], ) -> List[dict]: """A common format strategy for chat models, which will format the - input messages into a user message. + input messages into a system message (if provided) and a user message. Note this strategy maybe not suitable for all scenarios, and developers are encouraged to implement their own prompt @@ -270,11 +270,13 @@ def format_for_common_chat_models( # prompt1 [ + { + "role": "system", + "content": "You're a helpful assistant" + }, { "role": "user", "content": ( - "You're a helpful assistant\\n" - "\\n" "## Conversation History\\n" "Bob: Hi, how can I help you?\\n" "user: What's the date today?" @@ -340,11 +342,6 @@ def format_for_common_chat_models( ) content_components = [] - # Add system prompt at the beginning if provided - if sys_prompt is not None: - if not sys_prompt.endswith("\n"): - sys_prompt += "\n" - content_components.append(sys_prompt) # The conversation history is added to the user message if not empty if len(dialogue) > 0: @@ -357,6 +354,10 @@ def format_for_common_chat_models( }, ] + # Add system prompt at the beginning if provided + if sys_prompt is not None: + messages = [{"role": "system", "content": sys_prompt}] + messages + return messages def _save_model_invocation( diff --git a/src/agentscope/models/ollama_model.py b/src/agentscope/models/ollama_model.py index 0dec98e34..bcb6bc18b 100644 --- a/src/agentscope/models/ollama_model.py +++ b/src/agentscope/models/ollama_model.py @@ -290,10 +290,13 @@ def format( .. code-block:: python [ + { + "role": "system", + "content": "You're a helpful assistant" + }, { "role": "user", "content": ( - "You're a helpful assistant\\n\\n" "## Conversation History\\n" "Bob: Hi, how can I help you?\\n" "user: What's the date today?" @@ -329,7 +332,8 @@ def format( ) # record dialog history as a list of strings - system_content_template = [] + system_prompt = None + history_content_template = [] dialogue = [] # TODO: here we default the url links to images images = [] @@ -337,9 +341,6 @@ def format( if i == 0 and unit.role == "system": # system prompt system_prompt = _convert_to_str(unit.content) - if not system_prompt.endswith("\n"): - system_prompt += "\n" - system_content_template.append(system_prompt) else: # Merge all messages into a conversation history prompt dialogue.append( @@ -352,21 +353,28 @@ def format( if len(dialogue) != 0: dialogue_history = "\n".join(dialogue) - system_content_template.extend( + history_content_template.extend( ["## Conversation History", dialogue_history], ) - system_content = "\n".join(system_content_template) + history_content = "\n".join(history_content_template) - system_message = { + # The conversation history message + history_message = { "role": "user", - "content": system_content, + "content": history_content, } if len(images) != 0: - system_message["images"] = images + history_message["images"] = images + + if system_prompt is None: + return [history_message] - return [system_message] + return [ + {"role": "system", "content": system_prompt}, + history_message, + ] class OllamaEmbeddingWrapper(OllamaWrapperBase): diff --git a/src/agentscope/models/openai_model.py b/src/agentscope/models/openai_model.py index e25fc9061..7d7ccc081 100644 --- a/src/agentscope/models/openai_model.py +++ b/src/agentscope/models/openai_model.py @@ -9,7 +9,6 @@ Dict, Optional, Generator, - get_args, ) from loguru import logger @@ -474,7 +473,9 @@ def format( *args: Union[Msg, Sequence[Msg]], ) -> List[dict]: """Format the input string and dictionary into the format that - OpenAI Chat API required. + OpenAI Chat API required. If you're using a OpenAI-compatible model + without a prefix "gpt-" in its name, the format method will + automatically format the input messages into the required format. Args: args (`Union[Msg, Sequence[Msg]]`): @@ -487,17 +488,9 @@ def format( The formatted messages in the format that OpenAI Chat API required. """ - # Check if the OpenAI library is installed - try: - import openai - except ImportError as e: - raise ImportError( - "Cannot find openai package, please install it by " - "`pip install openai`", - ) from e # Format messages according to the model name - if self.model_name in get_args(openai.types.ChatModel): + if self.model_name.startswith("gpt-"): return OpenAIChatWrapper.static_format( *args, model_name=self.model_name, diff --git a/src/agentscope/models/post_model.py b/src/agentscope/models/post_model.py index fbd09bd0e..def456c2f 100644 --- a/src/agentscope/models/post_model.py +++ b/src/agentscope/models/post_model.py @@ -192,8 +192,9 @@ def format( self, *args: Union[Msg, Sequence[Msg]], ) -> Union[List[dict]]: - """Format the input messages into a list of dict, which is - compatible to OpenAI Chat API. + """Format the input messages into a list of dict according to the model + name. For example, if the model name is prefixed with "gpt-", the + input messages will be formatted for OpenAI models. Args: args (`Union[Msg, Sequence[Msg]]`): diff --git a/src/agentscope/models/yi_model.py b/src/agentscope/models/yi_model.py index 9d02dd17c..0acc314d2 100644 --- a/src/agentscope/models/yi_model.py +++ b/src/agentscope/models/yi_model.py @@ -230,11 +230,13 @@ def format( # prompt1 [ + { + "role": "system", + "content": "You're a helpful assistant" + }, { "role": "user", "content": ( - "You're a helpful assistant\\n" - "\\n" "## Conversation History\\n" "Bob: Hi, how can I help you?\\n" "user: What's the date today?" diff --git a/src/agentscope/models/zhipu_model.py b/src/agentscope/models/zhipu_model.py index c767dd5b4..27455ba9e 100644 --- a/src/agentscope/models/zhipu_model.py +++ b/src/agentscope/models/zhipu_model.py @@ -325,11 +325,13 @@ def format( # prompt1 [ + { + "role": "system", + "content": "You're a helpful assistant" + }, { "role": "user", "content": ( - "You're a helpful assistant\\n" - "\\n" "## Conversation History\\n" "Bob: Hi, how can I help you?\\n" "user: What's the date today?" diff --git a/tests/format_test.py b/tests/format_test.py index de0ccb510..217936ce0 100644 --- a/tests/format_test.py +++ b/tests/format_test.py @@ -238,11 +238,13 @@ def test_openai_chat_with_other_models( # correct format ground_truth = [ + { + "role": "system", + "content": "You are a helpful assistant", + }, { "role": "user", "content": ( - "You are a helpful assistant\n" - "\n" "## Conversation History\n" "user: What is the weather today?\n" "assistant: It is sunny today" @@ -260,11 +262,13 @@ def test_format_for_common_models(self) -> None: # correct format ground_truth = [ + { + "role": "system", + "content": "You are a helpful assistant", + }, { "role": "user", "content": ( - "You are a helpful assistant\n" - "\n" "## Conversation History\n" "user: What is the weather today?\n" "assistant: It is sunny today" @@ -282,11 +286,13 @@ def test_ollama_chat(self) -> None: # correct format ground_truth = [ + { + "role": "system", + "content": "You are a helpful assistant", + }, { "role": "user", "content": ( - "You are a helpful assistant\n" - "\n" "## Conversation History\n" "user: What is the weather today?\n" "assistant: It is sunny today" @@ -358,10 +364,12 @@ def test_dashscope_chat(self) -> None: ) ground_truth = [ + { + "role": "system", + "content": "You are a helpful assistant", + }, { "content": ( - "You are a helpful assistant\n" - "\n" "## Conversation History\n" "user: What is the weather today?\n" "assistant: It is sunny today" @@ -386,10 +394,12 @@ def test_zhipuai_chat(self) -> None: ) ground_truth = [ + { + "role": "system", + "content": "You are a helpful assistant", + }, { "content": ( - "You are a helpful assistant\n" - "\n" "## Conversation History\n" "user: What is the weather today?\n" "assistant: It is sunny today" @@ -414,11 +424,13 @@ def test_litellm_chat(self) -> None: ) ground_truth = [ + { + "role": "system", + "content": "You are a helpful assistant", + }, { "role": "user", "content": ( - "You are a helpful assistant\n" - "\n" "## Conversation History\n" "user: What is the weather today?\n" "assistant: It is sunny today"