Merge pull request #62 from daily-co/anthropic-support

Anthropic LLM service
This commit is contained in:
Moishe Lettvin
2024-03-18 13:36:39 -04:00
committed by GitHub
2 changed files with 37 additions and 1 deletions

View File

@@ -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",

View File

@@ -0,0 +1,35 @@
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)