Spaces:
Running
Running
Updated search
Browse files- app.py +1 -1
- corpus_query.py +147 -13
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from ring import get_openai_api_key
|
|
| 10 |
from ring import generate_response
|
| 11 |
from ring import generate_panel_response
|
| 12 |
|
| 13 |
-
__version__ = "0.3.
|
| 14 |
|
| 15 |
col1, col2 = st.columns([3, 1])
|
| 16 |
|
|
|
|
| 10 |
from ring import generate_response
|
| 11 |
from ring import generate_panel_response
|
| 12 |
|
| 13 |
+
__version__ = "0.3.1"
|
| 14 |
|
| 15 |
col1, col2 = st.columns([3, 1])
|
| 16 |
|
corpus_query.py
CHANGED
|
@@ -3,34 +3,167 @@ from llama_index.core.storage.storage_context import StorageContext
|
|
| 3 |
from llama_index.core.storage.index_store import SimpleIndexStore
|
| 4 |
from llama_index.core.storage.docstore import SimpleDocumentStore
|
| 5 |
from llama_index.core import load_index_from_storage
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
STORE_DIR = "openworm.ai_store"
|
| 8 |
SOURCE_DOCUMENT = "source document"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
docstore=SimpleDocumentStore.from_persist_dir(persist_dir=STORE_DIR),
|
| 12 |
-
vector_store=SimpleVectorStore.from_persist_dir(persist_dir=STORE_DIR),
|
| 13 |
-
index_store=SimpleIndexStore.from_persist_dir(persist_dir=STORE_DIR),
|
| 14 |
-
)
|
| 15 |
-
index_reloaded = load_index_from_storage(storage_context)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
|
| 21 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
response = query_engine.query(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
files_used = []
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
if SOURCE_DOCUMENT in v:
|
| 27 |
if v[SOURCE_DOCUMENT] not in files_used:
|
| 28 |
files_used.append(v[SOURCE_DOCUMENT])
|
| 29 |
|
| 30 |
srcs = '\n- '.join(files_used)
|
| 31 |
-
answer = f"""{
|
| 32 |
|
| 33 |
-
SOURCES:
|
| 34 |
- {srcs}"""
|
| 35 |
|
| 36 |
return answer
|
|
@@ -41,6 +174,7 @@ if __name__ == "__main__":
|
|
| 41 |
|
| 42 |
queries = [
|
| 43 |
"What are the main types of neurons and muscles in the C. elegans pharynx?",
|
|
|
|
| 44 |
]
|
| 45 |
|
| 46 |
for query in queries:
|
|
|
|
| 3 |
from llama_index.core.storage.index_store import SimpleIndexStore
|
| 4 |
from llama_index.core.storage.docstore import SimpleDocumentStore
|
| 5 |
from llama_index.core import load_index_from_storage
|
| 6 |
+
from llama_index.core import PromptTemplate
|
| 7 |
+
from llama_index.core import VectorStoreIndex, get_response_synthesizer
|
| 8 |
+
from llama_index.core.retrievers import VectorIndexRetriever
|
| 9 |
+
from llama_index.core.query_engine import RetrieverQueryEngine
|
| 10 |
|
| 11 |
STORE_DIR = "openworm.ai_store"
|
| 12 |
SOURCE_DOCUMENT = "source document"
|
| 13 |
|
| 14 |
+
LLM_GPT4o = "GPT4o"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def print_(text):
|
| 17 |
+
print(text)
|
| 18 |
|
| 19 |
|
| 20 |
+
def load_index(model):
|
| 21 |
+
OLLAMA_MODEL = model.replace("Ollama:", "") if model is not LLM_GPT4o else None
|
| 22 |
+
|
| 23 |
+
print_("Creating a storage context for %s" % model)
|
| 24 |
+
|
| 25 |
+
STORE_SUBFOLDER = (
|
| 26 |
+
"" if OLLAMA_MODEL is None else "/%s" % OLLAMA_MODEL.replace(":", "_")
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# index_reloaded =SimpleIndexStore.from_persist_dir(persist_dir=INDEX_STORE_DIR)
|
| 30 |
+
storage_context = StorageContext.from_defaults(
|
| 31 |
+
docstore=SimpleDocumentStore.from_persist_dir(
|
| 32 |
+
persist_dir=STORE_DIR + STORE_SUBFOLDER
|
| 33 |
+
),
|
| 34 |
+
vector_store=SimpleVectorStore.from_persist_dir(
|
| 35 |
+
persist_dir=STORE_DIR + STORE_SUBFOLDER
|
| 36 |
+
),
|
| 37 |
+
index_store=SimpleIndexStore.from_persist_dir(
|
| 38 |
+
persist_dir=STORE_DIR + STORE_SUBFOLDER
|
| 39 |
+
),
|
| 40 |
+
)
|
| 41 |
+
print_("Reloading index for %s" % model)
|
| 42 |
+
|
| 43 |
+
index_reloaded = load_index_from_storage(storage_context)
|
| 44 |
+
|
| 45 |
+
return index_reloaded
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def get_query_engine(index_reloaded, model, similarity_top_k=4):
|
| 49 |
+
OLLAMA_MODEL = model.replace("Ollama:", "") if model is not LLM_GPT4o else None
|
| 50 |
+
|
| 51 |
+
print_("Creating query engine for %s" % model)
|
| 52 |
+
|
| 53 |
+
# Based on: https://docs.llamaindex.ai/en/stable/examples/customization/prompts/completion_prompts/
|
| 54 |
+
|
| 55 |
+
text_qa_template_str = (
|
| 56 |
+
"Context information is"
|
| 57 |
+
" below.\n---------------------\n{context_str}\n---------------------\nUsing"
|
| 58 |
+
" both the context information and also using your own knowledge, answer"
|
| 59 |
+
" the question: {query_str}\nIf the context isn't helpful, you can also"
|
| 60 |
+
" answer the question on your own.\n"
|
| 61 |
+
)
|
| 62 |
+
text_qa_template = PromptTemplate(text_qa_template_str)
|
| 63 |
+
|
| 64 |
+
refine_template_str = (
|
| 65 |
+
"The original question is as follows: {query_str}\nWe have provided an"
|
| 66 |
+
" existing answer: {existing_answer}\nWe have the opportunity to refine"
|
| 67 |
+
" the existing answer (only if needed) with some more context"
|
| 68 |
+
" below.\n------------\n{context_msg}\n------------\nUsing both the new"
|
| 69 |
+
" context and your own knowledge, update or repeat the existing answer.\n"
|
| 70 |
+
)
|
| 71 |
+
refine_template = PromptTemplate(refine_template_str)
|
| 72 |
+
|
| 73 |
+
# create a query engine for the index
|
| 74 |
+
if OLLAMA_MODEL is not None:
|
| 75 |
+
llm = Ollama(model=OLLAMA_MODEL)
|
| 76 |
+
|
| 77 |
+
ollama_embedding = OllamaEmbedding(
|
| 78 |
+
model_name=OLLAMA_MODEL,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
query_engine = index_reloaded.as_query_engine(
|
| 82 |
+
llm=llm,
|
| 83 |
+
text_qa_template=text_qa_template,
|
| 84 |
+
refine_template=refine_template,
|
| 85 |
+
embed_model=ollama_embedding,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
query_engine.retriever.similarity_top_k = similarity_top_k
|
| 89 |
+
|
| 90 |
+
else: # use OpenAI...
|
| 91 |
+
# configure retriever
|
| 92 |
+
retriever = VectorIndexRetriever(
|
| 93 |
+
index=index_reloaded,
|
| 94 |
+
similarity_top_k=similarity_top_k,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# configure response synthesizer
|
| 98 |
+
response_synthesizer = get_response_synthesizer(
|
| 99 |
+
response_mode="refine",
|
| 100 |
+
text_qa_template=text_qa_template,
|
| 101 |
+
refine_template=refine_template,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
query_engine = RetrieverQueryEngine(
|
| 105 |
+
retriever=retriever,
|
| 106 |
+
response_synthesizer=response_synthesizer,
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
return query_engine
|
| 110 |
+
|
| 111 |
+
llm_ver = LLM_GPT4o
|
| 112 |
+
index_reloaded = load_index(llm_ver)
|
| 113 |
+
query_engine = get_query_engine(index_reloaded, llm_ver)
|
| 114 |
+
|
| 115 |
+
def process_query(query, model=llm_ver):
|
| 116 |
+
|
| 117 |
response = query_engine.query(query)
|
| 118 |
+
|
| 119 |
+
response_text = str(response)
|
| 120 |
+
|
| 121 |
+
if "<think>" in response_text: # Give deepseek a fighting chance...
|
| 122 |
+
response_text = (
|
| 123 |
+
response_text[0 : response_text.index("<think>")]
|
| 124 |
+
+ response_text[response_text.index("</think>") + 8 :]
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
metadata = response.metadata
|
| 128 |
+
cutoff = 0.2
|
| 129 |
+
files_used = []
|
| 130 |
+
for sn in response.source_nodes:
|
| 131 |
+
# print(sn)
|
| 132 |
+
sd = sn.metadata["source document"]
|
| 133 |
+
|
| 134 |
+
if sd not in files_used:
|
| 135 |
+
if len(files_used) == 0 or sn.score >= cutoff:
|
| 136 |
+
files_used.append(f"{sd} (score: {sn.score})")
|
| 137 |
+
|
| 138 |
+
file_info = ",\n ".join(files_used)
|
| 139 |
+
print_(f"""
|
| 140 |
+
===============================================================================
|
| 141 |
+
QUERY: {query}
|
| 142 |
+
MODEL: {model}
|
| 143 |
+
-------------------------------------------------------------------------------
|
| 144 |
+
RESPONSE: {response_text}
|
| 145 |
+
SOURCES:
|
| 146 |
+
{file_info}
|
| 147 |
+
===============================================================================
|
| 148 |
+
""")
|
| 149 |
+
|
| 150 |
+
return response_text, metadata
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def run_query(query):
|
| 154 |
+
response_text, metadata = process_query(query, "GPT4o")
|
| 155 |
files_used = []
|
| 156 |
+
|
| 157 |
+
for k in metadata:
|
| 158 |
+
v = metadata[k]
|
| 159 |
if SOURCE_DOCUMENT in v:
|
| 160 |
if v[SOURCE_DOCUMENT] not in files_used:
|
| 161 |
files_used.append(v[SOURCE_DOCUMENT])
|
| 162 |
|
| 163 |
srcs = '\n- '.join(files_used)
|
| 164 |
+
answer = f"""{response_text}
|
| 165 |
|
| 166 |
+
SOURCES OF ANSWER:
|
| 167 |
- {srcs}"""
|
| 168 |
|
| 169 |
return answer
|
|
|
|
| 174 |
|
| 175 |
queries = [
|
| 176 |
"What are the main types of neurons and muscles in the C. elegans pharynx?",
|
| 177 |
+
"Tell me about the egg laying apparatus",
|
| 178 |
]
|
| 179 |
|
| 180 |
for query in queries:
|