-
Notifications
You must be signed in to change notification settings - Fork 75
feat: Add Agent Behavior scorer for Python and TypeScript #208
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
Open
Abhijeet Prasad (AbhiPrasad)
wants to merge
3
commits into
main
Choose a base branch
from
abhi-feat-add-agent-behavior-scorer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
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,197 @@ | ||
| import { promises as fs } from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
|
|
||
| import { http, HttpResponse } from "msw"; | ||
| import { setupServer } from "msw/node"; | ||
| import { OpenAI } from "openai"; | ||
| import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest"; | ||
|
|
||
| import { Behavior, discoverAgentBehaviors } from "./llm"; | ||
|
|
||
| const server = setupServer(); | ||
| const behaviorProjects = new Set<string>(); | ||
|
|
||
| beforeAll(() => server.listen({ onUnhandledRequest: "error" })); | ||
| afterEach(async () => { | ||
| server.resetHandlers(); | ||
| await Promise.all( | ||
| [...behaviorProjects].map((root) => | ||
| fs.rm(root, { recursive: true, force: true }), | ||
| ), | ||
| ); | ||
| behaviorProjects.clear(); | ||
| }); | ||
| afterAll(() => server.close()); | ||
|
|
||
| async function createBehaviorProject(name = "verify-work") { | ||
| const root = await fs.mkdtemp(path.join(os.tmpdir(), "autoevals-behavior-")); | ||
| behaviorProjects.add(root); | ||
| const directory = path.join(root, ".agents", "behaviors", name); | ||
| await fs.mkdir(directory, { recursive: true }); | ||
| await fs.writeFile( | ||
| path.join(directory, "BEHAVIOR.md"), | ||
| `---\nname: ${name}\ndescription: Verify work before answering.\n---\n# Verify work\n\nThe agent MUST show its calculation.\n`, | ||
| ); | ||
| return root; | ||
| } | ||
|
|
||
| describe("Behavior", () => { | ||
| test("discovers a valid BEHAVIOR.md from a project root", async () => { | ||
| const root = await createBehaviorProject(); | ||
| const behaviors = await discoverAgentBehaviors(root); | ||
|
|
||
| expect(behaviors).toHaveLength(1); | ||
| expect(behaviors[0]).toMatchObject({ | ||
| name: "verify-work", | ||
| description: "Verify work before answering.", | ||
| }); | ||
| expect(behaviors[0]?.location).toBe( | ||
| path.join(root, ".agents", "behaviors", "verify-work", "BEHAVIOR.md"), | ||
| ); | ||
| }); | ||
|
|
||
| test("judges structured output against an explicitly provided behavior", async () => { | ||
| let prompt = ""; | ||
| let choice: "true" | "na" = "true"; | ||
| server.use( | ||
| http.post( | ||
| "https://api.openai.com/v1/chat/completions", | ||
| async ({ request }) => { | ||
| const body = (await request.json()) as { | ||
| messages: Array<{ content: string }>; | ||
| }; | ||
| prompt = body.messages[0]!.content; | ||
| return HttpResponse.json({ | ||
| id: "chatcmpl-behavior", | ||
| object: "chat.completion", | ||
| created: 0, | ||
| model: "gpt-4o-mini", | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| finish_reason: "tool_calls", | ||
| message: { | ||
| role: "assistant", | ||
| content: null, | ||
| tool_calls: [ | ||
| { | ||
| id: "call-behavior", | ||
| type: "function", | ||
| function: { | ||
| name: "select_choice", | ||
| arguments: JSON.stringify({ | ||
| choice, | ||
| reasons: "The calculation is visible.", | ||
| }), | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, | ||
| }); | ||
| }, | ||
| ), | ||
| ); | ||
|
|
||
| const result = await Behavior({ | ||
| behavior: { | ||
| name: "verify-work", | ||
| description: "Verify work before answering.", | ||
| body: "# Verify work\n\nThe agent MUST show its calculation.", | ||
| }, | ||
| input: { question: "What is 2 + 2?" }, | ||
| output: { events: [{ type: "answer", content: "2 + 2 = 4" }] }, | ||
| trace: { | ||
| getThread: async () => [ | ||
| { role: "system", content: "Use the calculator." }, | ||
| { role: "user", content: "What is 2 + 2?" }, | ||
| { role: "assistant", content: "2 + 2 = 4" }, | ||
| ], | ||
| }, | ||
| model: "gpt-4o-mini", | ||
| client: new OpenAI({ | ||
| apiKey: "test", | ||
| baseURL: "https://api.openai.com/v1", | ||
| }), | ||
| }); | ||
|
|
||
| expect(result.score).toBe(1); | ||
| expect(result.metadata?.choice).toBe("true"); | ||
| expect(result.metadata?.behavior).toMatchObject({ name: "verify-work" }); | ||
| expect(prompt).toContain("The agent MUST show its calculation."); | ||
| expect(prompt).toContain('"question":"What is 2 + 2?"'); | ||
| expect(prompt).toContain("System:\n Use the calculator."); | ||
|
|
||
| choice = "na"; | ||
| const notApplicable = await Behavior({ | ||
| behavior: { | ||
| name: "verify-work", | ||
| description: "Verify work before answering.", | ||
| body: "# Verify work\n\nThe agent MUST show its calculation.", | ||
| }, | ||
| output: "No calculation was requested.", | ||
| model: "gpt-4o-mini", | ||
| client: new OpenAI({ | ||
| apiKey: "test", | ||
| baseURL: "https://api.openai.com/v1", | ||
| }), | ||
| }); | ||
| expect(notApplicable.score).toBeNull(); | ||
|
|
||
| const root = await createBehaviorProject(); | ||
| await fs.writeFile( | ||
| path.join(root, "verify-work"), | ||
| "unrelated project file", | ||
| ); | ||
| choice = "true"; | ||
| const namedBehavior = await Behavior({ | ||
| behavior: "verify-work", | ||
| behaviorRoot: root, | ||
| output: "2 + 2 = 4", | ||
| model: "gpt-4o-mini", | ||
| client: new OpenAI({ | ||
| apiKey: "test", | ||
| baseURL: "https://api.openai.com/v1", | ||
| }), | ||
| }); | ||
| expect(namedBehavior.metadata?.behavior).toMatchObject({ | ||
| name: "verify-work", | ||
| }); | ||
| }); | ||
|
|
||
| test("includes invalid spec diagnostics when discovery finds no valid behavior", async () => { | ||
| const root = await createBehaviorProject(); | ||
| const behaviorFile = path.join( | ||
| root, | ||
| ".agents", | ||
| "behaviors", | ||
| "verify-work", | ||
| "BEHAVIOR.md", | ||
| ); | ||
| await fs.writeFile( | ||
| behaviorFile, | ||
| "---\nname: INVALID\ndescription: Invalid behavior.\n---\n# Invalid\n", | ||
| ); | ||
|
|
||
| await expect( | ||
| Behavior({ output: "done", behaviorRoot: root, model: "gpt-4o-mini" }), | ||
| ).rejects.toThrow("Diagnostics: Agent Behavior name"); | ||
| }); | ||
|
|
||
| test("requires an explicit selection when discovery finds multiple behaviors", async () => { | ||
| const root = await createBehaviorProject("first-behavior"); | ||
| const second = path.join(root, ".agents", "behaviors", "second-behavior"); | ||
| await fs.mkdir(second, { recursive: true }); | ||
| await fs.writeFile( | ||
| path.join(second, "BEHAVIOR.md"), | ||
| "---\nname: second-behavior\ndescription: A second behavior.\n---\n# Second\n", | ||
| ); | ||
|
|
||
| await expect( | ||
| Behavior({ output: "done", behaviorRoot: root, model: "gpt-4o-mini" }), | ||
| ).rejects.toThrow("Multiple Agent Behavior specs were discovered"); | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want to hit openai directly via
http(vs. an sdk) each time we test?