File size: 2,036 Bytes
ebd06cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e99c3d5
 
ebd06cc
 
 
 
 
 
 
 
 
 
 
 
 
f31e047
ebd06cc
 
 
 
 
 
 
 
 
 
 
 
 
c1313e9
 
 
ebd06cc
 
 
adeac66
0d3bce3
adeac66
 
 
 
ebd06cc
e99c3d5
 
 
cef520e
ebd06cc
e99c3d5
 
cef520e
ebd06cc
 
 
 
 
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
from datetime import datetime
import logging
import fastapi
from fastapi import Body, Depends
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
from pydantic import BaseModel
from enum import Enum
from typing import List, Dict, Any, Generator, Optional, cast, Callable
from chroma_intf import *
import baseInfra.dropbox_handler as dbh



async def catch_exceptions_middleware(
    request: Request, call_next: Callable[[Request], Any]
) -> Response:
    try:
        return await call_next(request)
    except Exception as e:
        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 ) -> 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
    """
    return getRelevantDocs(inStr)

@app.post(api_base+"/addTextDocument")
async def add_text_document(inStr: str,metadata: dict ) -> Any:
    """
     Add text and metadata to the db
    """
    return addText(inStr,metadata)

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

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

print(__name__)

if __name__ == '__main__' or __name__ == "src.main":
    uvicorn.run(app, host="0.0.0.0", port=8000)