-
Notifications
You must be signed in to change notification settings - Fork 504
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
examples: document how to log chats #685
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3928833
examples: document how to log chats
nbsp 46e53c1
oops
nbsp 9960015
write everything at context shutdown
nbsp 4bc436c
Revert "write everything at context shutdown"
nbsp 244455d
non-block, and show timestamp
nbsp 72f895e
aio
nbsp 04b8a01
ruff
nbsp 4074edd
make it nicer
nbsp 2efaa75
address feedback
nbsp f255a56
await function
nbsp 5a5d52f
rename file
nbsp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import asyncio | ||
from datetime import datetime | ||
|
||
from aiofile import async_open as open | ||
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 | ||
|
||
load_dotenv() | ||
|
||
|
||
async def entrypoint(ctx: JobContext): | ||
initial_ctx = llm.ChatContext().append( | ||
role="system", | ||
text=( | ||
"You are a voice assistant created by LiveKit. Your interface with users will be voice. " | ||
"You should use short and concise responses, and avoiding usage of unpronouncable punctuation." | ||
), | ||
) | ||
|
||
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY) | ||
|
||
assistant = VoiceAssistant( | ||
vad=silero.VAD.load(), | ||
stt=deepgram.STT(), | ||
llm=openai.LLM(), | ||
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)) | ||
|
||
log_queue = asyncio.Queue() | ||
|
||
@assistant.on("user_speech_committed") | ||
def on_user_speech_committed(msg: llm.ChatMessage): | ||
# convert string lists to strings, drop images | ||
if isinstance(msg.content, list): | ||
msg.content = "\n".join( | ||
"[image]" if isinstance(x, llm.ChatImage) else x for x in msg | ||
) | ||
log_queue.put_nowait(f"[{datetime.now()}] USER:\n{msg.content}\n\n") | ||
|
||
@assistant.on("agent_speech_committed") | ||
def on_agent_speech_committed(msg: llm.ChatMessage): | ||
log_queue.put_nowait(f"[{datetime.now()}] AGENT:\n{msg.content}\n\n") | ||
|
||
async def write_transcription(): | ||
async with open("transcriptions.log", "w") as f: | ||
while True: | ||
nbsp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
msg = await log_queue.get() | ||
if msg is None: | ||
break | ||
await f.write(msg) | ||
|
||
write_task = asyncio.create_task(write_transcription()) | ||
|
||
async def finish_queue(): | ||
log_queue.put_nowait(None) | ||
await write_task | ||
|
||
ctx.add_shutdown_callback(finish_queue) | ||
|
||
await assistant.say("Hey, how can I help you today?", allow_interruptions=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: rename the file to save_transcriptions? or save_chatctx?