Skip to content

Move runTask to common evals package #144

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 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/sandbox-container/evals/exec.eval.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { expect } from 'vitest'
import { describeEval } from 'vitest-evals'

import { runTask } from '@repo/eval-tools/src/runTask'
import { checkFactuality } from '@repo/eval-tools/src/scorers'
import { eachModel } from '@repo/eval-tools/src/test-models'

import { initializeClient, runTask } from './utils'
import { initializeClient } from './utils'

eachModel('$modelName', ({ model }) => {
describeEval('Runs a python file in a container', {
Expand Down
3 changes: 2 additions & 1 deletion apps/sandbox-container/evals/files.eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { assert, expect } from 'vitest'
import { describeEval } from 'vitest-evals'
import { z } from 'zod'

import { runTask } from '@repo/eval-tools/src/runTask'
import { checkFactuality } from '@repo/eval-tools/src/scorers'
import { eachModel } from '@repo/eval-tools/src/test-models'

import { initializeClient, runTask } from './utils'
import { initializeClient } from './utils'

eachModel('$modelName', ({ model }) => {
describeEval('Runs container file write', {
Expand Down
3 changes: 2 additions & 1 deletion apps/sandbox-container/evals/initialize.eval.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { describeEval } from 'vitest-evals'

import { runTask } from '@repo/eval-tools/src/runTask'
import { checkFactuality } from '@repo/eval-tools/src/scorers'
import { eachModel } from '@repo/eval-tools/src/test-models'

import { initializeClient, runTask } from './utils'
import { initializeClient } from './utils'

eachModel('$modelName', ({ model }) => {
describeEval('Runs container initialize', {
Expand Down
82 changes: 0 additions & 82 deletions apps/sandbox-container/evals/utils.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,7 @@
import { MCPClientManager } from 'agents/mcp/client'
import { jsonSchema, streamText, tool } from 'ai'
import { z } from 'zod'

import type { LanguageModelV1, StreamTextResult, ToolCallPart, ToolSet } from 'ai'

export async function initializeClient(): Promise<MCPClientManager> {
const clientManager = new MCPClientManager('test-client', '0.0.0')
await clientManager.connect('http://localhost:8976/sse')
return clientManager
}

export async function runTask(
clientManager: MCPClientManager,
model: LanguageModelV1,
input: string
): Promise<{
promptOutput: string
fullResult: StreamTextResult<ToolSet, never>
toolCalls: ToolCallPart[]
}> {
const tools = clientManager.listTools()
const toolSet: ToolSet = tools.reduce((acc, v) => {
if (!v.inputSchema.properties) {
v.inputSchema.properties = {}
}

acc[v.name] = tool({
parameters: jsonSchema(v.inputSchema as any),
description: v.description,
execute: async (args: any, opts) => {
try {
const res = await clientManager.callTool(
{
...v,
arguments: { ...args },
},
z.any() as any,
{ signal: opts.abortSignal }
)
return res.content
} catch (e) {
console.log('Error calling tool')
console.log(e)
return e
}
},
})
return acc
}, {} as ToolSet)

const res = streamText({
model,
system:
"You are an assistant responsible for evaluating the results of calling various tools. Given the user's query, use the tools available to you to answer the question.",
tools: toolSet,
prompt: input,
maxRetries: 1,
maxSteps: 10,
})

// consume the stream
// eslint-disable-next-line no-empty
for await (const _ of res.fullStream) {
}

// convert into an LLM readable result so our factuality checker can validate tool calls
let messagesWithTools = ''
const toolCalls: ToolCallPart[] = []
const messages = (await res.response).messages
for (const message of messages) {
console.log(message.content)
for (const messagePart of message.content) {
if (typeof messagePart === 'string') {
messagesWithTools += `<message_content type="text">${messagePart}</message_content>`
} else if (messagePart.type === 'tool-call') {
messagesWithTools += `<message_content type=${messagePart.type}>
<tool_name>${messagePart.toolName}</tool_name>
<tool_arguments>${JSON.stringify(messagePart.args)}</tool_arguments>
</message_content>`
toolCalls.push(messagePart)
} else if (messagePart.type === 'text') {
messagesWithTools += `<message_content type=${messagePart.type}>${messagePart.text}</message_content>`
}
}
}

return { promptOutput: messagesWithTools, fullResult: res, toolCalls }
}
3 changes: 2 additions & 1 deletion apps/workers-bindings/evals/accounts.eval.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { expect } from 'vitest'
import { describeEval } from 'vitest-evals'

import { runTask } from '@repo/eval-tools/src/runTask'
import { checkFactuality } from '@repo/eval-tools/src/scorers'
import { eachModel } from '@repo/eval-tools/src/test-models'

import { initializeClient, runTask } from './utils' // Assuming utils.ts will exist here
import { initializeClient } from './utils' // Assuming utils.ts will exist here

// Define a mock account ID for testing
const MOCK_ACCOUNT_ID = 'mock-account-12345'
Expand Down
3 changes: 2 additions & 1 deletion apps/workers-bindings/evals/hyperdrive.eval.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expect } from 'vitest'
import { describeEval } from 'vitest-evals'

import { runTask } from '@repo/eval-tools/src/runTask'
import { checkFactuality } from '@repo/eval-tools/src/scorers'
import { eachModel } from '@repo/eval-tools/src/test-models'
import { HYPERDRIVE_TOOLS } from '@repo/mcp-common/src/tools/hyperdrive'

import { initializeClient, runTask } from './utils' // Assuming utils.ts will exist here
import { initializeClient } from './utils' // Assuming utils.ts will exist here

// TODO: Add test for creating hyperdrive config with the following params once we can securely pass parameters to the tool. See: https://github.com/modelcontextprotocol/modelcontextprotocol/pull/382
// const HYPERDRIVE_NAME = 'neon-test-hyperdrive'
Expand Down
3 changes: 2 additions & 1 deletion apps/workers-bindings/evals/kv_namespaces.eval.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expect } from 'vitest'
import { describeEval } from 'vitest-evals'

import { runTask } from '@repo/eval-tools/src/runTask'
import { checkFactuality } from '@repo/eval-tools/src/scorers'
import { eachModel } from '@repo/eval-tools/src/test-models'
import { KV_NAMESPACE_TOOLS } from '@repo/mcp-common/src/tools/kv_namespace'

import { initializeClient, runTask } from './utils' // Assuming utils.ts will exist here
import { initializeClient } from './utils' // Assuming utils.ts will exist here

eachModel('$modelName', ({ model }) => {
describeEval('Create Cloudflare KV Namespace', {
Expand Down
83 changes: 0 additions & 83 deletions apps/workers-bindings/evals/utils.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,7 @@
import { MCPClientManager } from 'agents/mcp/client'
import { jsonSchema, streamText, tool } from 'ai'
import { z } from 'zod'

import type { LanguageModelV1, StreamTextResult, ToolCallPart, ToolSet } from 'ai'

export async function initializeClient(): Promise<MCPClientManager> {
const clientManager = new MCPClientManager('test-client', '0.0.0')
await clientManager.connect('http://localhost:8977/sse')
return clientManager
}

export async function runTask(
clientManager: MCPClientManager,
model: LanguageModelV1,
input: string
): Promise<{
promptOutput: string
fullResult: StreamTextResult<ToolSet, never>
toolCalls: ToolCallPart[]
}> {
const tools = clientManager.listTools()
const toolSet: ToolSet = tools.reduce((acc, v) => {
if (!v.inputSchema.properties) {
v.inputSchema.properties = {}
}

acc[v.name] = tool({
parameters: jsonSchema(v.inputSchema as any),
description: v.description,
execute: async (args: any, opts) => {
try {
const res = await clientManager.callTool(
{
...v,
arguments: { ...args },
},
z.any() as any,
{ signal: opts.abortSignal }
)
return res.content
} catch (e) {
console.log('Error calling tool')
console.log(e)
return e
}
},
})
return acc
}, {} as ToolSet)

const res = streamText({
model,
system:
"You are an assistant responsible for evaluating the results of calling various tools. Given the user's query, use the tools available to you to answer the question.",
tools: toolSet,
prompt: input,
maxRetries: 1,
maxSteps: 10,
})

// we need to consume the fill stream, so this is empty
// eslint-disable-next-line no-empty
for await (const _ of res.fullStream) {
}

// convert into an LLM readable result so our factuality checker can validate tool calls
let messagesWithTools = ''
const toolCalls: ToolCallPart[] = []
const response = await res.response
const messages = response.messages

for (const message of messages) {
for (const messagePart of message.content) {
if (typeof messagePart === 'string') {
messagesWithTools += `<message_content type="text">${messagePart}</message_content>`
} else if (messagePart.type === 'tool-call') {
messagesWithTools += `<message_content type=${messagePart.type}>
<tool_name>${messagePart.toolName}</tool_name>
<tool_arguments>${JSON.stringify(messagePart.args)}</tool_arguments>
</message_content>`
toolCalls.push(messagePart)
} else if (messagePart.type === 'text') {
messagesWithTools += `<message_content type=${messagePart.type}>${messagePart.text}</message_content>`
}
}
}

return { promptOutput: messagesWithTools, fullResult: res, toolCalls }
}
1 change: 1 addition & 0 deletions packages/eval-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"@ai-sdk/openai": "1.3.20",
"@cloudflare/vitest-pool-workers": "0.8.14",
"agents": "0.0.67",
"ai": "4.3.10",
"workers-ai-provider": "0.3.0",
"wrangler": "4.10.0",
Expand Down
84 changes: 84 additions & 0 deletions packages/eval-tools/src/runTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { type MCPClientManager } from 'agents/mcp/client'
import { jsonSchema, streamText, tool } from 'ai'
import { z } from 'zod'

import type { LanguageModelV1, StreamTextResult, ToolCallPart, ToolSet } from 'ai'

export async function runTask(
clientManager: MCPClientManager,
model: LanguageModelV1,
input: string
): Promise<{
promptOutput: string
fullResult: StreamTextResult<ToolSet, never>
toolCalls: ToolCallPart[]
}> {
const tools = clientManager.listTools()
const toolSet: ToolSet = tools.reduce((acc, v) => {
if (!v.inputSchema.properties) {
v.inputSchema.properties = {}
}

acc[v.name] = tool({
parameters: jsonSchema(v.inputSchema as any),
description: v.description,
execute: async (args: any, opts) => {
try {
const res = await clientManager.callTool(
{
...v,
arguments: { ...args },
},
z.any() as any,
{ signal: opts.abortSignal }
)
return res.content
} catch (e) {
console.log('Error calling tool')
console.log(e)
return e
}
},
})
return acc
}, {} as ToolSet)

const res = streamText({
model,
system:
"You are an assistant responsible for evaluating the results of calling various tools. Given the user's query, use the tools available to you to answer the question.",
tools: toolSet,
prompt: input,
maxRetries: 1,
maxSteps: 10,
})

// we need to consume the fill stream, so this is empty
// eslint-disable-next-line no-empty
for await (const _ of res.fullStream) {
}

// convert into an LLM readable result so our factuality checker can validate tool calls
let messagesWithTools = ''
const toolCalls: ToolCallPart[] = []
const response = await res.response
const messages = response.messages

for (const message of messages) {
for (const messagePart of message.content) {
if (typeof messagePart === 'string') {
messagesWithTools += `<message_content type="text">${messagePart}</message_content>`
} else if (messagePart.type === 'tool-call') {
messagesWithTools += `<message_content type=${messagePart.type}>
<tool_name>${messagePart.toolName}</tool_name>
<tool_arguments>${JSON.stringify(messagePart.args)}</tool_arguments>
</message_content>`
toolCalls.push(messagePart)
} else if (messagePart.type === 'text') {
messagesWithTools += `<message_content type=${messagePart.type}>${messagePart.text}</message_content>`
}
}
}

return { promptOutput: messagesWithTools, fullResult: res, toolCalls }
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading