|
| 1 | +# ------------------------------------------------------------------------------------ |
| 2 | +# A basic Shiny Chat example powered by Anthropic's Claude model. |
| 3 | +# To run it, you'll need an Anthropic API key. |
| 4 | +# To get one, follow the instructions at https://docs.anthropic.com/en/api/getting-started |
| 5 | +# ------------------------------------------------------------------------------------ |
| 6 | +import os |
| 7 | + |
| 8 | +from anthropic import AsyncAnthropic |
| 9 | +from app_utils import load_dotenv |
| 10 | + |
| 11 | +from shiny.express import ui |
| 12 | + |
| 13 | +# Either explicitly set the ANTHROPIC_API_KEY environment variable before launching the |
| 14 | +# app, or set them in a file named `.env`. The `python-dotenv` package will load `.env` |
| 15 | +# as environment variables which can later be read by `os.getenv()`. |
| 16 | +load_dotenv() |
| 17 | +llm = AsyncAnthropic(api_key=os.environ.get("ANTHROPIC_API_KEY")) |
| 18 | + |
| 19 | +# Set some Shiny page options |
| 20 | +ui.page_opts( |
| 21 | + title="Hello Anthropic Claude Chat", |
| 22 | + fillable=True, |
| 23 | + fillable_mobile=True, |
| 24 | +) |
| 25 | + |
| 26 | +# Create and display empty chat |
| 27 | +chat = ui.Chat(id="chat") |
| 28 | +chat.ui() |
| 29 | + |
| 30 | + |
| 31 | +# Define a callback to run when the user submits a message |
| 32 | +@chat.on_user_submit |
| 33 | +async def _(): |
| 34 | + # Get messages currently in the chat |
| 35 | + messages = chat.messages(format="anthropic") |
| 36 | + # Create a response message stream |
| 37 | + response = await llm.messages.create( |
| 38 | + model="claude-3-5-sonnet-20240620", |
| 39 | + messages=messages, |
| 40 | + stream=True, |
| 41 | + max_tokens=1000, |
| 42 | + ) |
| 43 | + # Append the response stream into the chat |
| 44 | + await chat.append_message_stream(response) |
0 commit comments