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

examples: document how to log chats #685

Merged
merged 11 commits into from
Sep 2, 2024
Merged

examples: document how to log chats #685

merged 11 commits into from
Sep 2, 2024

Conversation

nbsp
Copy link
Member

@nbsp nbsp commented Aug 29, 2024

mostly copying the minimal assistant example, with this section added:

async def log_context():
    with open("transcriptions.log", "w") as f:
        chat_ctx = assistant.chat_ctx.copy()
        for msg in chat_ctx.messages:
            f.write(msg.role + "\n")
            f.write(msg.content + "\n\n")

ctx.add_shutdown_callback(log_context)

@nbsp nbsp requested review from davidzhao and theomonnom August 29, 2024 20:42
Copy link

changeset-bot bot commented Aug 29, 2024

⚠️ No Changeset found

Latest commit: 5a5d52f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

msg.content = "\n".join(
"[image]" if isinstance(x, llm.ChatImage) else x for x in msg
)
with open("transcriptions.log", "a+") as f:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's avoid blocking ops.
we could also just get the assistant.chat_ctx in a shutdown callback and write everything at once?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I think we should not assume that everything will be buffered. it's better to demonstrate control where user may choose how to buffer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so should we have both a shutdown log and a real-time log?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example, what if I'd like to send transcriptions to my backend service on the fly to log? (in order to improve latency and minimize risk of losing data).

for really long sessions (think 2h+) we also don't want to buffer everything in memory.

ideally we'd also expose timing data (by emitting them in realtime, we can basically avoid having to tag timestamps)

additionally, some users may use summarization techniques with the LLM, so the chatcontext isn't an accurate representation of each sentence being said by the user/agent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new commit reverts the changes, going back to realtime, and also makes it non-blocking.
i also print the current datetime in the header for each message.

@nbsp nbsp requested a review from theomonnom August 29, 2024 21:09
await f.write(f"[{datetime.now()}] USER:\n{msg.content}\n\n")

async def log_agent_speech(msg: llm.ChatMessage):
async with open("transcriptions.log", "a+") as f:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using a queue here instead of opening the file each time?

IMO this would be a cleaner design:

  • log_user_speech and log_agent_speech push a dict with details to a asyncio.Queue
  • spin up only a single task to write to file
  • the task should flush periodically and close itself when disconnected
  • we can avoid creating separate tasks for these callbacks due to synchronization issues (i.e. if both things write at the same time, chatlog is corrupted)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, but this isn't production code. it shows that the agent and user texts are reachable like this and then you can do whatever with them: log to a file, append to a database, forward to somewhere else, etc. minimal_assistant isn't a demo you're gonna run on prod, having a lot of syscalls doesn't matter here imo

Copy link
Member Author

@nbsp nbsp Aug 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this now pushes to a queue, which is being read from continuously in a non-blocking way. when the room closes so does the queue.


@ctx.room.on("disconnected")
def on_disconnected(*args):
log_queue.task_done()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

task_done is doing nothing on its own. This is only relevant when using queue.join().

I would also prefer to use ctx.add_shutdown_callback instead of room disconnect

@nbsp nbsp force-pushed the nbsp/example/log branch from 87b966a to f255a56 Compare August 30, 2024 22:59
Copy link
Member

@davidzhao davidzhao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lg

Copy link
Member

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?

