From 91fffb004f57d23f957f799bfae4463a0606b32e Mon Sep 17 00:00:00 2001 From: Celina Hanouti Date: Fri, 15 Nov 2024 16:01:02 +0100 Subject: [PATCH] separate snippets --- docs/api-inference/tasks/chat-completion.md | 24 ++++-------- .../api-inference/tasks/image-text-to-text.md | 6 +-- scripts/api-inference/scripts/generate.ts | 39 ++++++++++--------- .../common/snippets-template.handlebars | 6 --- 4 files changed, 30 insertions(+), 45 deletions(-) diff --git a/docs/api-inference/tasks/chat-completion.md b/docs/api-inference/tasks/chat-completion.md index 3f4c9b369..2013b62a5 100644 --- a/docs/api-inference/tasks/chat-completion.md +++ b/docs/api-inference/tasks/chat-completion.md @@ -80,8 +80,6 @@ curl 'https://api-inference.huggingface.co/models/google/gemma-2-2b-it/v1/chat/c ```py -# With huggingface_hub client - from huggingface_hub import InferenceClient client = InferenceClient(api_key="hf_***") @@ -102,9 +100,9 @@ stream = client.chat.completions.create( for chunk in stream: print(chunk.choices[0].delta.content, end="") +``` -# With openai client - +```py from openai import OpenAI client = OpenAI( @@ -135,8 +133,6 @@ To use the Python client, see `huggingface_hub`'s [package reference](https://hu ```js -// With huggingface_hub client - import { HfInference } from "@huggingface/inference" const client = new HfInference("hf_***") @@ -161,9 +157,9 @@ for await (const chunk of stream) { console.log(newContent); } } +``` -// With openai client - +```js import { OpenAI } from "openai" const client = new OpenAI({ @@ -238,8 +234,6 @@ curl 'https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Visio ```py -# With huggingface_hub client - from huggingface_hub import InferenceClient client = InferenceClient(api_key="hf_***") @@ -271,9 +265,9 @@ stream = client.chat.completions.create( for chunk in stream: print(chunk.choices[0].delta.content, end="") +``` -# With openai client - +```py from openai import OpenAI client = OpenAI( @@ -315,8 +309,6 @@ To use the Python client, see `huggingface_hub`'s [package reference](https://hu ```js -// With huggingface_hub client - import { HfInference } from "@huggingface/inference" const client = new HfInference("hf_***") @@ -352,9 +344,9 @@ for await (const chunk of stream) { console.log(newContent); } } +``` -// With openai client - +```js import { OpenAI } from "openai" const client = new OpenAI({ diff --git a/docs/api-inference/tasks/image-text-to-text.md b/docs/api-inference/tasks/image-text-to-text.md index 193e66532..4bf6260b2 100644 --- a/docs/api-inference/tasks/image-text-to-text.md +++ b/docs/api-inference/tasks/image-text-to-text.md @@ -46,8 +46,6 @@ curl https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Vision ```py -# With huggingface_hub client - import requests API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Vision-Instruct" @@ -68,9 +66,9 @@ stream = client.chat.completions.create( for chunk in stream: print(chunk.choices[0].delta.content, end="") +``` -# With openai client - +```py import requests API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Vision-Instruct" diff --git a/scripts/api-inference/scripts/generate.ts b/scripts/api-inference/scripts/generate.ts index 2b4805912..d29176ea5 100644 --- a/scripts/api-inference/scripts/generate.ts +++ b/scripts/api-inference/scripts/generate.ts @@ -103,20 +103,13 @@ const formatSnippets = (result: snippets.types.InferenceSnippet | snippets.types // For single snippet, return just the content if (!Array.isArray(result) || result.length === 1) { const snippet = Array.isArray(result) ? result[0] : result; - return snippet.content; + return `\`\`\`${language}\n${snippet.content}\n\`\`\``; } - // Get the appropriate comment - const commentPrefix = { - 'py': '#', - 'js': '//', - 'bash': '#' - }[language] || '#'; - - // Show the snippet for each client + // For multiple snippets, wrap each one in its own code block return result .map(snippet => - `${commentPrefix} With ${snippet.client || defaultClient} client\n\n${snippet.content}` + `\`\`\`${language}\n${snippet.content}\n\`\`\`` ) .join('\n\n'); }; @@ -148,16 +141,14 @@ export function getInferenceSnippet( id: string, pipeline_tag: PipelineType, language: InferenceSnippetLanguage, - config?: JsonObject, - tags?: string[], ): string | undefined { const modelData = { id, pipeline_tag, mask_token: "[MASK]", library_name: "", - config: config ?? {}, - tags: tags ?? [], + config: {}, + tags: [], }; // @ts-ignore if (HAS_SNIPPET_FN[language](modelData)) { @@ -507,15 +498,25 @@ function fetchChatCompletion() { ); const mainModel = DATA.models[task.name][0]; + const mainModelData = { + // @ts-ignore + id: mainModel.id, + pipeline_tag: task.pipelineTag, + mask_token: "", + library_name: "", + // @ts-ignore + tags: ["conversational"], + // @ts-ignore + config: mainModel.config, + }; const taskSnippets = { // @ts-ignore - curl: getInferenceSnippet(mainModel.id, task.pipelineTag, "curl", mainModel.config, ["conversational"]), + curl: GET_SNIPPET_FN["curl"](mainModelData, "hf_***"), // @ts-ignore - python: getInferenceSnippet(mainModel.id, task.pipelineTag, "python", mainModel.config, ["conversational"]), + python: GET_SNIPPET_FN["python"](mainModelData, "hf_***"), // @ts-ignore - javascript: getInferenceSnippet(mainModel.id, task.pipelineTag, "js", mainModel.config, ["conversational"]), + javascript: GET_SNIPPET_FN["js"](mainModelData, "hf_***"), }; - console.log(taskSnippets); DATA.snippets[task.name] = SNIPPETS_TEMPLATE({ taskSnippets, taskSnakeCase: baseName.replace("-", "_"), @@ -548,4 +549,4 @@ await Promise.all( }), ); -console.log("✅ All done!"); +console.log("✅ All done!"); \ No newline at end of file diff --git a/scripts/api-inference/templates/common/snippets-template.handlebars b/scripts/api-inference/templates/common/snippets-template.handlebars index 2d0f099e2..09202f6ba 100644 --- a/scripts/api-inference/templates/common/snippets-template.handlebars +++ b/scripts/api-inference/templates/common/snippets-template.handlebars @@ -5,18 +5,14 @@ {{!-- cURL snippet (if exists) --}} {{#if taskSnippets.curl}} -```bash {{{taskSnippets.curl}}} -``` {{/if}} {{!-- Python snippet (if exists) --}} {{#if taskSnippets.python}} -```py {{{taskSnippets.python}}} -``` To use the Python client, see `huggingface_hub`'s [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.{{taskSnakeCase}}). @@ -25,9 +21,7 @@ To use the Python client, see `huggingface_hub`'s [package reference](https://hu {{!-- JavaScript snippet (if exists) --}} {{#if taskSnippets.javascript}} -```js {{{taskSnippets.javascript}}} -``` To use the JavaScript client, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/HfInference#{{taskAttached}}).