-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
togetherai.ts
282 lines (255 loc) Β· 7.84 KB
/
togetherai.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import {
LLM,
type BaseLLMCallOptions,
type BaseLLMParams,
} from "@langchain/core/language_models/llms";
import { GenerationChunk } from "@langchain/core/outputs";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { convertEventStreamToIterableReadableDataStream } from "../utils/event_source_parse.js";
interface TogetherAIInferenceResult {
object: string;
status: string;
prompt: Array<string>;
model: string;
model_owner: string;
tags: object;
num_returns: number;
args: {
model: string;
prompt: string;
temperature: number;
top_p: number;
top_k: number;
max_tokens: number;
stop: string[];
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
subjobs: Array<any>;
output: {
choices: Array<{
finish_reason: string;
index: number;
text: string;
}>;
raw_compute_time: number;
result_type: string;
};
}
/**
* Note that the modelPath is the only required parameter. For testing you
* can set this in the environment variable `LLAMA_PATH`.
*/
export interface TogetherAIInputs extends BaseLLMParams {
/**
* The API key to use for the TogetherAI API.
* @default {process.env.TOGETHER_AI_API_KEY}
*/
apiKey?: string;
/**
* The name of the model to query.
*/
modelName: string;
/**
* A decimal number that determines the degree of randomness in the response.
* A value of 1 will always yield the same output.
* A temperature less than 1 favors more correctness and is appropriate for question answering or summarization.
* A value greater than 1 introduces more randomness in the output.
* @default {0.7}
*/
temperature?: number;
/**
* Whether or not to stream tokens as they are generated.
* @default {false}
*/
streaming?: boolean;
/**
* The `topP` (nucleus) parameter is used to dynamically adjust the number of choices for each predicted token based on the cumulative probabilities.
* It specifies a probability threshold, below which all less likely tokens are filtered out.
* This technique helps to maintain diversity and generate more fluent and natural-sounding text.
* @default {0.7}
*/
topP?: number;
/**
* The `topK` parameter is used to limit the number of choices for the next predicted word or token.
* It specifies the maximum number of tokens to consider at each step, based on their probability of occurrence.
* This technique helps to speed up the generation process and can improve the quality of the generated text by focusing on the most likely options.
* @default {50}
*/
topK?: number;
/**
* A number that controls the diversity of generated text by reducing the likelihood of repeated sequences.
* Higher values decrease repetition.
* @default {1}
*/
repetitionPenalty?: number;
/**
* An integer that specifies how many top token log probabilities are included in the response for each token generation step.
*/
logprobs?: number;
/**
* Run an LLM-based input-output safeguard model on top of any model.
*/
safetyModel?: string;
/**
* Limit the number of tokens generated.
*/
maxTokens?: number;
/**
* A list of tokens at which the generation should stop.
*/
stop?: string[];
}
export interface TogetherAICallOptions
extends BaseLLMCallOptions,
Pick<
TogetherAIInputs,
| "modelName"
| "temperature"
| "topP"
| "topK"
| "repetitionPenalty"
| "logprobs"
| "safetyModel"
| "maxTokens"
| "stop"
> {}
export class TogetherAI extends LLM<TogetherAICallOptions> {
lc_serializable = true;
declare CallOptions: TogetherAICallOptions;
static inputs: TogetherAIInputs;
temperature = 0.7;
topP = 0.7;
topK = 50;
modelName: string;
streaming = false;
repetitionPenalty = 1;
logprobs?: number;
maxTokens?: number;
safetyModel?: string;
stop?: string[];
private apiKey: string;
private inferenceAPIUrl = "https://api.together.xyz/inference";
static lc_name() {
return "TogetherAI";
}
constructor(inputs: TogetherAIInputs) {
super(inputs);
const apiKey =
inputs.apiKey ?? getEnvironmentVariable("TOGETHER_AI_API_KEY");
if (!apiKey) {
throw new Error("TOGETHER_AI_API_KEY not found.");
}
this.apiKey = apiKey;
this.temperature = inputs?.temperature ?? this.temperature;
this.topK = inputs?.topK ?? this.topK;
this.topP = inputs?.topP ?? this.topP;
this.modelName = inputs.modelName;
this.streaming = inputs.streaming ?? this.streaming;
this.repetitionPenalty = inputs.repetitionPenalty ?? this.repetitionPenalty;
this.logprobs = inputs.logprobs;
this.safetyModel = inputs.safetyModel;
this.maxTokens = inputs.maxTokens;
this.stop = inputs.stop;
}
_llmType() {
return "together_ai";
}
private constructHeaders() {
return {
accept: "application/json",
"content-type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
};
}
private constructBody(prompt: string, options?: this["ParsedCallOptions"]) {
const body = {
model: options?.modelName ?? this?.modelName,
prompt,
temperature: this?.temperature ?? options?.temperature,
top_k: this?.topK ?? options?.topK,
top_p: this?.topP ?? options?.topP,
repetition_penalty: this?.repetitionPenalty ?? options?.repetitionPenalty,
logprobs: this?.logprobs ?? options?.logprobs,
stream_tokens: this?.streaming,
safety_model: this?.safetyModel ?? options?.safetyModel,
max_tokens: this?.maxTokens ?? options?.maxTokens,
stop: this?.stop ?? options?.stop,
};
return body;
}
async completionWithRetry(
prompt: string,
options?: this["ParsedCallOptions"]
) {
return this.caller.call(async () => {
const fetchResponse = await fetch(this.inferenceAPIUrl, {
method: "POST",
headers: {
...this.constructHeaders(),
},
body: JSON.stringify(this.constructBody(prompt, options)),
});
if (fetchResponse.status === 200) {
return fetchResponse.json();
}
const errorResponse = await fetchResponse.json();
throw new Error(
`Error getting prompt completion from Together AI. ${JSON.stringify(
errorResponse,
null,
2
)}`
);
});
}
/** @ignore */
async _call(
prompt: string,
options?: this["ParsedCallOptions"]
): Promise<string> {
const response: TogetherAIInferenceResult = await this.completionWithRetry(
prompt,
options
);
const outputText = response.output.choices[0].text;
return outputText ?? "";
}
async *_streamResponseChunks(
prompt: string,
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): AsyncGenerator<GenerationChunk> {
const fetchResponse = await fetch(this.inferenceAPIUrl, {
method: "POST",
headers: {
...this.constructHeaders(),
},
body: JSON.stringify(this.constructBody(prompt, options)),
});
if (fetchResponse.status !== 200 ?? !fetchResponse.body) {
const errorResponse = await fetchResponse.json();
throw new Error(
`Error getting prompt completion from Together AI. ${JSON.stringify(
errorResponse,
null,
2
)}`
);
}
const stream = convertEventStreamToIterableReadableDataStream(
fetchResponse.body
);
for await (const chunk of stream) {
if (chunk !== "[DONE]") {
const parsedChunk = JSON.parse(chunk);
const generationChunk = new GenerationChunk({
text: parsedChunk.choices[0].text ?? "",
});
yield generationChunk;
// eslint-disable-next-line no-void
void runManager?.handleLLMNewToken(generationChunk.text ?? "");
}
}
}
}