Skip to content

Commit

Permalink
Fix OpenAI Assistants API example (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzhao authored Oct 15, 2024
1 parent 1aa497e commit 070d05b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 78 deletions.
52 changes: 52 additions & 0 deletions examples/voice-pipeline-agent/openai_assistant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import logging

from dotenv import load_dotenv
from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, cli, llm
from livekit.agents.pipeline import VoicePipelineAgent
from livekit.plugins import deepgram, openai, silero
from livekit.plugins.openai.beta import (
AssistantCreateOptions,
AssistantLLM,
AssistantOptions,
OnFileUploadedInfo,
)

load_dotenv()
logger = logging.getLogger("openai_assistant")


async def entrypoint(ctx: JobContext):
"""This example demonstrates a VoicePipelineAgent that uses OpenAI's Assistant API as the LLM"""
initial_ctx = llm.ChatContext()

await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
participant = await ctx.wait_for_participant()

# When you add a ChatMessage that contain images, AssistantLLM will upload them
# to OpenAI's Assistant API.
# It's up to you to remove them if desired or otherwise manage them going forward.
def on_file_uploaded(info: OnFileUploadedInfo):
logger.info(f"{info.type} uploaded: {info.openai_file_object}")

agent = VoicePipelineAgent(
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",
)
),
on_file_uploaded=on_file_uploaded,
),
tts=openai.TTS(),
chat_ctx=initial_ctx,
)
agent.start(ctx.room, participant)
await agent.say("Hey, how can I help you today?", allow_interruptions=False)


if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
12 changes: 12 additions & 0 deletions livekit-plugins/livekit-plugins-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ pip install livekit-plugins-openai
## Pre-requisites

You'll need an API key from OpenAI. It can be set as an environment variable: `OPENAI_API_KEY`

## OpenAI Beta Features

### Assistants API

In addition to LLM, STT, and TTS, this package also supports using [OpenAI's Assistants API](https://platform.openai.com/docs/assistants/overview) as a LLM.

The Assistants API is a stateful API that holds the conversation state on the server-side.

The `AssistantLLM` class gives you a LLM-like interface to interact with the Assistant API.

For examples of using Assistants API with VoicePipelineAssistant, see the [openai assistants API example](https://github.com/livekit/agents/blob/main/examples/voice-pipeline-agent/openai_assistant.py)

This file was deleted.

0 comments on commit 070d05b

Please sign in to comment.