From eacaea7db4636573858807fccca9e9fba75f5b96 Mon Sep 17 00:00:00 2001 From: Moishe Lettvin Date: Fri, 15 Mar 2024 19:40:37 -0400 Subject: [PATCH 1/2] Anthropic LLM service --- pyproject.toml | 3 +- src/dailyai/services/anthropic_llm_service.py | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/dailyai/services/anthropic_llm_service.py diff --git a/pyproject.toml b/pyproject.toml index 00958d83c..d2e61fdbb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "dailyai" -version = "0.0.3" +version = "0.0.3.1" description = "An open source framework for real-time, multi-modal, conversational AI applications" license = { text = "BSD 2-Clause License" } readme = "README.md" @@ -21,6 +21,7 @@ classifiers = [ ] dependencies = [ "aiohttp", + "anthropic", "azure-cognitiveservices-speech", "daily-python", "fal", diff --git a/src/dailyai/services/anthropic_llm_service.py b/src/dailyai/services/anthropic_llm_service.py new file mode 100644 index 000000000..e7d25e1cc --- /dev/null +++ b/src/dailyai/services/anthropic_llm_service.py @@ -0,0 +1,51 @@ +import asyncio +import os +from typing import AsyncGenerator +from anthropic import AsyncAnthropic +from dailyai.pipeline.frames import Frame, LLMMessagesQueueFrame, TextFrame + +from dailyai.services.ai_services import LLMService + + +class AnthropicLLMService(LLMService): + + def __init__(self, api_key, model="claude-3-opus-20240229", max_tokens=1024): + super().__init__() + self.client = AsyncAnthropic(api_key=api_key) + self.model = model + self.max_tokens = max_tokens + + async def process_frame(self, frame: Frame) -> AsyncGenerator[Frame, None]: + if not isinstance(frame, LLMMessagesQueueFrame): + yield frame + + stream = await self.client.messages.create( + max_tokens=self.max_tokens, + messages=[ + { + "role": "user", + "content": "Hello, Claude", + } + ], + model=self.model, + stream=True, + ) + async for event in stream: + if event.type == "content_block_delta": + yield TextFrame(event.delta.text) + + +async def test(): + service = AnthropicLLMService(api_key=os.getenv("ANTHROPIC_API_KEY")) + messages = [ + { + "role": "user", + "content": "Hello, Claude", + } + ] + async for frame in service.process_frame(LLMMessagesQueueFrame(messages)): + print(frame) + + +if __name__=="__main__": + asyncio.run(test()) From c91fa39a99c46daf83c9f31d6dbcc0f289003703 Mon Sep 17 00:00:00 2001 From: Moishe Lettvin Date: Fri, 15 Mar 2024 19:42:46 -0400 Subject: [PATCH 2/2] Remove testing code --- src/dailyai/services/anthropic_llm_service.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/dailyai/services/anthropic_llm_service.py b/src/dailyai/services/anthropic_llm_service.py index e7d25e1cc..a58c610d7 100644 --- a/src/dailyai/services/anthropic_llm_service.py +++ b/src/dailyai/services/anthropic_llm_service.py @@ -33,19 +33,3 @@ class AnthropicLLMService(LLMService): async for event in stream: if event.type == "content_block_delta": yield TextFrame(event.delta.text) - - -async def test(): - service = AnthropicLLMService(api_key=os.getenv("ANTHROPIC_API_KEY")) - messages = [ - { - "role": "user", - "content": "Hello, Claude", - } - ] - async for frame in service.process_frame(LLMMessagesQueueFrame(messages)): - print(frame) - - -if __name__=="__main__": - asyncio.run(test())