Skip to content

Commit

Permalink
WTF
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-c committed Feb 6, 2025
1 parent 6700d95 commit 62da2b0
Show file tree
Hide file tree
Showing 23 changed files with 54 additions and 57 deletions.
3 changes: 1 addition & 2 deletions packages/tasks-gen/scripts/generate-snippets-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ function generateInferenceSnippet(
provider: SnippetInferenceProvider,
opts?: Record<string, unknown>
): InferenceSnippet[] {
const generatedSnippets = GET_SNIPPET_FN[language](model, "api_token", provider, opts);
return Array.isArray(generatedSnippets) ? generatedSnippets : [generatedSnippets];
return GET_SNIPPET_FN[language](model, "api_token", provider, opts);
}

async function getExpectedInferenceSnippet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const chatCompletion = await client.chatCompletion({
}
],
provider: "hf-inference",
max_tokens: 500
max_tokens: 500,
});

console.log(chatCompletion.choices[0].message);
console.log(chatCompletion.choices[0].message);
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const chatCompletion = await client.chatCompletion({
}
],
provider: "together",
max_tokens: 500
max_tokens: 500,
});

console.log(chatCompletion.choices[0].message);
console.log(chatCompletion.choices[0].message);
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
completion = client.chat.completions.create(
model="meta-llama/Llama-3.1-8B-Instruct",
messages=messages,
max_tokens=500
max_tokens=500,
)

print(completion.choices[0].message)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
completion = client.chat.completions.create(
model="meta-llama/Llama-3.1-8B-Instruct",
messages=messages,
max_tokens=500
max_tokens=500,
)

print(completion.choices[0].message)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAI } from "openai";

const client = new OpenAI({
baseURL: "https://router.huggingface.co/hf-inference",
baseURL: "https://router.huggingface.co/hf-inference/v1",
apiKey: "api_token"
});

Expand All @@ -13,7 +13,7 @@ const chatCompletion = await client.chat.completions.create({
content: "What is the capital of France?"
}
],
max_tokens: 500
max_tokens: 500,
});

console.log(chatCompletion.choices[0].message);
console.log(chatCompletion.choices[0].message);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from openai import OpenAI

client = OpenAI(
base_url="https://router.huggingface.co/hf-inference",
base_url="https://router.huggingface.co/hf-inference/v1",
api_key="api_token"
)

Expand All @@ -15,7 +15,7 @@
completion = client.chat.completions.create(
model="meta-llama/Llama-3.1-8B-Instruct",
messages=messages,
max_tokens=500
max_tokens=500,
)

print(completion.choices[0].message)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const chatCompletion = await client.chat.completions.create({
content: "What is the capital of France?"
}
],
max_tokens: 500
max_tokens: 500,
});

console.log(chatCompletion.choices[0].message);
console.log(chatCompletion.choices[0].message);
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
completion = client.chat.completions.create(
model="meta-llama/Llama-3.1-8B-Instruct",
messages=messages,
max_tokens=500
max_tokens=500,
)

print(completion.choices[0].message)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const stream = client.chatCompletionStream({
}
],
provider: "hf-inference",
max_tokens: 500
max_tokens: 500,
});

for await (const chunk of stream) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAI } from "openai";

const client = new OpenAI({
baseURL: "https://router.huggingface.co/hf-inference",
baseURL: "https://router.huggingface.co/hf-inference/v1",
apiKey: "api_token"
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from openai import OpenAI

client = OpenAI(
base_url="https://router.huggingface.co/hf-inference",
base_url="https://router.huggingface.co/hf-inference/v1",
api_key="api_token"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const chatCompletion = await client.chatCompletion({
}
],
provider: "hf-inference",
max_tokens: 500
max_tokens: 500,
});

console.log(chatCompletion.choices[0].message);
console.log(chatCompletion.choices[0].message);
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
completion = client.chat.completions.create(
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
messages=messages,
max_tokens=500
max_tokens=500,
)

print(completion.choices[0].message)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAI } from "openai";

const client = new OpenAI({
baseURL: "https://router.huggingface.co/hf-inference",
baseURL: "https://router.huggingface.co/hf-inference/v1",
apiKey: "api_token"
});

Expand All @@ -24,7 +24,7 @@ const chatCompletion = await client.chat.completions.create({
]
}
],
max_tokens: 500
max_tokens: 500,
});

