-
Notifications
You must be signed in to change notification settings - Fork 2k
/
replicate.ts
205 lines (177 loc) Β· 5.77 KB
/
replicate.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { LLM, type BaseLLMParams } from "@langchain/core/language_models/llms";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import { GenerationChunk } from "@langchain/core/outputs";
import type ReplicateInstance from "replicate";
/**
* Interface defining the structure of the input data for the Replicate
* class. It includes details about the model to be used, any additional
* input parameters, and the API key for the Replicate service.
*/
export interface ReplicateInput {
// owner/model_name:version
model: `${string}/${string}:${string}`;
input?: {
// different models accept different inputs
[key: string]: string | number | boolean;
};
apiKey?: string;
/** The key used to pass prompts to the model. */
promptKey?: string;
}
/**
* Class responsible for managing the interaction with the Replicate API.
* It handles the API key and model details, makes the actual API calls,
* and converts the API response into a format usable by the rest of the
* LangChain framework.
* @example
* ```typescript
* const model = new Replicate({
* model: "replicate/flan-t5-xl:3ae0799123a1fe11f8c89fd99632f843fc5f7a761630160521c4253149754523",
* });
*
* const res = await model.invoke(
* "Question: What would be a good company name for a company that makes colorful socks?\nAnswer:"
* );
* console.log({ res });
* ```
*/
export class Replicate extends LLM implements ReplicateInput {
static lc_name() {
return "Replicate";
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
apiKey: "REPLICATE_API_TOKEN",
};
}
lc_serializable = true;
model: ReplicateInput["model"];
input: ReplicateInput["input"];
apiKey: string;
promptKey?: string;
constructor(fields: ReplicateInput & BaseLLMParams) {
super(fields);
const apiKey =
fields?.apiKey ??
getEnvironmentVariable("REPLICATE_API_KEY") ?? // previous environment variable for backwards compatibility
getEnvironmentVariable("REPLICATE_API_TOKEN"); // current environment variable, matching the Python library
if (!apiKey) {
throw new Error(
"Please set the REPLICATE_API_TOKEN environment variable"
);
}
this.apiKey = apiKey;
this.model = fields.model;
this.input = fields.input ?? {};
this.promptKey = fields.promptKey;
}
_llmType() {
return "replicate";
}
/** @ignore */
async _call(
prompt: string,
options: this["ParsedCallOptions"]
): Promise<string> {
const replicate = await this._prepareReplicate();
const input = await this._getReplicateInput(replicate, prompt);
const output = await this.caller.callWithOptions(
{ signal: options.signal },
() =>
replicate.run(this.model, {
input,
})
);
if (typeof output === "string") {
return output;
} else if (Array.isArray(output)) {
return output.join("");
} else {
// Note this is a little odd, but the output format is not consistent
// across models, so it makes some amount of sense.
return String(output);
}
}
async *_streamResponseChunks(
prompt: string,
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<GenerationChunk> {
const replicate = await this._prepareReplicate();
const input = await this._getReplicateInput(replicate, prompt);
const stream = await this.caller.callWithOptions(
{ signal: options?.signal },
async () =>
replicate.stream(this.model, {
input,
})
);
for await (const chunk of stream) {
if (chunk.event === "output") {
yield new GenerationChunk({ text: chunk.data, generationInfo: chunk });
await runManager?.handleLLMNewToken(chunk.data ?? "");
}
// stream is done
if (chunk.event === "done")
yield new GenerationChunk({
text: "",
generationInfo: { finished: true },
});
}
}
/** @ignore */
static async imports(): Promise<{
Replicate: typeof ReplicateInstance;
}> {
try {
const { default: Replicate } = await import("replicate");
return { Replicate };
} catch (e) {
throw new Error(
"Please install replicate as a dependency with, e.g. `yarn add replicate`"
);
}
}
private async _prepareReplicate(): Promise<ReplicateInstance> {
const imports = await Replicate.imports();
return new imports.Replicate({
userAgent: "langchain",
auth: this.apiKey,
});
}
private async _getReplicateInput(
replicate: ReplicateInstance,
prompt: string
) {
if (this.promptKey === undefined) {
const [modelString, versionString] = this.model.split(":");
const version = await replicate.models.versions.get(
modelString.split("/")[0],
modelString.split("/")[1],
versionString
);
const openapiSchema = version.openapi_schema;
const inputProperties: { "x-order": number | undefined }[] =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(openapiSchema as any)?.components?.schemas?.Input?.properties;
if (inputProperties === undefined) {
this.promptKey = "prompt";
} else {
const sortedInputProperties = Object.entries(inputProperties).sort(
([_keyA, valueA], [_keyB, valueB]) => {
const orderA = valueA["x-order"] || 0;
const orderB = valueB["x-order"] || 0;
return orderA - orderB;
}
);
this.promptKey = sortedInputProperties[0][0] ?? "prompt";
}
}
return {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
[this.promptKey!]: prompt,
...this.input,
};
}
}