File size: 1,494 Bytes
1257299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { InferenceClient } from "@huggingface/inference";

const apiKey = process.env.HUGGING_FACE_API_TOKEN;

if (!apiKey) {
  throw new Error("Missing HUGGING_FACE_API_TOKEN environment variable.");
}

const hf = new InferenceClient(apiKey);

// Generate text embeddings
export async function generateEmbedding(text: string): Promise<number[]> {
  try {
    const raw = await hf.featureExtraction({
      model: "sentence-transformers/all-MiniLM-L6-v2",
      inputs: text,
    });

    // Normalize HuggingFace outputs into flat number[]
    if (typeof raw === "number") {
      return [raw];
    }

    if (Array.isArray(raw)) {
      // raw could be number[] OR number[][]
      if (typeof raw[0] === "number") {
        return raw as number[];
      }

      // number[][] → flatten
      return (raw as number[][]).flat();
    }

    throw new Error("Unexpected embedding format from HuggingFace.");
  } catch (error: unknown) {
    console.error("Embedding generation error:", error);
    throw error;
  }
}

// Text generation
export async function runInference(
  modelId: string,
  inputs: string,
  parameters?: Record<string, unknown>,
) {
  try {
    const response = await hf.textGeneration({
      model: modelId,
      inputs,
      parameters: {
        max_new_tokens: 250,
        temperature: 0.7,
        ...parameters,
      },
    });

    return response;
  } catch (error: unknown) {
    console.error("Hugging Face Inference Error:", error);
    throw error;
  }
}