diff --git a/packages/tasks/src/snippets/curl.ts b/packages/tasks/src/snippets/curl.ts index 2104a0c29c..c8f39b677e 100644 --- a/packages/tasks/src/snippets/curl.ts +++ b/packages/tasks/src/snippets/curl.ts @@ -10,7 +10,7 @@ export const snippetBasic = (model: ModelDataMinimal, accessToken: string): stri -H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"`; export const snippetTextGeneration = (model: ModelDataMinimal, accessToken: string): string => { - if (model.config?.tokenizer_config?.chat_template) { + if (model.tags.includes("conversational")) { // Conversational model detected, so we display a code snippet that features the Messages API return `curl 'https://api-inference.huggingface.co/models/${model.id}/v1/chat/completions' \\ -H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}" \\ @@ -27,6 +27,32 @@ export const snippetTextGeneration = (model: ModelDataMinimal, accessToken: stri } }; +export const snippetImageTextToTextGeneration = (model: ModelDataMinimal, accessToken: string): string => { + if (model.tags.includes("conversational")) { + // Conversational model detected, so we display a code snippet that features the Messages API + return `curl 'https://api-inference.huggingface.co/models/${model.id}/v1/chat/completions' \\ +-H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}" \\ +-H 'Content-Type: application/json' \\ +-d '{ + "model": "${model.id}", + "messages": [ + { + "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."} + ] + } + ], + "max_tokens": 500, + "stream": false +}' +`; + } else { + return snippetBasic(model, accessToken); + } +}; + export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string): string => `curl https://api-inference.huggingface.co/models/${model.id} \\ -X POST \\ @@ -51,6 +77,7 @@ export const curlSnippets: Partial { });`; export const snippetTextGeneration = (model: ModelDataMinimal, accessToken: string): string => { - if (model.config?.tokenizer_config?.chat_template) { + if (model.tags.includes("conversational")) { // Conversational model detected, so we display a code snippet that features the Messages API return `import { HfInference } from "@huggingface/inference"; @@ -41,6 +41,35 @@ for await (const chunk of inference.chatCompletionStream({ return snippetBasic(model, accessToken); } }; + +export const snippetImageTextToTextGeneration = (model: ModelDataMinimal, accessToken: string): string => { + if (model.tags.includes("conversational")) { + // Conversational model detected, so we display a code snippet that features the Messages API + return `import { HfInference } from "@huggingface/inference"; + +const inference = new HfInference("${accessToken || `{API_TOKEN}`}"); +const imageUrl = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"; + +for await (const chunk of inference.chatCompletionStream({ + model: "${model.id}", + messages: [ + { + "role": "user", + "content": [ + {"type": "image_url", "image_url": {"url": imageUrl}}, + {"type": "text", "text": "Describe this image in one sentence."}, + ], + } + ], + max_tokens: 500, +})) { + process.stdout.write(chunk.choices[0]?.delta?.content || ""); +}`; + } else { + return snippetBasic(model, accessToken); + } +}; + export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string): string => `async function query(data) { const response = await fetch( @@ -156,6 +185,7 @@ export const jsSnippets: Partial `from huggingface_hub import InferenceClient -client = InferenceClient( - "${model.id}", - token="${accessToken || "{API_TOKEN}"}", -) +client = InferenceClient(api_key="${accessToken || "{API_TOKEN}"}") for message in client.chat_completion( + model="${model.id}", messages=[{"role": "user", "content": "What is the capital of France?"}], max_tokens=500, stream=True, ): print(message.choices[0].delta.content, end="")`; +export const snippetConversationalWithImage = (model: ModelDataMinimal, accessToken: string): string => + `from huggingface_hub import InferenceClient + +client = InferenceClient(api_key="${accessToken || "{API_TOKEN}"}") + +image_url = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" + +for message in client.chat_completion( + model="${model.id}", + messages=[ + { + "role": "user", + "content": [ + {"type": "image_url", "image_url": {"url": image_url}}, + {"type": "text", "text": "Describe this image in one sentence."}, + ], + } + ], + max_tokens=500, + stream=True, +): + print(message.choices[0].delta.content, end="")`; + export const snippetZeroShotClassification = (model: ModelDataMinimal): string => `def query(payload): response = requests.post(API_URL, headers=headers, json=payload) @@ -153,9 +174,12 @@ export const pythonSnippets: Partial; +export type ModelDataMinimal = Pick< + ModelData, + "id" | "pipeline_tag" | "mask_token" | "library_name" | "config" | "tags" +>;