-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentModels.ts
More file actions
105 lines (88 loc) · 3 KB
/
Copy pathagentModels.ts
File metadata and controls
105 lines (88 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
import {
type CompletionAdapter,
} from "adminforth";
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
import type { LLMResult } from "@langchain/core/outputs";
import type { Messages, Command } from "@langchain/langgraph";
import {
createSequenceDebugMiddleware,
} from "./middleware/sequenceDebug.js";
import type { AgentModeCompletionAdapter, AgentModelPurpose } from "../application/ports.js";
import type { AgentTurnContext, AgentTurnObservability } from "../domain/turnTypes.js";
export type { AgentModeCompletionAdapter, AgentModelPurpose } from "../application/ports.js";
export type AgentChatModel = BaseChatModel<any, any>;
export type AgentMiddleware = ReturnType<typeof createSequenceDebugMiddleware>;
export type AgentTurnModels = {
model: AgentChatModel;
summaryModel: AgentChatModel;
modelMiddleware?: AgentMiddleware[];
};
export type AgentRuntimeRunInput = {
models: AgentTurnModels;
systemPrompt: string;
input: { messages: Messages } | Command;
context: AgentTurnContext;
observability: AgentTurnObservability;
branchFromCheckpointId?: string;
};
type AgentChatModelSpec = {
model: AgentChatModel;
middleware: AgentMiddleware[];
};
type PendingLlmRun = {
startedAt: number;
};
function isLangChainAgentCompletionAdapter(
adapter: CompletionAdapter,
): adapter is AgentModeCompletionAdapter {
return typeof (adapter as AgentModeCompletionAdapter)
.getLangChainAgentSpec === "function";
}
async function getAgentChatModelSpec(params: {
adapter: AgentModeCompletionAdapter;
maxTokens: number;
purpose: AgentModelPurpose;
}): Promise<AgentChatModelSpec> {
const spec = await params.adapter.getLangChainAgentSpec({
maxTokens: params.maxTokens,
purpose: params.purpose,
});
return {
model: spec.model as AgentChatModel,
middleware: (spec.middleware ?? []) as AgentMiddleware[],
};
}
class AgentLlmMetricsLogger extends BaseCallbackHandler {
name = "AgentLlmMetricsLogger";
lc_prefer_streaming = true;
private readonly pendingRuns = new Map<string, PendingLlmRun>();
async handleLLMStart(_llm: unknown, _prompts: string[], runId: string) {
this.pendingRuns.set(runId, { startedAt: Date.now() });
}
async handleLLMEnd(_output: LLMResult, runId: string) {
this.pendingRuns.delete(runId);
}
async handleLLMError(_error: unknown, runId: string) {
this.pendingRuns.delete(runId);
}
}
export function createAgentLlmMetricsLogger() {
return new AgentLlmMetricsLogger();
}
export async function createAgentChatModel(params: {
adapter: CompletionAdapter;
maxTokens: number;
purpose: AgentModelPurpose;
}) {
if (!isLangChainAgentCompletionAdapter(params.adapter)) {
throw new Error(
"AdminForth Agent requires completionAdapter to implement getLangChainAgentSpec({ maxTokens, purpose }).",
);
}
return await getAgentChatModelSpec({
adapter: params.adapter,
maxTokens: params.maxTokens,
purpose: params.purpose,
});
}