Skip to content

Commit

Permalink
separate snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
hanouticelina committed Nov 15, 2024
1 parent 435a3fe commit 91fffb0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 45 deletions.
24 changes: 8 additions & 16 deletions docs/api-inference/tasks/chat-completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ curl 'https://api-inference.huggingface.co/models/google/gemma-2-2b-it/v1/chat/c

<python>
```py
# With huggingface_hub client

from huggingface_hub import InferenceClient

client = InferenceClient(api_key="hf_***")
Expand All @@ -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(
Expand Down Expand Up @@ -135,8 +133,6 @@ To use the Python client, see `huggingface_hub`'s [package reference](https://hu

<js>
```js
// With huggingface_hub client

import { HfInference } from "@huggingface/inference"

const client = new HfInference("hf_***")
Expand All @@ -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({
Expand Down Expand Up @@ -238,8 +234,6 @@ curl 'https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Visio

<python>
```py
# With huggingface_hub client

from huggingface_hub import InferenceClient

client = InferenceClient(api_key="hf_***")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -315,8 +309,6 @@ To use the Python client, see `huggingface_hub`'s [package reference](https://hu

<js>
```js
// With huggingface_hub client

import { HfInference } from "@huggingface/inference"

const client = new HfInference("hf_***")
Expand Down Expand Up @@ -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({
Expand Down
6 changes: 2 additions & 4 deletions docs/api-inference/tasks/image-text-to-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ curl https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Vision

<python>
```py
# With huggingface_hub client

import requests

API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Vision-Instruct"
Expand All @@ -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"
Expand Down
39 changes: 20 additions & 19 deletions scripts/api-inference/scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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("-", "_"),
Expand Down Expand Up @@ -548,4 +549,4 @@ await Promise.all(
}),
);

console.log("✅ All done!");
console.log("✅ All done!");
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
{{!-- cURL snippet (if exists) --}}
{{#if taskSnippets.curl}}
<curl>
```bash
{{{taskSnippets.curl}}}
```
</curl>
{{/if}}

{{!-- Python snippet (if exists) --}}
{{#if taskSnippets.python}}
<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}}).
</python>
Expand All @@ -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>
```js
{{{taskSnippets.javascript}}}
```

To use the JavaScript client, see `huggingface.js`'s [package reference](https://huggingface.co/docs/huggingface.js/inference/classes/HfInference#{{taskAttached}}).
</js>
Expand Down

0 comments on commit 91fffb0

Please sign in to comment.