Skip to content

add snippets tests & response_format support #1524

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
72 changes: 70 additions & 2 deletions packages/inference/src/snippets/getInferenceSnippets.ts
Original file line number Diff line number Diff line change
@@ -305,16 +305,44 @@ const prepareConversationalInput = (
temperature?: GenerationParameters["temperature"];
max_tokens?: GenerationParameters["max_new_tokens"];
top_p?: GenerationParameters["top_p"];
response_format?: Record<string, unknown>;
}
): object => {
return {
messages: opts?.messages ?? getModelInputSnippet(model),
...(opts?.temperature ? { temperature: opts?.temperature } : undefined),
...(opts?.max_tokens ? { max_tokens: opts?.max_tokens } : undefined),
...(opts?.top_p ? { top_p: opts?.top_p } : undefined),
...(opts?.response_format ? { response_format: opts?.response_format } : undefined),
};
};

const prepareTextGenerationInput = (
model: ModelDataMinimal,
opts?: {
streaming?: boolean;
temperature?: GenerationParameters["temperature"];
max_tokens?: GenerationParameters["max_new_tokens"];
top_p?: GenerationParameters["top_p"];
response_format?: Record<string, unknown>;
}
): object => {
const base = { inputs: getModelInputSnippet(model) };
const parameters: Record<string, unknown> = {};

if (opts?.temperature !== undefined) parameters.temperature = opts.temperature;
if (opts?.max_tokens !== undefined) parameters.max_new_tokens = opts.max_tokens;
if (opts?.top_p !== undefined) parameters.top_p = opts.top_p;
if (opts?.response_format !== undefined) parameters.response_format = opts.response_format;

// Only add parameters if there are any
if (Object.keys(parameters).length > 0) {
return { ...base, parameters };
}

return base;
};

const prepareQuestionAnsweringInput = (model: ModelDataMinimal): object => {
const data = JSON.parse(getModelInputSnippet(model) as string);
return { question: data.question, context: data.context };
@@ -355,7 +383,7 @@ const snippets: Partial<
"tabular-regression": snippetGenerator("tabular"),
"table-question-answering": snippetGenerator("tableQuestionAnswering", prepareTableQuestionAnsweringInput),
"text-classification": snippetGenerator("basic"),
"text-generation": snippetGenerator("basic"),
"text-generation": snippetGenerator("basic", prepareTextGenerationInput),
"text-to-audio": snippetGenerator("textToAudio"),
"text-to-image": snippetGenerator("textToImage"),
"text-to-speech": snippetGenerator("textToSpeech"),
@@ -393,7 +421,7 @@ function formatBody(obj: object, format: "curl" | "json" | "python" | "ts"): str
return indentString(
Object.entries(obj)
.map(([key, value]) => {
const formattedValue = JSON.stringify(value, null, 4).replace(/"/g, '"');
const formattedValue = formatPythonValue(value, 1);
return `${key}=${formattedValue},`;
})
.join("\n")
@@ -408,6 +436,46 @@ function formatBody(obj: object, format: "curl" | "json" | "python" | "ts"): str
}
}

function formatPythonValue(obj: unknown, depth?: number): string {
depth = depth ?? 0;

/// Case boolean - convert to Python format
if (typeof obj === "boolean") {
return obj ? "True" : "False";
}

/// Case null - convert to Python format
if (obj === null) {
return "None";
}

/// Case number or string
if (typeof obj !== "object") {
return JSON.stringify(obj);
}

/// Case array
if (Array.isArray(obj)) {
const items = obj
.map((item) => {
const formatted = formatPythonValue(item, depth + 1);
return `${" ".repeat(4 * (depth + 1))}${formatted},`;
})
.join("\n");
return `[\n${items}\n${" ".repeat(4 * depth)}]`;
}

/// Case mapping (object)
const entries = Object.entries(obj);
const lines = entries
.map(([key, value]) => {
const formattedValue = formatPythonValue(value, depth + 1);
return `${" ".repeat(4 * (depth + 1))}"${key}": ${formattedValue},`;
})
.join("\n");
return `{\n${lines}\n${" ".repeat(4 * depth)}}`;
}

function formatTsObject(obj: unknown, depth?: number): string {
depth = depth ?? 0;

Loading
Oops, something went wrong.
Loading
Oops, something went wrong.