File size: 1,544 Bytes
414512a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// This service calls the Hugging Face Inference API.
// It's best for models fine-tuned for JSON output, like Llama 3.
export const callHuggingFaceAPI = async (prompt, apiKey, maxTokens = 2000) => {
if (!apiKey) {
throw new Error("Hugging Face API Key is required.");
}
// Recommended model: Meta's Llama 3 is excellent at following instructions.
const API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct";
const headers = {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json",
};
const payload = {
inputs: prompt,
parameters: {
max_new_tokens: maxTokens,
return_full_text: false, // Important: only return the generated text
temperature: 0.6, // A bit of creativity but still factual
top_p: 0.9,
}
};
try {
const response = await fetch(API_URL, {
method: "POST",
headers: headers,
body: JSON.stringify(payload),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Hugging Face API request failed: ${response.status} - ${errorText}`);
}
const data = await response.json();
// The response is an array, we take the first element's generated text.
if (data && data[0] && data[0].generated_text) {
return data[0].generated_text;
} else {
throw new Error("Invalid response structure from Hugging Face API.");
}
} catch (error) {
console.error("Hugging Face API Error:", error);
throw error;
}
}; |