This message was deleted.
# ask-ai
s
This message was deleted.
t
Thanks for reaching out, Anigilaje. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To get a summary of a conversation before saving it in LiveKit, you can access the session history using the
session.history
property. This provides the entire conversation data, which you can process or summarize as needed. For example, you can use the following approach to save the conversation history:
Copy code
async def write_transcript():
    current_date = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"/tmp/transcript_{ctx.room.name}_{current_date}.json"

    with open(filename, 'w') as f:
        json.dump(session.history.to_dict(), f, indent=2)

    print(f"Transcript for {ctx.room.name} saved to {filename}")

ctx.add_shutdown_callback(write_transcript)
This will save the complete conversation history to a JSON file when the session ends. You can modify this process to generate a summary before saving if needed. Sources: Session recording and transcripts | LiveKit Docs
m
i a alrwdy saving full conversation. but how do i summarizebefore saving ?
t
Hey there! Want to know how to create a quick summary of a conversation? I've got a friendly approach that'll help you out. Let me walk you through a simple method that'll make summarizing chats a breeze! 1. First, grab the full conversation history using
session.history
- it's super easy! 2. Then, you'll want to pull out those messages and send them to a cool summarization tool. OpenAI's GPT models or other NLP libraries work great for this. 3. Generate your summary and save it - you can keep it alongside the full conversation or replace it entirely. Here's a practical example to help you out:
Copy code
from openai import ChatCompletion

async def summarize_conversation(history):
    messages = [item.text_content for item in history.items]
    conversation_text = "\n".join(messages)

    response = ChatCompletion.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "Summarize the following conversation."},
            {"role": "user", "content": conversation_text}
        ]
    )

    summary = response["choices"][0]["message"]["content"]
    return summary

# Example usage
summary = await summarize_conversation(session.history)
print("Summary:", summary)
Pretty cool, right? This approach uses a smart language model to whip up a neat, concise summary of your conversation. Once you've got it, you can easily save the summary to your database or file system. It's like having a helpful assistant that can capture the essence of any chat in just a few words!