diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 73e8bc1..45b57a3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,4 +31,8 @@ jobs: uses: abatilo/actions-poetry@7b6d33e44b4f08d7021a1dee3c044e9c253d6439 - name: Install dependencies - run: poetry install --all-extras + run: | + for dir in examples/*/; do + echo "Installing $dir" + poetry -C "$dir" install + done diff --git a/.gitignore b/.gitignore index 36c1b23..1daee47 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .python-version poetry.lock __pycache__/ +.env diff --git a/README.md b/README.md index 490c7d9..ea1d587 100644 --- a/README.md +++ b/README.md @@ -8,106 +8,31 @@ This demo requires Python 3.10 or higher. ## Build Instructions -This repository includes examples for `OpenAI`, `Bedrock`, and `LangChain` for multi-provider support. Depending on your preferred provider, you may have to take some additional steps. +This repository includes examples for `OpenAI`, `Bedrock`, `Gemini`, `LangChain`, `LangGraph`, `Judge`, and `Observability`. Depending on your preferred provider, you may have to take some additional steps. ### General setup 1. [Create an AI Config](https://launchdarkly.com/docs/home/ai-configs/create) using the key specified in each example, or copy the key of existing AI Config in your LaunchDarkly project that you want to evaluate. -1. Set the environment variable `LAUNCHDARKLY_SDK_KEY` to your LaunchDarkly SDK key and `LAUNCHDARKLY_AI_CONFIG_KEY` to the AI Config key; otherwise, an AI Config of `sample-ai-config` or `sample-ai-agent-config` will be assumed for most examples. - - ```bash - export LAUNCHDARKLY_SDK_KEY="1234567890abcdef" - export LAUNCHDARKLY_AI_CONFIG_KEY="sample-ai-config" - ``` 1. Ensure you have [Poetry](https://python-poetry.org/) installed. -### Provider-Specific Setup - -#### OpenAI setup (single provider) - -1. Install the required dependencies with `poetry install -E openai` or `poetry install --all-extras`. -1. Set the environment variable `OPENAI_API_KEY` to your OpenAI key. -1. On the command line, run `poetry run openai-example`. - -#### Chat with observability (observability plugin example) - -This example demonstrates how to use the LaunchDarkly observability SDK plugin to monitor AI chat operations. For more details, see the [Python SDK observability reference](https://launchdarkly.com/docs/sdk/observability/python). +1. Create a `.env` file in the repository root with at least your LaunchDarkly SDK key: -1. Install the required dependencies with `poetry install -E observability` or `poetry install --all-extras`. -1. Set the environment variable for your AI provider (e.g., `OPENAI_API_KEY`), or configure your AI Config to use a different provider. -1. Optionally, set service identification: - ```bash - export SERVICE_NAME="my-ai-service" - export SERVICE_VERSION="1.0.0" ``` -1. On the command line, run `poetry run chat-observability-example`. - -The observability plugin automatically captures and sends data to LaunchDarkly: -- **Observability tab**: SDK operations, flag evaluations, error monitoring, logging, and distributed tracing -- **AI Config Monitoring tab**: Token usage, duration, success/error rates, and custom metadata for filtering and analysis - -View your data in the LaunchDarkly dashboard under **Observability** tabs. - -#### Bedrock setup (single provider) - -1. Install the required dependencies with `poetry install -E bedrock` or `poetry install --all-extras`. -1. Ensure the required AWS credentials can be [auto-detected by the `boto3` library](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html). Examples might include environment variables, role providers, or shared credential files. -1. On the command line, run `poetry run bedrock-example`. - -#### Gemini setup (single provider) - -1. Install the required dependencies with `poetry install -E gemini` or `poetry install --all-extras`. -1. Set the environment variable `GOOGLE_API_KEY` to your Google API key. -1. On the command line, run `poetry run gemini-example`. - -#### LangChain setup (multiple providers) - -This example uses `OpenAI`, `Bedrock`, and `Gemini` LangChain provider packages. You can add additional LangChain providers using the `poetry add` command. - -1. Install all dependencies with `poetry install -E langchain` or `poetry install --all-extras`. -1. Set up API keys for the providers you want to use. -1. On the command line, run `poetry run langchain-example` - -#### LangGraph setup (multiple providers, single agent) - -1. Install all dependencies with `poetry install -E langgraph` or `poetry install --all-extras`. -1. Set up API keys for the providers you want to use. -1. Optionally set this environment variable to use a different agent config: - ```bash - export LAUNCHDARKLY_AGENT_CONFIG_KEY="sample-ai-agent-config" + LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key ``` -1. On the command line, run `poetry run langgraph-agent-example`. - -#### LangGraph setup (multiple providers, multiple agents) -1. Install all dependencies with `poetry install -E langgraph` or `poetry install --all-extras`. -1. Set up API keys for the providers you want to use. -1. [Create an AI Config (Agent-based)](https://launchdarkly.com/docs/home/ai-configs/agents) using the keys below. Write a goal for each config and enable it with targeting rules. -1. Optionally set these environment variables to use different agent configs: - ```bash - export LAUNCHDARKLY_ANALYZER_CONFIG_KEY="code-review-analyzer" - export LAUNCHDARKLY_DOCUMENTATION_CONFIG_KEY="code-review-documentation" - ``` -1. On the command line, run `poetry run langgraph-multi-agent-example`. - -#### Judge setup (judge evaluation) - -These examples demonstrate how to use LaunchDarkly's judge functionality to evaluate AI responses for accuracy, relevance, and other metrics. - -1. Install dependencies with `poetry install -E langchain` or `poetry install --all-extras`. -1. Set up API keys for the provider you want to use (OpenAI, Bedrock, or Gemini). -1. [Create an AI Config](https://launchdarkly.com/docs/home/ai-configs/create) for chat functionality. -1. [Create a Judge Config](https://launchdarkly.com/docs/home/ai-configs/judges) for evaluation. -1. Set the required environment variables: - ```bash - export LAUNCHDARKLY_SDK_KEY="your-sdk-key" - export LAUNCHDARKLY_AI_CONFIG_KEY="sample-ai-config" - export LAUNCHDARKLY_AI_JUDGE_KEY="sample-ai-judge-accuracy" - ``` - Note: The default values are `sample-ai-config` for AI Config and `sample-ai-judge-accuracy` for Judge Config if not specified. + Each example README describes the full set of environment variables needed. The `.env` file is loaded automatically when running any example. -##### Available judge examples: +### Examples -- **Chat with automatic judge evaluation** (`poetry run chat-judge-example`): Uses the chat functionality which automatically evaluates responses with any judges defined in the AI config. -- **Direct judge evaluation** (`poetry run direct-judge-example`): Evaluates specific input/output pairs using a judge configuration directly. +| Example | Description | README | +| --- | --- | --- | +| **OpenAI** | Single provider using OpenAI | [examples/openai](examples/openai/README.md) | +| **Bedrock** | Single provider using AWS Bedrock | [examples/bedrock](examples/bedrock/README.md) | +| **Gemini** | Single provider using Google Gemini | [examples/gemini](examples/gemini/README.md) | +| **LangChain** | Multiple providers via LangChain | [examples/langchain](examples/langchain/README.md) | +| **LangGraph Agent** | Single agent using LangGraph | [examples/langgraph_agent](examples/langgraph_agent/README.md) | +| **LangGraph Multi-Agent** | Multiple agents using LangGraph | [examples/langgraph_multi_agent](examples/langgraph_multi_agent/README.md) | +| **Judge** | Judge evaluation of AI responses | [examples/judge](examples/judge/README.md) | +| **Chat with Observability** | Observability plugin for AI chat monitoring | [examples/chat_observability](examples/chat_observability/README.md) | diff --git a/examples/bedrock/README.md b/examples/bedrock/README.md new file mode 100644 index 0000000..af7a6a2 --- /dev/null +++ b/examples/bedrock/README.md @@ -0,0 +1,43 @@ +# Bedrock Example (Single Provider) + +This example demonstrates how to use LaunchDarkly's AI Config with the AWS Bedrock provider. + +## Prerequisites + +- Python 3.10 or higher +- [Poetry](https://python-poetry.org/) installed +- A LaunchDarkly account with an [AI Config](https://launchdarkly.com/docs/home/ai-configs/create) created +- AWS credentials configured for Bedrock access + +## Setup + +1. Create a `.env` file in this directory with the following variables: + + ``` + LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key + LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config + ``` + + > `LAUNCHDARKLY_AI_CONFIG_KEY` defaults to `sample-ai-config` if not set. + +2. Ensure your AWS credentials can be [auto-detected by the `boto3` library](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html). You can set them in your `.env` file: + + ``` + AWS_ACCESS_KEY_ID=your-access-key-id + AWS_SECRET_ACCESS_KEY=your-secret-access-key + AWS_DEFAULT_REGION=us-east-1 + ``` + + Other options include role providers or shared credential files. + +3. Install the required dependencies: + + ```bash + poetry install + ``` + +## Run + +```bash +poetry run bedrock-example +``` diff --git a/examples/bedrock_example.py b/examples/bedrock/bedrock_example.py similarity index 95% rename from examples/bedrock_example.py rename to examples/bedrock/bedrock_example.py index ad87994..b7e8a32 100755 --- a/examples/bedrock_example.py +++ b/examples/bedrock/bedrock_example.py @@ -1,11 +1,14 @@ import os +from dotenv import load_dotenv import ldclient from ldclient import Context from ldclient.config import Config from ldai import LDAIClient import boto3 -client = boto3.client("bedrock-runtime", region_name="us-east-1") +load_dotenv() + +client = boto3.client("bedrock-runtime", region_name=os.getenv('AWS_DEFAULT_REGION', 'us-east-1')) # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') diff --git a/examples/bedrock/pyproject.toml b/examples/bedrock/pyproject.toml new file mode 100644 index 0000000..323cab0 --- /dev/null +++ b/examples/bedrock/pyproject.toml @@ -0,0 +1,21 @@ +[tool.poetry] +name = "hello-python-ai-bedrock" +version = "0.1.0" +description = "Hello LaunchDarkly for Python AI - Bedrock" +authors = ["LaunchDarkly "] +license = "Apache-2.0" +readme = "README.md" +packages = [{include = "bedrock_example.py"}] + +[tool.poetry.scripts] +bedrock-example = "bedrock_example:main" + +[tool.poetry.dependencies] +python = "^3.10" +python-dotenv = ">=1.0.0" +launchdarkly-server-sdk-ai = "^0.17.0" +boto3 = ">=0.2.0" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/examples/chat_observability/README.md b/examples/chat_observability/README.md new file mode 100644 index 0000000..6af7b83 --- /dev/null +++ b/examples/chat_observability/README.md @@ -0,0 +1,48 @@ +# Chat with Observability (Observability Plugin Example) + +This example demonstrates how to use the LaunchDarkly observability SDK plugin to monitor AI chat operations. For more details, see the [Python SDK observability reference](https://launchdarkly.com/docs/sdk/observability/python). + +The observability plugin automatically captures and sends data to LaunchDarkly: + +- **Observability tab**: SDK operations, flag evaluations, error monitoring, logging, and distributed tracing +- **AI Config Monitoring tab**: Token usage, duration, success/error rates, and custom metadata for filtering and analysis + +View your data in the LaunchDarkly dashboard under **Observability** tabs. + +## Prerequisites + +- Python 3.10 or higher +- [Poetry](https://python-poetry.org/) installed +- A LaunchDarkly account with an [AI Config](https://launchdarkly.com/docs/home/ai-configs/create) created +- An API key for your AI provider (e.g., OpenAI) + +## Setup + +1. Create a `.env` file in this directory with the following variables: + + ``` + LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key + LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config + OPENAI_API_KEY=your-openai-api-key + ``` + + > `LAUNCHDARKLY_AI_CONFIG_KEY` defaults to `sample-ai-config` if not set. + + Optionally, set service identification: + + ``` + SERVICE_NAME=my-ai-service + SERVICE_VERSION=1.0.0 + ``` + +2. Install the required dependencies: + + ```bash + poetry install + ``` + +## Run + +```bash +poetry run chat-observability-example +``` diff --git a/examples/chat_observability_example.py b/examples/chat_observability/chat_observability_example.py similarity index 98% rename from examples/chat_observability_example.py rename to examples/chat_observability/chat_observability_example.py index 296ec4d..1ecc7f7 100644 --- a/examples/chat_observability_example.py +++ b/examples/chat_observability/chat_observability_example.py @@ -1,4 +1,5 @@ import os +from dotenv import load_dotenv import asyncio import logging import ldclient @@ -7,6 +8,8 @@ from ldai import LDAIClient, AICompletionConfigDefault from ldobserve import ObservabilityConfig, ObservabilityPlugin +load_dotenv() + logging.getLogger('ldclient').setLevel(logging.WARNING) # Set sdk_key to your LaunchDarkly SDK key. diff --git a/examples/chat_observability/pyproject.toml b/examples/chat_observability/pyproject.toml new file mode 100644 index 0000000..2860356 --- /dev/null +++ b/examples/chat_observability/pyproject.toml @@ -0,0 +1,22 @@ +[tool.poetry] +name = "hello-python-ai-chat-observability" +version = "0.1.0" +description = "Hello LaunchDarkly for Python AI - Chat with Observability" +authors = ["LaunchDarkly "] +license = "Apache-2.0" +readme = "README.md" +packages = [{include = "chat_observability_example.py"}] + +[tool.poetry.scripts] +chat-observability-example = "chat_observability_example:main" + +[tool.poetry.dependencies] +python = "^3.10" +python-dotenv = ">=1.0.0" +launchdarkly-server-sdk-ai = "^0.17.0" +launchdarkly-observability = ">=0.1.0" +openai = ">=0.2.0" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/examples/gemini/README.md b/examples/gemini/README.md new file mode 100644 index 0000000..5b7422d --- /dev/null +++ b/examples/gemini/README.md @@ -0,0 +1,34 @@ +# Gemini Example (Single Provider) + +This example demonstrates how to use LaunchDarkly's AI Config with the Google Gemini provider. + +## Prerequisites + +- Python 3.10 or higher +- [Poetry](https://python-poetry.org/) installed +- A LaunchDarkly account with an [AI Config](https://launchdarkly.com/docs/home/ai-configs/create) created +- A [Google API key](https://aistudio.google.com/apikey) + +## Setup + +1. Create a `.env` file in this directory with the following variables: + + ``` + LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key + LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config + GOOGLE_API_KEY=your-google-api-key + ``` + + > `LAUNCHDARKLY_AI_CONFIG_KEY` defaults to `sample-ai-config` if not set. + +2. Install the required dependencies: + + ```bash + poetry install + ``` + +## Run + +```bash +poetry run gemini-example +``` diff --git a/examples/gemini_example.py b/examples/gemini/gemini_example.py similarity index 99% rename from examples/gemini_example.py rename to examples/gemini/gemini_example.py index d53735e..ac2bcd3 100644 --- a/examples/gemini_example.py +++ b/examples/gemini/gemini_example.py @@ -1,4 +1,5 @@ import os +from dotenv import load_dotenv import ldclient from ldclient import Context from ldclient.config import Config @@ -8,6 +9,8 @@ from google.genai import types from typing import List, Optional, Tuple +load_dotenv() + # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') diff --git a/examples/gemini/pyproject.toml b/examples/gemini/pyproject.toml new file mode 100644 index 0000000..df28938 --- /dev/null +++ b/examples/gemini/pyproject.toml @@ -0,0 +1,21 @@ +[tool.poetry] +name = "hello-python-ai-gemini" +version = "0.1.0" +description = "Hello LaunchDarkly for Python AI - Gemini" +authors = ["LaunchDarkly "] +license = "Apache-2.0" +readme = "README.md" +packages = [{include = "gemini_example.py"}] + +[tool.poetry.scripts] +gemini-example = "gemini_example:main" + +[tool.poetry.dependencies] +python = "^3.10" +python-dotenv = ">=1.0.0" +launchdarkly-server-sdk-ai = "^0.17.0" +google-genai = "^1.30.0" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/examples/judge/README.md b/examples/judge/README.md new file mode 100644 index 0000000..1f0ccf6 --- /dev/null +++ b/examples/judge/README.md @@ -0,0 +1,54 @@ +# Judge Examples (Judge Evaluation) + +These examples demonstrate how to use LaunchDarkly's judge functionality to evaluate AI responses for accuracy, relevance, and other metrics. + +## Prerequisites + +- Python 3.10 or higher +- [Poetry](https://python-poetry.org/) installed +- A LaunchDarkly account with an [AI Config](https://launchdarkly.com/docs/home/ai-configs/create) created for chat functionality +- A [Judge Config](https://launchdarkly.com/docs/home/ai-configs/judges) created for evaluation +- API keys for the provider you want to use (OpenAI, Bedrock, or Gemini) + +## Setup + +1. Create a `.env` file in this directory with the following variables: + + ``` + LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key + LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config + LAUNCHDARKLY_AI_JUDGE_KEY=sample-ai-judge-accuracy + ``` + + > `LAUNCHDARKLY_AI_CONFIG_KEY` defaults to `sample-ai-config` if not set. + > `LAUNCHDARKLY_AI_JUDGE_KEY` defaults to `sample-ai-judge-accuracy` if not set. + + Add the API key for your chosen provider: + + ``` + OPENAI_API_KEY=your-openai-api-key + ``` + +2. Install the required dependencies: + + ```bash + poetry install + ``` + +## Run + +### Chat with automatic judge evaluation + +Uses the chat functionality which automatically evaluates responses with any judges defined in the AI config. + +```bash +poetry run chat-judge-example +``` + +### Direct judge evaluation + +Evaluates specific input/output pairs using a judge configuration directly. + +```bash +poetry run direct-judge-example +``` diff --git a/examples/chat_judge_example.py b/examples/judge/chat_judge_example.py similarity index 98% rename from examples/chat_judge_example.py rename to examples/judge/chat_judge_example.py index 57094f8..ca99daa 100644 --- a/examples/chat_judge_example.py +++ b/examples/judge/chat_judge_example.py @@ -1,4 +1,5 @@ import os +from dotenv import load_dotenv import json import asyncio import ldclient @@ -6,6 +7,8 @@ from ldclient.config import Config from ldai import LDAIClient, AICompletionConfigDefault +load_dotenv() + # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') diff --git a/examples/direct_judge_example.py b/examples/judge/direct_judge_example.py similarity index 98% rename from examples/direct_judge_example.py rename to examples/judge/direct_judge_example.py index 7d2faa6..f8126d4 100644 --- a/examples/direct_judge_example.py +++ b/examples/judge/direct_judge_example.py @@ -1,4 +1,5 @@ import os +from dotenv import load_dotenv import json import asyncio import ldclient @@ -6,6 +7,8 @@ from ldclient.config import Config from ldai import LDAIClient, AIJudgeConfigDefault +load_dotenv() + # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') diff --git a/examples/judge/pyproject.toml b/examples/judge/pyproject.toml new file mode 100644 index 0000000..8305d36 --- /dev/null +++ b/examples/judge/pyproject.toml @@ -0,0 +1,27 @@ +[tool.poetry] +name = "hello-python-ai-judge" +version = "0.1.0" +description = "Hello LaunchDarkly for Python AI - Judge" +authors = ["LaunchDarkly "] +license = "Apache-2.0" +readme = "README.md" +packages = [ + {include = "chat_judge_example.py"}, + {include = "direct_judge_example.py"}, +] + +[tool.poetry.scripts] +chat-judge-example = "chat_judge_example:main" +direct-judge-example = "direct_judge_example:main" + +[tool.poetry.dependencies] +python = "^3.10" +python-dotenv = ">=1.0.0" +launchdarkly-server-sdk-ai = "^0.17.0" +launchdarkly-server-sdk-ai-openai = "^0.3.0" +launchdarkly-server-sdk-ai-langchain = "^0.4.0" +openai = ">=1.0.0" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/examples/langchain/README.md b/examples/langchain/README.md new file mode 100644 index 0000000..a4827df --- /dev/null +++ b/examples/langchain/README.md @@ -0,0 +1,42 @@ +# LangChain Example (Multiple Providers) + +This example demonstrates how to use LaunchDarkly's AI Config with LangChain, supporting multiple providers including OpenAI, Bedrock, and Gemini. You can add additional LangChain providers using the `poetry add` command. + +## Prerequisites + +- Python 3.10 or higher +- [Poetry](https://python-poetry.org/) installed +- A LaunchDarkly account with an [AI Config](https://launchdarkly.com/docs/home/ai-configs/create) created +- API keys for the providers you want to use + +## Setup + +1. Create a `.env` file in this directory with the following variables: + + ``` + LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key + LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config + ``` + + > `LAUNCHDARKLY_AI_CONFIG_KEY` defaults to `sample-ai-config` if not set. + + Add the API keys for the providers you want to use: + + ``` + OPENAI_API_KEY=your-openai-api-key + GOOGLE_API_KEY=your-google-api-key + AWS_ACCESS_KEY_ID=your-access-key-id + AWS_SECRET_ACCESS_KEY=your-secret-access-key + ``` + +2. Install the required dependencies: + + ```bash + poetry install + ``` + +## Run + +```bash +poetry run langchain-example +``` diff --git a/examples/langchain_example.py b/examples/langchain/langchain_example.py similarity index 98% rename from examples/langchain_example.py rename to examples/langchain/langchain_example.py index 60e604f..836e1eb 100644 --- a/examples/langchain_example.py +++ b/examples/langchain/langchain_example.py @@ -1,4 +1,5 @@ import os +from dotenv import load_dotenv import asyncio import ldclient from ldclient import Context @@ -7,6 +8,8 @@ from ldai_langchain import get_ai_metrics_from_response from langchain.chat_models import init_chat_model +load_dotenv() + # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') diff --git a/examples/langchain/pyproject.toml b/examples/langchain/pyproject.toml new file mode 100644 index 0000000..4fe9151 --- /dev/null +++ b/examples/langchain/pyproject.toml @@ -0,0 +1,26 @@ +[tool.poetry] +name = "hello-python-ai-langchain" +version = "0.1.0" +description = "Hello LaunchDarkly for Python AI - LangChain" +authors = ["LaunchDarkly "] +license = "Apache-2.0" +readme = "README.md" +packages = [{include = "langchain_example.py"}] + +[tool.poetry.scripts] +langchain-example = "langchain_example:main" + +[tool.poetry.dependencies] +python = "^3.10" +python-dotenv = ">=1.0.0" +launchdarkly-server-sdk-ai = "^0.17.0" +launchdarkly-server-sdk-ai-langchain = "^0.4.0" +langchain = "^1.0.0" +langchain-core = "^1.0.0" +langchain-openai = "^1.0.0" +langchain-google-genai = "^4.0.0" +langchain-aws = "^1.0.0" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/examples/langgraph_agent/README.md b/examples/langgraph_agent/README.md new file mode 100644 index 0000000..a5be720 --- /dev/null +++ b/examples/langgraph_agent/README.md @@ -0,0 +1,42 @@ +# LangGraph Agent Example (Single Agent) + +This example demonstrates how to use LaunchDarkly's AI Config with LangGraph to create a single ReAct agent with tool support. + +## Prerequisites + +- Python 3.10 or higher +- [Poetry](https://python-poetry.org/) installed +- A LaunchDarkly account with an [AI Config (Agent-based)](https://launchdarkly.com/docs/home/ai-configs/agents) created +- API keys for the providers you want to use + +## Setup + +1. Create a `.env` file in this directory with the following variables: + + ``` + LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key + LAUNCHDARKLY_AGENT_CONFIG_KEY=sample-ai-agent-config + ``` + + > `LAUNCHDARKLY_AGENT_CONFIG_KEY` defaults to `sample-ai-agent-config` if not set. + + Add the API keys for the providers you want to use: + + ``` + OPENAI_API_KEY=your-openai-api-key + GOOGLE_API_KEY=your-google-api-key + AWS_ACCESS_KEY_ID=your-access-key-id + AWS_SECRET_ACCESS_KEY=your-secret-access-key + ``` + +2. Install the required dependencies: + + ```bash + poetry install + ``` + +## Run + +```bash +poetry run langgraph-agent-example +``` diff --git a/examples/langgraph_agent_example.py b/examples/langgraph_agent/langgraph_agent_example.py similarity index 98% rename from examples/langgraph_agent_example.py rename to examples/langgraph_agent/langgraph_agent_example.py index 37f5212..f65b0d7 100644 --- a/examples/langgraph_agent_example.py +++ b/examples/langgraph_agent/langgraph_agent_example.py @@ -1,4 +1,5 @@ import os +from dotenv import load_dotenv import ldclient from pprint import pprint from ldclient import Context @@ -9,6 +10,8 @@ from langchain.chat_models import init_chat_model from langgraph.prebuilt import create_react_agent +load_dotenv() + # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') diff --git a/examples/langgraph_agent/pyproject.toml b/examples/langgraph_agent/pyproject.toml new file mode 100644 index 0000000..b69c805 --- /dev/null +++ b/examples/langgraph_agent/pyproject.toml @@ -0,0 +1,27 @@ +[tool.poetry] +name = "hello-python-ai-langgraph-agent" +version = "0.1.0" +description = "Hello LaunchDarkly for Python AI - LangGraph Agent" +authors = ["LaunchDarkly "] +license = "Apache-2.0" +readme = "README.md" +packages = [{include = "langgraph_agent_example.py"}] + +[tool.poetry.scripts] +langgraph-agent-example = "langgraph_agent_example:main" + +[tool.poetry.dependencies] +python = "^3.10" +python-dotenv = ">=1.0.0" +launchdarkly-server-sdk-ai = "^0.17.0" +launchdarkly-server-sdk-ai-langchain = "^0.4.0" +langchain = "^1.0.0" +langchain-core = "^1.0.0" +langchain-openai = "^1.0.0" +langchain-google-genai = "^4.0.0" +langchain-aws = "^1.0.0" +langgraph = "^1.0.0" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/examples/langgraph_multi_agent/README.md b/examples/langgraph_multi_agent/README.md new file mode 100644 index 0000000..00e3566 --- /dev/null +++ b/examples/langgraph_multi_agent/README.md @@ -0,0 +1,44 @@ +# LangGraph Multi-Agent Example (Multiple Agents) + +This example demonstrates how to use LaunchDarkly's AI Config with LangGraph to orchestrate multiple agents in a code review workflow. + +## Prerequisites + +- Python 3.10 or higher +- [Poetry](https://python-poetry.org/) installed +- A LaunchDarkly account with [AI Configs (Agent-based)](https://launchdarkly.com/docs/home/ai-configs/agents) created using the keys below. Write a goal for each config and enable it with targeting rules. +- API keys for the providers you want to use + +## Setup + +1. Create a `.env` file in this directory with the following variables: + + ``` + LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key + LAUNCHDARKLY_ANALYZER_CONFIG_KEY=code-review-analyzer + LAUNCHDARKLY_DOCUMENTATION_CONFIG_KEY=code-review-documentation + ``` + + > `LAUNCHDARKLY_ANALYZER_CONFIG_KEY` defaults to `code-review-analyzer` if not set. + > `LAUNCHDARKLY_DOCUMENTATION_CONFIG_KEY` defaults to `code-review-documentation` if not set. + + Add the API keys for the providers you want to use: + + ``` + OPENAI_API_KEY=your-openai-api-key + GOOGLE_API_KEY=your-google-api-key + AWS_ACCESS_KEY_ID=your-access-key-id + AWS_SECRET_ACCESS_KEY=your-secret-access-key + ``` + +2. Install the required dependencies: + + ```bash + poetry install + ``` + +## Run + +```bash +poetry run langgraph-multi-agent-example +``` diff --git a/examples/langgraph_multi_agent_example.py b/examples/langgraph_multi_agent/langgraph_multi_agent_example.py similarity index 99% rename from examples/langgraph_multi_agent_example.py rename to examples/langgraph_multi_agent/langgraph_multi_agent_example.py index 424b30f..8325074 100644 --- a/examples/langgraph_multi_agent_example.py +++ b/examples/langgraph_multi_agent/langgraph_multi_agent_example.py @@ -1,4 +1,5 @@ import os +from dotenv import load_dotenv import ldclient from ldclient import Context from ldclient.config import Config @@ -11,6 +12,8 @@ from langgraph.types import Command from typing_extensions import TypedDict +load_dotenv() + # Set sdk_key to your LaunchDarkly SDK key. sdk_key = os.getenv('LAUNCHDARKLY_SDK_KEY') diff --git a/examples/langgraph_multi_agent/pyproject.toml b/examples/langgraph_multi_agent/pyproject.toml new file mode 100644 index 0000000..4d6a176 --- /dev/null +++ b/examples/langgraph_multi_agent/pyproject.toml @@ -0,0 +1,28 @@ +[tool.poetry] +name = "hello-python-ai-langgraph-multi-agent" +version = "0.1.0" +description = "Hello LaunchDarkly for Python AI - LangGraph Multi-Agent" +authors = ["LaunchDarkly "] +license = "Apache-2.0" +readme = "README.md" +packages = [{include = "langgraph_multi_agent_example.py"}] + +[tool.poetry.scripts] +langgraph-multi-agent-example = "langgraph_multi_agent_example:main" + +[tool.poetry.dependencies] +python = "^3.10" +python-dotenv = ">=1.0.0" +launchdarkly-server-sdk-ai = "^0.17.0" +launchdarkly-server-sdk-ai-langchain = "^0.4.0" +langchain = "^1.0.0" +langchain-core = "^1.0.0" +langchain-openai = "^1.0.0" +langchain-google-genai = "^4.0.0" +langchain-aws = "^1.0.0" +langgraph = "^1.0.0" +typing-extensions = ">=4.0.0" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/examples/openai/README.md b/examples/openai/README.md new file mode 100644 index 0000000..87c94dc --- /dev/null +++ b/examples/openai/README.md @@ -0,0 +1,34 @@ +# OpenAI Example (Single Provider) + +This example demonstrates how to use LaunchDarkly's AI Config with the OpenAI provider. + +## Prerequisites + +- Python 3.10 or higher +- [Poetry](https://python-poetry.org/) installed +- A LaunchDarkly account with an [AI Config](https://launchdarkly.com/docs/home/ai-configs/create) created +- An [OpenAI API key](https://platform.openai.com/api-keys) + +## Setup + +1. Create a `.env` file in this directory with the following variables: + + ``` + LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key + LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config + OPENAI_API_KEY=your-openai-api-key + ``` + + > `LAUNCHDARKLY_AI_CONFIG_KEY` defaults to `sample-ai-config` if not set. + +2. Install the required dependencies: + + ```bash + poetry install + ``` + +## Run + +```bash +poetry run openai-example +``` diff --git a/examples/openai_example.py b/examples/openai/openai_example.py similarity index 98% rename from examples/openai_example.py rename to examples/openai/openai_example.py index 168d8e8..0a11e5c 100755 --- a/examples/openai_example.py +++ b/examples/openai/openai_example.py @@ -1,4 +1,5 @@ import os +from dotenv import load_dotenv import ldclient from ldclient import Context from ldclient.config import Config @@ -6,6 +7,8 @@ from ldai_openai import get_ai_metrics_from_response from openai import OpenAI +load_dotenv() + openai_client = OpenAI() # Set sdk_key to your LaunchDarkly SDK key. diff --git a/examples/openai/pyproject.toml b/examples/openai/pyproject.toml new file mode 100644 index 0000000..9cc7f27 --- /dev/null +++ b/examples/openai/pyproject.toml @@ -0,0 +1,22 @@ +[tool.poetry] +name = "hello-python-ai-openai" +version = "0.1.0" +description = "Hello LaunchDarkly for Python AI - OpenAI" +authors = ["LaunchDarkly "] +license = "Apache-2.0" +readme = "README.md" +packages = [{include = "openai_example.py"}] + +[tool.poetry.scripts] +openai-example = "openai_example:main" + +[tool.poetry.dependencies] +python = "^3.10" +python-dotenv = ">=1.0.0" +launchdarkly-server-sdk-ai = "^0.17.0" +launchdarkly-server-sdk-ai-openai = "^0.3.0" +openai = ">=1.0.0" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/pyproject.toml b/pyproject.toml deleted file mode 100644 index a6b3c4f..0000000 --- a/pyproject.toml +++ /dev/null @@ -1,48 +0,0 @@ -[tool.poetry] -name = "hello-python-ai" -version = "0.1.0" -description = "Hello LaunchDarkly for Python AI - OpenAI" -authors = ["LaunchDarkly "] -license = "Apache-2.0" -readme = "README.md" -packages = [ { include = "examples" } ] - -[tool.poetry.scripts] -bedrock-example = 'examples.bedrock_example:main' -openai-example = 'examples.openai_example:main' -gemini-example = 'examples.gemini_example:main' -langchain-example = 'examples.langchain_example:main' -langgraph-agent-example = 'examples.langgraph_agent_example:main' -langgraph-multi-agent-example = 'examples.langgraph_multi_agent_example:main' -chat-judge-example = 'examples.chat_judge_example:main' -chat-observability-example = 'examples.chat_observability_example:main' -direct-judge-example = 'examples.direct_judge_example:main' - -[tool.poetry.dependencies] -python = "^3.10" -launchdarkly-server-sdk-ai = "^0.17.0" -launchdarkly-server-sdk-ai-langchain = "^0.4.0" -launchdarkly-server-sdk-ai-openai = "^0.3.0" -launchdarkly-observability = { version = ">=0.1.0", optional = true } - -boto3 = { version = ">=0.2.0", optional = true } -openai = { version = ">=0.2.0", optional = true } -google-genai = { version = "^1.30.0", optional = true } -langchain = {version = "^1.0.0", optional = true} -langchain-aws = {version = "^1.0.0", optional = true} -langchain-core = {version = "^1.0.0", optional = true} -langchain-google-genai = {version = "^4.0.0", optional = true} -langchain-openai = {version = "^1.0.0", optional = true} -langgraph = {version = "^1.0.0", optional = true} - -[tool.poetry.extras] -bedrock = ["boto3"] -openai = ["openai"] -gemini = ["google-genai"] -langchain = ["langchain", "langchain-core", "langchain-openai", "langchain-google-genai", "langchain-aws"] -langgraph = ["langgraph", "typing-extensions"] -observability = ["launchdarkly-observability", "openai"] - -[build-system] -requires = ["poetry-core"] -build-backend = "poetry.core.masonry.api"