Spaces:
Sleeping
Sleeping
Download lib/inference.ts from itseffi/ai-product-evals-framework: direct link, hf CLI and curl.
- Browser
- Download file 1.51 kB
-
https://huggingface.co/spaces/itseffi/ai-product-evals-framework/resolve/main/lib/inference.ts
- Command line
-
hf download hf://spaces/itseffi/ai-product-evals-framework/lib/inference.ts
-
curl -L -o inference.ts https://huggingface.co/spaces/itseffi/ai-product-evals-framework/resolve/main/lib/inference.ts
1.51 kB
| export interface InferenceResult { | |
| text: string | |
| latencyMs: number | |
| success: boolean | |
| error?: string | |
| } | |
| export const DEFAULT_MODEL = "HuggingFaceTB/SmolLM3-3B" | |
| /** | |
| * Run inference via the server-side API route. | |
| * The API route handles the HF_TOKEN from environment variables. | |
| */ | |
| export async function runInference( | |
| messages: { role: string; content: string }[], | |
| model: string = DEFAULT_MODEL, | |
| options?: { | |
| temperature?: number | |
| max_tokens?: number | |
| } | |
| ): Promise<InferenceResult> { | |
| const startTime = Date.now() | |
| try { | |
| const response = await fetch("/api/inference", { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| }, | |
| body: JSON.stringify({ | |
| messages, | |
| model: model || DEFAULT_MODEL, | |
| temperature: options?.temperature ?? 0.7, | |
| max_tokens: options?.max_tokens ?? 1024, | |
| }), | |
| }) | |
| const data = await response.json() | |
| if (!response.ok || !data.success) { | |
| return { | |
| text: "", | |
| latencyMs: Date.now() - startTime, | |
| success: false, | |
| error: data.error || "Inference failed", | |
| } | |
| } | |
| return { | |
| text: data.text, | |
| latencyMs: data.latencyMs || (Date.now() - startTime), | |
| success: true, | |
| } | |
| } catch (error) { | |
| const errorMessage = error instanceof Error ? error.message : "Unknown error" | |
| return { | |
| text: "", | |
| latencyMs: Date.now() - startTime, | |
| success: false, | |
| error: errorMessage, | |
| } | |
| } | |
| } | |