-
Notifications
You must be signed in to change notification settings - Fork 46
fix(exp): Add run in github experiment #837
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
789026f
added
nina-kollman 3f23a68
Merge branch 'main' of https://github.com/traceloop/openllmetry-js in…
nina-kollman 6f9a2e4
real setting
nina-kollman 8b5c4be
setting
nina-kollman 5573148
Merge branch 'main' of https://github.com/traceloop/openllmetry-js in…
nina-kollman 11092cd
works
nina-kollman 0619959
pretty
nina-kollman 6a12586
pr comments
nina-kollman d3573c7
pretty
nina-kollman 9db388d
pr comment
nina-kollman b4a5572
pretty
nina-kollman 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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import * as traceloop from "@traceloop/node-server-sdk"; | ||
| import { OpenAI } from "openai"; | ||
| import type { | ||
| ExperimentTaskFunction, | ||
| TaskInput, | ||
| TaskOutput, | ||
| } from "@traceloop/node-server-sdk"; | ||
|
|
||
| import "dotenv/config"; | ||
|
|
||
| const main = async () => { | ||
| console.log("Starting GitHub experiment sample"); | ||
| traceloop.initialize({ | ||
| appName: "sample_github_experiment", | ||
| apiKey: process.env.TRACELOOP_API_KEY, | ||
| disableBatch: true, | ||
| traceloopSyncEnabled: true, | ||
| }); | ||
|
|
||
| try { | ||
| await traceloop.waitForInitialization(); | ||
| } catch (error) { | ||
| console.error( | ||
| "Failed to initialize Traceloop SDK:", | ||
| error instanceof Error ? error.message : String(error), | ||
| ); | ||
| console.error("Initialization error details:", error); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const client = traceloop.getClient(); | ||
| if (!client) { | ||
| console.error("Failed to initialize Traceloop client"); | ||
| return; | ||
| } | ||
|
|
||
| console.log("🚀 GitHub Experiment Sample Application"); | ||
| console.log("=======================================\n"); | ||
|
|
||
| // Initialize OpenAI client | ||
| const openai = new OpenAI({ | ||
| apiKey: process.env.OPENAI_API_KEY, | ||
| }); | ||
|
|
||
| /** | ||
| * Example task function that generates a response using OpenAI | ||
| * This will be executed locally in GitHub Actions | ||
| */ | ||
| const researchTask: ExperimentTaskFunction = async ( | ||
| row: TaskInput, | ||
| ): Promise<TaskOutput> => { | ||
| console.log(`Processing question: ${row.query}`); | ||
| const question = String(row.query ?? ""); | ||
|
|
||
| const response = await openai.chat.completions.create({ | ||
| model: "gpt-4o", | ||
| messages: [ | ||
| { | ||
| role: "system", | ||
| content: | ||
| "You are a helpful research assistant. Provide concise, accurate answers.", | ||
| }, | ||
| { role: "user", content: question }, | ||
| ], | ||
| temperature: 0.7, | ||
| max_tokens: 500, | ||
| }); | ||
|
|
||
| const answer = response.choices?.[0]?.message?.content || ""; | ||
|
|
||
| return { | ||
| question, | ||
| sentence: answer, | ||
| completion: answer, | ||
| }; | ||
| }; | ||
|
|
||
| try { | ||
| console.log("\n🧪 Running experiment..."); | ||
| console.log( | ||
| " If in GitHub Actions, will run in GitHub context automatically\n", | ||
| ); | ||
|
|
||
| const results = await client.experiment.run(researchTask, { | ||
| datasetSlug: "research-queries", | ||
| datasetVersion: "v2", | ||
| evaluators: [ | ||
| "research-relevancy", | ||
| "categories", | ||
| "research-facts-counter", | ||
| ], | ||
| experimentSlug: "research-ts", | ||
| }); | ||
|
|
||
| console.log("\n✅ Experiment completed successfully!"); | ||
| console.log("Results:", JSON.stringify(results, null, 2)); | ||
|
|
||
| // Check if this is a GitHub response (has experimentSlug but not taskResults) | ||
| if ("taskResults" in results) { | ||
| // Local execution result | ||
| console.log(`\n - Task Results: ${results.taskResults.length}`); | ||
| console.log(` - Errors: ${results.errors.length}`); | ||
| if (results.experimentId) { | ||
| console.log(` - Experiment ID: ${results.experimentId}`); | ||
| } | ||
| } else { | ||
| // GitHub execution result | ||
| console.log( | ||
| "\n💡 Results will be posted as a comment on the pull request by the backend", | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| console.error( | ||
| "\n❌ Error in GitHub experiment:", | ||
| error instanceof Error ? error.message : String(error), | ||
| ); | ||
| if (error instanceof Error && error.stack) { | ||
| console.error("Stack trace:", error.stack); | ||
| } | ||
| process.exit(1); | ||
| } | ||
| }; | ||
|
|
||
| // Error handling for the main function | ||
| main().catch((error) => { | ||
| console.error("💥 Application failed:", error.message); | ||
| process.exit(1); | ||
| }); |
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
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.