-
Notifications
You must be signed in to change notification settings - Fork 7
pydantic-ai demos #303
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
Open
itayd
wants to merge
30
commits into
main
Choose a base branch
from
itay/pydantic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
pydantic-ai demos #303
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
0df230b
wip
itayd 7bcc309
wip
itayd 96f8743
wip
itayd 5577d17
wip
itayd afdc60f
wip
itayd 0f55df6
wip
itayd 445a804
wip
itayd 597ab26
wip
itayd 84fe2e2
wip
itayd 4c7b39d
wip
itayd 7b9c5ba
wip
itayd 07d45e4
wip
itayd bf5f87f
work
itayd 99afe2c
work
itayd 13d1281
work
itayd 978a6f5
work
itayd 216ae86
work
itayd bb3b0b3
work
itayd a77d077
work
itayd d1467cf
work
itayd 44ccf3c
work
itayd 3dbc2b1
work
itayd 8185dba
work
itayd 2371234
fix
itayd f7193f7
work
itayd 2b31a6d
work
itayd 79994fc
work
itayd 0b656d4
work
itayd c88fbdd
fix
itayd 6af836d
fix
itayd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| --- | ||
| title: Ask - Slack Q&A Bot | ||
| description: Simple one-shot question answering bot for Slack using Pydantic AI | ||
| integrations: ["slack"] | ||
| categories: ["AI", "Samples"] | ||
| tags: | ||
| [ | ||
| "slack_bot", | ||
| "pydantic_ai", | ||
| "question_answering", | ||
| "stateless", | ||
| "single_turn", | ||
| ] | ||
| --- | ||
|
|
||
| # Ask - Slack Q&A Bot 🤖 | ||
|
|
||
| [](https://app.autokitteh.cloud/template?template-name=pydantic-ask) | ||
|
|
||
| A simple Slack bot that answers questions using Pydantic AI. Send a message starting with `!ask` and get an instant AI-powered response in a thread. Each question is treated independently without conversation history. | ||
|
|
||
| ### What AutoKitteh Provides | ||
|
|
||
| - **Slack Integration**: Seamless Slack event handling and message posting | ||
| - **Event Filtering**: Automatic routing of `!ask` commands to the handler | ||
| - **Connection Management**: Secure credential storage for Slack and AI providers | ||
| - **Reliable Execution**: Guaranteed message processing and response delivery | ||
|
|
||
| ## Features | ||
|
|
||
| - **One-Shot Q&A**: Each question is independent with no conversation memory | ||
| - **Threaded Responses**: Replies appear as threads to keep channels clean | ||
| - **Configurable Models**: Switch between any Pydantic AI-compatible model | ||
| - **Simple Integration**: Just add the bot to your Slack workspace and start asking | ||
|
|
||
| ## How It Works | ||
|
|
||
| 1. **User posts message**: `!ask What is the capital of France?` | ||
| 2. **AutoKitteh triggers**: Filters and routes the message to the handler | ||
| 3. **AI processes**: Pydantic AI agent generates a concise response | ||
| 4. **Bot replies**: Response appears in a thread attached to the original message | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| Slack Message (!ask) → AutoKitteh Event Trigger → Pydantic AI Agent → Slack Thread Reply | ||
| ``` | ||
|
|
||
| ### Key Components | ||
|
|
||
| - **`handlers.py`**: Event handler that processes Slack messages and generates AI responses | ||
| - **`autokitteh.yaml`**: AutoKitteh project configuration with Slack trigger and event filter | ||
|
|
||
| ## Setup | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| - AutoKitteh account (cloud or self-hosted) | ||
| - Slack workspace where you can install apps | ||
| - API key for your chosen AI provider (Anthropic, OpenAI, etc.) | ||
|
|
||
| ### Installation | ||
|
|
||
| 1. Start using AutoKitteh Cloud: | ||
|
|
||
| [](https://app.autokitteh.cloud/template?template-name=pydantic-ask) | ||
|
|
||
| 2. Configure your API key: | ||
| - Navigate to the project configuration in AutoKitteh UI | ||
| - Go to the **Variables** tab | ||
| - Set `ANTHROPIC_API_KEY` (or your provider's key) | ||
| - Optionally change `MODEL_NAME` (default: `anthropic:claude-sonnet-4-0`) | ||
|
|
||
| 3. Initialize the Slack connection: | ||
| - Go to the **Connections** tab | ||
| - Initialize the `slack` connection | ||
| - Follow the OAuth flow to authorize your Slack workspace | ||
|
|
||
| 4. Invite the bot to a channel: | ||
| - In Slack, type `/invite @AutoKitteh` in any channel | ||
| - Or add it from the channel details | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Asking Questions | ||
|
|
||
| Simply type `!ask` followed by your question in any channel where the bot is present: | ||
|
|
||
| ``` | ||
| !ask What is the speed of light? | ||
| !ask Explain quantum entanglement in simple terms | ||
| !ask Who wrote "The Great Gatsby"? | ||
| ``` | ||
|
|
||
| The bot will respond in a thread with a concise answer. | ||
|
|
||
| ### Changing the AI Model | ||
|
|
||
| Update the `MODEL_NAME` variable in `autokitteh.yaml` or in the AutoKitteh UI: | ||
|
|
||
| ```yaml | ||
| vars: | ||
| - name: MODEL_NAME | ||
| value: "anthropic:claude-sonnet-4-0" # or "openai:gpt-4o", etc. | ||
| ``` | ||
|
|
||
| Supported model formats: | ||
| - Anthropic: `anthropic:claude-sonnet-4-0` | ||
| - OpenAI: `openai:gpt-4o` | ||
| - Any other Pydantic AI-compatible provider | ||
|
|
||
| ## Technical Details | ||
|
|
||
| ### Pydantic AI Integration | ||
|
|
||
| The project uses [Pydantic AI](https://ai.pydantic.dev/) for AI interactions: | ||
|
|
||
| - **Stateless Agent**: No conversation history - each question is independent | ||
| - **Concise Instructions**: Agent is configured to respond in one sentence | ||
| - **Synchronous Processing**: Uses `run_sync()` for simple request/response flow | ||
|
|
||
| ### Event Filtering | ||
|
|
||
| The AutoKitteh trigger uses a filter to only process relevant messages: | ||
|
|
||
| ```python | ||
| filter: "data.thread_ts == '' && data.text.startsWith('!ask')" | ||
| ``` | ||
|
|
||
| This ensures: | ||
| - Only top-level messages are processed (not thread replies) | ||
| - Only messages starting with `!ask` trigger the bot | ||
|
|
||
| ### Response Format | ||
|
|
||
| Responses are posted as threaded replies with the format: | ||
|
|
||
| ``` | ||
| `<model-name>` says: | ||
| ```<answer>``` | ||
| ``` | ||
|
|
||
| This keeps the main channel clean while providing context about which model generated the response. | ||
|
|
||
| ## Comparison with Other Samples | ||
|
|
||
| | Sample | Conversation History | UI | Use Case | | ||
| |--------|---------------------|-----|----------| | ||
| | **ask** | ❌ No | Slack | Quick one-off questions | | ||
| | **chat** | ✅ Yes | Slack | Multi-turn conversations | | ||
| | **chat_with_ui** | ✅ Yes | Web | Browser-based chat | | ||
| | **gamble** | ✅ Yes | Slack | Interactive AI games | | ||
|
|
||
| ## Development | ||
|
|
||
| Run type checking and validation locally: | ||
|
|
||
| ```bash | ||
| ak make | ||
| ``` | ||
|
|
||
| Deploy from the command line: | ||
|
|
||
| 1. **Install the CLI**: https://docs.autokitteh.com/get_started/install | ||
| 2. **Authenticate**: `ak auth login` | ||
| 3. **Deploy**: `ak deploy` | ||
| 4. **Initialize connections**: Log in to https://autokitteh.cloud and set up Slack | ||
| 5. **Start asking**: Use `!ask` in Slack | ||
|
|
||
| ## Customization Ideas | ||
|
|
||
| - **Change response style**: Modify the agent's instructions for different tones | ||
| - **Add context**: Pass additional information to the agent (company docs, user data, etc.) | ||
| - **Multi-language**: Detect language and respond accordingly | ||
| - **Rate limiting**: Track usage per user or channel | ||
| - **Custom commands**: Add more `!` commands for different behaviors | ||
|
|
||
| ## Known Limitations | ||
|
|
||
| - No conversation memory across questions | ||
| - Single sentence responses (by design) | ||
| - Requires the bot to be in the channel | ||
| - No support for direct messages (DMs) - only channels |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| version: v2 | ||
|
|
||
| project: | ||
| name: pydantic_ask | ||
|
|
||
| vars: | ||
| - name: ANTHROPIC_API_KEY | ||
| value: "" | ||
| secret: true | ||
| - name: MODEL_NAME | ||
| value: "claude-sonnet-4-0" | ||
|
|
||
| connections: | ||
| - name: slack | ||
| integration: slack | ||
| - name: anthropic | ||
| integration: anthropic | ||
|
|
||
| triggers: | ||
| - name: slack_message | ||
| connection: slack | ||
| event_type: message | ||
| filter: "data.thread_ts == '' && data.text.startsWith('!ask')" | ||
| call: handlers.py:on_slack_message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """Slack Q&A Bot - Simple one-shot question answering using Pydantic AI | ||
|
|
||
| This module implements a stateless Slack bot that answers questions using AI. | ||
| Each message is treated independently without conversation history. | ||
|
|
||
| Architecture: | ||
| Slack Message (!ask) → AutoKitteh Trigger → Pydantic AI Agent → Slack Reply | ||
|
|
||
| Key Features: | ||
| - One-shot Q&A (no conversation history) | ||
| - Configurable AI model via environment variable | ||
| - Threaded replies in Slack | ||
| - Synchronous processing | ||
| """ | ||
|
|
||
| from os import getenv | ||
|
|
||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.anthropic import AnthropicModel | ||
|
|
||
| from autokitteh import Event | ||
| from autokitteh.pydantic import anthropic_pydantic_ai_provider | ||
| from autokitteh.slack import slack_client | ||
|
|
||
|
|
||
| # Initialize Slack client using AutoKitteh connection | ||
| _slack = slack_client("slack") | ||
|
|
||
| # AI model configuration | ||
| _MODEL_NAME = getenv("MODEL_NAME", "claude-sonnet-4-0") | ||
|
|
||
| # Create Anthropic model with AutoKitteh provider | ||
| model = AnthropicModel( | ||
| _MODEL_NAME, | ||
| provider=anthropic_pydantic_ai_provider("anthropic") | ||
| ) | ||
|
|
||
| # Create Pydantic AI agent with the configured model | ||
| # This agent is stateless - each run is independent | ||
| agent = Agent( | ||
| model=model, | ||
| instructions="Be concise, reply with one sentence.", | ||
| ) | ||
|
|
||
|
|
||
| def on_slack_message(event: Event) -> None: | ||
| """Handle incoming Slack messages starting with !ask command. | ||
|
|
||
| Triggered by Slack messages matching the filter in autokitteh.yaml: | ||
| - Must not be in a thread (thread_ts == '') | ||
| - Must start with '!ask' | ||
|
|
||
| Args: | ||
| event: AutoKitteh event containing Slack message data | ||
|
|
||
| Process: | ||
| 1. Extract question text (remove !ask prefix) | ||
| 2. Send to Pydantic AI agent for processing | ||
| 3. Post response back to Slack in a thread | ||
|
|
||
| Note: | ||
| No conversation history is maintained - each question is independent. | ||
| """ | ||
| data = event.data | ||
| # Remove the !ask command prefix and any leading/trailing whitespace | ||
| q = data.text.removeprefix("!ask").strip() | ||
|
|
||
| print(f"Q: {q}") | ||
|
|
||
| # Run agent synchronously - no message history passed | ||
| a = agent.run_sync(q).output | ||
|
|
||
| print(f"A: {a}") | ||
|
|
||
| # Post response in a thread attached to the original message | ||
| _slack.chat_postMessage( | ||
| channel=event.data.channel, | ||
| text=f"```{a}```", | ||
| thread_ts=event.data.ts, # Creates a thread reply | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.