Skip to content

Commit bda4010

Browse files
committed
Merge 'origin/main' into branch feat/create-template-json
2 parents 7ba15cc + 6e7c468 commit bda4010

File tree

2 files changed

+45
-1
lines changed
  • examples/chat

2 files changed

+45
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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)

examples/chat/playground/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"openai": ["gpt-4o", "gpt-3.5-turbo"],
1717
"claude": [
1818
"claude-3-opus-20240229",
19-
"claude-3-sonnet-20240229",
19+
"claude-3-5-sonnet-20240620",
2020
"claude-3-haiku-20240307",
2121
],
2222
"google": ["gemini-1.5-pro-latest"],

0 commit comments

Comments
 (0)