| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
| title | Large Context Window LLM | |||||
|---|---|---|---|---|---|---|
| category | pipeline-llm | |||||
| tags |
|
|||||
| difficulty | intermediate | |||||
| description | Agent using Gemini 2.0 Flash to analyze War and Peace with large context window | |||||
| demonstrates |
|
In this recipe you will load a full novel into the LLM context and discuss it with the caller. Gemini 2.0 Flash handles the long prompt; the agent quotes passages on request.
LIVEKIT_URL=your_livekit_url LIVEKIT_API_KEY=your_api_key LIVEKIT_API_SECRET=your_api_secret
pip install "livekit-agents[silero]" python-dotenvLoad environment variables and set up logging.
import logging
from pathlib import Path
from dotenv import load_dotenv
from livekit.agents import JobContext, JobProcess, cli, Agent, AgentSession, AgentServer, inference
from livekit.plugins import silero
load_dotenv()
logger = logging.getLogger("google_llm")
logger.setLevel(logging.INFO)
server = AgentServer()Load the War and Peace text from lib/war_and_peace.txt so it can be embedded directly into the prompt.
book_path = Path(__file__).parent / "lib" / "war_and_peace.txt"
try:
with open(book_path, "r", encoding="utf-8") as f:
war_and_peace_text = f.read()
except FileNotFoundError:
logger.error(f"Could not find book at {book_path}")
war_and_peace_text = "War and Peace text not found."We preload the VAD model. Inside the session, we configure the AgentSession with Deepgram STT, Gemini LLM (for large context), and OpenAI TTS (with custom instructions for tone).
def prewarm(proc: JobProcess):
proc.userdata["vad"] = silero.VAD.load()
server.setup_fnc = prewarm
@server.rtc_session()
async def entrypoint(ctx: JobContext):
session = AgentSession(
stt=deepgram.STT(),
llm=google.LLM(model="gemini-2.5-flash"),
tts=openai.TTS(instructions="You are a literary discussion assistant with a pleasant voice. Speak in a natural, conversational tone that conveys enthusiasm for literature."),
vad=ctx.proc.userdata["vad"],
)
agent = Agent(
instructions=f"""
You are a War and Peace book club assistant. You help users discuss and understand Leo Tolstoy's novel "War and Peace."
You can answer questions about the plot, characters, themes, historical context, and literary analysis of the book.
Here is the complete text of the book that you can reference:
{war_and_peace_text}
Be concise but informative in your responses. If asked about specific passages, quote directly from the text.
""",
)
# ...Greet the user and start the session.
@session.on("session_start")
def on_session_start():
session.generate_reply("Welcome to the War and Peace book club! I'm here to discuss Leo Tolstoy's epic novel with you. What would you like to talk about?")
await session.start(agent=agent, room=ctx.room)
await ctx.connect()
if __name__ == "__main__":
cli.run_app(server)python large_context.py consoleimport logging
from pathlib import Path
from dotenv import load_dotenv
from livekit.agents import JobContext, JobProcess, cli, Agent, AgentSession, AgentServer, inference
from livekit.plugins import openai, google, deepgram, silero
load_dotenv()
logger = logging.getLogger("google_llm")
logger.setLevel(logging.INFO)
# Load book text once
book_path = Path(__file__).parent / "lib" / "war_and_peace.txt"
try:
with open(book_path, "r", encoding="utf-8") as f:
war_and_peace_text = f.read()
except FileNotFoundError:
logger.error(f"Could not find book at {book_path}")
war_and_peace_text = "War and Peace text not found."
server = AgentServer()
def prewarm(proc: JobProcess):
proc.userdata["vad"] = silero.VAD.load()
server.setup_fnc = prewarm
@server.rtc_session()
async def entrypoint(ctx: JobContext):
session = AgentSession(
stt=deepgram.STT(),
llm=google.LLM(model="gemini-2.0-flash"),
tts=openai.TTS(instructions="You are a literary discussion assistant with a pleasant voice. Speak in a natural, conversational tone that conveys enthusiasm for literature."),
vad=ctx.proc.userdata["vad"],
)
agent = Agent(
instructions=f"""
You are a War and Peace book club assistant. You help users discuss and understand Leo Tolstoy's novel "War and Peace."
You can answer questions about the plot, characters, themes, historical context, and literary analysis of the book.
Here is the complete text of the book that you can reference:
{war_and_peace_text}
Be concise but informative in your responses. If asked about specific passages, quote directly from the text.
""",
)
@session.on("session_start")
def on_session_start():
session.generate_reply("Welcome to the War and Peace book club! I'm here to discuss Leo Tolstoy's epic novel with you. What would you like to talk about?")
await session.start(agent=agent, room=ctx.room)
await ctx.connect()
if __name__ == "__main__":
cli.run_app(server)| Back | FazBrowse Home | New Git URL |