From c3397488e3254070874108f5bbe2821a4bb50577 Mon Sep 17 00:00:00 2001 From: Luciano Scarpulla Date: Mon, 4 Nov 2024 17:18:59 +0800 Subject: [PATCH 1/3] fix: antropic system message --- simplemind/providers/anthropic.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/simplemind/providers/anthropic.py b/simplemind/providers/anthropic.py index ffa776d..b31936a 100644 --- a/simplemind/providers/anthropic.py +++ b/simplemind/providers/anthropic.py @@ -1,5 +1,5 @@ from functools import cached_property -from typing import TYPE_CHECKING, Type, TypeVar, Iterator +from typing import TYPE_CHECKING, Iterator, Type, TypeVar import instructor from pydantic import BaseModel @@ -49,15 +49,24 @@ class Anthropic(BaseProvider): return instructor.from_anthropic(self.client) @logger - def send_conversation(self, conversation: "Conversation", **kwargs) -> "Message": + def send_conversation( + self, conversation: "Conversation", **kwargs + ) -> "Message": """Send a conversation to the Anthropic API.""" from ..models import Message + system_prompt = filter( + lambda msg: msg.role == "system", conversation.messages + ) + messages = [ - {"role": msg.role, "content": msg.text} for msg in conversation.messages + {"role": msg.role, "content": msg.text} + for msg in conversation.messages + if msg.role != "system" ] response = self.client.messages.create( + system=next(system_prompt, None), model=conversation.llm_model or self.DEFAULT_MODEL, messages=messages, **{**self.DEFAULT_KWARGS, **kwargs}, From 57d54abf248c9b2457e08459bc0739be093f57a9 Mon Sep 17 00:00:00 2001 From: Luciano Scarpulla Date: Mon, 4 Nov 2024 17:23:48 +0800 Subject: [PATCH 2/3] remove unrelted changes --- simplemind/providers/anthropic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplemind/providers/anthropic.py b/simplemind/providers/anthropic.py index b31936a..9adc769 100644 --- a/simplemind/providers/anthropic.py +++ b/simplemind/providers/anthropic.py @@ -1,5 +1,5 @@ from functools import cached_property -from typing import TYPE_CHECKING, Iterator, Type, TypeVar +from typing import TYPE_CHECKING, Type, TypeVar, Iterator import instructor from pydantic import BaseModel From 37334a21c5923ffeefefff44d0dd2faa135cfa5e Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 4 Nov 2024 11:14:02 -0500 Subject: [PATCH 3/3] Update simplemind/providers/anthropic.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- simplemind/providers/anthropic.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/simplemind/providers/anthropic.py b/simplemind/providers/anthropic.py index 9adc769..6abb6b8 100644 --- a/simplemind/providers/anthropic.py +++ b/simplemind/providers/anthropic.py @@ -55,10 +55,10 @@ class Anthropic(BaseProvider): """Send a conversation to the Anthropic API.""" from ..models import Message - system_prompt = filter( - lambda msg: msg.role == "system", conversation.messages - ) - + system_messages = [msg for msg in conversation.messages if msg.role == "system"] + if len(system_messages) > 1: + logger.warning("Multiple system messages found. Using the first one.") + system_prompt = system_messages[0] if system_messages else None messages = [ {"role": msg.role, "content": msg.text} for msg in conversation.messages