-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
chat_models.ts
174 lines (153 loc) Β· 4.59 KB
/
chat_models.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { expect } from "@jest/globals";
import {
BaseChatModelCallOptions,
LangSmithParams,
} from "@langchain/core/language_models/chat_models";
import { BaseMessageChunk } from "@langchain/core/messages";
import { z } from "zod";
import { StructuredTool } from "@langchain/core/tools";
import {
BaseChatModelsTests,
BaseChatModelsTestsFields,
RecordStringAny,
} from "../base.js";
const person = /* #__PURE__ */ z
.object({
name: z.string().describe("Name of the person"),
age: z.number().int().positive().describe("Age of the person"),
})
.describe("A person");
class PersonTool extends StructuredTool {
name = "PersonTool";
description = person.description ?? "description";
schema = person;
async _call(input: z.infer<typeof person>) {
return JSON.stringify(input);
}
}
export abstract class ChatModelUnitTests<
CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions,
OutputMessageType extends BaseMessageChunk = BaseMessageChunk,
ConstructorArgs extends RecordStringAny = RecordStringAny
> extends BaseChatModelsTests<CallOptions, OutputMessageType, ConstructorArgs> {
constructor(
fields: BaseChatModelsTestsFields<
CallOptions,
OutputMessageType,
ConstructorArgs
>
) {
const standardChatModelParams: RecordStringAny = {
temperature: 0,
maxTokens: 100,
timeout: 60,
stopSequences: [],
maxRetries: 2,
};
super({
...fields,
constructorArgs: {
...standardChatModelParams,
...fields.constructorArgs,
},
});
}
/**
* Override this method if the chat model being tested does not
* support all expected LangSmith parameters.
* @returns {Partial<LangSmithParams>} The LangSmith parameters expected by the chat model.
*/
expectedLsParams(): Partial<LangSmithParams> {
return {
ls_provider: "string",
ls_model_name: "string",
ls_model_type: "chat",
ls_temperature: 0,
ls_max_tokens: 0,
ls_stop: ["Array<string>"],
};
}
testChatModelInit() {
const chatModel = new this.Cls(this.constructorArgs);
expect(chatModel).toBeDefined();
}
testChatModelInitApiKey() {
const params = { ...this.constructorArgs, apiKey: "test" };
const chatModel = new this.Cls(params);
expect(chatModel).toBeDefined();
}
testChatModelInitStreaming() {
const params = { ...this.constructorArgs, streaming: true };
const chatModel = new this.Cls(params);
expect(chatModel).toBeDefined();
}
testChatModelWithBindTools() {
if (!this.chatModelHasToolCalling) {
return;
}
const chatModel = new this.Cls(this.constructorArgs);
expect(chatModel.bindTools?.([new PersonTool()])).toBeDefined();
}
testChatModelWithStructuredOutput() {
if (!this.chatModelHasStructuredOutput) {
return;
}
const chatModel = new this.Cls(this.constructorArgs);
expect((chatModel as any).withStructuredOutput?.(person)).toBeDefined();
}
testStandardParams() {
const expectedParams = this.expectedLsParams();
const chatModel = new this.Cls(this.constructorArgs);
const lsParams = chatModel.getLsParams({} as any);
expect(lsParams).toBeDefined();
expect(Object.keys(lsParams).sort()).toEqual(
Object.keys(expectedParams).sort()
);
}
/**
* Run all unit tests for the chat model.
* Each test is wrapped in a try/catch block to prevent the entire test suite from failing.
* If a test fails, the error is logged to the console, and the test suite continues.
* @returns {boolean}
*/
runTests(): boolean {
let allTestsPassed = true;
try {
this.testChatModelInit();
} catch (e: any) {
allTestsPassed = false;
console.error("testChatModelInit failed", e);
}
try {
this.testChatModelInitApiKey();
} catch (e: any) {
allTestsPassed = false;
console.error("testChatModelInitApiKey failed", e);
}
try {
this.testChatModelInitStreaming();
} catch (e: any) {
allTestsPassed = false;
console.error("testChatModelInitStreaming failed", e);
}
try {
this.testChatModelWithBindTools();
} catch (e: any) {
allTestsPassed = false;
console.error("testChatModelWithBindTools failed", e);
}
try {
this.testChatModelWithStructuredOutput();
} catch (e: any) {
allTestsPassed = false;
console.error("testChatModelWithStructuredOutput failed", e);
}
try {
this.testStandardParams();
} catch (e: any) {
allTestsPassed = false;
console.error("testStandardParams failed", e);
}
return allTestsPassed;
}
}