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