nurulajt commited on
Commit
b810e9b
·
verified ·
1 Parent(s): ea169b3

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +221 -57
README.md CHANGED
@@ -123,79 +123,127 @@ Response:
123
  ```json
124
  {
125
  "status": "healthy",
126
- "models_loaded": ["jobbertv2", "jina"],
127
- "voyage_available": false
 
128
  }
129
  ```
130
 
131
- ### Generate Embeddings
132
 
133
- #### JobBERT v2 (Job Titles)
134
 
 
 
 
135
  ```bash
136
- curl -X POST http://localhost:7860/embed \
137
  -H "Content-Type: application/json" \
138
  -d '{
139
- "texts": ["Software Engineer", "Data Scientist", "Product Manager"],
140
- "model": "jobbertv2"
141
  }'
142
  ```
143
 
144
- #### JobBERT v3 (Latest, Recommended)
145
-
146
  ```bash
147
- curl -X POST http://localhost:7860/embed \
148
  -H "Content-Type: application/json" \
 
149
  -d '{
150
- "texts": ["Software Engineer", "Data Scientist", "Product Manager"],
151
- "model": "jobbertv3"
152
  }'
153
  ```
154
 
155
- #### Jina AI (with task specification)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
  ```bash
158
- curl -X POST http://localhost:7860/embed \
159
  -H "Content-Type: application/json" \
160
  -d '{
161
- "texts": ["What is machine learning?", "How does AI work?"],
162
- "model": "jina",
163
- "task": "retrieval.query"
164
  }'
165
  ```
166
 
167
- **Jina AI Tasks:**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  - `retrieval.query`: For search queries
169
  - `retrieval.passage`: For documents
170
  - `text-matching`: For similarity (default)
171
- - `classification`: For classification
172
- - `separation`: For clustering
173
 
174
  #### Voyage AI (requires API key)
175
 
176
  ```bash
177
- curl -X POST http://localhost:7860/embed \
178
  -H "Content-Type: application/json" \
179
- -d '{
180
- "texts": ["This is a document to embed"],
181
- "model": "voyage",
182
- "input_type": "document"
183
- }'
184
  ```
185
 
186
- **Voyage AI Input Types:**
187
  - `document`: For documents/passages
188
  - `query`: For search queries
189
 
190
- ### Response Format
 
 
 
 
 
 
 
 
 
 
 
191
 
 
192
  ```json
193
  {
194
- "embeddings": [
195
- [0.123, -0.456, 0.789, ...],
196
- [0.234, -0.567, 0.890, ...]
197
- ],
198
- "model": "jobbertv2",
199
  "dimension": 768,
200
  "num_texts": 2
201
  }
@@ -207,48 +255,164 @@ curl -X POST http://localhost:7860/embed \
207
  curl http://localhost:7860/models
208
  ```
209
 
210
- ## Python Client Example
 
 
211
 
212
  ```python
213
  import requests
214
 
215
- url = "http://localhost:7860/embed"
 
216
 
217
- # JobBERT v3 (recommended)
218
- response = requests.post(url, json={
219
- "texts": ["Software Engineer", "Data Scientist"],
220
- "model": "jobbertv3"
221
- })
222
- result = response.json()
223
- embeddings = result["embeddings"]
224
- print(f"Got {len(embeddings)} embeddings of dimension {result['dimension']}")
225
 
226
- # JobBERT v2
227
- response = requests.post(url, json={
228
- "texts": ["Product Manager"],
229
- "model": "jobbertv2"
230
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
  # Jina AI with task
233
- response = requests.post(url, json={
234
- "texts": ["What is Python?"],
235
- "model": "jina",
236
- "task": "retrieval.query"
237
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
- # Voyage AI
240
  response = requests.post(url, json={
241
- "texts": ["Document text here"],
242
- "model": "voyage",
243
- "input_type": "document"
244
  })
 
 
 
245
  ```
246
 
247
  ## Environment Variables
248
 
249
  - `PORT`: Server port (default: 7860)
 
 
250
  - `VOYAGE_API_KEY`: Voyage AI API key (optional, required for Voyage embeddings)
251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  ## Interactive Documentation
253
 
254
  Once the API is running, visit:
 
123
  ```json
124
  {
125
  "status": "healthy",
126
+ "models_loaded": ["jobbertv2", "jobbertv3", "jina"],
127
+ "voyage_available": false,
128
+ "api_key_required": false
129
  }
130
  ```
131
 
132
+ ### Generate Embeddings (Elasticsearch Compatible)
133
 
134
+ The main `/embed` endpoint uses Elasticsearch inference API format with model selection via query parameter.
135
 
136
+ #### Single Text (JobBERT v3 - default)
137
+
138
+ Without API key:
139
  ```bash
140
+ curl -X POST "http://localhost:7860/embed" \
141
  -H "Content-Type: application/json" \
142
  -d '{
143
+ "input": "Software Engineer"
 
144
  }'
145
  ```
