File size: 3,309 Bytes
7bdfa11
efc6d83
f63d3a3
efc6d83
 
 
 
 
 
7bdfa11
efc6d83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bdfa11
efc6d83
b88a32e
efc6d83
b88a32e
efc6d83
 
 
f2f337f
efc6d83
f2f337f
efc6d83
 
 
 
 
 
 
 
 
 
f2f337f
efc6d83
f2f337f
efc6d83
 
 
 
 
 
 
 
 
 
f2f337f
efc6d83
f2f337f
efc6d83
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
title: FFEmbeds
emoji: 😻
colorFrom: red
colorTo: blue
sdk: gradio
sdk_version: 5.49.1
license: apache-2.0
short_description: Qwen 4b Embeddings for Games
---
# 😻 FF Embeddings API

**Powered by Qwen3-Embedding-4B** - Advanced 4B-parameter multilingual embedding model with matryoshka truncation


## πŸš€ Quick Start

### Python

```python
import requests
import numpy as np

url = "https://YOUR_USERNAME-fairfate-embeddings.hf.space/api/predict"

# Generate embeddings
texts = [
    "Storm King's Thunder - Epic D&D 5E giant-themed adventure",
    "Curse of Strahd - Gothic horror campaign in Ravenloft"
]

response = requests.post(
    url,
    json={
        "data": [texts, True, 1536],  # [texts, use_instruction, dimensions]
        "fn_index": 0
    }
)

result = response.json()
embeddings = np.array(result["data"][0])

# Calculate similarity
def cosine_similarity(a, b):
    return np.dot(a, b)  # Already normalized

similarity = cosine_similarity(embeddings[0], embeddings[1])
print(f"Similarity: {similarity:.3f}")  # ~0.65 (related but different)
```

### TypeScript/JavaScript

```typescript
interface EmbeddingResponse {
  data: number[][][];
}

async function getEmbeddings(
  texts: string[],
  useInstruction: boolean = true,
  dimensions: number = 1536
): Promise<number[][]> {
  const response = await fetch(
    'https://YOUR_USERNAME-fairfate-embeddings.hf.space/api/predict',
    {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        data: [texts, useInstruction, dimensions],
        fn_index: 0
      })
    }
  );

  const result: EmbeddingResponse = await response.json();
  return result.data[0];
}

// Usage
const embeddings = await getEmbeddings([
  "Pathfinder 2E Core Rulebook",
  "D&D 5E Player's Handbook"
]);

// Calculate cosine similarity (already normalized)
function cosineSimilarity(a: number[], b: number[]): number {
  return a.reduce((sum, val, i) => sum + val * b[i], 0);
}

const similarity = cosineSimilarity(embeddings[0], embeddings[1]);
console.log(`Similarity: ${similarity.toFixed(3)}`); // ~0.78 (both RPG rulebooks)
```

### cURL

```bash
curl -X POST \
  https://YOUR_USERNAME-fairfate-embeddings.hf.space/api/predict \
  -H "Content-Type: application/json" \
  -d '{
    "data": [
      ["Cyberpunk detective noir campaign for Shadowrun"],
      true,
      1536
    ],
    "fn_index": 0
  }'
```

## πŸ“– API Reference

### Endpoint

```
POST /api/predict
```

### Request Format

```json
{
  "data": [
    ["text1", "text2", ...],  // Array of texts to embed
    true,                      // use_instruction (boolean)
    1536                       // output_dimensions (32-3584, production: 1536)
  ],
  "fn_index": 0
}
```

### Response Format

```json
{
  "data": [
    [
      [0.123, -0.456, 0.789, ...],  // First embedding (1536 dims)
      [-0.234, 0.567, -0.890, ...]  // Second embedding (1536 dims)
    ]
  ]
}
```

### Parameters

| Parameter | Type | Default | Range | Description |
|-----------|------|---------|-------|-------------|
| `texts` | string[] | required | - | Texts to embed |
| `use_instruction` | boolean | true | - | Add instruction prefix (recommended) |
| `output_dimensions` | number | 1536 | 32-2560 | Output embedding size (production: 1536) |