-
Notifications
You must be signed in to change notification settings - Fork 0
/
subquery.py
234 lines (218 loc) · 7.85 KB
/
subquery.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from guidance.models import OpenAIChat
from llama_index.core import QueryBundle, Settings
from llama_index.core.base.response.schema import RESPONSE_TYPE
from llama_index.core.schema import NodeWithScore
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.llms.openai import OpenAI
from llama_index.question_gen.guidance import GuidanceQuestionGenerator
from tc_hivemind_backend.db.utils.preprocess_text import BasePreprocessor
from tc_hivemind_backend.embeddings.cohere import CohereEmbedding
from utils.globals import INVALID_QUERY_RESPONSE, NO_ANSWER_REFERENCE
from utils.qdrant_utils import QDrantUtils
from utils.query_engine import (
DEFAULT_GUIDANCE_SUB_QUESTION_PROMPT_TMPL,
CustomSubQuestionQueryEngine,
GDriveQueryEngine,
GitHubQueryEngine,
MediaWikiQueryEngine,
NotionQueryEngine,
TelegramDualQueryEngine,
TelegramQueryEngine,
WebsiteQueryEngine,
prepare_discord_engine_auto_filter,
)
def query_multiple_source(
query: str,
community_id: str,
discord: bool = False,
discourse: bool = False,
google: bool = False,
notion: bool = False,
telegram: bool = False,
github: bool = False,
mediaWiki: bool = False,
website: bool = False,
) -> tuple[str, list[NodeWithScore]]:
"""
query multiple platforms and get an answer from the multiple
Parameters
------------
query : str
the user question
community_id : str
the community id to get their data
discord : bool
if `True` then add the engine to the subquery_generator
default is set to False
discourse : bool
if `True` then add the engine to the subquery_generator
default is set to False
google : bool
if `True` then add the engine to the subquery_generator
default is set to False
notion : bool
if `True` then add the engine to the subquery_generator
default is set to False
telegram : bool
if `True` then add the engine to the subquery_generator
default is set to False
github : bool
if `True` then add the engine to the subquery_generator
default is set to False
Returns
--------
response : str,
the response to the user query from the LLM
using the engines of the given platforms (platform equal to True)
source_nodes : list[NodeWithScore]
the list of nodes that were source of answering
"""
query_engine_tools: list[QueryEngineTool] = []
tools: list[ToolMetadata] = []
qdrant_utils = QDrantUtils(community_id)
# wrapper for more clarity
check_collection = qdrant_utils.check_collection_exist
# query engine perparation
# tools_metadata and query_engine_tools
if discord:
discord_query_engine = prepare_discord_engine_auto_filter(
community_id,
query,
)
tool_metadata = ToolMetadata(
name="Discord",
description="Contains messages and summaries of conversations from the Discord platform of the community",
)
tools.append(tool_metadata)
query_engine_tools.append(
QueryEngineTool(
query_engine=discord_query_engine,
metadata=tool_metadata,
)
)
if discourse:
raise NotImplementedError
if google and check_collection("google"):
google_query_engine = GDriveQueryEngine(community_id=community_id).prepare()
tool_metadata = ToolMetadata(
name="Google-Drive",
description=(
"Stores and manages documents, spreadsheets, presentations,"
" and other files for the community."
),
)
query_engine_tools.append(
QueryEngineTool(
query_engine=google_query_engine,
metadata=tool_metadata,
)
)
if notion and check_collection("notion"):
notion_query_engine = NotionQueryEngine(community_id=community_id).prepare()
tool_metadata = ToolMetadata(
name="Notion",
description=(
"Centralizes notes, wikis, project plans, and to-dos for the community."
),
)
query_engine_tools.append(
QueryEngineTool(
query_engine=notion_query_engine,
metadata=tool_metadata,
)
)
if telegram and check_collection("telegram"):
# checking if the summaries was available
if check_collection("telegram_summary"):
telegram_query_engine = TelegramDualQueryEngine(
community_id=community_id
).prepare()
else:
telegram_query_engine = TelegramQueryEngine(
community_id=community_id
).prepare()
tool_metadata = ToolMetadata(
name="Telegram",
description=(
"Contains messages, conversations, and media from the Telegram platform,"
" used for group discussions within the community."
),
)
query_engine_tools.append(
QueryEngineTool(
query_engine=telegram_query_engine,
metadata=tool_metadata,
)
)
if github and check_collection("github"):
github_query_engine = GitHubQueryEngine(community_id=community_id).prepare()
tool_metadata = ToolMetadata(
name="GitHub",
description=(
"Hosts commits and conversations from Github issues and"
" pull requests from the selected repositories"
),
)
query_engine_tools.append(
QueryEngineTool(
query_engine=github_query_engine,
metadata=tool_metadata,
)
)
if mediaWiki and check_collection("mediawiki"):
mediawiki_query_engine = MediaWikiQueryEngine(
community_id=community_id
).prepare()
tool_metadata = ToolMetadata(
name="WikiPedia",
description="Hosts articles about any information on internet",
)
query_engine_tools.append(
QueryEngineTool(
query_engine=mediawiki_query_engine,
metadata=tool_metadata,
)
)
if website and check_collection("website"):
website_query_engine = WebsiteQueryEngine(community_id=community_id).prepare()
tool_metadata = ToolMetadata(
name="Website",
description=(
"Hosts a diverse collection of crawled data from various "
"online sources to facilitate community insights and analysis."
),
)
query_engine_tools.append(
QueryEngineTool(
query_engine=website_query_engine,
metadata=tool_metadata,
)
)
if not BasePreprocessor().extract_main_content(text=query):
response = INVALID_QUERY_RESPONSE
source_nodes = []
return response, source_nodes
embed_model = CohereEmbedding()
llm = OpenAI("gpt-4o-mini")
Settings.embed_model = embed_model
Settings.llm = llm
question_gen = GuidanceQuestionGenerator.from_defaults(
guidance_llm=OpenAIChat("gpt-4"),
verbose=False,
prompt_template_str=DEFAULT_GUIDANCE_SUB_QUESTION_PROMPT_TMPL,
)
s_engine = CustomSubQuestionQueryEngine.from_defaults(
question_gen=question_gen,
query_engine_tools=query_engine_tools,
use_async=False,
verbose=False,
)
query_embedding = embed_model.get_text_embedding(text=query)
result: tuple[RESPONSE_TYPE, list[NodeWithScore]] = s_engine.query(
QueryBundle(query_str=query, embedding=query_embedding)
)
response, source_nodes = result
if source_nodes == []:
return NO_ANSWER_REFERENCE, source_nodes
else:
return response.response, source_nodes