Skip to content

Commit 52c28d9

Browse files
committed
add list endpoint
1 parent 2308c52 commit 52c28d9

File tree

3 files changed

+138
-5
lines changed

3 files changed

+138
-5
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,20 @@ curl -X POST \
2424
}'
2525
```
2626

27-
### 2. Add Single Embedding to Vector Store
27+
### 2. List Vector Stores
28+
```bash
29+
# List all vector stores
30+
curl -X GET \
31+
http://localhost:8000/v1/vector_stores \
32+
-H "Authorization: Bearer your-api-key"
33+
34+
# List with pagination (limit and after parameters)
35+
curl -X GET \
36+
"http://localhost:8000/v1/vector_stores?limit=10&after=vs_abc123" \
37+
-H "Authorization: Bearer your-api-key"
38+
```
39+
40+
### 3. Add Single Embedding to Vector Store
2841
```bash
2942
curl -X POST \
3043
http://localhost:8000/v1/vector_stores/vs_abc123/embeddings \
@@ -41,7 +54,7 @@ curl -X POST \
4154
}'
4255
```
4356

44-
### 3. Add Multiple Embeddings (Batch)
57+
### 4. Add Multiple Embeddings (Batch)
4558
```bash
4659
curl -X POST \
4760
http://localhost:8000/v1/vector_stores/vs_abc123/embeddings/batch \
@@ -63,7 +76,7 @@ curl -X POST \
6376
}'
6477
```
6578

66-
### 4. Search Vector Store
79+
### 5. Search Vector Store
6780
```bash
6881
curl -X POST \
6982
http://localhost:8000/v1/vector_stores/vs_abc123/search \
@@ -234,6 +247,28 @@ Any embedding model supported by LiteLLM proxy can be used. Examples:
234247
}
235248
```
236249

250+
### Vector Store List Response
251+
```json
252+
{
253+
"object": "list",
254+
"data": [
255+
{
256+
"id": "vs_abc123",
257+
"object": "vector_store",
258+
"created_at": 1699024800,
259+
"name": "Support FAQ",
260+
"usage_bytes": 1024,
261+
"file_counts": {"completed": 5, "total": 5},
262+
"status": "completed",
263+
"metadata": {}
264+
}
265+
],
266+
"first_id": "vs_abc123",
267+
"last_id": "vs_def456",
268+
"has_more": false
269+
}
270+
```
271+
237272
### Search Response
238273
```json
239274
{

main.py

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
EmbeddingCreateRequest,
1818
EmbeddingResponse,
1919
EmbeddingBatchCreateRequest,
20-
EmbeddingBatchCreateResponse
20+
EmbeddingBatchCreateResponse,
21+
VectorStoreListResponse
2122
)
2223
from config import settings
2324
from embedding_service import embedding_service
@@ -126,6 +127,95 @@ async def create_vector_store(
126127
raise HTTPException(status_code=500, detail=f"Failed to create vector store: {str(e)}")
127128

128129

130+
@app.get("/v1/vector_stores", response_model=VectorStoreListResponse)
131+
async def list_vector_stores(
132+
limit: Optional[int] = 20,
133+
after: Optional[str] = None,
134+
before: Optional[str] = None,
135+
api_key: str = Depends(get_api_key)
136+
):
137+
"""
138+
List vector stores with optional pagination.
139+
"""
140+
try:
141+
limit = min(limit or 20, 100) # Cap at 100 results
142+
143+
vector_store_table = settings.table_names["vector_stores"]
144+
145+
# Build base query
146+
base_query = f"""
147+
SELECT id, name, file_counts, status, usage_bytes, expires_after, expires_at, last_active_at, metadata,
148+
EXTRACT(EPOCH FROM created_at)::bigint as created_at_timestamp
149+
FROM {vector_store_table}
150+
"""
151+
152+
# Add pagination conditions
153+
conditions = []
154+
params = []
155+
param_count = 1
156+
157+
if after:
158+
conditions.append(f"id > ${param_count}")
159+
params.append(after)
160+
param_count += 1
161+
162+
if before:
163+
conditions.append(f"id < ${param_count}")
164+
params.append(before)
165+
param_count += 1
166+
167+
if conditions:
168+
base_query += " WHERE " + " AND ".join(conditions)
169+
170+
# Add ordering and limit
171+
final_query = base_query + f" ORDER BY created_at DESC LIMIT {limit + 1}"
172+
173+
# Execute query
174+
results = await db.query_raw(final_query, *params)
175+
176+
# Check if there are more results
177+
has_more = len(results) > limit
178+
if has_more:
179+
results = results[:limit] # Remove extra result
180+
181+
# Convert to response format
182+
vector_stores = []
183+
for row in results:
184+
created_at = int(row["created_at_timestamp"])
185+
expires_at = int(row["expires_at"].timestamp()) if row.get("expires_at") else None
186+
last_active_at = int(row["last_active_at"].timestamp()) if row.get("last_active_at") else None
187+
188+
vector_store = VectorStoreResponse(
189+
id=row["id"],
190+
created_at=created_at,
191+
name=row["name"],
192+
usage_bytes=row["usage_bytes"] or 0,
193+
file_counts=row["file_counts"] or {"in_progress": 0, "completed": 0, "failed": 0, "cancelled": 0, "total": 0},
194+
status=row["status"],
195+
expires_after=row["expires_after"],
196+
expires_at=expires_at,
197+
last_active_at=last_active_at,
198+
metadata=row["metadata"]
199+
)
200+
vector_stores.append(vector_store)
201+
202+
# Determine first_id and last_id
203+
first_id = vector_stores[0].id if vector_stores else None
204+
last_id = vector_stores[-1].id if vector_stores else None
205+
206+
return VectorStoreListResponse(
207+
data=vector_stores,
208+
first_id=first_id,
209+
last_id=last_id,
210+
has_more=has_more
211+
)
212+
213+
except Exception as e:
214+
import traceback
215+
traceback.print_exc()
216+
raise HTTPException(status_code=500, detail=f"Failed to list vector stores: {str(e)}")
217+
218+
129219
@app.post("/v1/vector_stores/{vector_store_id}/search", response_model=VectorStoreSearchResponse)
130220
async def search_vector_store(
131221
vector_store_id: str,

models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ class EmbeddingBatchCreateRequest(BaseModel):
6767
class EmbeddingBatchCreateResponse(BaseModel):
6868
object: str = "embedding.batch"
6969
data: List[EmbeddingResponse]
70-
created: int
70+
created: int
71+
72+
73+
class VectorStoreListResponse(BaseModel):
74+
object: str = "list"
75+
data: List[VectorStoreResponse]
76+
first_id: Optional[str] = None
77+
last_id: Optional[str] = None
78+
has_more: bool = False

0 commit comments

Comments
 (0)