Basic Streaming Agent

Code

1from kern.agent import Agent, RunOutputEvent # noqa
2from kern.models.mistral import MistralChat
3
4agent = Agent(
5 model=MistralChat(
6 id="mistral-large-latest",
7 ),
8 markdown=True,
9)
10
11# Get the response in a variable
12# run_response: Iterator[RunOutputEvent] = agent.run("Share a 2 sentence horror story", stream=True)
13# for chunk in run_response:
14# print(chunk.content)
15
16# Print the response in the terminal
17agent.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 MISTRAL_API_KEY=xxx

Install dependencies

1uv pip install -U mistralai kern-ai

Run Agent

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