Skip to content

Commit

Permalink
[HOTFIX] Fix the error in format function by adding system message (i…
Browse files Browse the repository at this point in the history
…f provided) (#472)
  • Loading branch information
DavdGao authored Oct 24, 2024
1 parent 1c26592 commit 9cde8a6
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 52 deletions.
6 changes: 4 additions & 2 deletions src/agentscope/models/dashscope_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down
6 changes: 4 additions & 2 deletions src/agentscope/models/litellm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down
17 changes: 9 additions & 8 deletions src/agentscope/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?"
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand Down
30 changes: 19 additions & 11 deletions src/agentscope/models/ollama_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down Expand Up @@ -329,17 +332,15 @@ 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 = []
for i, unit in enumerate(input_msgs):
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(
Expand All @@ -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):
Expand Down
15 changes: 4 additions & 11 deletions src/agentscope/models/openai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Dict,
Optional,
Generator,
get_args,
)

from loguru import logger
Expand Down Expand Up @@ -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]]`):
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/agentscope/models/post_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]`):
Expand Down
6 changes: 4 additions & 2 deletions src/agentscope/models/yi_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down
6 changes: 4 additions & 2 deletions src/agentscope/models/zhipu_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down
36 changes: 24 additions & 12 deletions tests/format_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 9cde8a6

Please sign in to comment.