-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
fireworks.ts
142 lines (119 loc) Β· 3.87 KB
/
fireworks.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
import {
type OpenAIClient,
type OpenAICallOptions,
type OpenAIInput,
type OpenAICoreRequestOptions,
OpenAI,
} from "@langchain/openai";
import type { BaseLLMParams } from "@langchain/core/language_models/llms";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
type FireworksUnsupportedArgs =
| "frequencyPenalty"
| "presencePenalty"
| "bestOf"
| "logitBias";
type FireworksUnsupportedCallOptions = "functions" | "function_call" | "tools";
export type FireworksCallOptions = Partial<
Omit<OpenAICallOptions, FireworksUnsupportedCallOptions>
>;
/**
* Wrapper around Fireworks API for large language models
*
* Fireworks API is compatible to the OpenAI API with some limitations described in
* https://readme.fireworks.ai/docs/openai-compatibility.
*
* To use, you should have the `openai` package installed and
* the `FIREWORKS_API_KEY` environment variable set.
*/
export class Fireworks extends OpenAI<FireworksCallOptions> {
static lc_name() {
return "Fireworks";
}
_llmType() {
return "fireworks";
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
fireworksApiKey: "FIREWORKS_API_KEY",
};
}
lc_serializable = true;
fireworksApiKey?: string;
constructor(
fields?: Partial<
Omit<OpenAIInput, "openAIApiKey" | FireworksUnsupportedArgs>
> &
BaseLLMParams & { fireworksApiKey?: string }
) {
const fireworksApiKey =
fields?.fireworksApiKey || getEnvironmentVariable("FIREWORKS_API_KEY");
if (!fireworksApiKey) {
throw new Error(
`Fireworks API key not found. Please set the FIREWORKS_API_KEY environment variable or provide the key into "fireworksApiKey"`
);
}
super({
...fields,
openAIApiKey: fireworksApiKey,
modelName: fields?.modelName || "accounts/fireworks/models/llama-v2-13b",
configuration: {
baseURL: "https://api.fireworks.ai/inference/v1",
},
});
this.fireworksApiKey = fireworksApiKey;
}
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.CompletionCreateParamsStreaming,
options?: OpenAICoreRequestOptions
): Promise<AsyncIterable<OpenAIClient.Completion>>;
async completionWithRetry(
request: OpenAIClient.CompletionCreateParamsNonStreaming,
options?: OpenAICoreRequestOptions
): Promise<OpenAIClient.Completions.Completion>;
/**
* Calls the Fireworks API with retry logic in case of failures.
* @param request The request to send to the Fireworks API.
* @param options Optional configuration for the API call.
* @returns The response from the Fireworks API.
*/
async completionWithRetry(
request:
| OpenAIClient.CompletionCreateParamsStreaming
| OpenAIClient.CompletionCreateParamsNonStreaming,
options?: OpenAICoreRequestOptions
): Promise<
AsyncIterable<OpenAIClient.Completion> | OpenAIClient.Completions.Completion
> {
// https://readme.fireworks.ai/docs/openai-compatibility#api-compatibility
if (Array.isArray(request.prompt)) {
if (request.prompt.length > 1) {
throw new Error("Multiple prompts are not supported by Fireworks");
}
const prompt = request.prompt[0];
if (typeof prompt !== "string") {
throw new Error("Only string prompts are supported by Fireworks");
}
request.prompt = prompt;
}
delete request.frequency_penalty;
delete request.presence_penalty;
delete request.best_of;
delete request.logit_bias;
if (request.stream === true) {
return super.completionWithRetry(request, options);
}
return super.completionWithRetry(request, options);
}
}