|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import dotenv from 'dotenv'; |
| 3 | + |
| 4 | +import { init, type LDContext } from '@launchdarkly/node-server-sdk'; |
| 5 | +import { initAi } from '@launchdarkly/server-sdk-ai'; |
| 6 | + |
| 7 | +dotenv.config({ override: true }); |
| 8 | + |
| 9 | +// Environment variables |
| 10 | +const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY; |
| 11 | +const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config'; |
| 12 | +const judgeKey = process.env.LAUNCHDARKLY_JUDGE_KEY || 'ld-ai-judge-accuracy'; |
| 13 | + |
| 14 | +// Validate required environment variables |
| 15 | +if (!sdkKey) { |
| 16 | + console.error('*** Please set the LAUNCHDARKLY_SDK_KEY env first'); |
| 17 | + process.exit(1); |
| 18 | +} |
| 19 | + |
| 20 | +// Initialize LaunchDarkly client |
| 21 | +const ldClient = init(sdkKey); |
| 22 | + |
| 23 | +// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard |
| 24 | +// soon after you run the demo. |
| 25 | +const context: LDContext = { |
| 26 | + kind: 'user', |
| 27 | + key: 'example-user-key', |
| 28 | + name: 'Sandy', |
| 29 | +}; |
| 30 | + |
| 31 | +async function main(): Promise<void> { |
| 32 | + try { |
| 33 | + await ldClient.waitForInitialization({ timeout: 10 }); |
| 34 | + console.log('*** SDK successfully initialized'); |
| 35 | + } catch (error) { |
| 36 | + console.log(`*** SDK failed to initialize: ${error}`); |
| 37 | + process.exit(1); |
| 38 | + } |
| 39 | + |
| 40 | + const aiClient = initAi(ldClient); |
| 41 | + |
| 42 | + try { |
| 43 | + // Example using the chat functionality which automates the judge evaluation |
| 44 | + const defaultValue = { |
| 45 | + enabled: false, |
| 46 | + }; |
| 47 | + |
| 48 | + const chat = await aiClient.createChat(aiConfigKey, context, defaultValue, { |
| 49 | + companyName: 'LaunchDarkly', |
| 50 | + }); |
| 51 | + |
| 52 | + if (!chat) { |
| 53 | + console.log('*** AI chat configuration is not enabled'); |
| 54 | + process.exit(0); |
| 55 | + } |
| 56 | + |
| 57 | + console.log('\n*** Starting chat:'); |
| 58 | + const userInput = 'How can LaunchDarkly help me?'; |
| 59 | + console.log('User Input:', userInput); |
| 60 | + |
| 61 | + // The invoke method will automatically evaluate the chat response with any judges defined in the AI config |
| 62 | + const chatResponse = await chat.invoke(userInput); |
| 63 | + console.log('Chat Response:', chatResponse.message.content); |
| 64 | + |
| 65 | + // Log judge evaluation results with full detail |
| 66 | + const evalResults = await chatResponse.evaluations; |
| 67 | + console.log('Judge results:', JSON.stringify(evalResults, null, 2)); |
| 68 | + |
| 69 | + // Example of using the judge functionality with direct input and output |
| 70 | + // Get AI judge configuration from LaunchDarkly |
| 71 | + const judge = await aiClient.createJudge(judgeKey, context, { enabled: false }); |
| 72 | + |
| 73 | + if (!judge) { |
| 74 | + console.log('*** AI judge configuration is not enabled'); |
| 75 | + process.exit(0); |
| 76 | + } |
| 77 | + |
| 78 | + console.log('\n*** Starting judge evaluation of direct input and output:'); |
| 79 | + const input = 'You are a helpful assistant for the company LaunchDarkly. How can you help me?'; |
| 80 | + const output = |
| 81 | + 'I can answer any question you have except for questions about the company LaunchDarkly.'; |
| 82 | + |
| 83 | + console.log('Input:', input); |
| 84 | + console.log('Output:', output); |
| 85 | + |
| 86 | + const judgeResponse = await judge.evaluate(input, output); |
| 87 | + |
| 88 | + // Track the judge evaluation scores on the tracker for the aiConfig you are evaluating |
| 89 | + // Example: |
| 90 | + // aiConfig.tracker.trackEvalScores(judgeResponse?.evals); |
| 91 | + |
| 92 | + console.log('Judge Response:', judgeResponse); |
| 93 | + |
| 94 | + console.log('Success.'); |
| 95 | + } catch (err) { |
| 96 | + console.error('Error:', err); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +main(); |
0 commit comments