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

Support OpenAI Assistants API #601

Merged
merged 20 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
2 changes: 2 additions & 0 deletions livekit-agents/livekit/agents/llm/chat_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ChatMessage:
content: str | list[str | ChatImage] | None = None
tool_calls: list[function_context.FunctionCallInfo] | None = None
tool_call_id: str | None = None
_metadata: dict[str, Any] = field(default_factory=dict, repr=False, init=False)

@staticmethod
def create_tool_from_called_function(
Expand Down Expand Up @@ -104,6 +105,7 @@ def copy(self):
@dataclass
class ChatContext:
messages: list[ChatMessage] = field(default_factory=list)
_metadata: dict[str, Any] = field(default_factory=dict, repr=False, init=False)

def append(
self, *, text: str = "", images: list[ChatImage] = [], role: ChatRole = "system"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.


from . import beta
from .embeddings import EmbeddingData, create_embeddings
from .llm import LLM, LLMStream
from .models import TTSModels, TTSVoices, WhisperModels
Expand All @@ -25,6 +27,7 @@
"LLM",
"LLMStream",
"WhisperModels",
"beta",
"TTSModels",
"TTSVoices",
"create_embeddings",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# OpenAI Beta Features

## Assistants API

Example usage:

```python
import asyncio

from dotenv import load_dotenv
from livekit import rtc
from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, cli, llm
from livekit.agents.voice_assistant import VoiceAssistant
from livekit.plugins import deepgram, openai, silero
from livekit.plugins.openai.beta import (
AssistantCreateOptions,
AssistantLLM,
AssistantOptions,
)

load_dotenv()


async def entrypoint(ctx: JobContext):
initial_ctx = llm.ChatContext()

await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)

assistant = VoiceAssistant(
vad=silero.VAD.load(),
stt=deepgram.STT(),
llm=AssistantLLM(
assistant_opts=AssistantOptions(
create_options=AssistantCreateOptions(
model="gpt-4o",
instructions="You are a voice assistant created by LiveKit. Your interface with users will be voice.",
name="KITT",
)
)
),
tts=openai.TTS(),
chat_ctx=initial_ctx,
)
assistant.start(ctx.room)

# listen to incoming chat messages, only required if you'd like the agent to
# answer incoming messages from Chat
chat = rtc.ChatManager(ctx.room)

async def answer_from_text(txt: str):
chat_ctx = assistant.chat_ctx.copy()
chat_ctx.append(role="user", text=txt)
stream = assistant.llm.chat(chat_ctx=chat_ctx)
await assistant.say(stream)

@chat.on("message_received")
def on_chat_received(msg: rtc.ChatMessage):
if msg.message:
asyncio.create_task(answer_from_text(msg.message))

await asyncio.sleep(1)
await assistant.say("Hey, how can I help you today?", allow_interruptions=True)


if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
```

## TODO
- tool calling
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .assistant_llm import (
AssistantCreateOptions,
AssistantLLM,
AssistantLoadOptions,
AssistantOptions,
)

__all__ = [
"AssistantLLM",
"AssistantOptions",
"AssistantCreateOptions",
"AssistantLoadOptions",
]
Loading
Loading