Skip to content

Refactor conversational input to getModelInputSnippet #989

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

Merged
merged 2 commits into from
Oct 29, 2024
Merged
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
18 changes: 1 addition & 17 deletions packages/tasks/src/snippets/curl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,7 @@ export const snippetTextGeneration = (
if (model.tags.includes("conversational")) {
// Conversational model detected, so we display a code snippet that features the Messages API
const streaming = opts?.streaming ?? true;
const exampleMessages: ChatCompletionInputMessage[] =
model.pipeline_tag === "text-generation"
? [{ role: "user", content: "What is the capital of France?" }]
: [
{
role: "user",
content: [
{
type: "image_url",
image_url: {
url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg",
},
},
{ type: "text", text: "Describe this image in one sentence." },
],
},
];
const exampleMessages = getModelInputSnippet(model) as ChatCompletionInputMessage[];
const messages = opts?.messages ?? exampleMessages;

const config = {
Expand Down
50 changes: 40 additions & 10 deletions packages/tasks/src/snippets/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PipelineType } from "../pipelines";
import type { ChatCompletionInputMessage } from "../tasks";
import type { ModelDataMinimal } from "./types";

const inputsZeroShotClassification = () =>
Expand Down Expand Up @@ -40,7 +41,30 @@ const inputsTextClassification = () => `"I like you. I love you"`;

const inputsTokenClassification = () => `"My name is Sarah Jessica Parker but you can call me Jessica"`;

const inputsTextGeneration = () => `"Can you please let us know more details about your "`;
const inputsTextGeneration = (model: ModelDataMinimal): string | ChatCompletionInputMessage[] => {
if (model.tags.includes("conversational")) {
return model.pipeline_tag === "text-generation"
? [{ role: "user", content: "What is the capital of France?" }]
: [
{
role: "user",
content: [
{
type: "text",
text: "Describe this image in one sentence.",
},
{
type: "image_url",
image_url: {
url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg",
},
},
],
},
];
}
return `"Can you please let us know more details about your "`;
};

const inputsText2TextGeneration = () => `"The answer to the universe is"`;

Expand Down Expand Up @@ -84,7 +108,7 @@ const inputsTabularPrediction = () =>
const inputsZeroShotImageClassification = () => `"cats.jpg"`;

const modelInputSnippets: {
[key in PipelineType]?: (model: ModelDataMinimal) => string;
[key in PipelineType]?: (model: ModelDataMinimal) => string | ChatCompletionInputMessage[];
} = {
"audio-to-audio": inputsAudioToAudio,
"audio-classification": inputsAudioClassification,
Expand Down Expand Up @@ -116,18 +140,24 @@ const modelInputSnippets: {

// Use noWrap to put the whole snippet on a single line (removing new lines and tabulations)
// Use noQuotes to strip quotes from start & end (example: "abc" -> abc)
export function getModelInputSnippet(model: ModelDataMinimal, noWrap = false, noQuotes = false): string {
export function getModelInputSnippet(
model: ModelDataMinimal,
noWrap = false,
noQuotes = false
): string | ChatCompletionInputMessage[] {
if (model.pipeline_tag) {
const inputs = modelInputSnippets[model.pipeline_tag];
if (inputs) {
let result = inputs(model);
if (noWrap) {
result = result.replace(/(?:(?:\r?\n|\r)\t*)|\t+/g, " ");
}
if (noQuotes) {
const REGEX_QUOTES = /^"(.+)"$/s;
const match = result.match(REGEX_QUOTES);
result = match ? match[1] : result;
if (typeof result === "string") {
if (noWrap) {
result = result.replace(/(?:(?:\r?\n|\r)\t*)|\t+/g, " ");
}
if (noQuotes) {
const REGEX_QUOTES = /^"(.+)"$/s;
const match = result.match(REGEX_QUOTES);
result = match ? match[1] : result;
}
}
return result;
}
Expand Down
18 changes: 1 addition & 17 deletions packages/tasks/src/snippets/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,7 @@ export const snippetTextGeneration = (
if (model.tags.includes("conversational")) {
// Conversational model detected, so we display a code snippet that features the Messages API
const streaming = opts?.streaming ?? true;
const exampleMessages: ChatCompletionInputMessage[] =
model.pipeline_tag === "text-generation"
? [{ role: "user", content: "What is the capital of France?" }]
: [
{
role: "user",
content: [
{
type: "image_url",
image_url: {
url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg",
},
},
{ type: "text", text: "Describe this image in one sentence." },
],
},
];
const exampleMessages = getModelInputSnippet(model) as ChatCompletionInputMessage[];
const messages = opts?.messages ?? exampleMessages;
const messagesStr = stringifyMessages(messages, { sep: ",\n\t\t", start: "[\n\t\t", end: "\n\t]" });

Expand Down
18 changes: 1 addition & 17 deletions packages/tasks/src/snippets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,7 @@ export const snippetConversational = (
}
): InferenceSnippet[] => {
const streaming = opts?.streaming ?? true;
const exampleMessages: ChatCompletionInputMessage[] =
model.pipeline_tag === "text-generation"
? [{ role: "user", content: "What is the capital of France?" }]
: [
{
role: "user",
content: [
{
type: "image_url",
image_url: {
url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg",
},
},
{ type: "text", text: "Describe this image in one sentence." },
],
},
];
const exampleMessages = getModelInputSnippet(model) as ChatCompletionInputMessage[];
const messages = opts?.messages ?? exampleMessages;
const messagesStr = stringifyMessages(messages, {
sep: ",\n\t",
Expand Down
Loading