@huggingface/tasks
Advanced tools
Comparing version 0.12.29 to 0.12.30
import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks"; | ||
export interface StringifyMessagesOptions { | ||
sep: string; | ||
start: string; | ||
end: string; | ||
export declare function stringifyMessages(messages: ChatCompletionInputMessage[], opts?: { | ||
indent?: string; | ||
attributeKeyQuotes?: boolean; | ||
customContentEscaper?: (str: string) => string; | ||
} | ||
export declare function stringifyMessages(messages: ChatCompletionInputMessage[], opts: StringifyMessagesOptions): string; | ||
}): string; | ||
type PartialGenerationParameters = Partial<Pick<GenerationParameters, "temperature" | "max_tokens" | "top_p">>; | ||
export interface StringifyGenerationConfigOptions { | ||
sep: string; | ||
start: string; | ||
end: string; | ||
export declare function stringifyGenerationConfig(config: PartialGenerationParameters, opts: { | ||
indent: string; | ||
attributeValueConnector: string; | ||
attributeKeyQuotes?: boolean; | ||
} | ||
export declare function stringifyGenerationConfig(config: PartialGenerationParameters, opts: StringifyGenerationConfigOptions): string; | ||
}): string; | ||
export {}; | ||
//# sourceMappingURL=common.d.ts.map |
{ | ||
"name": "@huggingface/tasks", | ||
"packageManager": "pnpm@8.10.5", | ||
"version": "0.12.29", | ||
"version": "0.12.30", | ||
"description": "List of ML tasks for huggingface.co/tasks", | ||
@@ -49,2 +49,3 @@ "repository": "https://github.com/huggingface/huggingface.js.git", | ||
"check": "tsc", | ||
"test": "vitest run", | ||
"inference-codegen": "tsx scripts/inference-codegen.ts && prettier --write src/tasks/*/inference.ts", | ||
@@ -51,0 +52,0 @@ "inference-tgi-import": "tsx scripts/inference-tgi-import.ts && prettier --write src/tasks/text-generation/spec/*.json && prettier --write src/tasks/chat-completion/spec/*.json", |
import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks"; | ||
export interface StringifyMessagesOptions { | ||
sep: string; | ||
start: string; | ||
end: string; | ||
attributeKeyQuotes?: boolean; | ||
customContentEscaper?: (str: string) => string; | ||
export function stringifyMessages( | ||
messages: ChatCompletionInputMessage[], | ||
opts?: { | ||
indent?: string; | ||
attributeKeyQuotes?: boolean; | ||
customContentEscaper?: (str: string) => string; | ||
} | ||
): string { | ||
let messagesStr = JSON.stringify(messages, null, "\t"); | ||
if (opts?.indent) { | ||
messagesStr = messagesStr.replaceAll("\n", `\n${opts.indent}`); | ||
} | ||
if (!opts?.attributeKeyQuotes) { | ||
messagesStr = messagesStr.replace(/"([^"]+)":/g, "$1:"); | ||
} | ||
if (opts?.customContentEscaper) { | ||
messagesStr = opts.customContentEscaper(messagesStr); | ||
} | ||
return messagesStr; | ||
} | ||
export function stringifyMessages(messages: ChatCompletionInputMessage[], opts: StringifyMessagesOptions): string { | ||
const keyRole = opts.attributeKeyQuotes ? `"role"` : "role"; | ||
const keyContent = opts.attributeKeyQuotes ? `"content"` : "content"; | ||
const messagesStringified = messages.map(({ role, content }) => { | ||
if (typeof content === "string") { | ||
content = JSON.stringify(content).slice(1, -1); | ||
if (opts.customContentEscaper) { | ||
content = opts.customContentEscaper(content); | ||
} | ||
return `{ ${keyRole}: "${role}", ${keyContent}: "${content}" }`; | ||
} else { | ||
2; | ||
content = content.map(({ image_url, text, type }) => ({ | ||
type, | ||
image_url, | ||
...(text ? { text: JSON.stringify(text).slice(1, -1) } : undefined), | ||
})); | ||
content = JSON.stringify(content).slice(1, -1); | ||
if (opts.customContentEscaper) { | ||
content = opts.customContentEscaper(content); | ||
} | ||
return `{ ${keyRole}: "${role}", ${keyContent}: [${content}] }`; | ||
} | ||
}); | ||
return opts.start + messagesStringified.join(opts.sep) + opts.end; | ||
} | ||
type PartialGenerationParameters = Partial<Pick<GenerationParameters, "temperature" | "max_tokens" | "top_p">>; | ||
export interface StringifyGenerationConfigOptions { | ||
sep: string; | ||
start: string; | ||
end: string; | ||
attributeValueConnector: string; | ||
attributeKeyQuotes?: boolean; | ||
} | ||
export function stringifyGenerationConfig( | ||
config: PartialGenerationParameters, | ||
opts: StringifyGenerationConfigOptions | ||
opts: { | ||
indent: string; | ||
attributeValueConnector: string; | ||
attributeKeyQuotes?: boolean; | ||
} | ||
): string { | ||
const quote = opts.attributeKeyQuotes ? `"` : ""; | ||
return ( | ||
opts.start + | ||
Object.entries(config) | ||
.map(([key, val]) => `${quote}${key}${quote}${opts.attributeValueConnector}${val}`) | ||
.join(opts.sep) + | ||
opts.end | ||
); | ||
return Object.entries(config) | ||
.map(([key, val]) => `${quote}${key}${quote}${opts.attributeValueConnector}${val}`) | ||
.join(`,${opts.indent}`); | ||
} |
@@ -44,5 +44,3 @@ import type { PipelineType } from "../pipelines.js"; | ||
"messages": ${stringifyMessages(messages, { | ||
sep: ",\n\t\t", | ||
start: `[\n\t\t`, | ||
end: `\n\t]`, | ||
indent: "\t", | ||
attributeKeyQuotes: true, | ||
@@ -52,5 +50,3 @@ customContentEscaper: (str) => str.replace(/'/g, "'\\''"), | ||
${stringifyGenerationConfig(config, { | ||
sep: ",\n ", | ||
start: "", | ||
end: "", | ||
indent: "\n ", | ||
attributeKeyQuotes: true, | ||
@@ -57,0 +53,0 @@ attributeValueConnector: ": ", |
@@ -131,2 +131,3 @@ import type { PipelineType } from "../pipelines"; | ||
"text-generation": inputsTextGeneration, | ||
"image-text-to-text": inputsTextGeneration, | ||
"text-to-image": inputsTextToImage, | ||
@@ -133,0 +134,0 @@ "text-to-speech": inputsTextToSpeech, |
@@ -45,3 +45,3 @@ import type { PipelineType } from "../pipelines.js"; | ||
const messages = opts?.messages ?? exampleMessages; | ||
const messagesStr = stringifyMessages(messages, { sep: ",\n\t\t", start: "[\n\t\t", end: "\n\t]" }); | ||
const messagesStr = stringifyMessages(messages, { indent: "\t" }); | ||
@@ -54,5 +54,3 @@ const config = { | ||
const configStr = stringifyGenerationConfig(config, { | ||
sep: ",\n\t", | ||
start: "", | ||
end: "", | ||
indent: "\n\t", | ||
attributeValueConnector: ": ", | ||
@@ -59,0 +57,0 @@ }); |
@@ -21,8 +21,3 @@ import type { PipelineType } from "../pipelines.js"; | ||
const messages = opts?.messages ?? exampleMessages; | ||
const messagesStr = stringifyMessages(messages, { | ||
sep: ",\n\t", | ||
start: `[\n\t`, | ||
end: `\n]`, | ||
attributeKeyQuotes: true, | ||
}); | ||
const messagesStr = stringifyMessages(messages, { attributeKeyQuotes: true }); | ||
@@ -35,5 +30,3 @@ const config = { | ||
const configStr = stringifyGenerationConfig(config, { | ||
sep: ",\n\t", | ||
start: "", | ||
end: "", | ||
indent: "\n\t", | ||
attributeValueConnector: "=", | ||
@@ -60,3 +53,3 @@ }); | ||
for chunk in stream: | ||
print(chunk.choices[0].delta.content)`, | ||
print(chunk.choices[0].delta.content, end="")`, | ||
}, | ||
@@ -82,3 +75,3 @@ { | ||
for chunk in stream: | ||
print(chunk.choices[0].delta.content)`, | ||
print(chunk.choices[0].delta.content, end="")`, | ||
}, | ||
@@ -85,0 +78,0 @@ ]; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1422352
397
35647