Spaces:
Sleeping
Sleeping
Commit
·
e5aa264
1
Parent(s):
a153556
removing qdrant from app
Browse files
app.py
CHANGED
@@ -1,50 +1,22 @@
|
|
1 |
-
import os
|
|
|
2 |
from typing import List
|
3 |
from chainlit.types import AskFileResponse
|
4 |
from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader
|
5 |
from aimakerspace.openai_utils.prompts import UserRolePrompt, SystemRolePrompt
|
|
|
6 |
from aimakerspace.openai_utils.chatmodel import ChatOpenAI
|
7 |
-
from aimakerspace.openai_utils.embedding import EmbeddingModel
|
8 |
import chainlit as cl
|
9 |
from PyPDF2 import PdfReader
|
10 |
-
from qdrant_client import QdrantClient
|
11 |
-
from qdrant_client.http import models
|
12 |
|
13 |
system_template = "Use the following context to answer a users question. If you cannot find the answer in the context, say you don't know the answer."
|
14 |
system_role_prompt = SystemRolePrompt(system_template)
|
|
|
15 |
user_prompt_template = "Context:\n{context}\n\nQuestion:\n{question}"
|
16 |
user_role_prompt = UserRolePrompt(user_prompt_template)
|
17 |
|
18 |
-
class QdrantVectorStore:
|
19 |
-
def __init__(self, collection_name="my_collection"):
|
20 |
-
self.client = QdrantClient(":memory:")
|
21 |
-
self.collection_name = collection_name
|
22 |
-
self.embedding_model = EmbeddingModel()
|
23 |
-
|
24 |
-
async def abuild_from_list(self, texts: List[str]):
|
25 |
-
self.client.recreate_collection(
|
26 |
-
collection_name=self.collection_name,
|
27 |
-
vectors_config=models.VectorParams(size=1536, distance=models.Distance.COSINE),
|
28 |
-
)
|
29 |
-
for i, text in enumerate(texts):
|
30 |
-
vector = await self.embedding_model.aembed_query(text)
|
31 |
-
self.client.upsert(
|
32 |
-
collection_name=self.collection_name,
|
33 |
-
points=[models.PointStruct(id=i, vector=vector, payload={"text": text})]
|
34 |
-
)
|
35 |
-
return self
|
36 |
-
|
37 |
-
def search_by_text(self, query: str, k: int = 4):
|
38 |
-
vector = self.embedding_model.embed_query(query)
|
39 |
-
results = self.client.search(
|
40 |
-
collection_name=self.collection_name,
|
41 |
-
query_vector=vector,
|
42 |
-
limit=k
|
43 |
-
)
|
44 |
-
return [(hit.payload["text"], hit.score) for hit in results]
|
45 |
-
|
46 |
class RetrievalAugmentedQAPipeline:
|
47 |
-
def __init__(self, llm: ChatOpenAI(), vector_db_retriever:
|
48 |
self.llm = llm
|
49 |
self.vector_db_retriever = vector_db_retriever
|
50 |
|
@@ -98,7 +70,7 @@ async def on_chat_start():
|
|
98 |
texts = process_file(file)
|
99 |
print(f"Processing {len(texts)} text chunks")
|
100 |
|
101 |
-
vector_db =
|
102 |
vector_db = await vector_db.abuild_from_list(texts)
|
103 |
|
104 |
chat_openai = ChatOpenAI()
|
|
|
1 |
+
import os
|
2 |
+
import tempfile
|
3 |
from typing import List
|
4 |
from chainlit.types import AskFileResponse
|
5 |
from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader
|
6 |
from aimakerspace.openai_utils.prompts import UserRolePrompt, SystemRolePrompt
|
7 |
+
from aimakerspace.vectordatabase import VectorDatabase
|
8 |
from aimakerspace.openai_utils.chatmodel import ChatOpenAI
|
|
|
9 |
import chainlit as cl
|
10 |
from PyPDF2 import PdfReader
|
|
|
|
|
11 |
|
12 |
system_template = "Use the following context to answer a users question. If you cannot find the answer in the context, say you don't know the answer."
|
13 |
system_role_prompt = SystemRolePrompt(system_template)
|
14 |
+
|
15 |
user_prompt_template = "Context:\n{context}\n\nQuestion:\n{question}"
|
16 |
user_role_prompt = UserRolePrompt(user_prompt_template)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
class RetrievalAugmentedQAPipeline:
|
19 |
+
def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
|
20 |
self.llm = llm
|
21 |
self.vector_db_retriever = vector_db_retriever
|
22 |
|
|
|
70 |
texts = process_file(file)
|
71 |
print(f"Processing {len(texts)} text chunks")
|
72 |
|
73 |
+
vector_db = VectorDatabase()
|
74 |
vector_db = await vector_db.abuild_from_list(texts)
|
75 |
|
76 |
chat_openai = ChatOpenAI()
|