File size: 8,397 Bytes
c88e992 af59f8a c88e992 d6aeaee c88e992 9a0c7ad c88e992 d6aeaee af59f8a c88e992 d6aeaee 93f8db8 c88e992 0f67a7d 46ca867 6690db1 46ca867 30ebce4 c88e992 d6aeaee 67ba71d 9a0c278 67ba71d c88e992 d6aeaee c88e992 af59f8a c88e992 6690db1 d6aeaee c88e992 93f8db8 af59f8a c88e992 a5d8aec 46fb50d a5d8aec 30ebce4 8f88f4c 78d902f cf75fb7 a5d8aec 2182c09 9a0c278 2182c09 af59f8a 2182c09 30ebce4 094ca20 30ebce4 2182c09 af59f8a 2182c09 8f56967 6690db1 9e15676 6ba99b2 cd89355 9e15676 6ba99b2 e502576 6c54142 6690db1 2182c09 af59f8a 2182c09 46ca867 a5d8aec 46ca867 db5d737 5f34b1a c88e992 46ca867 4c44fc9 46ca867 4c44fc9 af59f8a 46ca867 7d1decc db5d737 d6aeaee db5d737 af59f8a 46ca867 d6aeaee 3117674 d6aeaee 534df4d d6aeaee af59f8a d6aeaee a5d8aec af59f8a d6aeaee 3117674 d6aeaee af59f8a d6aeaee af59f8a d6aeaee af59f8a d6aeaee af59f8a d6aeaee 534df4d d6aeaee af59f8a d6aeaee 7d1decc c88e992 |
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
import fastapi
import json,time
import uvicorn
from fastapi import HTTPException , status
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI as Response
from sse_starlette.sse import EventSourceResponse
from starlette.responses import StreamingResponse
from starlette.requests import Request
import chromadb
from chromadb.config import Settings, System
from pydantic import BaseModel
from typing import List, Dict, Any, Generator, Optional, cast, Callable
from chromadb.api.types import (
Documents,
Embeddings,
EmbeddingFunction,
IDs,
Include,
Metadatas,
Where,
WhereDocument,
GetResult,
QueryResult,
CollectionMetadata,
)
from chromadb.errors import (
ChromaError,
InvalidUUIDError,
InvalidDimensionException,
)
from chromadb.server.fastapi.types import (
AddEmbedding,
DeleteEmbedding,
GetEmbedding,
QueryEmbedding,
RawSql, # Results,
CreateCollection,
UpdateCollection,
UpdateEmbedding,
)
from chromadb.api import API
from chromadb.config import System
import chromadb.utils.embedding_functions as ef
import pandas as pd
import requests
import json,os
from typing import Sequence
from chromadb.api.models.Collection import Collection
import chromadb.errors as errors
from uuid import UUID
from chromadb.telemetry import Telemetry
from overrides import override
import dropbox_handler as dbh
async def catch_exceptions_middleware(
request: Request, call_next: Callable[[Request], Any]
) -> Response:
try:
return await call_next(request)
except ChromaError as e:
return JSONResponse(
content={"error": e.name(), "message": e.message()}, status_code=e.code()
)
except Exception as e:
return JSONResponse(content={"error": repr(e)}, status_code=500)
def _uuid(uuid_str: str) -> UUID:
try:
return UUID(uuid_str)
except ValueError:
raise InvalidUUIDError(f"Could not parse {uuid_str} as a UUID")
dbh.restoreFolder("/index/chroma")
app = fastapi.FastAPI(title="ChromaDB")
app.middleware("http")(catch_exceptions_middleware)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
api_base="/api/v1"
embedding_function=ef.DefaultEmbeddingFunction()
bkend=chromadb.Client(Settings(
chroma_db_impl="duckdb+parquet",
persist_directory="./index/chroma" # Optional, defaults to .chromadb/ in the current directory
))
class PathRequest(BaseModel):
dir: str = "/"
@app.get(api_base+"")
def heartbeat():
print("Received heartbeat request")
return {"nanosecond heartbeat":int(time.time_ns())}
@app.get("/info")
def read_info(request: Request):
print(request)
request_method = request.method
request_headers = request.headers
request_body = request.body
print(request_headers)
response= JSONResponse(content= {"nanosecond heartbeat":int(time.time_ns())})
response.set_cookie(key="my_cookie", value="12345", httponly=True, secure=True)
return response
@app.post(api_base+"/reset")
def reset():
print("Received reset request")
dbh.restoreFolder("/index/chroma")
return bkend.reset()
@app.get(api_base+"/version")
def version():
print("Received version request")
return bkend.get_version()
@app.post(api_base+"/persist")
def persist():
print("Received persist request")
retVal=bkend.persist()
dbh.backupFolder("/index/chroma")
return retVal
@app.post(api_base+"/raw_sql")
def raw_sql(raw_sql: RawSql):
print("Received raw_sql request")
return bkend.raw_sql(raw_sql.raw_sql)
@app.post(api_base+"/walk")
def walk(path: PathRequest):
print("Received walk request")
dirs=[]
try:
for root, items, files in os.walk(path.dir,topdown=True):
for item in items:
dirs.append(item)
except Exception:
print("got exception",Exception)
response= JSONResponse(content= {"dirs":dirs})
return response
@app.get(api_base+"/heartbeat")
def heartbeat1():
print("Received heartbeat1 request")
return heartbeat()
@app.get(api_base+"/collections")
def list_collections(request: Request):
print("Received list_collections request")
print(request.cookies)
return bkend.list_collections()
@app.post(api_base+"/collections")
def create_collection( request: Request, collection: CreateCollection ) :
print("Received request to create_collection")
print(request.cookies)
return bkend.create_collection(name=collection.name,metadata=collection.metadata,embedding_function=embedding_function,get_or_create=collection.get_or_create)
@app.get(api_base+"/collections/{collection_name}")
def get_collection( request: Request,collection_name: str) :
print("Received get_collection request")
print(request.cookies)
return bkend.get_collection(collection_name,embedding_function=embedding_function)
@app.post(api_base+"/collections/{collection_id}/add")
def add(collection_id:str , add:AddEmbedding) -> None:
print("Received add request")
try:
result=bkend._add(collection_id=_uuid(collection_id),embeddings=add.embeddings,metadatas=add.metadatas,documents=add.documents,ids=add.ids,increment_index=add.increment_index)
except InvalidDimensionException as e:
raise HTTPException(status_code=500, detail=str(e))
print(result)
return result
@app.post(api_base+"/collections/{collection_id}/update")
def update(collection_id:str , update:UpdateEmbedding) -> None:
print("Received update request")
return bkend._update(ids=update.ids, collection_id=_uuid(collection_id), embeddings=update.embeddings, documents=update.documents, metadatas=update.metadatas)
@app.post(api_base+"/collections/{collection_id}/upsert")
def upsert(collection_id:str, upsert: AddEmbedding):
print("Received upsert request")
#try:
# print("Received upsert request",upsert)
#except Exception:
# pass
return bkend._upsert(collection_id=_uuid(collection_id),embeddings=upsert.embeddings,metadatas=upsert.metadatas,documents=upsert.documents,ids=upsert.ids,increment_index=upsert.increment_index)
@app.post(api_base+"/collections/{collection_id}/get")
def get( collection_id: str, get: GetEmbedding) -> GetResult:
print("Received get request")
return bkend._get(collection_id=_uuid(collection_id), ids=get.ids, where=get.where,
where_document=get.where_document, sort=get.sort, limit=get.limit,
offset=get.offset, include=get.include)
@app.post(api_base+"/collections/{collection_id}/delete")
def delete(collection_id: str, delete: DeleteEmbedding) -> List[UUID]:
print("Received delete request")
return bkend._delete(where=delete.where, ids=delete.ids,
collection_id=_uuid(collection_id), where_document=delete.where_document)
@app.get(api_base+"/collections/{collection_id}/count")
def count(collection_id:str) ->int:
print("Received count request")
return bkend._count(_uuid(collection_id))
@app.post(api_base+"/collections/{collection_id}/query")
def get_nearest_neighbors(collection_id: str, query: QueryEmbedding) -> QueryResult:
print("Received get_nearest_neighbors request")
return bkend._query(collection_id=_uuid(collection_id), where=query.where, where_document=query.where_document,
query_embeddings=query.query_embeddings, n_results=query.n_results, include=query.include)
@app.post(api_base+"/collections/{collection_name}/create_index")
def create_index(collection_name:str)-> bool:
print("Received create_index request")
return bkend.create_index(collection_name)
@app.put(api_base+"/collections/{collection_id}")
def modify(collection_id: str, collection: UpdateCollection) -> None:
print("Received modify-collection request")
return bkend._modify(id=_uuid(collection_id), new_name=collection.new_name, new_metadata=collection.new_metadata)
@app.delete(api_base+"/collections/{collection_name}")
def delete_collection(collection_name:str) -> None:
print("Received delete_collection request")
return bkend.delete_collection(collection_name)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
|