@orq-ai/evaluatorq 1.2.2 → 1.2.3-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/integrations/ai-sdk/index.d.ts +2 -0
- package/dist/lib/integrations/ai-sdk/index.d.ts.map +1 -1
- package/dist/lib/integrations/ai-sdk/index.js +1 -0
- package/dist/lib/integrations/ai-sdk/simulation-adapter.d.ts +47 -0
- package/dist/lib/integrations/ai-sdk/simulation-adapter.d.ts.map +1 -0
- package/dist/lib/integrations/ai-sdk/simulation-adapter.js +58 -0
- package/dist/lib/integrations/langchain/index.d.ts +2 -0
- package/dist/lib/integrations/langchain/index.d.ts.map +1 -1
- package/dist/lib/integrations/langchain/index.js +1 -0
- package/dist/lib/integrations/langchain/simulation-adapter.d.ts +49 -0
- package/dist/lib/integrations/langchain/simulation-adapter.d.ts.map +1 -0
- package/dist/lib/integrations/langchain/simulation-adapter.js +110 -0
- package/dist/lib/integrations/simulation/adapters.d.ts +57 -0
- package/dist/lib/integrations/simulation/adapters.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/adapters.js +64 -0
- package/dist/lib/integrations/simulation/agents/base.d.ts +90 -0
- package/dist/lib/integrations/simulation/agents/base.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/agents/base.js +227 -0
- package/dist/lib/integrations/simulation/agents/index.d.ts +10 -0
- package/dist/lib/integrations/simulation/agents/index.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/agents/index.js +6 -0
- package/dist/lib/integrations/simulation/agents/judge.d.ts +50 -0
- package/dist/lib/integrations/simulation/agents/judge.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/agents/judge.js +313 -0
- package/dist/lib/integrations/simulation/agents/user-simulator.d.ts +41 -0
- package/dist/lib/integrations/simulation/agents/user-simulator.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/agents/user-simulator.js +82 -0
- package/dist/lib/integrations/simulation/convert.d.ts +22 -0
- package/dist/lib/integrations/simulation/convert.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/convert.js +124 -0
- package/dist/lib/integrations/simulation/evaluators/index.d.ts +50 -0
- package/dist/lib/integrations/simulation/evaluators/index.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/evaluators/index.js +100 -0
- package/dist/lib/integrations/simulation/generators/datapoint-generator.d.ts +60 -0
- package/dist/lib/integrations/simulation/generators/datapoint-generator.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/generators/datapoint-generator.js +223 -0
- package/dist/lib/integrations/simulation/generators/first-message-generator.d.ts +38 -0
- package/dist/lib/integrations/simulation/generators/first-message-generator.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/generators/first-message-generator.js +131 -0
- package/dist/lib/integrations/simulation/generators/index.d.ts +15 -0
- package/dist/lib/integrations/simulation/generators/index.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/generators/index.js +10 -0
- package/dist/lib/integrations/simulation/generators/persona-generator.d.ts +60 -0
- package/dist/lib/integrations/simulation/generators/persona-generator.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/generators/persona-generator.js +333 -0
- package/dist/lib/integrations/simulation/generators/scenario-generator.d.ts +77 -0
- package/dist/lib/integrations/simulation/generators/scenario-generator.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/generators/scenario-generator.js +545 -0
- package/dist/lib/integrations/simulation/index.d.ts +33 -0
- package/dist/lib/integrations/simulation/index.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/index.js +35 -0
- package/dist/lib/integrations/simulation/quality/index.d.ts +5 -0
- package/dist/lib/integrations/simulation/quality/index.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/quality/index.js +4 -0
- package/dist/lib/integrations/simulation/quality/message-perturbation.d.ts +25 -0
- package/dist/lib/integrations/simulation/quality/message-perturbation.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/quality/message-perturbation.js +150 -0
- package/dist/lib/integrations/simulation/runner/index.d.ts +5 -0
- package/dist/lib/integrations/simulation/runner/index.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/runner/index.js +4 -0
- package/dist/lib/integrations/simulation/runner/simulation.d.ts +57 -0
- package/dist/lib/integrations/simulation/runner/simulation.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/runner/simulation.js +336 -0
- package/dist/lib/integrations/simulation/schemas.d.ts +104 -0
- package/dist/lib/integrations/simulation/schemas.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/schemas.js +76 -0
- package/dist/lib/integrations/simulation/simulation/index.d.ts +49 -0
- package/dist/lib/integrations/simulation/simulation/index.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/simulation/index.js +159 -0
- package/dist/lib/integrations/simulation/types.d.ts +101 -0
- package/dist/lib/integrations/simulation/types.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/types.js +90 -0
- package/dist/lib/integrations/simulation/utils/dataset-export.d.ts +31 -0
- package/dist/lib/integrations/simulation/utils/dataset-export.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/utils/dataset-export.js +146 -0
- package/dist/lib/integrations/simulation/utils/extract-json.d.ts +17 -0
- package/dist/lib/integrations/simulation/utils/extract-json.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/utils/extract-json.js +106 -0
- package/dist/lib/integrations/simulation/utils/prompt-builders.d.ts +34 -0
- package/dist/lib/integrations/simulation/utils/prompt-builders.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/utils/prompt-builders.js +147 -0
- package/dist/lib/integrations/simulation/utils/sanitize.d.ts +15 -0
- package/dist/lib/integrations/simulation/utils/sanitize.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/utils/sanitize.js +20 -0
- package/dist/lib/integrations/simulation/wrap-agent.d.ts +65 -0
- package/dist/lib/integrations/simulation/wrap-agent.d.ts.map +1 -0
- package/dist/lib/integrations/simulation/wrap-agent.js +140 -0
- package/dist/lib/send-results.d.ts.map +1 -1
- package/dist/lib/send-results.js +17 -2
- package/dist/lib/types.d.ts +2 -2
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +24 -2
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { generateItemId } from "../common/index.js";
|
|
2
2
|
export { buildInputFromSteps, buildOpenResponsesFromSteps, convertToOpenResponses, } from "./convert.js";
|
|
3
|
+
export type { AISdkSimulationAdapterOptions } from "./simulation-adapter.js";
|
|
4
|
+
export { fromAISdkAgent } from "./simulation-adapter.js";
|
|
3
5
|
export type { AgentJobOptions, StepData } from "./types.js";
|
|
4
6
|
export { wrapAISdkAgent } from "./wrap-agent.js";
|
|
5
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/integrations/ai-sdk/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,sBAAsB,GACvB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/integrations/ai-sdk/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,sBAAsB,GACvB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,6BAA6B,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,YAAY,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// Re-export common utilities for backwards compatibility
|
|
2
2
|
export { generateItemId } from "../common/index.js";
|
|
3
3
|
export { buildInputFromSteps, buildOpenResponsesFromSteps, convertToOpenResponses, } from "./convert.js";
|
|
4
|
+
export { fromAISdkAgent } from "./simulation-adapter.js";
|
|
4
5
|
export { wrapAISdkAgent } from "./wrap-agent.js";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter to use Vercel AI SDK agents as simulation targets.
|
|
3
|
+
*
|
|
4
|
+
* Converts an AI SDK Agent into a `targetCallback` compatible with
|
|
5
|
+
* the simulation framework's `simulate()` and `wrapSimulationAgent()`.
|
|
6
|
+
*/
|
|
7
|
+
import type { Agent, ToolSet } from "ai";
|
|
8
|
+
import type { ChatMessage } from "../simulation/types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Options for the AI SDK simulation adapter.
|
|
11
|
+
*/
|
|
12
|
+
export interface AISdkSimulationAdapterOptions {
|
|
13
|
+
/**
|
|
14
|
+
* System instructions to prepend to every call.
|
|
15
|
+
* If omitted, no system message is added — the agent uses its own defaults.
|
|
16
|
+
*/
|
|
17
|
+
instructions?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Wraps a Vercel AI SDK Agent as a simulation `targetCallback`.
|
|
21
|
+
*
|
|
22
|
+
* The returned function accepts `ChatMessage[]` (the simulation's message format)
|
|
23
|
+
* and returns the agent's text response as a string.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { ToolLoopAgent, tool } from "ai";
|
|
28
|
+
* import { createOpenAI } from "@ai-sdk/openai";
|
|
29
|
+
* import { fromAISdkAgent } from "@orq-ai/evaluatorq/ai-sdk";
|
|
30
|
+
* import { simulate } from "@orq-ai/evaluatorq/simulation";
|
|
31
|
+
*
|
|
32
|
+
* const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
33
|
+
* const agent = new ToolLoopAgent({
|
|
34
|
+
* model: openai("gpt-4o"),
|
|
35
|
+
* tools: { ... },
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* const results = await simulate({
|
|
39
|
+
* evaluationName: "my-agent-sim",
|
|
40
|
+
* targetCallback: fromAISdkAgent(agent),
|
|
41
|
+
* personas: [...],
|
|
42
|
+
* scenarios: [...],
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function fromAISdkAgent<TOOLS extends ToolSet>(agent: Agent<never, TOOLS, never>, options?: AISdkSimulationAdapterOptions): (messages: ChatMessage[]) => Promise<string>;
|
|
47
|
+
//# sourceMappingURL=simulation-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation-adapter.d.ts","sourceRoot":"","sources":["../../../../src/lib/integrations/ai-sdk/simulation-adapter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAgB,OAAO,EAAE,MAAM,IAAI,CAAC;AAEvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAE1D;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,cAAc,CAAC,KAAK,SAAS,OAAO,EAClD,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,EACjC,OAAO,GAAE,6BAAkC,GAC1C,CAAC,QAAQ,EAAE,WAAW,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,CA8B9C"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter to use Vercel AI SDK agents as simulation targets.
|
|
3
|
+
*
|
|
4
|
+
* Converts an AI SDK Agent into a `targetCallback` compatible with
|
|
5
|
+
* the simulation framework's `simulate()` and `wrapSimulationAgent()`.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Wraps a Vercel AI SDK Agent as a simulation `targetCallback`.
|
|
9
|
+
*
|
|
10
|
+
* The returned function accepts `ChatMessage[]` (the simulation's message format)
|
|
11
|
+
* and returns the agent's text response as a string.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { ToolLoopAgent, tool } from "ai";
|
|
16
|
+
* import { createOpenAI } from "@ai-sdk/openai";
|
|
17
|
+
* import { fromAISdkAgent } from "@orq-ai/evaluatorq/ai-sdk";
|
|
18
|
+
* import { simulate } from "@orq-ai/evaluatorq/simulation";
|
|
19
|
+
*
|
|
20
|
+
* const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
|
21
|
+
* const agent = new ToolLoopAgent({
|
|
22
|
+
* model: openai("gpt-4o"),
|
|
23
|
+
* tools: { ... },
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* const results = await simulate({
|
|
27
|
+
* evaluationName: "my-agent-sim",
|
|
28
|
+
* targetCallback: fromAISdkAgent(agent),
|
|
29
|
+
* personas: [...],
|
|
30
|
+
* scenarios: [...],
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function fromAISdkAgent(agent, options = {}) {
|
|
35
|
+
const { instructions } = options;
|
|
36
|
+
return async (messages) => {
|
|
37
|
+
const modelMessages = [];
|
|
38
|
+
if (instructions) {
|
|
39
|
+
modelMessages.push({
|
|
40
|
+
role: "system",
|
|
41
|
+
content: instructions,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
for (const msg of messages) {
|
|
45
|
+
modelMessages.push({
|
|
46
|
+
role: msg.role,
|
|
47
|
+
content: msg.content,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const result = await agent.generate({ messages: modelMessages });
|
|
51
|
+
const text = result.text;
|
|
52
|
+
if (!text) {
|
|
53
|
+
console.warn("AI SDK fromAISdkAgent: agent result contained no text — returning empty string. " +
|
|
54
|
+
"If your agent only makes tool calls, ensure it also produces a final text response.");
|
|
55
|
+
}
|
|
56
|
+
return text ?? "";
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
24
|
export { convertToOpenResponses } from "./convert.js";
|
|
25
|
+
export type { LangChainSimulationAdapterOptions } from "./simulation-adapter.js";
|
|
26
|
+
export { fromLangChainAgent } from "./simulation-adapter.js";
|
|
25
27
|
export type { AgentJobOptions, ContentBlock, LangChainInvocable, LangChainMessage, LangChainResult, ResponseMetadata, ToolCall, ToolDefinition, UsageMetadata, } from "./types.js";
|
|
26
28
|
export { extractToolsFromAgent, wrapLangChainAgent, wrapLangGraphAgent, } from "./wrap-agent.js";
|
|
27
29
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/integrations/langchain/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,YAAY,EACV,eAAe,EACf,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,QAAQ,EACR,cAAc,EACd,aAAa,GACd,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,qBAAqB,EACrB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/integrations/langchain/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,YAAY,EAAE,iCAAiC,EAAE,MAAM,yBAAyB,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,YAAY,EACV,eAAe,EACf,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,QAAQ,EACR,cAAc,EACd,aAAa,GACd,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,qBAAqB,EACrB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter to use LangChain/LangGraph agents as simulation targets.
|
|
3
|
+
*
|
|
4
|
+
* Converts a LangChain invocable into a `targetCallback` compatible with
|
|
5
|
+
* the simulation framework's `simulate()` and `wrapSimulationAgent()`.
|
|
6
|
+
*/
|
|
7
|
+
import type { ChatMessage } from "../simulation/types.js";
|
|
8
|
+
import type { LangChainInvocable } from "./types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Options for the LangChain simulation adapter.
|
|
11
|
+
*/
|
|
12
|
+
export interface LangChainSimulationAdapterOptions {
|
|
13
|
+
/**
|
|
14
|
+
* System instructions to prepend to every call.
|
|
15
|
+
* If omitted, no system message is added — the agent uses its own defaults.
|
|
16
|
+
*/
|
|
17
|
+
instructions?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Wraps a LangChain/LangGraph agent as a simulation `targetCallback`.
|
|
21
|
+
*
|
|
22
|
+
* The returned function accepts `ChatMessage[]` (the simulation's message format)
|
|
23
|
+
* and returns the agent's text response as a string.
|
|
24
|
+
*
|
|
25
|
+
* Compatible with:
|
|
26
|
+
* - LangChain agents created via `createAgent()` (langchain 1.x)
|
|
27
|
+
* - LangGraph compiled graphs (`StateGraph.compile()`)
|
|
28
|
+
* - Any object with an `invoke({ messages })` method
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
33
|
+
* import { createReactAgent } from "@langchain/langgraph/prebuilt";
|
|
34
|
+
* import { fromLangChainAgent } from "@orq-ai/evaluatorq/langchain";
|
|
35
|
+
* import { simulate } from "@orq-ai/evaluatorq/simulation";
|
|
36
|
+
*
|
|
37
|
+
* const model = new ChatOpenAI({ model: "gpt-4o" });
|
|
38
|
+
* const agent = createReactAgent({ llm: model, tools: [...] });
|
|
39
|
+
*
|
|
40
|
+
* const results = await simulate({
|
|
41
|
+
* evaluationName: "my-agent-sim",
|
|
42
|
+
* targetCallback: fromLangChainAgent(agent),
|
|
43
|
+
* personas: [...],
|
|
44
|
+
* scenarios: [...],
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function fromLangChainAgent(agent: LangChainInvocable, options?: LangChainSimulationAdapterOptions): (messages: ChatMessage[]) => Promise<string>;
|
|
49
|
+
//# sourceMappingURL=simulation-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation-adapter.d.ts","sourceRoot":"","sources":["../../../../src/lib/integrations/langchain/simulation-adapter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,KAAK,EAAgB,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEnE;;GAEG;AACH,MAAM,WAAW,iCAAiC;IAChD;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AA8BD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,kBAAkB,EACzB,OAAO,GAAE,iCAAsC,GAC9C,CAAC,QAAQ,EAAE,WAAW,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,CAoE9C"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter to use LangChain/LangGraph agents as simulation targets.
|
|
3
|
+
*
|
|
4
|
+
* Converts a LangChain invocable into a `targetCallback` compatible with
|
|
5
|
+
* the simulation framework's `simulate()` and `wrapSimulationAgent()`.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Extract text content from a LangChain message's content field.
|
|
9
|
+
* Handles both string content and ContentBlock[] arrays.
|
|
10
|
+
*/
|
|
11
|
+
function extractTextContent(content) {
|
|
12
|
+
if (typeof content === "string") {
|
|
13
|
+
return content;
|
|
14
|
+
}
|
|
15
|
+
if (Array.isArray(content)) {
|
|
16
|
+
const parts = [];
|
|
17
|
+
for (const block of content) {
|
|
18
|
+
if (typeof block === "string") {
|
|
19
|
+
parts.push(block);
|
|
20
|
+
}
|
|
21
|
+
else if (typeof block === "object" &&
|
|
22
|
+
block !== null &&
|
|
23
|
+
block.type === "text") {
|
|
24
|
+
parts.push(block.text ?? "");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return parts.join("");
|
|
28
|
+
}
|
|
29
|
+
return content ? String(content) : "";
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Wraps a LangChain/LangGraph agent as a simulation `targetCallback`.
|
|
33
|
+
*
|
|
34
|
+
* The returned function accepts `ChatMessage[]` (the simulation's message format)
|
|
35
|
+
* and returns the agent's text response as a string.
|
|
36
|
+
*
|
|
37
|
+
* Compatible with:
|
|
38
|
+
* - LangChain agents created via `createAgent()` (langchain 1.x)
|
|
39
|
+
* - LangGraph compiled graphs (`StateGraph.compile()`)
|
|
40
|
+
* - Any object with an `invoke({ messages })` method
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
45
|
+
* import { createReactAgent } from "@langchain/langgraph/prebuilt";
|
|
46
|
+
* import { fromLangChainAgent } from "@orq-ai/evaluatorq/langchain";
|
|
47
|
+
* import { simulate } from "@orq-ai/evaluatorq/simulation";
|
|
48
|
+
*
|
|
49
|
+
* const model = new ChatOpenAI({ model: "gpt-4o" });
|
|
50
|
+
* const agent = createReactAgent({ llm: model, tools: [...] });
|
|
51
|
+
*
|
|
52
|
+
* const results = await simulate({
|
|
53
|
+
* evaluationName: "my-agent-sim",
|
|
54
|
+
* targetCallback: fromLangChainAgent(agent),
|
|
55
|
+
* personas: [...],
|
|
56
|
+
* scenarios: [...],
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function fromLangChainAgent(agent, options = {}) {
|
|
61
|
+
const { instructions } = options;
|
|
62
|
+
return async (messages) => {
|
|
63
|
+
const langchainMessages = [];
|
|
64
|
+
if (instructions) {
|
|
65
|
+
langchainMessages.push({ role: "system", content: instructions });
|
|
66
|
+
}
|
|
67
|
+
for (const msg of messages) {
|
|
68
|
+
langchainMessages.push({ role: msg.role, content: msg.content });
|
|
69
|
+
}
|
|
70
|
+
const result = await agent.invoke({ messages: langchainMessages });
|
|
71
|
+
// Handle agents that return a plain string directly
|
|
72
|
+
if (typeof result === "string")
|
|
73
|
+
return result;
|
|
74
|
+
if (typeof result.output === "string") {
|
|
75
|
+
return result.output;
|
|
76
|
+
}
|
|
77
|
+
// Extract the last AI/assistant message from the result
|
|
78
|
+
const resultMessages = result.messages;
|
|
79
|
+
if (!resultMessages || resultMessages.length === 0) {
|
|
80
|
+
console.warn("LangChain fromLangChainAgent: agent result contained no messages — returning empty string. " +
|
|
81
|
+
"Ensure the agent returns a state with a `messages` array, or return a string directly.");
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
// Walk backwards to find the last AI message
|
|
85
|
+
for (let i = resultMessages.length - 1; i >= 0; i--) {
|
|
86
|
+
const msg = resultMessages[i];
|
|
87
|
+
// Check for BaseMessage with _getType()
|
|
88
|
+
if (typeof msg._getType === "function") {
|
|
89
|
+
if (msg._getType() === "ai") {
|
|
90
|
+
return extractTextContent(msg.content);
|
|
91
|
+
}
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
// Check for dict format with type field
|
|
95
|
+
if (msg.type === "ai" || msg.role === "assistant") {
|
|
96
|
+
return extractTextContent(msg.content);
|
|
97
|
+
}
|
|
98
|
+
// Check for constructor format (lc serialization)
|
|
99
|
+
if (msg.type === "constructor" &&
|
|
100
|
+
Array.isArray(msg.id) &&
|
|
101
|
+
msg.id.includes("AIMessage")) {
|
|
102
|
+
const kwargs = msg.kwargs;
|
|
103
|
+
return extractTextContent(kwargs?.content);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Fallback: return the content of the last message regardless of type
|
|
107
|
+
const lastMsg = resultMessages[resultMessages.length - 1];
|
|
108
|
+
return extractTextContent(lastMsg?.content);
|
|
109
|
+
};
|
|
110
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convenience adapters for creating simulation targetCallbacks.
|
|
3
|
+
*
|
|
4
|
+
* These helpers create `targetCallback` functions from common agent sources,
|
|
5
|
+
* so users don't need to wire the plumbing themselves.
|
|
6
|
+
*/
|
|
7
|
+
import type { ChatMessage } from "./types.js";
|
|
8
|
+
/**
|
|
9
|
+
* Creates a simulation `targetCallback` from an Orq deployment key.
|
|
10
|
+
*
|
|
11
|
+
* This is the same bridge that `simulate()` and `wrapSimulationAgent()` use
|
|
12
|
+
* internally when you pass `agentKey`, but exposed as a standalone function
|
|
13
|
+
* so you can compose it freely.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { fromOrqDeployment, simulate } from "@orq-ai/evaluatorq/simulation";
|
|
18
|
+
*
|
|
19
|
+
* const callback = fromOrqDeployment("my-agent-deployment-key");
|
|
20
|
+
*
|
|
21
|
+
* const results = await simulate({
|
|
22
|
+
* evaluationName: "my-sim",
|
|
23
|
+
* targetCallback: callback,
|
|
24
|
+
* personas: [...],
|
|
25
|
+
* scenarios: [...],
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function fromOrqDeployment(agentKey: string): (messages: ChatMessage[]) => Promise<string>;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a simulation `targetCallback` from a plain function that calls
|
|
32
|
+
* an OpenAI-compatible chat completions API.
|
|
33
|
+
*
|
|
34
|
+
* Useful for raw OpenAI SDK, Azure OpenAI, or any OpenAI-compatible provider.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* import OpenAI from "openai";
|
|
39
|
+
* import { fromChatCompletions, simulate } from "@orq-ai/evaluatorq/simulation";
|
|
40
|
+
*
|
|
41
|
+
* const client = new OpenAI();
|
|
42
|
+
* const callback = fromChatCompletions(async (messages) => {
|
|
43
|
+
* const res = await client.chat.completions.create({
|
|
44
|
+
* model: "gpt-4o",
|
|
45
|
+
* messages,
|
|
46
|
+
* });
|
|
47
|
+
* return res.choices[0]?.message.content ?? "";
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* await simulate({ targetCallback: callback, ... });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function fromChatCompletions(fn: (messages: Array<{
|
|
54
|
+
role: string;
|
|
55
|
+
content: string;
|
|
56
|
+
}>) => string | Promise<string>): (messages: ChatMessage[]) => Promise<string>;
|
|
57
|
+
//# sourceMappingURL=adapters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapters.d.ts","sourceRoot":"","sources":["../../../../src/lib/integrations/simulation/adapters.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,MAAM,GACf,CAAC,QAAQ,EAAE,WAAW,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,CAS9C;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,mBAAmB,CACjC,EAAE,EAAE,CACF,QAAQ,EAAE,KAAK,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,KAC/C,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,GAC5B,CAAC,QAAQ,EAAE,WAAW,EAAE,KAAK,OAAO,CAAC,MAAM,CAAC,CAI9C"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convenience adapters for creating simulation targetCallbacks.
|
|
3
|
+
*
|
|
4
|
+
* These helpers create `targetCallback` functions from common agent sources,
|
|
5
|
+
* so users don't need to wire the plumbing themselves.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Creates a simulation `targetCallback` from an Orq deployment key.
|
|
9
|
+
*
|
|
10
|
+
* This is the same bridge that `simulate()` and `wrapSimulationAgent()` use
|
|
11
|
+
* internally when you pass `agentKey`, but exposed as a standalone function
|
|
12
|
+
* so you can compose it freely.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { fromOrqDeployment, simulate } from "@orq-ai/evaluatorq/simulation";
|
|
17
|
+
*
|
|
18
|
+
* const callback = fromOrqDeployment("my-agent-deployment-key");
|
|
19
|
+
*
|
|
20
|
+
* const results = await simulate({
|
|
21
|
+
* evaluationName: "my-sim",
|
|
22
|
+
* targetCallback: callback,
|
|
23
|
+
* personas: [...],
|
|
24
|
+
* scenarios: [...],
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function fromOrqDeployment(agentKey) {
|
|
29
|
+
if (!agentKey.trim()) {
|
|
30
|
+
throw new Error("agentKey must be a non-empty string");
|
|
31
|
+
}
|
|
32
|
+
return async (messages) => {
|
|
33
|
+
const { invoke } = await import("../../deployment-helper.js");
|
|
34
|
+
return invoke(agentKey, { messages });
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates a simulation `targetCallback` from a plain function that calls
|
|
39
|
+
* an OpenAI-compatible chat completions API.
|
|
40
|
+
*
|
|
41
|
+
* Useful for raw OpenAI SDK, Azure OpenAI, or any OpenAI-compatible provider.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* import OpenAI from "openai";
|
|
46
|
+
* import { fromChatCompletions, simulate } from "@orq-ai/evaluatorq/simulation";
|
|
47
|
+
*
|
|
48
|
+
* const client = new OpenAI();
|
|
49
|
+
* const callback = fromChatCompletions(async (messages) => {
|
|
50
|
+
* const res = await client.chat.completions.create({
|
|
51
|
+
* model: "gpt-4o",
|
|
52
|
+
* messages,
|
|
53
|
+
* });
|
|
54
|
+
* return res.choices[0]?.message.content ?? "";
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* await simulate({ targetCallback: callback, ... });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function fromChatCompletions(fn) {
|
|
61
|
+
return async (messages) => {
|
|
62
|
+
return fn(messages.map((m) => ({ role: m.role, content: m.content })));
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base agent class for simulation agents.
|
|
3
|
+
*
|
|
4
|
+
* Provides common functionality for all agents in the simulation system,
|
|
5
|
+
* including LLM interaction with retry logic.
|
|
6
|
+
*/
|
|
7
|
+
import OpenAI from "openai";
|
|
8
|
+
import type { ChatMessage, TokenUsage } from "../types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Result of a single LLM call, including optional tool calls.
|
|
11
|
+
*/
|
|
12
|
+
export interface LLMResult {
|
|
13
|
+
content: string;
|
|
14
|
+
tool_calls?: OpenAI.Chat.Completions.ChatCompletionMessageToolCall[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Configuration options for constructing an agent.
|
|
18
|
+
*/
|
|
19
|
+
export interface AgentConfig {
|
|
20
|
+
/** Model identifier (default: "azure/gpt-4o-mini"). */
|
|
21
|
+
model?: string;
|
|
22
|
+
/** Pre-existing OpenAI client to reuse. The agent will NOT close an injected client. */
|
|
23
|
+
client?: OpenAI;
|
|
24
|
+
/** API key override. Ignored when `client` is provided. */
|
|
25
|
+
apiKey?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Abstract base class for simulation agents.
|
|
29
|
+
*
|
|
30
|
+
* Provides common LLM interaction functionality with exponential-backoff
|
|
31
|
+
* retry logic and cumulative token-usage tracking.
|
|
32
|
+
*
|
|
33
|
+
* **Client injection**: pass an existing `OpenAI` client via `config.client`
|
|
34
|
+
* to share a single HTTP connection across multiple agents. The agent will
|
|
35
|
+
* NOT close an injected client -- the caller is responsible for its lifecycle.
|
|
36
|
+
*/
|
|
37
|
+
export declare abstract class BaseAgent {
|
|
38
|
+
protected model: string;
|
|
39
|
+
protected client: OpenAI;
|
|
40
|
+
private clientOwned;
|
|
41
|
+
private usage;
|
|
42
|
+
constructor(config?: AgentConfig);
|
|
43
|
+
/** Agent name for identification. */
|
|
44
|
+
abstract get name(): string;
|
|
45
|
+
/** System prompt for this agent. */
|
|
46
|
+
abstract get systemPrompt(): string;
|
|
47
|
+
/**
|
|
48
|
+
* Generate a text response for a conversation.
|
|
49
|
+
*
|
|
50
|
+
* @param messages - Conversation history
|
|
51
|
+
* @param options - Temperature, maxTokens, and timeout overrides
|
|
52
|
+
* @returns The agent's response text
|
|
53
|
+
* @throws {Error} If the LLM call returns no content
|
|
54
|
+
*/
|
|
55
|
+
respondAsync(messages: ChatMessage[], options?: {
|
|
56
|
+
temperature?: number;
|
|
57
|
+
maxTokens?: number;
|
|
58
|
+
timeout?: number;
|
|
59
|
+
signal?: AbortSignal;
|
|
60
|
+
}): Promise<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Get cumulative token usage for this agent.
|
|
63
|
+
*/
|
|
64
|
+
getUsage(): TokenUsage;
|
|
65
|
+
/**
|
|
66
|
+
* Reset token usage counters to zero.
|
|
67
|
+
*/
|
|
68
|
+
resetUsage(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Close the underlying HTTP client.
|
|
71
|
+
*
|
|
72
|
+
* Only closes clients that the agent created itself (not injected ones).
|
|
73
|
+
*/
|
|
74
|
+
close(): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Call the LLM with retry logic (exponential backoff).
|
|
77
|
+
*
|
|
78
|
+
* Retries on rate-limit (429) and server errors (500+). All other errors
|
|
79
|
+
* are raised immediately.
|
|
80
|
+
*/
|
|
81
|
+
protected callLLM(messages: ChatMessage[], options?: {
|
|
82
|
+
temperature?: number;
|
|
83
|
+
maxTokens?: number;
|
|
84
|
+
timeout?: number;
|
|
85
|
+
tools?: OpenAI.Chat.Completions.ChatCompletionTool[];
|
|
86
|
+
/** External abort signal — aborts in-flight LLM requests immediately. */
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
}): Promise<LLMResult>;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/lib/integrations/simulation/agents/base.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAQ3D;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,6BAA6B,EAAE,CAAC;CACtE;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,uDAAuD;IACvD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,2DAA2D;IAC3D,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAUD;;;;;;;;;GASG;AACH,8BAAsB,SAAS;IAC7B,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC;IACxB,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,WAAW,CAAU;IAC7B,OAAO,CAAC,KAAK,CAAa;gBAEd,MAAM,CAAC,EAAE,WAAW;IA2BhC,qCAAqC;IACrC,QAAQ,KAAK,IAAI,IAAI,MAAM,CAAC;IAE5B,oCAAoC;IACpC,QAAQ,KAAK,YAAY,IAAI,MAAM,CAAC;IAMpC;;;;;;;OAOG;IACG,YAAY,CAChB,QAAQ,EAAE,WAAW,EAAE,EACvB,OAAO,CAAC,EAAE;QACR,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GACA,OAAO,CAAC,MAAM,CAAC;IAiBlB;;OAEG;IACH,QAAQ,IAAI,UAAU;IAItB;;OAEG;IACH,UAAU,IAAI,IAAI;IAIlB;;;;OAIG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAgB5B;;;;;OAKG;cACa,OAAO,CACrB,QAAQ,EAAE,WAAW,EAAE,EACvB,OAAO,CAAC,EAAE;QACR,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,KAAK,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,kBAAkB,EAAE,CAAC;QACrD,yEAAyE;QACzE,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GACA,OAAO,CAAC,SAAS,CAAC;CAsHtB"}
|