146
 
147
+ With API key:
 
148
  ```bash
149
+ curl -X POST "http://localhost:7860/embed" \
150
  -H "Content-Type: application/json" \
151
+ -H "Authorization: Bearer YOUR_API_KEY" \
152
  -d '{
153
+ "input": "Software Engineer"
 
154
  }'
155
  ```
156
 
157
+ Response:
158
+ ```json
159
+ {
160
+ "embedding": [0.123, -0.456, 0.789, ...]
161
+ }
162
+ ```
163
+
164
+ #### Single Text with Model Selection
165
+
166
+ ```bash
167
+ # JobBERT v2
168
+ curl -X POST "http://localhost:7860/embed?model=jobbertv2" \
169
+ -H "Content-Type: application/json" \
170
+ -d '{"input": "Data Scientist"}'
171
+
172
+ # JobBERT v3 (recommended)
173
+ curl -X POST "http://localhost:7860/embed?model=jobbertv3" \
174
+ -H "Content-Type: application/json" \
175
+ -d '{"input": "Product Manager"}'
176
+
177
+ # Jina AI
178
+ curl -X POST "http://localhost:7860/embed?model=jina" \
179
+ -H "Content-Type: application/json" \
180
+ -d '{"input": "Machine Learning Engineer"}'
181
+ ```
182
+
183
+ #### Multiple Texts (Batch)
184
 
185
  ```bash
186
+ curl -X POST "http://localhost:7860/embed?model=jobbertv3" \
187
  -H "Content-Type: application/json" \
188
  -d '{
189
+ "input": ["Software Engineer", "Data Scientist", "Product Manager"]
 
 
190
  }'
191
  ```
192
 
193
+ Response:
194
+ ```json
195
+ {
196
+ "embeddings": [
197
+ [0.123, -0.456, ...],
198
+ [0.234, -0.567, ...],
199
+ [0.345, -0.678, ...]
200
+ ]
201
+ }
202
+ ```
203
+
204
+ #### Jina AI with Task Type
205
+
206
+ ```bash
207
+ curl -X POST "http://localhost:7860/embed?model=jina&task=retrieval.query" \
208
+ -H "Content-Type: application/json" \
209
+ -d '{"input": "What is machine learning?"}'
210
+ ```
211
+
212
+ **Jina AI Tasks (query parameter):**
213
  - `retrieval.query`: For search queries
214
  - `retrieval.passage`: For documents
215
  - `text-matching`: For similarity (default)
 
 
216
 
217
  #### Voyage AI (requires API key)
218
 
219
  ```bash
220
+ curl -X POST "http://localhost:7860/embed?model=voyage&input_type=document" \
221
  -H "Content-Type: application/json" \
222
+ -d '{"input": "This is a document to embed"}'
 
 
 
 
223
  ```
224
 
225
+ **Voyage AI Input Types (query parameter):**
226
  - `document`: For documents/passages
227
  - `query`: For search queries
228
 
229
+ ### Batch Endpoint (Original Format)
230
+
231
+ For compatibility, the original batch endpoint is still available at `/embed/batch`:
232
+
233
+ ```bash
234
+ curl -X POST http://localhost:7860/embed/batch \
235
+ -H "Content-Type: application/json" \
236
+ -d '{
237
+ "texts": ["Software Engineer", "Data Scientist"],
238
+ "model": "jobbertv3"
239
+ }'
240
+ ```
241
 
242
+ Response includes metadata:
243
  ```json
244
  {
245
+ "embeddings": [[0.123, ...], [0.234, ...]],
246
+ "model": "jobbertv3",
 
 
 
247
  "dimension": 768,
248
  "num_texts": 2
249
  }
 
255
  curl http://localhost:7860/models
256
  ```
257
 
258
+ ## Python Client Examples
259
+
260
+ ### Elasticsearch-Compatible Format (Recommended)
261
 