@nbsp nbsp merged commit 17a1e27 into main Sep 2, 2024
8 checks passed
@nbsp nbsp deleted the nbsp/example/log branch September 2, 2024 04:37
donnyyung added a commit to okolabs/livekit-agents that referenced this pull request Sep 26, 2024
* Fix deepgram English check (livekit#625)

* Cartesia bump to 0.4.0 (livekit#624)

* Introduce manual package release (livekit#626)

* Use the correct working directory in the manual publish job (livekit#627)

* Modified RAG plugin (livekit#629)

Co-authored-by: Théo Monnom <theo.monnom@outlook.com>

* Revert "nltk: fix broken punkt download" (livekit#630)

* Expose WorkerType explicitly (livekit#632)

* openai: allow sending user IDs (livekit#633)

* silero: fix vad padding & choppy audio  (livekit#631)

* ipc: use our own duplex instead of mp.Queue (livekit#634)

* llm: fix optional arguments & non-hashable list (livekit#637)

* Add agent_name to WorkerOptions (livekit#636)

* Support OpenAI Assistants API (livekit#601)

* voiceassistant: fix will_synthesize_assistant_reply race (livekit#638)

* silero: adjust vad activation threshold (livekit#639)

* Version Packages (livekit#615)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* voiceassistant: fix llm not having the full chat context on bad interruption timing (livekit#640)

* livekit-plugins-browser: handle mouse/keyboard inputs on devmode  (livekit#644)

* nltk: fix another semver break (livekit#647)

* livekit-plugins-browser: python API (livekit#645)

* Delete test.py (livekit#652)

* livekit-plugins-browser: prepare for release (livekit#653)

* Version Packages (livekit#641)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Revert "Version Packages" (livekit#659)

* fix release workflow (livekit#661)

* Version Packages (livekit#660)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Add ServerMessage.termination handler (livekit#635)

Co-authored-by: Théo Monnom <theo.8bits@gmail.com>

* Introduce anthropic plugin (livekit#655)

* fix uninitialized SpeechHandle error on interruption  (livekit#665)

* voiceassistant: avoid stacking assistant replies when allow_interruptions=False (livekit#667)

* fix: disconnect event may now have some arguments  (livekit#668)

* Anthropic requires the first message to be a non empty 'user' role (livekit#669)

* support clova speech (livekit#439)

* Updated readme with LLM options (livekit#671)

* Update README.md (livekit#666)

* plugins: add docstrings explaining API keys (livekit#672)

* Disable anthropic test due to 429s (livekit#675)

* Remove duplicate entry from plugin table (livekit#673)

* Version Packages (livekit#662)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* deepgram: switch the default model to phonecall (livekit#676)

* update livekit to 0.14.0 and await tracksubscribed (livekit#678)

* Fix Google STT exception when no valid speech is recognized (livekit#680)

* Introduce easy api for starting tasks for remote participants (livekit#679)

* examples: document how to log chats (livekit#685)

* Version Packages (livekit#677)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* voiceassistant: keep punctuations when sending agent transcription (livekit#648)

* Pass context into participant entrypoint (livekit#694)

* Version Packages (livekit#693)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Update examples to use participant_entrypoint (livekit#695)

* voiceassistant: add VoiceAssistantState (livekit#654)

Co-authored-by: Théo Monnom <theo.8bits@gmail.com>

* Fix anthropic package publishing (livekit#701)

* fix non pickleable log (livekit#691)

* Revert "Update examples to use participant_entrypoint" (livekit#702)

* google-tts: ignore wav header (livekit#703)

* fix examples (livekit#704)

* skip processing of choice.delta when it is None (livekit#705)

* delete duplicate code (livekit#707)

* voiceassistant: skip speech initialization if interrupted  (livekit#715)

* Ensure room.name is available before connection (livekit#716)

* Add deepseek LLMs at OpenAI plugin (livekit#714)

* add threaded job runners (livekit#684)

* voiceassistant: add before_tts_cb callback (livekit#706)

* voiceassistant: fix mark_audio_segment_end with no audio data (livekit#719)

* add JobContext.wait_for_participant (livekit#712)

* Enable Google TTS with application default credentials (livekit#721)

* improve gracefully_cancel logic (livekit#720)

* bump required livekit version to 0.15.2 (livekit#722)

* elevenlabs: expose enable_ssml_parsing (livekit#723)

* Version Packages (livekit#697)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* release anthropic (livekit#724)

* Version Packages (livekit#725)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Update examples to use wait_for_participant (livekit#726)

Co-authored-by: Théo Monnom <theo.8bits@gmail.com>

* Introduce function calling to OpenAI Assistants (livekit#710)

Co-authored-by: Théo Monnom <theo.8bits@gmail.com>

* tts_forwarder: don't raise inside mark_{audio,text}_segment_end when nothing was pushed (livekit#730)

* Add Cerebras to OpenAI Plugin (livekit#731)

* Fixes to Anthropic Function Calling (livekit#708)

* ci: don't run tests on forks (livekit#739)

* Only send actual audio to Deepgram (livekit#738)

* Add support for cartesia voice control (livekit#740)

Co-authored-by: Théo Monnom <theo.8bits@gmail.com>

* Version Packages (livekit#727)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Allow setting LLM temperature with VoiceAssistant (livekit#741)

* Update STT sample README (livekit#709)

* avoid returning tiny frames from TTS (livekit#747)

* run tests on main (and make skipping clearer) (livekit#748)

* voiceassistant: avoid tiny frames on playout (livekit#750)

* limit concurrent process init to 1 (livekit#751)

* windows: default to threaded executor & fix dev mode  (livekit#755)

* improve graceful shutdown  (livekit#756)

* better dev defaults (livekit#762)

* 11labs: send phoneme in one entire xml chunk (livekit#766)

* ipc: fix process not starting if num_idle_processes is zero (livekit#763)

* limit noisy logs & keep the root logger info (livekit#768)

* use os.exit to exit forcefully  (livekit#770)

* Fix Assistant API Vision Capabilities (livekit#771)

* voiceassistant: allow to cancel llm generation inside before_llm_cb (livekit#753)

* Remove useless logs (livekit#773)

* voiceassistant: expose min_endpointing_delay (livekit#752)

* Add typing-extensions as a dependency (livekit#778)

* rename voice_assistant.state to agent.state (livekit#772)

Co-authored-by: aoife cassidy <aoife@livekit.io>

* bump rtc (livekit#782)

* Version Packages (livekit#744)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* added livekit-plugins-playht text-to-speech (livekit#735)

* Fix function for OpenAI Assistants (livekit#784)

* fix the problem of infinite loop when agent speech is interrupted (livekit#790)

---------

Co-authored-by: David Zhao <dz@livekit.io>
Co-authored-by: Neil Dwyer <neildwyer1991@gmail.com>
Co-authored-by: Alejandro Figar Gutierrez <afigar@me.com>
Co-authored-by: Théo Monnom <theo.monnom@outlook.com>
Co-authored-by: Théo Monnom <theo.8bits@gmail.com>
Co-authored-by: aoife cassidy <aoife@livekit.io>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: josephkieu <168809198+josephkieu@users.noreply.github.com>
Co-authored-by: Mehadi Hasan Menon <104126711+mehadi92@users.noreply.github.com>
Co-authored-by: lukasIO <mail@lukasseiler.de>
Co-authored-by: xsg22 <111886011+xsg22@users.noreply.github.com>
Co-authored-by: Yuan He <183649+lenage@users.noreply.github.com>
Co-authored-by: Ryan Sinnet <rsinnet@users.noreply.github.com>
Co-authored-by: Henry Tu <henry@henrytu.me>
Co-authored-by: Ben Cherry <bcherry@gmail.com>
Co-authored-by: Jaydev <jaydevjadav.015@gmail.com>
Co-authored-by: Jax <anyetiangong@qq.com>
SuJingnan pushed a commit to SuJingnan/agents that referenced this pull request Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants