File size: 3,049 Bytes
03a7adf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""RAG2



Automatically generated by Colab.



Original file is located at

    https://colab.research.google.com/drive/1fskQmtugai5co1I64Hv3iAcKQKUKHAzU

"""
#####importPackages
from langchain_text_splitters import CharacterTextSplitter
import os
import chromadb
from chromadb.utils import embedding_functions
import sentence_transformers 
from sentence_transformers import SentenceTransformer
import ollama


#####splitBioModels
text_splitter2 = CharacterTextSplitter(
    separator = "  // ",
    chunk_size=100,
    chunk_overlap=20,
    length_function=len,
    is_separator_regex=False,
)

final_items = []

directory = r"data/*"  
files = os.listdir(directory)

for file in files:
    file_path = os.path.join(directory, file)
    with open(file_path, 'r') as f:
        file_content = f.read()
        items = text_splitter2.create_documents([file_content])
        final_items.extend(items)

#####createVectorDB

CHROMA_DATA_PATH = r"CHROMA_EMBEDDINGS_PATH"
COLLECTION_NAME = "BioRAG_Collection"
EMBED_MODEL = "all-MiniLM-L6-v2"
client = chromadb.PersistentClient(path = CHROMA_DATA_PATH)

embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction(
    model_name=EMBED_MODEL
)

collection = client.create_collection(
    name = "BioRAG_Collection",
    embedding_function=embedding_func,
    metadata={"hnsw:space": "cosine"},
)

documents = []

#####createDocuments
for item in final_items:
    print(item)
    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. '
    documents5 = ollama.generate(model = "llama3", prompt=prompt)
    documents2 = documents5["response"]
    documents.append(documents2) 

collection.add(
    documents = documents,
    ids=[f"id{i}" for i in range(len(documents))]
)

#####generateResponse
while 1==1:
    query_text = input("What question would you like to ask BioRAG? If you would like to end the session, please type 'STOP'." )
    if query_text == "STOP":
        break
    query_results = collection.query(
        query_texts = query_text,
        n_results=5,
    )
    best_recommendation = query_results['documents']

    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.



    This is the piece of context necessary: {best_recommendation}



    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.



    Question: {query_text}



    """
    response = ollama.generate(model = "llama3", prompt=prompt_template)
    print(response['response'])