Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] text-generation for Replicate #1181

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/inference/src/providers/replicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export const REPLICATE_API_BASE_URL = "https://api.replicate.com";
type ReplicateId = string;

export const REPLICATE_SUPPORTED_MODEL_IDS: ProviderMapping<ReplicateId> = {
"text-generation": {
"deepseek-ai/DeepSeek-R1": "deepseek-ai/deepseek-r1",
"meta/meta-llama-3-70b": "meta/meta-llama-3-70b",
"meta/meta-llama-3-8b": "meta/meta-llama-3-8b",
},
"text-to-image": {
"black-forest-labs/FLUX.1-dev": "black-forest-labs/flux-dev",
"black-forest-labs/FLUX.1-schnell": "black-forest-labs/flux-schnell",
Expand Down
36 changes: 36 additions & 0 deletions packages/inference/src/tasks/nlp/textGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import type {
} from "@huggingface/tasks";
import { InferenceOutputError } from "../../lib/InferenceOutputError";
import type { BaseArgs, Options } from "../../types";
import { omit } from "../../utils/omit";
import { toArray } from "../../utils/toArray";
import { request } from "../custom/request";

export type { TextGenerationInput, TextGenerationOutput };

interface ReplicateTextCompletionOutput {
status: string;
output?: string[];
}

interface TogeteherTextCompletionOutput extends Omit<ChatCompletionOutput, "choices"> {
choices: Array<{
text: string;
Expand Down Expand Up @@ -43,6 +49,36 @@ export async function textGeneration(
return {
generated_text: completion.text,
};
} else if (args.provider === "replicate") {
const payload = {
...omit(args, ["inputs", "parameters"]),
...args.parameters,
prompt: args.inputs,
};

const raw = await request<ReplicateTextCompletionOutput>(payload, {
...options,
taskHint: "text-generation",
});

if (typeof raw !== "object" || !("status" in raw)) {
throw new InferenceOutputError("Incomplete response");
}

const status = raw.status;
if (status === "starting") {
throw new InferenceOutputError("Replicate server-side time out");
}

if (!("output" in raw && Array.isArray(raw?.output))) {
throw new InferenceOutputError("Invalid response: no output");
}

const joined_output = raw.output.join("");

return {
generated_text: joined_output,
};
} else {
const res = toArray(
await request<TextGenerationOutput | TextGenerationOutput[]>(args, {
Expand Down