From d5d8e31447268348dd6ad547199ac7ee60269a57 Mon Sep 17 00:00:00 2001 From: mattie ruth backman Date: Thu, 15 Aug 2024 16:47:28 -0400 Subject: [PATCH 1/2] add cache tokens to metrics event --- src/pipecat/services/anthropic.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/pipecat/services/anthropic.py b/src/pipecat/services/anthropic.py index 088db7861..e3c8766d9 100644 --- a/src/pipecat/services/anthropic.py +++ b/src/pipecat/services/anthropic.py @@ -205,10 +205,13 @@ class AnthropicLLMService(LLMService): finally: await self.stop_processing_metrics() await self.push_frame(LLMFullResponseEndFrame()) + comp_tokens = completion_tokens if not use_completion_tokens_estimate else completion_tokens_estimate await self._report_usage_metrics( prompt_tokens=prompt_tokens, - completion_tokens=(completion_tokens if not use_completion_tokens_estimate - else completion_tokens_estimate)) + completion_tokens=comp_tokens, + cache_creation_input_tokens=cache_creation_input_tokens, + cache_read_input_tokens=cache_read_input_tokens + ) async def process_frame(self, frame: Frame, direction: FrameDirection): await super().process_frame(frame, direction) @@ -240,13 +243,20 @@ class AnthropicLLMService(LLMService): def _estimate_tokens(self, text: str) -> int: return int(len(re.split(r'[^\w]+', text)) * 1.3) - async def _report_usage_metrics(self, prompt_tokens: int, completion_tokens: int): - if prompt_tokens or completion_tokens: + async def _report_usage_metrics( + self, + prompt_tokens: int, + completion_tokens: int, + cache_creation_input_tokens: int, + cache_read_input_tokens: int): + if prompt_tokens or completion_tokens or cache_creation_input_tokens or cache_read_input_tokens: tokens = { "processor": self.name, "model": self._model, "prompt_tokens": prompt_tokens, "completion_tokens": completion_tokens, + "cache_creation_input_tokens": cache_creation_input_tokens, + "cache_read_input_tokens": cache_read_input_tokens, "total_tokens": prompt_tokens + completion_tokens } await self.start_llm_usage_metrics(tokens) From 848db985fceb0ede627a77b8e5e8d8934e1d934a Mon Sep 17 00:00:00 2001 From: mattie ruth backman Date: Thu, 15 Aug 2024 16:49:48 -0400 Subject: [PATCH 2/2] bump anthropic in 3.10 requirements --- linux-py3.10-requirements.txt | 2 +- macos-py3.10-requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/linux-py3.10-requirements.txt b/linux-py3.10-requirements.txt index 7b42fd5e3..dd9969964 100644 --- a/linux-py3.10-requirements.txt +++ b/linux-py3.10-requirements.txt @@ -16,7 +16,7 @@ aiosignal==1.3.1 # via aiohttp annotated-types==0.7.0 # via pydantic -anthropic==0.28.1 +anthropic==0.34.0 # via # openpipe # pipecat-ai (pyproject.toml) diff --git a/macos-py3.10-requirements.txt b/macos-py3.10-requirements.txt index a764e62d4..c94f6e41a 100644 --- a/macos-py3.10-requirements.txt +++ b/macos-py3.10-requirements.txt @@ -16,7 +16,7 @@ aiosignal==1.3.1 # via aiohttp annotated-types==0.7.0 # via pydantic -anthropic==0.28.1 +anthropic==0.34.0 # via # openpipe # pipecat-ai (pyproject.toml)