File size: 4,978 Bytes
ebd06cc
f73cf1f
ebd06cc
 
 
37419af
ebd06cc
 
 
 
 
 
11dccc3
ebd06cc
 
775521b
e99c3d5
3c13580
57baf4b
ebd06cc
57baf4b
775521b
 
857c3ab
 
ebd06cc
11dccc3
1acc6c8
11dccc3
 
 
 
857c3ab
 
11dccc3
ebd06cc
 
 
 
 
8d7feb0
 
f4b28b5
ebd06cc
 
3c13580
93bc1c1
3c13580
ebd06cc
 
 
 
f31e047
ebd06cc
 
 
 
 
 
 
 
 
 
 
37419af
ebd06cc
c1313e9
 
 
ebd06cc
57baf4b
 
88a42ac
 
 
d462b73
57baf4b
 
ebd06cc
80d6857
 
 
 
 
 
 
 
 
01de2b3
80d6857
 
 
d462b73
80d6857
 
 
adeac66
857c3ab
adeac66
 
 
57baf4b
ee33749
57baf4b
d462b73
57baf4b
 
ebd06cc
e99c3d5
 
ee4103c
ebd06cc
e99c3d5
 
0756fa3
ebd06cc
857c3ab
 
c738eed
857c3ab
5d4719e
857c3ab
 
 
 
5d4719e
 
857c3ab
 
c738eed
 
5d4719e
857c3ab
 
ee4103c
9467b57
ee4103c
 
ebd06cc
 
 
80d6857
57baf4b
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
from datetime import datetime
import logging,os
import fastapi
from fastapi import Body, Depends
import uvicorn
from fastapi import BackgroundTasks,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
from pydantic import BaseModel, Extra
from enum import Enum
from typing import List, Dict, Any, Generator, Optional, cast, Callable
from chromaIntf import ChromaIntf
import baseInfra.dropbox_handler as dbh
import traceback
from pathlib import Path

logger=logging.getLogger("root")
chromaIntf=ChromaIntf()

class PathRequest(BaseModel):
    dir: str = "/"

class MetaD(BaseModel):
    timestamp: Optional[str]= datetime.now().isoformat()
    class Config:
        allow_population_by_field_name = True
        extra = Extra.allow

class DocWithMeta(BaseModel):
    text: str = ""
    metadata: Optional[MetaD] = MetaD()

async def catch_exceptions_middleware(
    request: Request, call_next: Callable[[Request], Any]
) -> Response:
    try:
        #print("In exception cater middleware")
        #print(request.headers)
        #print(await request.body())
        return await call_next(request)
    except Exception as e:
        print(repr(e))
        print("from the catch exception")
        traceback.print_exc()
        return JSONResponse(content={"error": repr(e)}, status_code=500)



app = fastapi.FastAPI(title="Maya Persistence")
app.middleware("http")(catch_exceptions_middleware)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
api_base="/api/v1"

@app.post(api_base+"/getMatchingDocs")
async def get_matching_docs(inStr: str, kwargs: Dict [Any, Any] ,background_tasks:BackgroundTasks) -> Any:
    """
    Gets the query embeddings and uses metadata appropriately and gets the matching docs for query
    TODO: Add parameter for type of query and number of docs to return
    TODO: Add parameter to return the source information as well
    """
    logger.info("================================================")
    logger.info("Received new request at /getMatchingDocs:"+inStr)
    #retVal=await chromaIntf.getRelevantDocs(inStr,kwargs)
    background_tasks.add_task(chromaIntf.getRelevantDocs,inStr,kwargs)
    retVal="added to queue"
    logger.info("Returning :"+retVal)
    logger.info("================================================")
    return retVal

@app.post(api_base+"/getMatchingDocsSync")
async def get_matching_docs_sync(inStr: str, kwargs: Dict [Any, Any]) -> Any:
    """
    Gets the query embeddings and uses metadata appropriately and gets the matching docs for query
    This is synchronous version
    TODO: Add parameter for type of query and number of docs to return
    TODO: Add parameter to return the source information as well
    """
    logger.info("================================================")
    logger.info("Received new request at /getMatchingDocsSync:"+inStr)
    retVal=await chromaIntf.getRelevantDocs(inStr,kwargs)
    #background_tasks.add_task(chromaIntf.getRelevantDocs,inStr,kwargs)
    #retVal="added to queue"
    logger.info(f"Returning: {len(retVal)} results")
    logger.info("================================================")
    return retVal

@app.post(api_base+"/addTextDocument")
async def add_text_document(inDoc: DocWithMeta ) -> Any:
    """
     Add text and metadata to the db
    """
    logger.info("================================================")
    logger.info("Received new request at /addTextDocuments:"+inDoc.text)
    retVal= await chromaIntf.addText(inDoc.text,inDoc.metadata)
    logger.info(f"Returning: {len(retVal)} results")
    logger.info("================================================")
    return retVal

@app.get(api_base+"/persist")
async def persist_db():
    return await chromaIntf.persist()

@app.get(api_base+"/reset")
async def reset_db():
    return await dbh.restoreFolder("db")

@app.post(api_base+"/walk")
def walk(path: PathRequest):
    print("Received walk request for",path)
    dirs=[]
    fileList=[]
    try:
        for root, items, files in os.walk(path.dir,topdown=True):
            for item in items:
                dirs.append(item)
            for filea in files:
                fileList.append(filea)
    except Exception:
        print("got exception",Exception)
    print("dirs ",dirs)
    print("files ",fileList)
    response= JSONResponse(content= {"dirs":dirs,"files":fileList})
    return response

@app.get(api_base+"/list")
async def list_docs():
    return await chromaIntf.listDocs()

print(__name__)

if __name__ == '__main__' or __name__ == "src.main":
    uvicorn.run(f"{Path(__file__).stem}:app", host="0.0.0.0", port=8000,workers=1)
    #uvicorn.run(app, host="0.0.0.0", port=8000)