Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Consolidate External Service Plugins and Refactor code #988

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions livekit-agents/livekit/agents/embedder/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from livekit.agents.embedder.base import Embedder

__all__ = ["Embedder"]
17 changes: 17 additions & 0 deletions livekit-agents/livekit/agents/embedder/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Dict, List, Optional, Tuple

from pydantic import BaseModel, ConfigDict


class Embedder(BaseModel):
"""Base class for managing embedders"""

dimensions: Optional[int] = 1536

model_config = ConfigDict(arbitrary_types_allowed=True)

def get_embedding(self, text: str) -> List[float]:
raise NotImplementedError

def get_embedding_and_usage(self, text: str) -> Tuple[List[float], Optional[Dict]]:
raise NotImplementedError
71 changes: 71 additions & 0 deletions livekit-agents/livekit/agents/embedder/openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from typing import Any, Dict, List, Optional, Tuple

from livekit.agents.embedder.base import Embedder
from livekit.agents.utils.log import logger
from typing_extensions import Literal

try:
from openai import OpenAI as OpenAIClient
from openai.types.create_embedding_response import CreateEmbeddingResponse
except ImportError:
raise ImportError("`openai` not installed. Please install it using `pip install openai`.")


class OpenAIEmbedder(Embedder):
model: str = "text-embedding-3-small"
dimensions: int = 1536
encoding_format: Literal["float", "base64"] = "float"
user: Optional[str] = None
api_key: Optional[str] = None
organization: Optional[str] = None
base_url: Optional[str] = None
request_params: Optional[Dict[str, Any]] = None
client_params: Optional[Dict[str, Any]] = None
openai_client: Optional[OpenAIClient] = None

@property
def client(self) -> OpenAIClient:
if self.openai_client:
return self.openai_client

_client_params: Dict[str, Any] = {}
if self.api_key:
_client_params["api_key"] = self.api_key
if self.organization:
_client_params["organization"] = self.organization
if self.base_url:
_client_params["base_url"] = self.base_url
if self.client_params:
_client_params.update(self.client_params)
return OpenAIClient(**_client_params)

def _response(self, text: str) -> CreateEmbeddingResponse:
_request_params: Dict[str, Any] = {
"input": text,
"model": self.model,
"encoding_format": self.encoding_format,
}
if self.user is not None:
_request_params["user"] = self.user
if self.model.startswith("text-embedding-3"):
_request_params["dimensions"] = self.dimensions
if self.request_params:
_request_params.update(self.request_params)
return self.client.embeddings.create(**_request_params)

def get_embedding(self, text: str) -> List[float]:
response: CreateEmbeddingResponse = self._response(text=text)
try:
return response.data[0].embedding
except Exception as e:
logger.warning(e)
return []

def get_embedding_and_usage(self, text: str) -> Tuple[List[float], Optional[Dict]]:
response: CreateEmbeddingResponse = self._response(text=text)

embedding = response.data[0].embedding
usage = response.usage
if usage:
return embedding, usage.model_dump()
return embedding, None
2 changes: 1 addition & 1 deletion livekit-agents/livekit/agents/llm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from . import _oai_api
from .base import LLM, ChatChunk, Choice, ChoiceDelta, LLMStream
from .chat_context import ChatAudio, ChatContext, ChatImage, ChatMessage, ChatRole
from .function_context import (
USE_DOCSTRING,
Expand All @@ -10,7 +11,6 @@
TypeInfo,
ai_callable,
)
from .llm import LLM, ChatChunk, Choice, ChoiceDelta, LLMStream

__all__ = [
"LLM",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "0.6.8"

from .base import LLM, LLMStream

__all__ = [
"LLM",
"LLMStream",
]
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
import httpx
from livekit import rtc
from livekit.agents import llm, utils
from livekit.agents.utils.log import logger

import anthropic
try:
import anthropic
except ImportError:
logger.error("`anthropic` not installed. Please install it using `pip install anthropic`")
raise ImportError

from .log import logger
from .models import (
ChatModels,
)
Expand Down Expand Up @@ -508,4 +512,4 @@ def _sanitize_primitive(
if choices and value not in choices:
raise ValueError(f"invalid value {value}, not in {choices}")

return value
return value
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import abc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "0.2.1"

from . import beta, realtime
from .openai import LLM, LLMStream

__all__ = [
"LLM",
"LLMStream",
"beta",
"realtime"
]
Loading