-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
togetherai.ts
167 lines (148 loc) Β· 4.51 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
import type { BaseChatModelParams } from "@langchain/core/language_models/chat_models";
import {
type OpenAIClient,
type ChatOpenAICallOptions,
type OpenAIChatInput,
type OpenAICoreRequestOptions,
ChatOpenAI,
} from "@langchain/openai";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
type TogetherAIUnsupportedArgs =
| "frequencyPenalty"
| "presencePenalty"
| "logitBias"
| "functions";
type TogetherAIUnsupportedCallOptions = "functions" | "function_call";
export interface ChatTogetherAICallOptions
extends Omit<ChatOpenAICallOptions, TogetherAIUnsupportedCallOptions> {
response_format: {
type: "json_object";
schema: Record<string, unknown>;
};
}
export interface ChatTogetherAIInput
extends Omit<OpenAIChatInput, "openAIApiKey" | TogetherAIUnsupportedArgs>,
BaseChatModelParams {
/**
* The TogetherAI API key to use for requests.
* Alias for `apiKey`
* @default process.env.TOGETHER_AI_API_KEY
*/
togetherAIApiKey?: string;
/**
* The TogetherAI API key to use for requests.
* @default process.env.TOGETHER_AI_API_KEY
*/
apiKey?: string;
}
/**
* Wrapper around TogetherAI API for large language models fine-tuned for chat
*
* TogetherAI API is compatible to the OpenAI API with some limitations. View the
* full API ref at:
* @link {https://docs.together.ai/reference/chat-completions}
*
* To use, you should have the `TOGETHER_AI_API_KEY` environment variable set.
* @example
* ```typescript
* const model = new ChatTogetherAI({
* temperature: 0.9,
* apiKey: process.env.TOGETHER_AI_API_KEY,
* });
*
* const response = await model.invoke([new HumanMessage("Hello there!")]);
* console.log(response);
* ```
*/
export class ChatTogetherAI extends ChatOpenAI<ChatTogetherAICallOptions> {
static lc_name() {
return "ChatTogetherAI";
}
_llmType() {
return "togetherAI";
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
togetherAIApiKey: "TOGETHER_AI_API_KEY",
apiKey: "TOGETHER_AI_API_KEY",
};
}
lc_serializable = true;
constructor(
fields?: Partial<
Omit<OpenAIChatInput, "openAIApiKey" | TogetherAIUnsupportedArgs>
> &
BaseChatModelParams & {
/**
* Prefer `apiKey`
*/
togetherAIApiKey?: string;
/**
* The TogetherAI API key to use.
*/
apiKey?: string;
}
) {
const togetherAIApiKey =
fields?.apiKey ||
fields?.togetherAIApiKey ||
getEnvironmentVariable("TOGETHER_AI_API_KEY");
if (!togetherAIApiKey) {
throw new Error(
`TogetherAI API key not found. Please set the TOGETHER_AI_API_KEY environment variable or provide the key into "togetherAIApiKey"`
);
}
super({
...fields,
model: fields?.model || "mistralai/Mixtral-8x7B-Instruct-v0.1",
apiKey: togetherAIApiKey,
configuration: {
baseURL: "https://api.together.xyz/v1/",
},
});
}
toJSON() {
const result = super.toJSON();
if (
"kwargs" in result &&
typeof result.kwargs === "object" &&
result.kwargs != null
) {
delete result.kwargs.openai_api_key;
delete result.kwargs.configuration;
}
return result;
}
async completionWithRetry(
request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming,
options?: OpenAICoreRequestOptions
): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
async completionWithRetry(
request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,
options?: OpenAICoreRequestOptions
): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
/**
* Calls the TogetherAI API with retry logic in case of failures.
* @param request The request to send to the TogetherAI API.
* @param options Optional configuration for the API call.
* @returns The response from the TogetherAI API.
*/
async completionWithRetry(
request:
| OpenAIClient.Chat.ChatCompletionCreateParamsStreaming
| OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming,
options?: OpenAICoreRequestOptions
): Promise<
| AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>
| OpenAIClient.Chat.Completions.ChatCompletion
> {
delete request.frequency_penalty;
delete request.presence_penalty;
delete request.logit_bias;
delete request.functions;
if (request.stream === true) {
return super.completionWithRetry(request, options);
}
return super.completionWithRetry(request, options);
}
}