|
17 | 17 | EmbeddingCreateRequest, |
18 | 18 | EmbeddingResponse, |
19 | 19 | EmbeddingBatchCreateRequest, |
20 | | - EmbeddingBatchCreateResponse |
| 20 | + EmbeddingBatchCreateResponse, |
| 21 | + VectorStoreListResponse |
21 | 22 | ) |
22 | 23 | from config import settings |
23 | 24 | from embedding_service import embedding_service |
@@ -126,6 +127,95 @@ async def create_vector_store( |
126 | 127 | raise HTTPException(status_code=500, detail=f"Failed to create vector store: {str(e)}") |
127 | 128 |
|
128 | 129 |
|
| 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 | + |
129 | 219 | @app.post("/v1/vector_stores/{vector_store_id}/search", response_model=VectorStoreSearchResponse) |
130 | 220 | async def search_vector_store( |
131 | 221 | vector_store_id: str, |
|
0 commit comments