-
Notifications
You must be signed in to change notification settings - Fork 30
chore: Add example for judge usage #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,8 @@ yarn-error.log | |
| dump.rdb | ||
| .wrangler | ||
| stats.html | ||
|
|
||
| # Environment files | ||
| .env | ||
| .env.local | ||
| .env.*.local | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/sdk/server-ai/examples/judge-evaluation/.env.example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # LaunchDarkly SDK Key (required) | ||
| # Get this from your LaunchDarkly account settings | ||
| LAUNCHDARKLY_SDK_KEY=your-launchdarkly-sdk-key-here | ||
|
|
||
| # AI Config Key for the main chat configuration (optional) | ||
| # This is the key of your AI Config in LaunchDarkly | ||
| LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-config | ||
|
|
||
| # Judge Config Key for evaluation (optional) | ||
| # This is the key of your Judge AI Config in LaunchDarkly | ||
| LAUNCHDARKLY_JUDGE_KEY=ld-ai-judge-accuracy | ||
|
|
||
| # OpenAI API Key (required if using OpenAI provider) | ||
| # Get this from https://platform.openai.com/api-keys | ||
| OPENAI_API_KEY=your-openai-api-key-here | ||
|
|
72 changes: 72 additions & 0 deletions
72
packages/sdk/server-ai/examples/judge-evaluation/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # LaunchDarkly AI SDK Judge Evaluation Example | ||
|
|
||
| This package demonstrates the integration of LaunchDarkly's AI SDK Judge functionality for evaluating AI responses using AI Configs with `mode: "judge"`. | ||
|
|
||
| ## Installation and Build | ||
|
|
||
| When running as part of the js-core mono-repo the project will use local dependencies. | ||
| As such those dependencies need built. | ||
|
|
||
| In the root of the repository run: | ||
|
|
||
| ```bash | ||
| yarn | ||
| ``` | ||
|
|
||
| And then | ||
|
|
||
| ```bash | ||
| yarn build | ||
| ``` | ||
|
|
||
| ## AI Config Setup | ||
|
|
||
| Make sure you have an AI Config configured in LaunchDarkly with `mode: "judge"`: | ||
|
|
||
| 1. Install Judges in your AI Configs | ||
| 1. Create an AI Config in LaunchDarkly: | ||
| - Navigate to the AI Configs section in your LaunchDarkly dashboard | ||
| - Create a new AI Config with the key `sample-ai-config` | ||
| - Add a variation with the following settings: | ||
| - **Model Selection**: Select "OpenAI" as the provider and "gpt-3.5-turbo" as the model | ||
| - **Messages**: Add a system message with the content: "You are a helpful assistant for {{companyName}}. You should be friendly and informative." | ||
| - Save the variation | ||
| - Update the default target rule to use the newly created variation | ||
| - Attach one or more judges to your config | ||
|
|
||
| ## Configuration | ||
|
|
||
| Before running the example, make sure to set the following environment variables: | ||
|
|
||
| 1. Copy the example environment file: | ||
| ```bash | ||
| cp .env.example .env | ||
| ``` | ||
|
|
||
| 2. Edit `.env` and set the following environment variables: | ||
| - `LAUNCHDARKLY_SDK_KEY`: Your LaunchDarkly SDK key (required) | ||
| - `LAUNCHDARKLY_AI_CONFIG_KEY`: Your AI Config key (defaults to 'sample-ai-config') | ||
| - `LAUNCHDARKLY_JUDGE_KEY`: Your judge AI Config key (defaults to 'ld-ai-judge-accuracy') | ||
| - `OPENAI_API_KEY`: Your OpenAI API key (required if using OpenAI provider) | ||
|
|
||
| ## Usage | ||
|
|
||
| The main script (`index.js`) demonstrates how to: | ||
|
|
||
| 1. Initialize the LaunchDarkly SDK | ||
| 1. Set up a user context | ||
| 1. Initialize the LaunchDarkly AI client | ||
| 1. Create a chat for an AI Config with attached judges | ||
| 1. Create a judge for direct evaluation | ||
| 1. Evaluate AI text using the `evaluate()` method | ||
| 1. Handle evaluation results and errors | ||
|
|
||
| To run the example (in the judge-evaluation directory): | ||
|
|
||
| ```bash | ||
| yarn start | ||
| ``` | ||
|
|
||
| ## Note | ||
|
|
||
| This example uses the Judge functionality to evaluate AI responses. Make sure your LaunchDarkly AI Configs are set up correctly with `mode: "judge"` and include the necessary evaluation prompts and metrics. |
37 changes: 37 additions & 0 deletions
37
packages/sdk/server-ai/examples/judge-evaluation/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "name": "@launchdarkly/server-sdk-ai-judge", | ||
| "version": "1.0.0", | ||
| "description": "Example demonstrating LaunchDarkly AI SDK judge functionality for evaluating AI responses", | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "lint": "npx eslint . --ext .ts", | ||
| "start": "yarn build && node ./dist/index.js" | ||
| }, | ||
| "dependencies": { | ||
| "@launchdarkly/node-server-sdk": "^9.0.0", | ||
| "@launchdarkly/server-sdk-ai": "^0.12.3", | ||
| "@launchdarkly/server-sdk-ai-langchain": "^0.1.0", | ||
| "@launchdarkly/server-sdk-ai-openai": "^0.1.0", | ||
| "@launchdarkly/server-sdk-ai-vercel": "^0.1.0", | ||
| "dotenv": "^16.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@trivago/prettier-plugin-sort-imports": "^4.1.1", | ||
| "@tsconfig/node20": "20.1.4", | ||
| "@typescript-eslint/eslint-plugin": "^6.20.0", | ||
| "@typescript-eslint/parser": "^6.20.0", | ||
| "eslint": "^8.45.0", | ||
| "eslint-config-airbnb-base": "^15.0.0", | ||
| "eslint-config-airbnb-typescript": "^17.1.0", | ||
| "eslint-config-prettier": "^8.8.0", | ||
| "eslint-plugin-import": "^2.27.5", | ||
| "eslint-plugin-jest": "^27.6.3", | ||
| "eslint-plugin-prettier": "^5.0.0", | ||
| "jest": "^29.7.0", | ||
| "prettier": "^3.0.0", | ||
| "rimraf": "^5.0.5", | ||
| "typedoc": "0.25.0", | ||
| "typescript": "^5.5.3" | ||
| } | ||
| } |
106 changes: 106 additions & 0 deletions
106
packages/sdk/server-ai/examples/judge-evaluation/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* eslint-disable no-console */ | ||
| import dotenv from 'dotenv'; | ||
|
|
||
| import { init, type LDContext } from '@launchdarkly/node-server-sdk'; | ||
| import { initAi } from '@launchdarkly/server-sdk-ai'; | ||
|
|
||
| dotenv.config({ override: true }); | ||
|
|
||
| // Environment variables | ||
| const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY; | ||
| const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config'; | ||
| const judgeKey = process.env.LAUNCHDARKLY_JUDGE_KEY || 'ld-ai-judge-accuracy'; | ||
|
|
||
| // Validate required environment variables | ||
| if (!sdkKey) { | ||
| console.error('*** Please set the LAUNCHDARKLY_SDK_KEY env first'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // Initialize LaunchDarkly client | ||
| const ldClient = init(sdkKey); | ||
|
|
||
| // Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard | ||
| // soon after you run the demo. | ||
| const context: LDContext = { | ||
| kind: 'user', | ||
| key: 'example-user-key', | ||
| name: 'Sandy', | ||
| }; | ||
|
|
||
| async function main(): Promise<void> { | ||
| try { | ||
| await ldClient.waitForInitialization({ timeout: 10 }); | ||
| console.log('*** SDK successfully initialized'); | ||
| } catch (error) { | ||
| console.log(`*** SDK failed to initialize: ${error}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const aiClient = initAi(ldClient); | ||
|
|
||
| try { | ||
| // Example using the chat functionality which automates the judge evaluation | ||
| const defaultValue = { | ||
| enabled: false, | ||
| }; | ||
|
|
||
| const chat = await aiClient.createChat(aiConfigKey, context, defaultValue, { | ||
| company_name: 'LaunchDarkly', | ||
| }); | ||
|
|
||
| if (!chat) { | ||
| console.log('*** AI chat configuration is not enabled'); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| console.log('\n*** Starting chat:'); | ||
| const userInput = 'How can LaunchDarkly help me?'; | ||
| console.log('User Input:', userInput); | ||
|
|
||
| // The invoke method will automatically evaluate the chat response with any judges defined in the AI config | ||
| const chatResponse = await chat.invoke(userInput); | ||
| console.log('Chat Response:', chatResponse.message.content); | ||
|
|
||
| // Log judge evaluation results with full detail | ||
| const evalResults = await chatResponse.evaluations; | ||
| console.log('Judge results:', JSON.stringify(evalResults, null, 2)); | ||
|
|
||
| // Example of using the judge functionality with direct input and output | ||
| // Get AI judge configuration from LaunchDarkly | ||
| const judge = await aiClient.createJudge( | ||
| judgeKey, | ||
| context, | ||
| { enabled: false }, | ||
| undefined, | ||
| 'langchain', | ||
| ); | ||
|
|
||
| if (!judge) { | ||
| console.log('*** AI judge configuration is not enabled'); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| console.log('\n*** Starting judge evaluation of direct input and output:'); | ||
| const input = 'You are a helpful assistant for the company LaunchDarkly. How can you help me?'; | ||
| const output = | ||
| 'I can answer any question you have except for questions about the company LaunchDarkly.'; | ||
|
|
||
| console.log('Input:', input); | ||
| console.log('Output:', output); | ||
|
|
||
| const judgeResponse = await judge.evaluate(input, output); | ||
|
|
||
| // Track the judge evaluation scores on the tracker for the aiConfig you are evaluating | ||
| // Example: | ||
| // aiConfig.tracker.trackEvalScores(judgeResponse?.evals); | ||
|
|
||
| console.log('Judge Response:', judgeResponse); | ||
|
|
||
| console.log('Success.'); | ||
| } catch (err) { | ||
| console.error('Error:', err); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
18 changes: 18 additions & 0 deletions
18
packages/sdk/server-ai/examples/judge-evaluation/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2022", | ||
| "module": "ESNext", | ||
| "moduleResolution": "node", | ||
| "esModuleInterop": true, | ||
| "allowSyntheticDefaultImports": true, | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "outDir": "./dist", | ||
| "rootDir": "./src", | ||
| "declaration": true, | ||
| "sourceMap": true | ||
| }, | ||
| "include": ["src/**/*"], | ||
| "exclude": ["node_modules", "dist"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.