Delete rag2.py
Browse files
rag2.py
DELETED
@@ -1,93 +0,0 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""RAG2
|
3 |
-
|
4 |
-
Automatically generated by Colab.
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/1fskQmtugai5co1I64Hv3iAcKQKUKHAzU
|
8 |
-
"""
|
9 |
-
#####importPackages
|
10 |
-
from langchain_text_splitters import CharacterTextSplitter
|
11 |
-
import os
|
12 |
-
import chromadb
|
13 |
-
from chromadb.utils import embedding_functions
|
14 |
-
import sentence_transformers
|
15 |
-
from sentence_transformers import SentenceTransformer
|
16 |
-
import ollama
|
17 |
-
|
18 |
-
|
19 |
-
#####splitBioModels
|
20 |
-
text_splitter2 = CharacterTextSplitter(
|
21 |
-
separator = " // ",
|
22 |
-
chunk_size=100,
|
23 |
-
chunk_overlap=20,
|
24 |
-
length_function=len,
|
25 |
-
is_separator_regex=False,
|
26 |
-
)
|
27 |
-
|
28 |
-
final_items = []
|
29 |
-
|
30 |
-
directory = r"data/*"
|
31 |
-
files = os.listdir(directory)
|
32 |
-
|
33 |
-
for file in files:
|
34 |
-
file_path = os.path.join(directory, file)
|
35 |
-
with open(file_path, 'r') as f:
|
36 |
-
file_content = f.read()
|
37 |
-
items = text_splitter2.create_documents([file_content])
|
38 |
-
final_items.extend(items)
|
39 |
-
|
40 |
-
#####createVectorDB
|
41 |
-
|
42 |
-
CHROMA_DATA_PATH = r"CHROMA_EMBEDDINGS_PATH"
|
43 |
-
COLLECTION_NAME = "BioRAG_Collection"
|
44 |
-
EMBED_MODEL = "all-MiniLM-L6-v2"
|
45 |
-
client = chromadb.PersistentClient(path = CHROMA_DATA_PATH)
|
46 |
-
|
47 |
-
embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction(
|
48 |
-
model_name=EMBED_MODEL
|
49 |
-
)
|
50 |
-
|
51 |
-
collection = client.create_collection(
|
52 |
-
name = "BioRAG_Collection",
|
53 |
-
embedding_function=embedding_func,
|
54 |
-
metadata={"hnsw:space": "cosine"},
|
55 |
-
)
|
56 |
-
|
57 |
-
documents = []
|
58 |
-
|
59 |
-
#####createDocuments
|
60 |
-
for item in final_items:
|
61 |
-
print(item)
|
62 |
-
prompt = f'Please summarize this segment of Antimony: {item}. The summaries must be clear and concise. For Display Names, provide the value for each variable. Expand mathematical functions into words. Cross reference all parts of the provided context. Explain well without errors and in an easily understandable way. Write in a list format. '
|
63 |
-
documents5 = ollama.generate(model = "llama3", prompt=prompt)
|
64 |
-
documents2 = documents5["response"]
|
65 |
-
documents.append(documents2)
|
66 |
-
|
67 |
-
collection.add(
|
68 |
-
documents = documents,
|
69 |
-
ids=[f"id{i}" for i in range(len(documents))]
|
70 |
-
)
|
71 |
-
|
72 |
-
#####generateResponse
|
73 |
-
while 1==1:
|
74 |
-
query_text = input("What question would you like to ask BioRAG? If you would like to end the session, please type 'STOP'." )
|
75 |
-
if query_text == "STOP":
|
76 |
-
break
|
77 |
-
query_results = collection.query(
|
78 |
-
query_texts = query_text,
|
79 |
-
n_results=5,
|
80 |
-
)
|
81 |
-
best_recommendation = query_results['documents']
|
82 |
-
|
83 |
-
prompt_template = f"""Use the following pieces of context to answer the question at the end. If you don't know the answer, say so.
|
84 |
-
|
85 |
-
This is the piece of context necessary: {best_recommendation}
|
86 |
-
|
87 |
-
Cross-reference all pieces of context to define variables and other unknown entities. Calculate mathematical values based on provided matching variables. Remember previous responses if asked a follow up question.
|
88 |
-
|
89 |
-
Question: {query_text}
|
90 |
-
|
91 |
-
"""
|
92 |
-
response = ollama.generate(model = "llama3", prompt=prompt_template)
|
93 |
-
print(response['response'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|