-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (130 loc) · 4.55 KB
/
main.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import io
import contextlib
from llama_index.llms import ChatMessage, MessageRole
from llama_index import GPTVectorStoreIndex
from llama_index.indices.postprocessor import KeywordNodePostprocessor
from llama_index.query_engine import SubQuestionQueryEngine
from llama_index.tools import QueryEngineTool, ToolMetadata
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from llama_index import ServiceContext
from tools import search_the_web
from utils import convert_documents_into_nodes
from embedding import EmbedNodes
from keyword_extraction import extract_keywords_vanilla
from reranker import cohere_rerank
from llms.zephyr import llm
from config import config
import nest_asyncio
nest_asyncio.apply()
embed_model = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-mpnet-base-v2",
model_kwargs={"device": "cuda"},
encode_kwargs={"device": "cuda", "batch_size": 100})
service_context = ServiceContext.from_defaults(
llm=llm, embed_model=embed_model)
def sub_query(query, index, service_context):
vector_tool = QueryEngineTool(
index.as_query_engine(),
metadata=ToolMetadata(
name="vector_search",
description="Useful for searching for specific and correct facts."
)
)
query_engine = SubQuestionQueryEngine.from_defaults([vector_tool],
service_context=service_context,
verbose=True)
res = query_engine.query(query)
return res
def only_llm(query):
message = [
ChatMessage(
role=MessageRole.SYSTEM,
content=(
"Always answer the question correctly"
),
),
ChatMessage(
role=MessageRole.USER,
content=(
f"answer the question: {query}\n"
),
)]
return llm.chat(message).message.content
def gen_code(query):
message = [
ChatMessage(
role=MessageRole.SYSTEM,
content=(
"You are a Python programmar, you write functionally correct and good python codes"
),
),
ChatMessage(
role=MessageRole.USER,
content=(
f"Write python code for: {query}\n"
),
)]
res = llm.chat(message).message.content
output = io.StringIO()
code_to_execute = res.split("```")[1][6:].strip()
with contextlib.redirect_stdout(output):
exec(code_to_execute)
captured_output = output.getvalue()
output.close()
return captured_output, code_to_execute
def get_answer(query, extra_args):
include_images, improve_response = extra_args
loaded_docs = search_the_web(query, include_images)
nodes = convert_documents_into_nodes(loaded_docs)
embedded_nodes = EmbedNodes()(nodes)
index = GPTVectorStoreIndex(
nodes=embedded_nodes, service_context=service_context)
req_keywords = extract_keywords_vanilla(query)
keyword_processor = KeywordNodePostprocessor(
required_keywords=req_keywords)
if improve_response:
res = sub_query(query, index, service_context)
else:
index_query_engine = index.as_query_engine(
similarity_top_k=config.retriver_top_k, node_postprocessors=[cohere_rerank, keyword_processor])
res = index_query_engine.query(query)
refs = []
images = set()
for node in res.source_nodes:
if len(node.metadata) > 0:
refs.append(node.metadata.get('URL'))
if node.metadata.get("images") != None:
for img in node.metadata.get("images"):
images.add(img)
images = list(images)
output = {
"content": res.response,
"images": images,
"references": refs
}
return output
def process(query, extra_args):
is_web, is_code, is_img_query, is_sub_query = extra_args
if is_code:
ans = gen_code(query)
op, code = ans
content = f"Possible output: {op}<br>Code:<br><code>{code}</code>"
output = {
"content": content,
"images": [],
"references": []
}
return output
if not is_web:
ans = only_llm(query)
output = {
"content": ans,
"images": [],
"references": []
}
return output
return get_answer(query, (is_img_query, is_sub_query))
if __name__ == "__main__":
query = input("Enter the query please")
res = process(query, (True, False, False, False))
print(res)