diff --git a/examples/foundational/19a-tools-anthropic.py b/examples/foundational/19a-tools-anthropic.py index 8e0fc6bbc..e238de63c 100644 --- a/examples/foundational/19a-tools-anthropic.py +++ b/examples/foundational/19a-tools-anthropic.py @@ -62,7 +62,7 @@ async def main(): ) llm = AnthropicLLMService( - api_key=os.getenv("OPENAI_API_KEY"), + api_key=os.getenv("ANTHROPIC_API_KEY"), model="claude-3-5-sonnet-20240620" ) llm.register_function("get_weather", get_weather) @@ -86,10 +86,12 @@ async def main(): # todo: test with very short initial user message - messages = [{"role": "system", - "content": "You are a helpful assistant who can report the weather in any location in the universe. Respond concisely. Your response will be turned into speech so use only simple words and punctuation."}, - {"role": "user", - "content": " Start the conversation by introducing yourself."}] + # messages = [{"role": "system", + # "content": "You are a helpful assistant who can report the weather in any location in the universe. Respond concisely. Your response will be turned into speech so use only simple words and punctuation."}, + # {"role": "user", + # "content": " Start the conversation by introducing yourself."}] + + messages = [{"role": "user", "content": "Say 'hello' to start the conversation."}] context = OpenAILLMContext(messages, tools) context_aggregator = llm.create_context_aggregator(context) @@ -109,7 +111,7 @@ async def main(): async def on_first_participant_joined(transport, participant): transport.capture_participant_transcription(participant["id"]) # Kick off the conversation. - await task.queue_frames([LLMMessagesFrame(messages)]) + await task.queue_frames([context_aggregator.user().get_context_frame()]) runner = PipelineRunner() diff --git a/examples/foundational/19b-tools-video-anthropic.py b/examples/foundational/19b-tools-video-anthropic.py index 51a321bf9..4ba29ab37 100644 --- a/examples/foundational/19b-tools-video-anthropic.py +++ b/examples/foundational/19b-tools-video-anthropic.py @@ -137,7 +137,8 @@ If you need to use a tool, simply use the tool. Do not tell the user the tool yo """ messages = [{"role": "system", - "content": system_prompt, + "content": system_prompt}, + {"role": "user", "content": "Start the conversation by introducing yourself."}] context = OpenAILLMContext(messages, tools) @@ -161,7 +162,7 @@ If you need to use a tool, simply use the tool. Do not tell the user the tool yo transport.capture_participant_transcription(video_participant_id) transport.capture_participant_video(video_participant_id, framerate=0) # Kick off the conversation. - await task.queue_frames([LLMMessagesFrame(messages)]) + await task.queue_frames([context_aggregator.user().get_context_frame()]) runner = PipelineRunner() await runner.run(task) diff --git a/src/pipecat/services/anthropic.py b/src/pipecat/services/anthropic.py index ec19164c1..506d71243 100644 --- a/src/pipecat/services/anthropic.py +++ b/src/pipecat/services/anthropic.py @@ -110,7 +110,7 @@ class AnthropicLLMService(LLMService): await self.start_ttfb_metrics() response = await self._client.messages.create( - system=context.system, + system=context.system or [], messages=messages, tools=context.tools or [], model=self._model, @@ -255,7 +255,9 @@ class AnthropicLLMContext(OpenAILLMContext): @classmethod def from_messages(cls, messages: List[dict]) -> "AnthropicLLMContext": - return cls(messages=messages) + self = cls(messages=messages) + self._restructure_from_openai_messages() + return self @classmethod def from_image_frame(cls, frame: VisionImageRawFrame) -> "AnthropicLLMContext":