diff --git a/examples/chat/enterprise/aws-bedrock-anthropic/app.py b/examples/chat/enterprise/aws-bedrock-anthropic/app.py index 45de87e1d..8fd54db98 100644 --- a/examples/chat/enterprise/aws-bedrock-anthropic/app.py +++ b/examples/chat/enterprise/aws-bedrock-anthropic/app.py @@ -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"), diff --git a/examples/chat/enterprise/aws-bedrock-anthropic/app_utils.py b/examples/chat/enterprise/aws-bedrock-anthropic/app_utils.py new file mode 100644 index 000000000..6ebad7e8a --- /dev/null +++ b/examples/chat/enterprise/aws-bedrock-anthropic/app_utils.py @@ -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, + ) diff --git a/examples/chat/enterprise/aws-bedrock-anthropic/requirements.txt b/examples/chat/enterprise/aws-bedrock-anthropic/requirements.txt new file mode 100644 index 000000000..fb3b67026 --- /dev/null +++ b/examples/chat/enterprise/aws-bedrock-anthropic/requirements.txt @@ -0,0 +1,4 @@ +shiny +python-dotenv +tokenizers +anthropic diff --git a/examples/chat/enterprise/azure-openai/app.py b/examples/chat/enterprise/azure-openai/app.py index afca25585..9f8d35d38 100644 --- a/examples/chat/enterprise/azure-openai/app.py +++ b/examples/chat/enterprise/azure-openai/app.py @@ -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 @@ -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"), diff --git a/examples/chat/enterprise/azure-openai/app_utils.py b/examples/chat/enterprise/azure-openai/app_utils.py new file mode 100644 index 000000000..6ebad7e8a --- /dev/null +++ b/examples/chat/enterprise/azure-openai/app_utils.py @@ -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, + ) diff --git a/examples/chat/enterprise/azure-openai/requirements.txt b/examples/chat/enterprise/azure-openai/requirements.txt new file mode 100644 index 000000000..6e4a780cf --- /dev/null +++ b/examples/chat/enterprise/azure-openai/requirements.txt @@ -0,0 +1,4 @@ +shiny +python-dotenv +tokenizers +openai diff --git a/examples/chat/hello-providers/anthropic/app.py b/examples/chat/hello-providers/anthropic/app.py index 095010357..b2e09a06a 100644 --- a/examples/chat/hello-providers/anthropic/app.py +++ b/examples/chat/hello-providers/anthropic/app.py @@ -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 diff --git a/examples/chat/hello-providers/anthropic/app_utils.py b/examples/chat/hello-providers/anthropic/app_utils.py new file mode 100644 index 000000000..6ebad7e8a --- /dev/null +++ b/examples/chat/hello-providers/anthropic/app_utils.py @@ -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, + ) diff --git a/examples/chat/hello-providers/anthropic/requirements.txt b/examples/chat/hello-providers/anthropic/requirements.txt new file mode 100644 index 000000000..fb3b67026 --- /dev/null +++ b/examples/chat/hello-providers/anthropic/requirements.txt @@ -0,0 +1,4 @@ +shiny +python-dotenv +tokenizers +anthropic diff --git a/examples/chat/hello-providers/gemini/app.py b/examples/chat/hello-providers/gemini/app.py index f3e32f386..271ecf824 100644 --- a/examples/chat/hello-providers/gemini/app.py +++ b/examples/chat/hello-providers/gemini/app.py @@ -3,10 +3,7 @@ # 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 @@ -14,8 +11,7 @@ # 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 diff --git a/examples/chat/hello-providers/gemini/app_utils.py b/examples/chat/hello-providers/gemini/app_utils.py new file mode 100644 index 000000000..6ebad7e8a --- /dev/null +++ b/examples/chat/hello-providers/gemini/app_utils.py @@ -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, + ) diff --git a/examples/chat/hello-providers/gemini/requirements.txt b/examples/chat/hello-providers/gemini/requirements.txt new file mode 100644 index 000000000..f3e733a88 --- /dev/null +++ b/examples/chat/hello-providers/gemini/requirements.txt @@ -0,0 +1,4 @@ +shiny +python-dotenv +tokenizers +google-generativeai diff --git a/examples/chat/hello-providers/langchain/app.py b/examples/chat/hello-providers/langchain/app.py index b4dfc6595..8d0763ac3 100644 --- a/examples/chat/hello-providers/langchain/app.py +++ b/examples/chat/hello-providers/langchain/app.py @@ -5,9 +5,8 @@ # 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 @@ -15,8 +14,7 @@ # 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 @@ -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) diff --git a/examples/chat/hello-providers/langchain/app_utils.py b/examples/chat/hello-providers/langchain/app_utils.py new file mode 100644 index 000000000..6ebad7e8a --- /dev/null +++ b/examples/chat/hello-providers/langchain/app_utils.py @@ -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, + ) diff --git a/examples/chat/hello-providers/langchain/requirements.txt b/examples/chat/hello-providers/langchain/requirements.txt new file mode 100644 index 000000000..0391632e3 --- /dev/null +++ b/examples/chat/hello-providers/langchain/requirements.txt @@ -0,0 +1,4 @@ +shiny +python-dotenv +tokenizers +langchain-openai diff --git a/examples/chat/hello-providers/ollama/requirements.txt b/examples/chat/hello-providers/ollama/requirements.txt new file mode 100644 index 000000000..223d69e4b --- /dev/null +++ b/examples/chat/hello-providers/ollama/requirements.txt @@ -0,0 +1,3 @@ +shiny +tokenizers +ollama diff --git a/examples/chat/hello-providers/openai/app.py b/examples/chat/hello-providers/openai/app.py index 367c55fe9..d4581ef00 100644 --- a/examples/chat/hello-providers/openai/app.py +++ b/examples/chat/hello-providers/openai/app.py @@ -4,9 +4,8 @@ # 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 @@ -14,8 +13,7 @@ # 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 diff --git a/examples/chat/hello-providers/openai/app_utils.py b/examples/chat/hello-providers/openai/app_utils.py new file mode 100644 index 000000000..6ebad7e8a --- /dev/null +++ b/examples/chat/hello-providers/openai/app_utils.py @@ -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, + ) diff --git a/examples/chat/hello-providers/openai/requirements.txt b/examples/chat/hello-providers/openai/requirements.txt new file mode 100644 index 000000000..6e4a780cf --- /dev/null +++ b/examples/chat/hello-providers/openai/requirements.txt @@ -0,0 +1,4 @@ +shiny +python-dotenv +tokenizers +openai diff --git a/pyrightconfig.json b/pyrightconfig.json index 212089466..2dd28b423 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -15,5 +15,6 @@ "reportImportCycles": "none", "reportUnusedFunction": "none", "reportPrivateUsage": "none", - "reportUnnecessaryIsInstance": "none" + "reportUnnecessaryIsInstance": "none", + "executionEnvironments": [{ "root": "examples/" }] }