-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (57 loc) · 1.98 KB
/
Copy pathmain.py
File metadata and controls
69 lines (57 loc) · 1.98 KB
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
import os
import ollama
import chromadb
from docx_parser import DocumentParser
from langchain_text_splitters import RecursiveCharacterTextSplitter
DB_PATH = 'db/'
IN_PATH = 'test_docs/'
EMBEDDINGS_MODEL = 'mxbai-embed-large' #'nomic-embed-text'
CHAT_MODEL = 'llama3'
RESULTS_COUNT = 5
documents = []
text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=0)
for file in os.listdir(IN_PATH):
filename = os.fsdecode(file)
if filename.endswith(".docx"):
parsed_document = DocumentParser(IN_PATH + filename)
for _type, item in parsed_document.parse():
if _type == "paragraph":
if item["style_id"] == 'Normal':
#documents.append(text_splitter.split_text(item["text"]))
#print(text_splitter.split_text(item["text"]))
chunk_list = text_splitter.split_text(item["text"])
for chunk in chunk_list:
documents.append(chunk)
#client = chromadb.PersistentClient(path=DB_PATH)
client = chromadb.Client()
collection = client.create_collection(name="data_docs")
# store each document in a vector embedding database
for i, d in enumerate(documents):
response = ollama.embed(model=EMBEDDINGS_MODEL, input=d)
embeddings = response["embeddings"]
collection.add(
ids=[str(i)],
embeddings=embeddings,
documents=[d]
)
# an example input
#print("Enter a prompt:")
#question = input().strip()
question = "Has Answer Digital worked with NHS England Analytical teams?"
# generate an embedding for the input and retrieve the most relevant doc
response = ollama.embed(
model=EMBEDDINGS_MODEL,
input=question
)
results = collection.query(
query_embeddings=[response["embeddings"][0]],
n_results=RESULTS_COUNT
)
data = results['documents'][0][0]
print("Results: " + data)
# generate a response combining the prompt and data we retrieved in step 2
output = ollama.generate(
model=CHAT_MODEL,
prompt=f"Using this data: {data}. Respond to this prompt: {input}. Return one answer only."
)
print(output['response'])