Streaming Agent

Code

1from typing import Iterator # noqa
2
3from kern.agent import Agent, RunOutputEvent # noqa
4from kern.models.deepinfra import DeepInfra # noqa
5
6agent = Agent(
7 model=DeepInfra(id="meta-llama/Llama-2-70b-chat-hf"),
8 markdown=True,
9)
10
11# Get the response in a variable
12run_response: Iterator[RunOutputEvent] = agent.run(
13 "Share a 2 sentence horror story", stream=True
14)
15for chunk in run_response:
16 print(chunk.content)
17
18# Print the response in the terminal
19agent.print_response("Share a 2 sentence horror story", stream=True)

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export DEEPINFRA_API_KEY=xxx

Install dependencies

1uv pip install -U openai kern-ai

Run Agent

1python cookbook/11_models/deepinfra/basic_stream.py