Skip to content

Commit dde0830

Browse files
teofeliujulien-cWauplin
authored
Fireworks AI Conversational Models (#1177)
Added support for inference for Fireworks AI chat/instruct models --------- Co-authored-by: Julien Chaumond <[email protected]> Co-authored-by: Lucain <[email protected]>
1 parent f69c26c commit dde0830

File tree

9 files changed

+179
-12
lines changed

9 files changed

+179
-12
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
HF_REPLICATE_KEY: dummy
4646
HF_SAMBANOVA_KEY: dummy
4747
HF_TOGETHER_KEY: dummy
48+
HF_FIREWORKS_KEY: dummy
4849

4950
browser:
5051
runs-on: ubuntu-latest
@@ -85,6 +86,7 @@ jobs:
8586
HF_REPLICATE_KEY: dummy
8687
HF_SAMBANOVA_KEY: dummy
8788
HF_TOGETHER_KEY: dummy
89+
HF_FIREWORKS_KEY: dummy
8890

8991
e2e:
9092
runs-on: ubuntu-latest
@@ -152,3 +154,4 @@ jobs:
152154
HF_REPLICATE_KEY: dummy
153155
HF_SAMBANOVA_KEY: dummy
154156
HF_TOGETHER_KEY: dummy
157+
HF_FIREWORKS_KEY: dummy

packages/inference/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ Your access token should be kept private. If you need to protect it in front-end
4646

4747
You can send inference requests to third-party providers with the inference client.
4848

49-
Currently, we support the following providers: [Fal.ai](https://fal.ai), [Replicate](https://replicate.com), [Together](https://together.xyz) and [Sambanova](https://sambanova.ai).
49+
Currently, we support the following providers:
50+
- [Fal.ai](https://fal.ai)
51+
- [Fireworks AI](https://fireworks.ai)
52+
- [Replicate](https://replicate.com)
53+
- [Sambanova](https://sambanova.ai)
54+
- [Together](https://together.xyz)
5055

5156
To send requests to a third-party provider, you have to pass the `provider` parameter to the inference function. Make sure your request is authenticated with an access token.
5257
```ts
@@ -64,10 +69,11 @@ When authenticated with a Hugging Face access token, the request is routed throu
6469
When authenticated with a third-party provider key, the request is made directly against that provider's inference API.
6570

6671
Only a subset of models are supported when requesting third-party providers. You can check the list of supported models per pipeline tasks here:
67-
- [Fal.ai supported models](./src/providers/fal-ai.ts)
68-
- [Replicate supported models](./src/providers/replicate.ts)
69-
- [Sambanova supported models](./src/providers/sambanova.ts)
70-
- [Together supported models](./src/providers/together.ts)
72+
- [Fal.ai supported models](https://huggingface.co/api/partners/fal-ai/models)
73+
- [Fireworks AI supported models](https://huggingface.co/api/partners/fireworks-ai/models)
74+
- [Replicate supported models](https://huggingface.co/api/partners/replicate/models)
75+
- [Sambanova supported models](https://huggingface.co/api/partners/sambanova/models)
76+
- [Together supported models](https://huggingface.co/api/partners/together/models)
7177
- [HF Inference API (serverless)](https://huggingface.co/models?inference=warm&sort=trending)
7278

7379
**Important note:** To be compatible, the third-party API must adhere to the "standard" shape API we expect on HF model pages for each pipeline task type.

packages/inference/src/lib/getProviderModelId.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export async function getProviderModelId(
3030
options.taskHint === "text-generation" && options.chatCompletion ? "conversational" : options.taskHint;
3131

3232
// A dict called HARDCODED_MODEL_ID_MAPPING takes precedence in all cases (useful for dev purposes)
33-
if (HARDCODED_MODEL_ID_MAPPING[params.model]) {
34-
return HARDCODED_MODEL_ID_MAPPING[params.model];
33+
if (HARDCODED_MODEL_ID_MAPPING[params.provider]?.[params.model]) {
34+
return HARDCODED_MODEL_ID_MAPPING[params.provider][params.model];
3535
}
3636

3737
let inferenceProviderMapping: InferenceProviderMapping | null;

packages/inference/src/lib/makeRequestOptions.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FAL_AI_API_BASE_URL } from "../providers/fal-ai";
33
import { REPLICATE_API_BASE_URL } from "../providers/replicate";
44
import { SAMBANOVA_API_BASE_URL } from "../providers/sambanova";
55
import { TOGETHER_API_BASE_URL } from "../providers/together";
6+
import { FIREWORKS_AI_API_BASE_URL } from "../providers/fireworks-ai";
67
import type { InferenceProvider } from "../types";
78
import type { InferenceTask, Options, RequestArgs } from "../types";
89
import { isUrl } from "./isUrl";
@@ -208,6 +209,15 @@ function makeUrl(params: {
208209
}
209210
return baseUrl;
210211
}
212+
case "fireworks-ai": {
213+
const baseUrl = shouldProxy
214+
? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", params.provider)
215+
: FIREWORKS_AI_API_BASE_URL;
216+
if (params.taskHint === "text-generation" && params.chatCompletion) {
217+
return `${baseUrl}/v1/chat/completions`;
218+
}
219+
return baseUrl;
220+
}
211221
default: {
212222
const baseUrl = HF_HUB_INFERENCE_PROXY_TEMPLATE.replaceAll("{{PROVIDER}}", "hf-inference");
213223
const url = params.forceTask
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import type { ModelId } from "../types";
1+
import type { InferenceProvider } from "../types";
2+
import { type ModelId } from "../types";
23

34
type ProviderId = string;
4-
55
/**
66
* If you want to try to run inference for a new model locally before it's registered on huggingface.co
77
* for a given Inference Provider,
88
* you can add it to the following dictionary, for dev purposes.
9+
*
10+
* We also inject into this dictionary from tests.
911
*/
10-
export const HARDCODED_MODEL_ID_MAPPING: Record<ModelId, ProviderId> = {
12+
export const HARDCODED_MODEL_ID_MAPPING: Record<InferenceProvider, Record<ModelId, ProviderId>> = {
1113
/**
1214
* "HF model ID" => "Model ID on Inference Provider's side"
15+
*
16+
* Example:
17+
* "Qwen/Qwen2.5-Coder-32B-Instruct": "Qwen2.5-Coder-32B-Instruct",
1318
*/
14-
// "Qwen/Qwen2.5-Coder-32B-Instruct": "Qwen2.5-Coder-32B-Instruct",
19+
"fal-ai": {},
20+
"fireworks-ai": {},
21+
"hf-inference": {},
22+
replicate: {},
23+
sambanova: {},
24+
together: {},
1525
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai/inference";
2+
3+
/**
4+
* See the registered mapping of HF model ID => Fireworks model ID here:
5+
*
6+
* https://huggingface.co/api/partners/fireworks/models
7+
*
8+
* This is a publicly available mapping.
9+
*
10+
* If you want to try to run inference for a new model locally before it's registered on huggingface.co,
11+
* you can add it to the dictionary "HARDCODED_MODEL_ID_MAPPING" in consts.ts, for dev purposes.
12+
*
13+
* - If you work at Fireworks and want to update this mapping, please use the model mapping API we provide on huggingface.co
14+
* - If you're a community member and want to add a new supported HF model to Fireworks, please open an issue on the present repo
15+
* and we will tag Fireworks team members.
16+
*
17+
* Thanks!
18+
*/

packages/inference/src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ export interface Options {
4444

4545
export type InferenceTask = Exclude<PipelineType, "other">;
4646

47-
export const INFERENCE_PROVIDERS = ["fal-ai", "replicate", "sambanova", "together", "hf-inference"] as const;
47+
export const INFERENCE_PROVIDERS = [
48+
"fal-ai",
49+
"fireworks-ai",
50+
"hf-inference",
51+
"replicate",
52+
"sambanova",
53+
"together",
54+
] as const;
4855
export type InferenceProvider = (typeof INFERENCE_PROVIDERS)[number];
4956

5057
export interface BaseArgs {

packages/inference/test/HfInference.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { chatCompletion, HfInference } from "../src";
66
import { textToVideo } from "../src/tasks/cv/textToVideo";
77
import { readTestFile } from "./test-files";
88
import "./vcr";
9+
import { HARDCODED_MODEL_ID_MAPPING } from "../src/providers/consts";
910

1011
const TIMEOUT = 60000 * 3;
1112
const env = import.meta.env;
@@ -1077,4 +1078,51 @@ describe.concurrent("HfInference", () => {
10771078
);
10781079
});
10791080
});
1081+
1082+
describe.concurrent(
1083+
"Fireworks",
1084+
() => {
1085+
const client = new HfInference(env.HF_FIREWORKS_KEY);
1086+
1087+
HARDCODED_MODEL_ID_MAPPING["fireworks-ai"] = {
1088+
"deepseek-ai/DeepSeek-R1": "accounts/fireworks/models/deepseek-r1",
1089+
};
1090+
1091+
it("chatCompletion", async () => {
1092+
const res = await client.chatCompletion({
1093+
model: "deepseek-ai/DeepSeek-R1",
1094+
provider: "fireworks-ai",
1095+
messages: [{ role: "user", content: "Complete this sentence with words, one plus one is equal " }],
1096+
});
1097+
if (res.choices && res.choices.length > 0) {
1098+
const completion = res.choices[0].message?.content;
1099+
expect(completion).toContain("two");
1100+
}
1101+
});
1102+
1103+
it("chatCompletion stream", async () => {
1104+
const stream = client.chatCompletionStream({
1105+
model: "deepseek-ai/DeepSeek-R1",
1106+
provider: "fireworks-ai",
1107+
messages: [{ role: "user", content: "Say this is a test" }],
1108+
stream: true,
1109+
}) as AsyncGenerator<ChatCompletionStreamOutput>;
1110+
1111+
let fullResponse = "";
1112+
for await (const chunk of stream) {
1113+
if (chunk.choices && chunk.choices.length > 0) {
1114+
const content = chunk.choices[0].delta?.content;
1115+
if (content) {
1116+
fullResponse += content;
1117+
}
1118+
}
1119+
}
1120+
1121+
// Verify we got a meaningful response
1122+
expect(fullResponse).toBeTruthy();
1123+
expect(fullResponse.length).toBeGreaterThan(0);
1124+
});
1125+
},
1126+
TIMEOUT
1127+
);
10801128
});

packages/inference/test/tapes.json

Lines changed: 65 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)