replaces 379
This commit is contained in:
@@ -102,13 +102,15 @@ class AnthropicLLMService(LLMService):
|
|||||||
await self.push_frame(LLMFullResponseStartFrame())
|
await self.push_frame(LLMFullResponseStartFrame())
|
||||||
await self.start_processing_metrics()
|
await self.start_processing_metrics()
|
||||||
|
|
||||||
logger.debug(f"Generating chat: {context.get_messages_for_logging()}")
|
logger.debug(
|
||||||
|
f"Generating chat: {context.system} | {context.get_messages_for_logging()}")
|
||||||
|
|
||||||
messages = context.messages
|
messages = context.messages
|
||||||
|
|
||||||
await self.start_ttfb_metrics()
|
await self.start_ttfb_metrics()
|
||||||
|
|
||||||
response = await self._client.messages.create(
|
response = await self._client.messages.create(
|
||||||
|
system=context.system,
|
||||||
messages=messages,
|
messages=messages,
|
||||||
tools=context.tools or [],
|
tools=context.tools or [],
|
||||||
model=self._model,
|
model=self._model,
|
||||||
@@ -234,12 +236,12 @@ class AnthropicLLMContext(OpenAILLMContext):
|
|||||||
tools: list[dict] | None = None,
|
tools: list[dict] | None = None,
|
||||||
tool_choice: dict | None = None,
|
tool_choice: dict | None = None,
|
||||||
*,
|
*,
|
||||||
system: str | None = None
|
system: List | None = None
|
||||||
):
|
):
|
||||||
super().__init__(messages=messages, tools=tools, tool_choice=tool_choice)
|
super().__init__(messages=messages, tools=tools, tool_choice=tool_choice)
|
||||||
self._user_image_request_context = {}
|
self._user_image_request_context = {}
|
||||||
|
|
||||||
self.system_message = system
|
self.system = system
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_openai_context(cls, openai_context: OpenAILLMContext):
|
def from_openai_context(cls, openai_context: OpenAILLMContext):
|
||||||
@@ -248,18 +250,7 @@ class AnthropicLLMContext(OpenAILLMContext):
|
|||||||
tools=openai_context.tools,
|
tools=openai_context.tools,
|
||||||
tool_choice=openai_context.tool_choice,
|
tool_choice=openai_context.tool_choice,
|
||||||
)
|
)
|
||||||
# See if we should pull the system message out of our context.messages list. (For
|
self._restructure_from_openai_messages()
|
||||||
# compatibility with Open AI messages format.)
|
|
||||||
if self.messages and self.messages[0]["role"] == "system":
|
|
||||||
if len(self.messages) == 1:
|
|
||||||
# If we have only have a system message in the list, all we can really do
|
|
||||||
# without introducing too much magic is change the role to "user".
|
|
||||||
self.messages[0]["role"] = "user"
|
|
||||||
else:
|
|
||||||
# If we have more than one message, we'll pull the system message out of the
|
|
||||||
# list.
|
|
||||||
self.system_message = self.messages[0]["content"]
|
|
||||||
self.messages.pop(0)
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -276,6 +267,10 @@ class AnthropicLLMContext(OpenAILLMContext):
|
|||||||
text=frame.text)
|
text=frame.text)
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
def set_messages(self, messages: List):
|
||||||
|
self._messages[:] = messages
|
||||||
|
self._restructure_from_openai_messages()
|
||||||
|
|
||||||
def add_image_frame_message(
|
def add_image_frame_message(
|
||||||
self, *, format: str, size: tuple[int, int], image: bytes, text: str = None):
|
self, *, format: str, size: tuple[int, int], image: bytes, text: str = None):
|
||||||
buffer = io.BytesIO()
|
buffer = io.BytesIO()
|
||||||
@@ -316,6 +311,20 @@ class AnthropicLLMContext(OpenAILLMContext):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error adding message: {e}")
|
logger.error(f"Error adding message: {e}")
|
||||||
|
|
||||||
|
def _restructure_from_openai_messages(self):
|
||||||
|
# See if we should pull the system message out of our context.messages list. (For
|
||||||
|
# compatibility with Open AI messages format.)
|
||||||
|
if self.messages and self.messages[0]["role"] == "system":
|
||||||
|
if len(self.messages) == 1:
|
||||||
|
# If we have only have a system message in the list, all we can really do
|
||||||
|
# without introducing too much magic is change the role to "user".
|
||||||
|
self.messages[0]["role"] = "user"
|
||||||
|
else:
|
||||||
|
# If we have more than one message, we'll pull the system message out of the
|
||||||
|
# list.
|
||||||
|
self.system = self.messages[0]["content"]
|
||||||
|
self.messages.pop(0)
|
||||||
|
|
||||||
def get_messages_for_logging(self) -> str:
|
def get_messages_for_logging(self) -> str:
|
||||||
msgs = []
|
msgs = []
|
||||||
for message in self.messages:
|
for message in self.messages:
|
||||||
|
|||||||
Reference in New Issue
Block a user