From 2dee8a25d549c5707e04993b4cff6c8f236d3982 Mon Sep 17 00:00:00 2001 From: Su Yang Date: Tue, 19 Mar 2024 15:50:02 +0800 Subject: [PATCH] fix: anthropic system prompt not working (#2885) --- .../model_providers/anthropic/llm/llm.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/api/core/model_runtime/model_providers/anthropic/llm/llm.py b/api/core/model_runtime/model_providers/anthropic/llm/llm.py index ad74179353..724a0401b7 100644 --- a/api/core/model_runtime/model_providers/anthropic/llm/llm.py +++ b/api/core/model_runtime/model_providers/anthropic/llm/llm.py @@ -342,12 +342,20 @@ class AnthropicLargeLanguageModel(LargeLanguageModel): Convert prompt messages to dict list and system """ system = "" - prompt_message_dicts = [] - + first_loop = True for message in prompt_messages: if isinstance(message, SystemPromptMessage): - system += message.content + ("\n" if not system else "") - else: + message.content=message.content.strip() + if first_loop: + system=message.content + first_loop=False + else: + system+="\n" + system+=message.content + + prompt_message_dicts = [] + for message in prompt_messages: + if not isinstance(message, SystemPromptMessage): prompt_message_dicts.append(self._convert_prompt_message_to_dict(message)) return system, prompt_message_dicts