9201ef759e
Harness Compat / harness compat (push) Failing after 0s
CI / test on 3.12 (standard) (push) Has been cancelled
CI / test on 3.13 (standard) (push) Has been cancelled
CI / test on 3.14 (standard) (push) Has been cancelled
CI / test on 3.10 (all-extras) (push) Has been cancelled
CI / test on 3.11 (all-extras) (push) Has been cancelled
CI / test on 3.12 (all-extras) (push) Has been cancelled
CI / test on 3.14 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.10 (pydantic-evals) (push) Has been cancelled
CI / test on 3.11 (pydantic-evals) (push) Has been cancelled
CI / test on 3.12 (pydantic-evals) (push) Has been cancelled
CI / deploy-docs-preview (push) Has been cancelled
CI / build release artifacts (push) Has been cancelled
CI / publish to PyPI (push) Has been cancelled
CI / Send tweet (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / mypy (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / test on 3.10 (standard) (push) Has been cancelled
CI / test on 3.11 (standard) (push) Has been cancelled
CI / test on 3.13 (all-extras) (push) Has been cancelled
CI / test on 3.14 (all-extras) (push) Has been cancelled
CI / test on 3.10 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.11 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.12 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-evals) (push) Has been cancelled
CI / test on 3.14 (pydantic-evals) (push) Has been cancelled
CI / test on 3.10 (lowest-versions) (push) Has been cancelled
CI / test on 3.11 (lowest-versions) (push) Has been cancelled
CI / test on 3.12 (lowest-versions) (push) Has been cancelled
CI / test on 3.13 (lowest-versions) (push) Has been cancelled
CI / test on 3.14 (lowest-versions) (push) Has been cancelled
CI / test examples on 3.11 (push) Has been cancelled
CI / test examples on 3.12 (push) Has been cancelled
CI / test examples on 3.13 (push) Has been cancelled
CI / test examples on 3.14 (push) Has been cancelled
CI / coverage (push) Has been cancelled
CI / check (push) Has been cancelled
CI / deploy-docs (push) Has been cancelled
41 lines
1.3 KiB
Markdown
41 lines
1.3 KiB
Markdown
# Chat App with FastAPI
|
|
|
|
Simple chat app example build with FastAPI.
|
|
|
|
Demonstrates:
|
|
|
|
* [reusing chat history](../message-history.md)
|
|
* [serializing messages](../message-history.md#accessing-messages-from-results)
|
|
* [streaming responses](../output.md#streamed-results)
|
|
|
|
This demonstrates storing chat history between requests and using it to give the model context for new responses.
|
|
|
|
Most of the complex logic here is between `chat_app.py` which streams the response to the browser,
|
|
and `chat_app.ts` which renders messages in the browser.
|
|
|
|
## Running the Example
|
|
|
|
With [dependencies installed and environment variables set](./setup.md#usage), run:
|
|
|
|
```bash
|
|
python/uv-run -m pydantic_ai_examples.chat_app
|
|
```
|
|
|
|
Then open the app at [localhost:8000](http://localhost:8000).
|
|
|
|

|
|
|
|
## Example Code
|
|
|
|
Python code that runs the chat app:
|
|
|
|
```snippet {path="/examples/pydantic_ai_examples/chat_app.py"}```
|
|
|
|
Simple HTML page to render the app:
|
|
|
|
```snippet {path="/examples/pydantic_ai_examples/chat_app.html"}```
|
|
|
|
TypeScript to handle rendering the messages, to keep this simple (and at the risk of offending frontend developers) the typescript code is passed to the browser as plain text and transpiled in the browser.
|
|
|
|
```snippet {path="/examples/pydantic_ai_examples/chat_app.ts"}```
|