console.log(chatCompletion.choices[0].message);
console.log(chatCompletion.choices[0].message);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from openai import OpenAI

client = OpenAI(
base_url="https://router.huggingface.co/hf-inference",
base_url="https://router.huggingface.co/hf-inference/v1",
api_key="api_token"
)

Expand All @@ -26,7 +26,7 @@
completion = client.chat.completions.create(
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
messages=messages,
max_tokens=500
max_tokens=500,
)

print(completion.choices[0].message)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const stream = client.chatCompletionStream({
}
],
provider: "hf-inference",
max_tokens: 500
max_tokens: 500,
});

for await (const chunk of stream) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAI } from "openai";

const client = new OpenAI({
baseURL: "https://router.huggingface.co/hf-inference",
baseURL: "https://router.huggingface.co/hf-inference/v1",
apiKey: "api_token"
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from openai import OpenAI

client = OpenAI(
base_url="https://router.huggingface.co/hf-inference",
base_url="https://router.huggingface.co/hf-inference/v1",
api_key="api_token"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests

API_URL = "https://router.huggingface.co/hf-inference"
API_URL = "https://router.huggingface.co/hf-inference/v1"
headers = {"Authorization": "Bearer api_token"}

def query(payload):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests

API_URL = "https://router.huggingface.co/hf-inference"
API_URL = "https://router.huggingface.co/hf-inference/v1"
headers = {"Authorization": "Bearer api_token"}

def query(payload):
Expand Down
24 changes: 12 additions & 12 deletions packages/tasks/src/snippets/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export const snippetBasic = (
return [
...(model.pipeline_tag && model.pipeline_tag in HFJS_METHODS
? [
{
client: "huggingface.js",
content: `\
{
client: "huggingface.js",
content: `\
import { HfInference } from "@huggingface/inference";
const client = new HfInference("${accessToken || `{API_TOKEN}`}");
Expand All @@ -42,8 +42,8 @@ const output = await client.${HFJS_METHODS[model.pipeline_tag]}({
console.log(output);
`,
},
]
},
]
: []),
{
client: "fetch",
Expand Down Expand Up @@ -216,8 +216,8 @@ export const snippetZeroShotClassification = (model: ModelDataMinimal, accessTok
}
query({"inputs": ${getModelInputSnippet(
model
)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}).then((response) => {
model
)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}).then((response) => {
console.log(JSON.stringify(response));
});`,
},
Expand Down Expand Up @@ -248,9 +248,9 @@ const image = await client.textToImage({
},
...(provider === "hf-inference"
? [
{
client: "fetch",
content: `async function query(data) {
{
client: "fetch",
content: `async function query(data) {
const response = await fetch(
"https://router.huggingface.co/hf-inference/models/${model.id}",
{
Expand All @@ -268,8 +268,8 @@ const image = await client.textToImage({
query({"inputs": ${getModelInputSnippet(model)}}).then((response) => {
// Use image
});`,
},
]
},
]
: []),
];
};
Expand Down
24 changes: 11 additions & 13 deletions packages/tasks/src/snippets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ image = client.text_to_image(
},
...(provider === "fal-ai"
? [
{
client: "fal-client",
content: `\
{
client: "fal-client",
content: `\
import fal_client
result = fal_client.subscribe(
Expand All @@ -285,14 +285,14 @@ result = fal_client.subscribe(
)
print(result)
`,
},
]
},
]
: []),
...(provider === "hf-inference"
? [
{
client: "requests",
content: `\
{
client: "requests",
content: `\
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
Expand All @@ -305,8 +305,8 @@ image_bytes = query({
import io
from PIL import Image
image = Image.open(io.BytesIO(image_bytes))`,
},
]
},
]
: []),
];
};
Expand Down Expand Up @@ -443,8 +443,6 @@ export function getPythonInferenceSnippet(
? pythonSnippets[model.pipeline_tag]?.(model, accessToken, provider) ?? []
: [];

const baseUrl = HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider);

return snippets.map((snippet) => {
return {
...snippet,
Expand All @@ -453,7 +451,7 @@ export function getPythonInferenceSnippet(
? `\
import requests
API_URL = "${baseUrl}"
API_URL = "${openAIbaseUrl(provider)}"
headers = {"Authorization": ${accessToken ? `"Bearer ${accessToken}"` : `f"Bearer {API_TOKEN}"`}}
${snippet.content}`
Expand Down

0 comments on commit 62da2b0

Please sign in to comment.