moving to fastAPI
Browse files
Dockerfile
CHANGED
@@ -20,6 +20,6 @@ COPY --chown=user . $HOME/app
|
|
20 |
|
21 |
#RUN ls -al
|
22 |
|
23 |
-
|
24 |
|
25 |
-
CMD ["python","indexer.py"]
|
|
|
20 |
|
21 |
#RUN ls -al
|
22 |
|
23 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
24 |
|
25 |
+
#CMD ["python","indexer.py"]
|
main.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fastapi
|
2 |
+
import json
|
3 |
+
import uvicorn
|
4 |
+
from fastapi import HTTPException
|
5 |
+
from fastapi.responses import HTMLResponse
|
6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
7 |
+
from sse_starlette.sse import EventSourceResponse
|
8 |
+
from starlette.responses import StreamingResponse
|
9 |
+
from pydantic import BaseModel
|
10 |
+
from typing import List, Dict, Any, Generator, Optional
|
11 |
+
from server import client
|
12 |
+
from types import (
|
13 |
+
Documents,
|
14 |
+
Embeddings,
|
15 |
+
EmbeddingFunction,
|
16 |
+
IDs,
|
17 |
+
Include,
|
18 |
+
Metadatas,
|
19 |
+
Where,
|
20 |
+
WhereDocument,
|
21 |
+
GetResult,
|
22 |
+
QueryResult,
|
23 |
+
CollectionMetadata,
|
24 |
+
)
|
25 |
+
|
26 |
+
|
27 |
+
app = fastapi.FastAPI(title="ChromaDB")
|
28 |
+
app.add_middleware(
|
29 |
+
CORSMiddleware,
|
30 |
+
allow_origins=["*"],
|
31 |
+
allow_credentials=True,
|
32 |
+
allow_methods=["*"],
|
33 |
+
allow_headers=["*"],
|
34 |
+
)
|
35 |
+
api_base="/api/v1"
|
36 |
+
bkend=client()
|
37 |
+
|
38 |
+
@app.get(api_base+"")
|
39 |
+
def heartbeat():
|
40 |
+
return bkend.heartbeat()
|
41 |
+
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
45 |
+
|
server.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from chromadb.config import Settings
|
2 |
+
import chromadb
|
3 |
+
import time
|
4 |
+
|
5 |
+
class client():
|
6 |
+
def __init__(self):
|
7 |
+
self.db = chromadb.Client(Settings(
|
8 |
+
chroma_db_impl="duckdb+parquet",
|
9 |
+
persist_directory="/path/to/persist/directory" # Optional, defaults to .chromadb/ in the current directory
|
10 |
+
))
|
11 |
+
|
12 |
+
def heartbeat(self):
|
13 |
+
return int(time.time_ns())
|
14 |
+
|
15 |
+
def list_collections(self):
|
16 |
+
return self.db.list_collections()
|
types.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel
|
2 |
+
from typing import Any, Dict, List, Optional
|
3 |
+
#from chromadb.api.types import (
|
4 |
+
# CollectionMetadata,
|
5 |
+
# Include,
|
6 |
+
#)
|
7 |
+
from typing import Union, Literal
|
8 |
+
|
9 |
+
#TODO:Update version-Step1::This file is a copy from chromadb/server/fastapi/types.py
|
10 |
+
# Copy CollectionMetadata and Include from chromadb.api.types and comment that import
|
11 |
+
# Additionally import Union and Literal from typing for "Include to work"
|
12 |
+
CollectionMetadata = Dict[Any, Any]
|
13 |
+
|
14 |
+
Include = List[
|
15 |
+
Union[
|
16 |
+
Literal["documents"],
|
17 |
+
Literal["embeddings"],
|
18 |
+
Literal["metadatas"],
|
19 |
+
Literal["distances"],
|
20 |
+
]
|
21 |
+
]
|
22 |
+
|
23 |
+
|
24 |
+
class AddEmbedding(BaseModel): # type: ignore
|
25 |
+
# Pydantic doesn't handle Union types cleanly like Embeddings which has
|
26 |
+
# Union[int, float] so we use Any here to ensure data is parsed
|
27 |
+
# to its original type.
|
28 |
+
embeddings: Optional[List[Any]] = None
|
29 |
+
metadatas: Optional[List[Dict[Any, Any]]] = None
|
30 |
+
documents: Optional[List[str]] = None
|
31 |
+
ids: List[str]
|
32 |
+
increment_index: bool = True
|
33 |
+
|
34 |
+
|
35 |
+
class UpdateEmbedding(BaseModel): # type: ignore
|
36 |
+
embeddings: Optional[List[Any]] = None
|
37 |
+
metadatas: Optional[List[Dict[Any, Any]]] = None
|
38 |
+
documents: Optional[List[str]] = None
|
39 |
+
ids: List[str]
|
40 |
+
increment_index: bool = True
|
41 |
+
|
42 |
+
|
43 |
+
class QueryEmbedding(BaseModel): # type: ignore
|
44 |
+
# TODO: Pydantic doesn't bode well with recursive types so we use generic Dicts
|
45 |
+
# for Where and WhereDocument. This is not ideal, but it works for now since
|
46 |
+
# there is a lot of downstream validation.
|
47 |
+
where: Optional[Dict[Any, Any]] = {}
|
48 |
+
where_document: Optional[Dict[Any, Any]] = {}
|
49 |
+
query_embeddings: List[Any]
|
50 |
+
n_results: int = 10
|
51 |
+
include: Include = ["metadatas", "documents", "distances"]
|
52 |
+
|
53 |
+
|
54 |
+
class GetEmbedding(BaseModel): # type: ignore
|
55 |
+
ids: Optional[List[str]] = None
|
56 |
+
where: Optional[Dict[Any, Any]] = None
|
57 |
+
where_document: Optional[Dict[Any, Any]] = None
|
58 |
+
sort: Optional[str] = None
|
59 |
+
limit: Optional[int] = None
|
60 |
+
offset: Optional[int] = None
|
61 |
+
include: Include = ["metadatas", "documents"]
|
62 |
+
|
63 |
+
|
64 |
+
class RawSql(BaseModel): # type: ignore
|
65 |
+
raw_sql: str
|
66 |
+
|
67 |
+
|
68 |
+
class DeleteEmbedding(BaseModel): # type: ignore
|
69 |
+
ids: Optional[List[str]] = None
|
70 |
+
where: Optional[Dict[Any, Any]] = None
|
71 |
+
where_document: Optional[Dict[Any, Any]] = None
|
72 |
+
|
73 |
+
|
74 |
+
class CreateCollection(BaseModel): # type: ignore
|
75 |
+
name: str
|
76 |
+
metadata: Optional[CollectionMetadata] = None
|
77 |
+
get_or_create: bool = False
|
78 |
+
|
79 |
+
|
80 |
+
class UpdateCollection(BaseModel): # type: ignore
|
81 |
+
new_name: Optional[str] = None
|
82 |
+
new_metadata: Optional[CollectionMetadata] = None
|