262
  ```python
263
  import requests
264
 
265
+ BASE_URL = "http://localhost:7860"
266
+ API_KEY = "your-api-key-here" # Optional, only if API key is required
267
 
268
+ # Headers (include API key if required)
269
+ headers = {}
270
+ if API_KEY:
271
+ headers["Authorization"] = f"Bearer {API_KEY}"
 
 
 
 
272
 
273
+ # Single embedding (JobBERT v3 - default)
274
+ response = requests.post(
275
+ f"{BASE_URL}/embed",
276
+ headers=headers,
277
+ json={"input": "Software Engineer"}
278
+ )
279
+ result = response.json()
280
+ embedding = result["embedding"] # Single vector
281
+ print(f"Embedding dimension: {len(embedding)}")
282
+
283
+ # Single embedding with model selection
284
+ response = requests.post(
285
+ f"{BASE_URL}/embed?model=jina",
286
+ headers=headers,
287
+ json={"input": "Data Scientist"}
288
+ )
289
+ embedding = response.json()["embedding"]
290
+
291
+ # Batch embeddings
292
+ response = requests.post(
293
+ f"{BASE_URL}/embed?model=jobbertv3",
294
+ headers=headers,
295
+ json={"input": ["Software Engineer", "Data Scientist", "Product Manager"]}
296
+ )
297
+ result = response.json()
298
+ embeddings = result["embeddings"] # List of vectors
299
+ print(f"Generated {len(embeddings)} embeddings")
300
 
301
  # Jina AI with task
302
+ response = requests.post(
303
+ f"{BASE_URL}/embed?model=jina&task=retrieval.query",
304
+ headers=headers,
305
+ json={"input": "What is Python?"}
306
+ )
307
+
308
+ # Voyage AI with input type
309
+ response = requests.post(
310
+ f"{BASE_URL}/embed?model=voyage&input_type=document",
311
+ headers=headers,
312
+ json={"input": "Document text here"}
313
+ )
314
+ ```
315
+
316
+ ### Python Client Class with API Key Support
317
+
318
+ ```python
319
+ import requests
320
+ from typing import List, Union, Optional
321
+
322
+ class EmbeddingClient:
323
+ def __init__(self, base_url: str, api_key: Optional[str] = None, model: str = "jobbertv3"):
324
+ self.base_url = base_url
325
+ self.api_key = api_key
326
+ self.model = model
327
+ self.headers = {}
328
+ if api_key:
329
+ self.headers["Authorization"] = f"Bearer {api_key}"
330
+
331
+ def embed(self, text: Union[str, List[str]]) -> Union[List[float], List[List[float]]]:
332
+ """Get embeddings for single text or batch"""
333
+ response = requests.post(
334
+ f"{self.base_url}/embed?model={self.model}",
335
+ headers=self.headers,
336
+ json={"input": text}
337
+ )
338
+ response.raise_for_status()
339
+ result = response.json()
340
+
341
+ if isinstance(text, str):
342
+ return result["embedding"]
343
+ else:
344
+ return result["embeddings"]
345
+
346
+ # Usage
347
+ client = EmbeddingClient(
348
+ base_url="https://YOUR-SPACE.hf.space",
349
+ api_key="your-api-key-here", # Optional
350
+ model="jobbertv3"
351
+ )
352
+
353
+ # Single embedding
354
+ embedding = client.embed("Software Engineer")
355
+ print(f"Dimension: {len(embedding)}")
356
+
357
+ # Batch embeddings
358
+ embeddings = client.embed(["Software Engineer", "Data Scientist"])
359
+ print(f"Generated {len(embeddings)} embeddings")
360
+ ```
361
+
362
+ ### Batch Format (Original)
363
+
364
+ ```python
365
+ import requests
366
+
367
+ url = "http://localhost:7860/embed/batch"
368
 
 
369
  response = requests.post(url, json={
370
+ "texts": ["Software Engineer", "Data Scientist"],
371
+ "model": "jobbertv3"
 
372
  })
373
+ result = response.json()
374
+ embeddings = result["embeddings"]
375
+ print(f"Model: {result['model']}, Dimension: {result['dimension']}")
376
  ```
377
 
378
  ## Environment Variables
379
 
380
  - `PORT`: Server port (default: 7860)
381
+ - `API_KEY`: Your API key for authentication (optional, but recommended for production)
382
+ - `REQUIRE_API_KEY`: Set to `true` to enable API key authentication (default: `false`)
383
  - `VOYAGE_API_KEY`: Voyage AI API key (optional, required for Voyage embeddings)
384
 
385
+ ### Setting Up API Key Authentication
386
+
387
+ #### Local Development
388
+
389
+ ```bash
390
+ # Set environment variables
391
+ export API_KEY="your-secret-key-here"
392
+ export REQUIRE_API_KEY="true"
393
+
394
+ # Run the API
395
+ python api.py
396
+ ```
397
+
398
+ #### Hugging Face Spaces
399
+
400
+ 1. Go to your Space settings
401
+ 2. Click on "Variables and secrets"
402
+ 3. Add secrets:
403
+ - Name: `API_KEY`, Value: `your-secret-key-here`
404
+ - Name: `REQUIRE_API_KEY`, Value: `true`
405
+ 4. Restart your Space
406
+
407
+ #### Docker
408
+
409
+ ```bash
410
+ docker run -p 7860:7860 \
411
+ -e API_KEY="your-secret-key-here" \
412
+ -e REQUIRE_API_KEY="true" \
413
+ embedding-api
414
+ ```
415
+
416
  ## Interactive Documentation
417
 
418
  Once the API is running, visit: