Spaces:
No application file
No application file
EddyGiusepe
commited on
Commit
•
308112d
1
Parent(s):
d68aa6b
Usando o FastAPI para DB vetorial
Browse files
main.py
CHANGED
@@ -2,17 +2,64 @@
|
|
2 |
"""
|
3 |
Data Scientist.: Dr. Eddy Giusepe Chirinos Isidro
|
4 |
|
|
|
|
|
5 |
Executar este Script
|
6 |
====================
|
7 |
O seguinte comando iniciará o servidor:
|
8 |
|
9 |
$ fastapi dev main.py
|
10 |
"""
|
11 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
|
15 |
|
16 |
@app.get("/")
|
17 |
async def root():
|
18 |
-
return {"message": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
"""
|
3 |
Data Scientist.: Dr. Eddy Giusepe Chirinos Isidro
|
4 |
|
5 |
+
Link de estudo ---> https://levelup.gitconnected.com/building-vector-databases-with-fastapi-and-chromadb-0a1cd96fab08
|
6 |
+
|
7 |
Executar este Script
|
8 |
====================
|
9 |
O seguinte comando iniciará o servidor:
|
10 |
|
11 |
$ fastapi dev main.py
|
12 |
"""
|
13 |
+
from fastapi import FastAPI, HTTPException
|
14 |
+
from langchain_chroma import Chroma
|
15 |
+
from langchain_community.embeddings.sentence_transformer import (
|
16 |
+
SentenceTransformerEmbeddings,
|
17 |
+
)
|
18 |
+
|
19 |
+
from models import Query
|
20 |
+
from functions import create_db, delete_persisted_db
|
21 |
|
22 |
+
|
23 |
+
app = FastAPI(title='🤗 Usando FastAPI e Chroma para construir um DB Vetorial 🤗',
|
24 |
+
version='1.0',
|
25 |
+
description="""Data Scientist.: PhD. Eddy Giusepe Chirinos Isidro\n
|
26 |
+
Projeto end-to-end para DBVector""")
|
27 |
|
28 |
|
29 |
@app.get("/")
|
30 |
async def root():
|
31 |
+
return {"message": "Bem-vindo ao DB vetorial com FastAPI e ChromaDB!"}
|
32 |
+
|
33 |
+
# Criar o Database:
|
34 |
+
@app.get("/create/")
|
35 |
+
async def create_database():
|
36 |
+
create_db()
|
37 |
+
return {"message": "Database criado."}
|
38 |
+
|
39 |
+
#Delete database
|
40 |
+
@app.delete("/delete/")
|
41 |
+
async def delete_database():
|
42 |
+
try:
|
43 |
+
delete_persisted_db()
|
44 |
+
return {"message": "Database excluído."}
|
45 |
+
except FileNotFoundError as e:
|
46 |
+
raise HTTPException(status_code=404, detail=str(e))
|
47 |
+
|
48 |
+
#Fetch Chunks
|
49 |
+
@app.post("/neighbours/")
|
50 |
+
async def fetch_item(query: Query):
|
51 |
+
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2",
|
52 |
+
model_kwargs = {'device': 'cpu'}
|
53 |
+
)
|
54 |
+
|
55 |
+
db = Chroma(persist_directory="./chroma_db",
|
56 |
+
embedding_function=embedding_function
|
57 |
+
)
|
58 |
+
|
59 |
+
#print(db.get().keys())
|
60 |
+
#print(sorted(db.get()["ids"], key=int))
|
61 |
+
|
62 |
+
results = db.similarity_search(query.query, k=query.neighbours)
|
63 |
+
|
64 |
+
return {"message": "Vizinhos mais próximos encontrados.", "results": results}
|
65 |
+
|