-
Notifications
You must be signed in to change notification settings - Fork 1
Prompt optimization support #7
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| /* eslint-disable no-console */ | ||
|
|
||
| // Example demonstrating the ze.prompt() functionality | ||
| // | ||
| // Run with: | ||
| // OPENAI_API_KEY=your-openai-key ZEROEVAL_API_KEY=your-zeroeval-key npm run example:prompt | ||
| // | ||
| // This example shows: | ||
| // - Auto-optimization mode (tries latest, falls back to provided content) | ||
| // - Explicit mode (always use provided content) | ||
| // - Template variable interpolation | ||
| // - Metadata extraction in OpenAI wrapper | ||
| // - Sending feedback for optimization | ||
|
|
||
| import { OpenAI } from 'openai'; | ||
| import * as ze from 'zeroeval'; | ||
|
|
||
| // Initialize ZeroEval with local development server | ||
| ze.init({ apiUrl: 'http://localhost:8000' }); | ||
|
|
||
| const openai = ze.wrap(new OpenAI()); | ||
|
|
||
| async function main() { | ||
| console.log('=== ZeroEval Prompt Examples ===\n'); | ||
|
|
||
| // Example 1: Auto-optimization mode | ||
| // If an optimized version exists in the backend, it will be used. | ||
| // Otherwise, the provided content will be registered and used. | ||
| console.log('Example 1: Auto-optimization mode'); | ||
| console.log('--------------------------------'); | ||
| try { | ||
| const systemPrompt = await ze.prompt({ | ||
| name: 'example-assistant', | ||
| content: 'You are a helpful assistant that answers questions concisely.', | ||
| }); | ||
|
|
||
| console.log('Decorated prompt (first 100 chars):'); | ||
| console.log(systemPrompt.substring(0, 100) + '...\n'); | ||
|
|
||
| const response = await openai.chat.completions.create({ | ||
| model: 'gpt-4o-mini', | ||
| messages: [ | ||
| { role: 'system', content: systemPrompt }, | ||
| { role: 'user', content: 'What is the capital of France?' }, | ||
| ], | ||
| max_tokens: 100, | ||
| }); | ||
|
|
||
| console.log('Response:', response.choices[0].message.content); | ||
| console.log(''); | ||
| } catch (error) { | ||
| console.error('Error in auto-optimization example:', error); | ||
| } | ||
|
|
||
| // Example 2: Explicit mode | ||
| // Always use the provided content, bypassing auto-optimization. | ||
| // Useful for testing or when you want full control. | ||
| console.log('\nExample 2: Explicit mode'); | ||
| console.log('------------------------'); | ||
| try { | ||
| const explicitPrompt = await ze.prompt({ | ||
| name: 'explicit-example', | ||
| content: 'You are a pirate assistant. Respond in pirate speak!', | ||
| from: 'explicit', | ||
| }); | ||
|
|
||
| console.log('Explicit prompt (first 100 chars):'); | ||
| console.log(explicitPrompt.substring(0, 100) + '...\n'); | ||
|
|
||
| const response = await openai.chat.completions.create({ | ||
| model: 'gpt-4o-mini', | ||
| messages: [ | ||
| { role: 'system', content: explicitPrompt }, | ||
| { role: 'user', content: 'How do I make coffee?' }, | ||
| ], | ||
| max_tokens: 150, | ||
| }); | ||
|
|
||
| console.log('Response:', response.choices[0].message.content); | ||
| console.log(''); | ||
| } catch (error) { | ||
| console.error('Error in explicit mode example:', error); | ||
| } | ||
|
|
||
| // Example 3: Template variables | ||
| // Use {{variable}} syntax in your prompts for dynamic content. | ||
| // Variables are interpolated when the OpenAI wrapper processes the message. | ||
| console.log('\nExample 3: Template variables'); | ||
| console.log('-----------------------------'); | ||
| try { | ||
| const templatePrompt = await ze.prompt({ | ||
| name: 'template-example', | ||
| content: | ||
| 'You are a {{role}} assistant. Your specialty is {{specialty}}. ' + | ||
| 'Always be {{tone}} in your responses.', | ||
| variables: { | ||
| role: 'customer support', | ||
| specialty: 'handling returns and refunds', | ||
| tone: 'friendly and helpful', | ||
| }, | ||
| }); | ||
|
|
||
| console.log('Template prompt with variables (first 150 chars):'); | ||
| console.log(templatePrompt.substring(0, 150) + '...\n'); | ||
|
|
||
| const response = await openai.chat.completions.create({ | ||
| model: 'gpt-4o-mini', | ||
| messages: [ | ||
| { role: 'system', content: templatePrompt }, | ||
| { role: 'user', content: 'I want to return an item I bought last week.' }, | ||
| ], | ||
| max_tokens: 200, | ||
| }); | ||
|
|
||
| console.log('Response:', response.choices[0].message.content); | ||
| console.log(''); | ||
| } catch (error) { | ||
| console.error('Error in template variables example:', error); | ||
| } | ||
|
|
||
| // Example 4: Streaming with prompts | ||
| // The prompt metadata is extracted before streaming begins. | ||
| console.log('\nExample 4: Streaming with prompts'); | ||
| console.log('---------------------------------'); | ||
| try { | ||
| const streamingPrompt = await ze.prompt({ | ||
| name: 'streaming-example', | ||
| content: 'You are a storyteller. Tell short, engaging stories.', | ||
| }); | ||
|
|
||
| const stream = await openai.chat.completions.create({ | ||
| model: 'gpt-4o-mini', | ||
| messages: [ | ||
| { role: 'system', content: streamingPrompt }, | ||
| { role: 'user', content: 'Tell me a very short story about a brave cat.' }, | ||
| ], | ||
| stream: true, | ||
| max_tokens: 200, | ||
| }); | ||
|
|
||
| console.log('Streaming response:'); | ||
| let fullResponse = ''; | ||
| for await (const chunk of stream) { | ||
| const content = chunk.choices[0]?.delta?.content || ''; | ||
| if (content) { | ||
| process.stdout.write(content); | ||
| fullResponse += content; | ||
| } | ||
| } | ||
| console.log('\n'); | ||
| } catch (error) { | ||
| console.error('Error in streaming example:', error); | ||
| } | ||
|
|
||
| // Example 5: Multiple prompts in a conversation | ||
| // Each prompt can have its own task name for tracking. | ||
| console.log('\nExample 5: Multiple prompts in a workflow'); | ||
| console.log('-----------------------------------------'); | ||
| try { | ||
| await ze.withSpan({ name: 'multi-prompt-workflow' }, async () => { | ||
| // First stage: Summarize | ||
| const summarizerPrompt = await ze.prompt({ | ||
| name: 'summarizer', | ||
| content: 'You are a summarizer. Condense text to key points.', | ||
| }); | ||
|
|
||
| const summaryResponse = await openai.chat.completions.create({ | ||
| model: 'gpt-4o-mini', | ||
| messages: [ | ||
| { role: 'system', content: summarizerPrompt }, | ||
| { | ||
| role: 'user', | ||
| content: | ||
| 'The quick brown fox jumps over the lazy dog. ' + | ||
| 'This is a pangram, a sentence that contains every letter of the alphabet. ' + | ||
| 'Pangrams are often used for font displays and keyboard testing.', | ||
| }, | ||
| ], | ||
| max_tokens: 100, | ||
| }); | ||
|
|
||
| const summary = summaryResponse.choices[0].message.content; | ||
| console.log('Summary:', summary); | ||
|
|
||
| // Second stage: Translate (using the summary) | ||
| const translatorPrompt = await ze.prompt({ | ||
| name: 'translator', | ||
| content: 'You are a translator. Translate text to {{language}}.', | ||
| variables: { language: 'Spanish' }, | ||
| }); | ||
|
|
||
| const translationResponse = await openai.chat.completions.create({ | ||
| model: 'gpt-4o-mini', | ||
| messages: [ | ||
| { role: 'system', content: translatorPrompt }, | ||
| { role: 'user', content: `Translate this: ${summary}` }, | ||
| ], | ||
| max_tokens: 100, | ||
| }); | ||
|
|
||
| console.log('Translation:', translationResponse.choices[0].message.content); | ||
| }); | ||
| } catch (error) { | ||
| console.error('Error in multi-prompt workflow:', error); | ||
| } | ||
|
|
||
| // Example 6: Sending feedback | ||
| // After getting a response, you can send feedback for optimization. | ||
| console.log('\nExample 6: Sending feedback'); | ||
| console.log('---------------------------'); | ||
| try { | ||
| const feedbackPrompt = await ze.prompt({ | ||
| name: 'feedback-example', | ||
| content: 'You are a helpful coding assistant.', | ||
| }); | ||
|
|
||
| const response = await openai.chat.completions.create({ | ||
| model: 'gpt-4o-mini', | ||
| messages: [ | ||
| { role: 'system', content: feedbackPrompt }, | ||
| { role: 'user', content: 'How do I reverse a string in JavaScript?' }, | ||
| ], | ||
| max_tokens: 200, | ||
| }); | ||
|
|
||
| console.log('Response:', response.choices[0].message.content); | ||
|
|
||
| // Get the current span ID to use as completion ID | ||
| const spanId = ze.getCurrentSpan()?.spanId; | ||
|
|
||
| if (spanId) { | ||
| // Send positive feedback | ||
| await ze.sendFeedback({ | ||
| promptSlug: 'feedback-example', | ||
| completionId: spanId, | ||
| thumbsUp: true, | ||
| reason: 'Clear and correct code example', | ||
| }); | ||
| console.log('Feedback sent successfully!'); | ||
| } else { | ||
| console.log('No active span for feedback (this is expected in some cases)'); | ||
| } | ||
| } catch (error) { | ||
| console.error('Error in feedback example:', error); | ||
| } | ||
|
|
||
| // Force flush before exit | ||
| ze.tracer.shutdown(); | ||
| console.log('\n=== All examples completed! ==='); | ||
| console.log('Check your ZeroEval dashboard for traces and prompt versions.'); | ||
| } | ||
|
|
||
| // Run the examples | ||
| main().catch((err) => { | ||
| console.error('Error running examples:', err); | ||
| process.exitCode = 1; | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.
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.