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

Add Streamlit-based Weather Agent Example #827

Conversation

rmaceissoft
Copy link

@rmaceissoft rmaceissoft commented Jan 31, 2025

Solves: #826

Includes:

  • Added a new Streamlit example script: examples/pydantic_ai_examples/weather_agent_streamlit.py
  • Updated the pyproject.toml in the pydantic-ai-examples workspace to include the Streamlit dependency.
  • Modified weather_agent.py to postpone the model instance creation, allowing the model's api_key to be passed when running the agent instead of relying on the environment variable. (This change is necessary because there is currently no way to pass the model API key when running an agent.)

How to test:

  • Launch the streamlit app by running the following command (make sure you install streamlit before)
uv run streamlit run examples/pydantic_ai_examples/weather_agent_streamlit.py
  • You can also set default values via env variables:
MODEL_API_KEY=<KEY> WEATHER_API_KEY=<KEY> GEO_API_KEY=<KEY>uv run streamlit run examples/pydantic_ai_examples/weather_agent_streamlit.py

Screengrab:
https://www.loom.com/share/eb96048762a74d7dafd0ef3192c8ecb5

- Add new Streamlit example script `weather_agent_streamlit.py`
- Update pydantic-ai-examples workspace's  `pyproject.toml` to include Streamlit dependency
- Modify `weather_agent.py` to postpone the model instance creation, so model api ket can be passed when running agent
@github-actions github-actions bot temporarily deployed to deploy-preview January 31, 2025 19:42 Inactive
@sydney-runkle
Copy link
Contributor

Cool! Excited to review this later today - thanks for the contribution!

@sydney-runkle sydney-runkle self-assigned this Jan 31, 2025
@asaf
Copy link

asaf commented Feb 1, 2025

btw you should look at that https://github.com/ishswar/pydantic-ai-weather-agent it generates a widget out of the agent result which demonstrates how content could be visualized as well, maybe you can adapt (unless it makes the sample too complex) :)

@rmaceissoft
Copy link
Author

btw you should look at that https://github.com/ishswar/pydantic-ai-weather-agent it generates a widget out of the agent result which demonstrates how content could be visualized as well, maybe you can adapt (unless it makes the sample too complex) :)

@asaf Thanks for pointing that out! I'll take a look and see what ideas I can incorporate.

Here are a few things I'm planning to add/improve in this Streamlit-based example:

  • Enhance the rendering of model responses:
    • Add a loading indicator before the model response is returned and disable the send button.
    • Allow users to re-send a prompt from any previous message (modifying the chat history from that point).
    • Render tool calls as well.
    • Add a toggle to switch between streamed and non-streamed results.
    • Properly catch model errors and display a corresponding error message in the chat.
  • Allow users to clear chat history via a button in the sidebar.
  • Enable tuning of model settings (at least temperature) from the UI (sidebar).

@sydney-runkle I'd love to hear your thoughts on this.

@sydney-runkle
Copy link
Contributor

Alrighty, I've chatted with the team - we've decided that we don't want to include third party tools like this in our pydantic_ai_examples library because of the significant deps bloat introduced. That being said, we'll add an "external examples" page to the docs that highlights great use cases like this one!

@rmaceissoft
Copy link
Author

@sydney-runkle Thanks for the update! I completely understand the concern about dependency bloat. I'll keep track of the "external examples" page and work on wrapping up my example so it can be included there.

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.

None yet

3 participants