Download src/lib/ai/huggingface.js from NeoPy/hugging-c-ai: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/NeoPy/hugging-c-ai/resolve/main/src/lib/ai/huggingface.js
- Command line
-
hf download hf://NeoPy/hugging-c-ai/src/lib/ai/huggingface.js
-
curl -L -o huggingface.js https://huggingface.co/NeoPy/hugging-c-ai/resolve/main/src/lib/ai/huggingface.js
1.17 kB
| import { InferenceClient } from "@huggingface/inference"; | |
| export async function callHuggingFace({ | |
| model, messages, temperature = 1.0, maxTokens = 2048, | |
| stream = false, apiKey, onToken, | |
| }) { | |
| const client = new InferenceClient(apiKey); | |
| const modelName = model.replace(/^hf:/, ""); | |
| const chatMessages = messages.map((m) => ({ role: m.role, content: m.content })); | |
| if (stream) { | |
| let fullText = ""; | |
| const out = await client.chatCompletionStream({ | |
| model: modelName, | |
| messages: chatMessages, | |
| temperature: Number(temperature), | |
| max_tokens: Number(maxTokens), | |
| }); | |
| for await (const chunk of out) { | |
| const delta = chunk.choices?.[0]?.delta?.content; | |
| if (delta) { fullText += delta; onToken?.(delta); } | |
| } | |
| return { text: fullText, usage: null }; | |
| } | |
| const out = await client.chatCompletion({ | |
| model: modelName, | |
| messages: chatMessages, | |
| temperature: Number(temperature), | |
| max_tokens: Number(maxTokens), | |
| }); | |
| return { | |
| text: out.choices?.[0]?.message?.content || "", | |
| usage: { | |
| promptTokens: out.usage?.prompt_tokens, | |
| completionTokens: out.usage?.completion_tokens, | |
| }, | |
| }; | |
| } | |