Skip to main content

A reusable Streamlit-based chat interface for Ollama models using PydanticAI

Project description

Streamlit Ollama Agent

A reusable Python package that provides a Streamlit-based chat interface for Ollama models using PydanticAI. This package makes it easy to create chat applications with streaming responses and conversation history.

Features

  • 🚀 Easy-to-use Streamlit chat interface
  • 📝 Streaming responses with real-time updates
  • 💬 Conversation history support
  • 🔄 Compatible with any Ollama model
  • 🛠 Built on PydanticAI for robust type safety
  • ⚡ Dual streaming modes: OpenAI compatibility or direct Ollama

Installation

# From PyPI (once published)
pip install streamlit-ollama-agent

Quick Start

  1. Make sure you have Ollama running locally:
ollama run llama2  # or your preferred model
  1. Create a new Python file (e.g., chat_app.py):
import streamlit as st
from streamlit_ollama_agent import OllamaAgent

# Initialize the agent (using OpenAI compatibility mode)
agent = OllamaAgent(
    model_name="llama2",  # or your preferred model
    base_url="http://localhost:11434/v1"
)

# Or use direct Ollama streaming
agent = OllamaAgent(
    model_name="llama2",
    use_direct_streaming=True  # Bypass OpenAI compatibility for direct Ollama streaming
)

# Create your chat interface
st.title("Chat with Ollama")

# Use the built-in Streamlit app
from streamlit_ollama_agent import create_chat_app
create_chat_app(agent)
  1. Run your app:
streamlit run chat_app.py

Advanced Usage

Streaming Modes

The package supports two streaming modes:

  1. OpenAI Compatibility Mode (default):
    • Uses OpenAI's API format
    • Compatible with other OpenAI-like APIs
    • More standardized approach
agent = OllamaAgent(
    model_name="llama2",
    base_url="http://localhost:11434/v1",  # Ollama's OpenAI-compatible endpoint
    api_key="ollama"  # Not required for Ollama
)
  1. Direct Ollama Streaming:
    • Connects directly to Ollama's native API
    • Potentially faster
    • Access to Ollama-specific features
agent = OllamaAgent(
    model_name="llama2",
    use_direct_streaming=True
)

Custom Chat Interface

You can create your own custom chat interface using the OllamaAgent directly:

import asyncio
from streamlit_ollama_agent import OllamaAgent

agent = OllamaAgent()

async def get_response(prompt, history=None):
    async for chunk in agent.stream_response(prompt, history):
        # Handle each chunk of the response
        yield chunk

Configuration Options

agent = OllamaAgent(
    model_name="your-model",      # Choose your Ollama model
    base_url="your-url",          # Custom Ollama API URL (OpenAI mode)
    api_key="your-key",           # If needed (OpenAI mode)
    use_direct_streaming=False    # Choose streaming mode
)

Project Structure

streamlit-ollama-agent/
├── streamlit_ollama_agent/
│   ├── __init__.py
│   ├── agent.py          # OllamaAgent implementation
│   └── app.py           # Streamlit app implementation
├── setup.py
├── README.md
└── requirements.txt

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_ollama_agent-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_ollama_agent-0.1.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_ollama_agent-0.1.1.tar.gz.

File metadata

  • Download URL: streamlit_ollama_agent-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for streamlit_ollama_agent-0.1.1.tar.gz
Algorithm Hash digest
SHA256 65c0a8eabcb2de5c1b88327ce91e659ba28b070f4ca377f8cdb54acffec561e8
MD5 fbdd940dde2089e4c24de9c06eef10c2
BLAKE2b-256 bff49432001e0a209d8a335cb33314e0fe02b34120a0fbf22c3c972dbff454be

See more details on using hashes here.

File details

Details for the file streamlit_ollama_agent-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_ollama_agent-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 185ffc91f81dec538e9082b248f1f633eb9a4dc65329a210c485aae7223e9e76
MD5 3c10f8c29f3a783c1d42956e3a0f5141
BLAKE2b-256 10a214f961f4dabcd4e6b2272a3c38a2a87b3f77177c2a80d8b2dbbb7e9185d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page