Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions examples/chat/enterprise/aws-bedrock-anthropic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
# To get started, follow the instructions at https://aws.amazon.com/bedrock/claude/
# as well as https://github.com/anthropics/anthropic-sdk-python#aws-bedrock
# ------------------------------------------------------------------------------------
from pathlib import Path

from anthropic import AnthropicBedrock
from dotenv import load_dotenv

from shiny.express import ui

# In Shiny Core, do `from app_utils import load_dotenv`
from .app_utils import load_dotenv

# Either explicitly set the AWS environment variables before launching the app, or set
# them in a file named `.env`. The `python-dotenv` package will load `.env` as
# environment variables which can be read by `os.getenv()`.
_ = load_dotenv(Path(__file__).parent / ".env")

load_dotenv()
llm = AnthropicBedrock(
# aws_secret_key=os.getenv("AWS_SECRET_KEY"),
# aws_access_key=os.getenv("AWS_ACCESS_KEY"),
Expand Down
25 changes: 25 additions & 0 deletions examples/chat/enterprise/aws-bedrock-anthropic/app_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
from pathlib import Path

app_dir = Path(__file__).parent
env_file = app_dir / ".env"


def load_dotenv(dotenv_path: os.PathLike[str] = env_file, **kwargs) -> None:
"""
A convenience wrapper around `dotenv.load_dotenv` that warns if `dotenv` is not installed.
It also returns `None` to make it easier to ignore the return value.
"""
try:
import dotenv

dotenv.load_dotenv(dotenv_path=dotenv_path, **kwargs)
except ImportError:
import warnings

warnings.warn(
"Could not import `dotenv`. If you want to use `.env` files to "
"load environment variables, please install it using "
"`pip install python-dotenv`.",
stacklevel=2,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shiny
python-dotenv
tokenizers
anthropic
5 changes: 2 additions & 3 deletions examples/chat/enterprise/azure-openai/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
# To get setup, follow the instructions at https://learn.microsoft.com/en-us/azure/ai-services/openai/quickstart?tabs=command-line%2Cpython-new&pivots=programming-language-python#create-a-new-python-application
# ------------------------------------------------------------------------------------
import os
from pathlib import Path

import dotenv
from app_utils import load_dotenv
from openai import AzureOpenAI

from shiny.express import ui
Expand All @@ -15,7 +14,7 @@
# variables before launching the app, or set them in a file named `.env`. The
# `python-dotenv` package will load `.env` as environment variables which can later be
# read by `os.getenv()`.
dotenv.load_dotenv(Path(__file__).parent / ".env")
load_dotenv()

llm = AzureOpenAI(
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
Expand Down
25 changes: 25 additions & 0 deletions examples/chat/enterprise/azure-openai/app_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
from pathlib import Path

app_dir = Path(__file__).parent
env_file = app_dir / ".env"


def load_dotenv(dotenv_path: os.PathLike[str] = env_file, **kwargs) -> None:
"""
A convenience wrapper around `dotenv.load_dotenv` that warns if `dotenv` is not installed.
It also returns `None` to make it easier to ignore the return value.
"""
try:
import dotenv

dotenv.load_dotenv(dotenv_path=dotenv_path, **kwargs)
except ImportError:
import warnings

warnings.warn(
"Could not import `dotenv`. If you want to use `.env` files to "
"load environment variables, please install it using "
"`pip install python-dotenv`.",
stacklevel=2,
)
4 changes: 4 additions & 0 deletions examples/chat/enterprise/azure-openai/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shiny
python-dotenv
tokenizers
openai
6 changes: 2 additions & 4 deletions examples/chat/hello-providers/anthropic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
# To get one, follow the instructions at https://docs.anthropic.com/en/api/getting-started
# ------------------------------------------------------------------------------------
import os
from pathlib import Path

from anthropic import AsyncAnthropic
from dotenv import load_dotenv
from app_utils import load_dotenv

from shiny.express import ui

# Either explicitly set the ANTHROPIC_API_KEY environment variable before launching the
# app, or set them in a file named `.env`. The `python-dotenv` package will load `.env`
# as environment variables which can later be read by `os.getenv()`.
_ = load_dotenv(Path(__file__).parent / ".env")

load_dotenv()
llm = AsyncAnthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))

# Set some Shiny page options
Expand Down
25 changes: 25 additions & 0 deletions examples/chat/hello-providers/anthropic/app_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
from pathlib import Path

app_dir = Path(__file__).parent
env_file = app_dir / ".env"


def load_dotenv(dotenv_path: os.PathLike[str] = env_file, **kwargs) -> None:
"""
A convenience wrapper around `dotenv.load_dotenv` that warns if `dotenv` is not installed.
It also returns `None` to make it easier to ignore the return value.
"""
try:
import dotenv

dotenv.load_dotenv(dotenv_path=dotenv_path, **kwargs)
except ImportError:
import warnings

warnings.warn(
"Could not import `dotenv`. If you want to use `.env` files to "
"load environment variables, please install it using "
"`pip install python-dotenv`.",
stacklevel=2,
)
4 changes: 4 additions & 0 deletions examples/chat/hello-providers/anthropic/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shiny
python-dotenv
tokenizers
anthropic
8 changes: 2 additions & 6 deletions examples/chat/hello-providers/gemini/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
# To run it, you'll need a Google API key.
# To get one, follow the instructions at https://ai.google.dev/gemini-api/docs/get-started/tutorial?lang=python
# ------------------------------------------------------------------------------------

from pathlib import Path

from dotenv import load_dotenv
from app_utils import load_dotenv
from google.generativeai import GenerativeModel

from shiny.express import ui

# Either explicitly set the GOOGLE_API_KEY environment variable before launching the
# app, or set them in a file named `.env`. The `python-dotenv` package will load `.env`
# as environment variables which can later be read by `os.getenv()`.
_ = load_dotenv(Path(__file__).parent / ".env")

load_dotenv()
llm = GenerativeModel()

# Set some Shiny page options
Expand Down
25 changes: 25 additions & 0 deletions examples/chat/hello-providers/gemini/app_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
from pathlib import Path

app_dir = Path(__file__).parent
env_file = app_dir / ".env"


def load_dotenv(dotenv_path: os.PathLike[str] = env_file, **kwargs) -> None:
"""
A convenience wrapper around `dotenv.load_dotenv` that warns if `dotenv` is not installed.
It also returns `None` to make it easier to ignore the return value.
"""
try:
import dotenv

dotenv.load_dotenv(dotenv_path=dotenv_path, **kwargs)
except ImportError:
import warnings

warnings.warn(
"Could not import `dotenv`. If you want to use `.env` files to "
"load environment variables, please install it using "
"`pip install python-dotenv`.",
stacklevel=2,
)
4 changes: 4 additions & 0 deletions examples/chat/hello-providers/gemini/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shiny
python-dotenv
tokenizers
google-generativeai
8 changes: 3 additions & 5 deletions examples/chat/hello-providers/langchain/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
# To use other providers/models via LangChain, see https://python.langchain.com/v0.1/docs/modules/model_io/chat/quick_start/
# ------------------------------------------------------------------------------------
import os
from pathlib import Path

from dotenv import load_dotenv
from app_utils import load_dotenv
from langchain_openai import ChatOpenAI

from shiny.express import ui

# Either explicitly set the OPENAI_API_KEY environment variable before launching the
# app, or set them in a file named `.env`. The `python-dotenv` package will load `.env`
# as environment variables which can later be read by `os.getenv()`.
_ = load_dotenv(Path(__file__).parent / ".env")

load_dotenv()
llm = ChatOpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

# Set some Shiny page options
Expand All @@ -37,6 +35,6 @@ async def _():
# Get messages currently in the chat
messages = chat.messages()
# Create a response message stream
response = llm.astream(messages)
response = llm.astream(messages) # type: ignore
# Append the response stream into the chat
await chat.append_message_stream(response)
25 changes: 25 additions & 0 deletions examples/chat/hello-providers/langchain/app_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
from pathlib import Path

app_dir = Path(__file__).parent
env_file = app_dir / ".env"


def load_dotenv(dotenv_path: os.PathLike[str] = env_file, **kwargs) -> None:
"""
A convenience wrapper around `dotenv.load_dotenv` that warns if `dotenv` is not installed.
It also returns `None` to make it easier to ignore the return value.
"""
try:
import dotenv

dotenv.load_dotenv(dotenv_path=dotenv_path, **kwargs)
except ImportError:
import warnings

warnings.warn(
"Could not import `dotenv`. If you want to use `.env` files to "
"load environment variables, please install it using "
"`pip install python-dotenv`.",
stacklevel=2,
)
4 changes: 4 additions & 0 deletions examples/chat/hello-providers/langchain/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shiny
python-dotenv
tokenizers
langchain-openai
3 changes: 3 additions & 0 deletions examples/chat/hello-providers/ollama/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
shiny
tokenizers
ollama
6 changes: 2 additions & 4 deletions examples/chat/hello-providers/openai/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
# To get setup, follow the instructions at https://platform.openai.com/docs/quickstart
# ------------------------------------------------------------------------------------
import os
from pathlib import Path

from dotenv import load_dotenv
from app_utils import load_dotenv
from openai import AsyncOpenAI

from shiny.express import ui

# Either explicitly set the OPENAI_API_KEY environment variable before launching the
# app, or set them in a file named `.env`. The `python-dotenv` package will load `.env`
# as environment variables which can later be read by `os.getenv()`.
_ = load_dotenv(Path(__file__).parent / ".env")

load_dotenv()
llm = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

# Set some Shiny page options
Expand Down
25 changes: 25 additions & 0 deletions examples/chat/hello-providers/openai/app_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
from pathlib import Path

app_dir = Path(__file__).parent
env_file = app_dir / ".env"


def load_dotenv(dotenv_path: os.PathLike[str] = env_file, **kwargs) -> None:
"""
A convenience wrapper around `dotenv.load_dotenv` that warns if `dotenv` is not installed.
It also returns `None` to make it easier to ignore the return value.
"""
try:
import dotenv

dotenv.load_dotenv(dotenv_path=dotenv_path, **kwargs)
except ImportError:
import warnings

warnings.warn(
"Could not import `dotenv`. If you want to use `.env` files to "
"load environment variables, please install it using "
"`pip install python-dotenv`.",
stacklevel=2,
)
4 changes: 4 additions & 0 deletions examples/chat/hello-providers/openai/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shiny
python-dotenv
tokenizers
openai
3 changes: 2 additions & 1 deletion pyrightconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"reportImportCycles": "none",
"reportUnusedFunction": "none",
"reportPrivateUsage": "none",
"reportUnnecessaryIsInstance": "none"
"reportUnnecessaryIsInstance": "none",
"executionEnvironments": [{ "root": "examples/" }]
}