-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.py
666 lines (548 loc) · 18.7 KB
/
search.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import argparse
import configparser
import json
import logging
import random
import re
import requests
import sys
import threading
import time
from duckduckgo_search import DDGS
from flask import Flask, render_template, request, jsonify
from urllib.parse import urlparse
#logging.basicConfig(level=logging.ERROR) # uncomment for debugging
# dBP dBBBBBb dBP.dBBBBP dBBBP dBBBBBb
# BB BP dBP
# dBP dBP BB dBP `BBBBb dBBP dBBBBK
# dBP dBP BB dBP dBP dBP dBP BB
# dBBBBP dBBBBBBB dBP dBBBBP' dBBBBP dBP dB'
#
# Local AI Search by av1d
APPNAME = 'LAISer'
VERSION = '0.1'
def search(search_query: str, num_results_to_return: int) -> list:
# perform a search on duckduckgo.
results = DDGS().text(keywords=search_query)
text_container = []
for result in results[:num_results_to_return]:
text = {
'title': result['title'],
'href': result['href'],
'body': result['body'] # summary / meta
}
text_container.append(text)
source_links.append(result['href'])
return text_container # list of dictionaries
def news(search_query: str, num_results_to_return: int) -> list:
# fetch the news from duckduckgo.
results = DDGS().news(keywords=search_query)
news_container = []
for result in results[:num_results_to_return]:
news = {
'title': result['title'], # article title
'url': result['url'],
'body': result['body'], # summary
'source': result['source'] # news outlet
}
news_container.append(news)
source_links.append(result['url'])
return news_container # list of dictionaries
def _wikipedia_summary(page_title: str) -> list:
# Get Wikipedia page summary based on page title
url = f"https://en.wikipedia.org/w/api.php?"\
f"format=json&"\
f"action=query&"\
f"prop=extracts&"\
f"exintro=&"\
f"explaintext=&"\
f"redirects=1&"\
f"titles={page_title}"
response = requests.get(url)
data = response.json()
# full summary text:
text = next(iter(data['query']['pages'].values()))['extract']
if TRIM_WIKIPEDIA_SUMMARY == True:
sentences = text.split('.') # assumes there are no ?/!
trimmed_text = '. '.join(sentences[:TRIM_WIKIPEDIA_LINES]) + '.'
return [{'summary':trimmed_text}]
else:
return [{'summary':text}]
def wikipedia(search_arg) -> str:
# returns plain text string of page summary
def trim_url(url):
parsed_url = urlparse(url)
path = parsed_url.path
last_part = path.rsplit('/', 1)[-1]
return last_part
# get the most relevant Wikipedia result from DuckDuckGo
search_result = search(f"site:wikipedia.org {search_arg}", 1)
# add the result to the overall source_links list
source_links.append(search_result[0]['href'])
# Get the Wikipedia page title
wiki_page_title = trim_url(search_result[0]['href'])
# Get the page summary based on that title
summary = _wikipedia_summary(wiki_page_title)
return summary
def perform_searches(search_query: str) -> list:
print("Getting Wikipedia summary...") if not SILENT else None
wikipedia_summary = wikipedia(search_query)
wikipedia_summary = format_llama_request(
wikipedia_summary,
"wikipedia"
)
print("Getting search results...") if not SILENT else None
search_result = search(search_query, SEARCH_RESULT_COUNT)
search_result = format_llama_request(
search_result,
"search"
)
print("Getting news results...") if not SILENT else None
news_result = news(search_query, NEWS_RESULT_COUNT)
news_result = format_llama_request(
news_result,
"news"
)
search_results = {
'wikipedia_summary': wikipedia_summary,
'search_result': search_result,
'news_result': news_result
}
return search_results
def _is_llama_online() -> bool:
# check if the API we're using is online and reachable
if API_TO_USE == 'llama.cpp':
url = f"http://"\
f"{LLAMA_IP}"\
f":"\
f"{LLAMA_PORT}"\
f"/health"
headers = {"Content-Type": "application/json"}
try:
response = requests.get(url, headers=headers)
except Exception as e:
print(f"There was an error connecting to {API_TO_USE}")
return False
if response.status_code == 200:
response_json = response.json()
if (
"status" in response_json and
response_json["status"] == "ok"
):
print(
f"{API_TO_USE} is online."
) if not SILENT else None
return True
else:
print(f"{API_TO_USE} is offline or returning a bad status!")
return False
if API_TO_USE == 'ollama':
try:
response = requests.get(OLLAMA_BASE_URL)
except Exception as e:
print(f"There was an error connecting to {API_TO_USE}")
return False
if response.status_code == 200:
if response.text == 'Ollama is running':
print(
f"{API_TO_USE} is online."
) if not SILENT else None
return True
else:
print(f"{API_TO_USE} is offline or returning a bad status!")
return False
def feed_the_llama(query: str) -> str:
if API_TO_USE == 'llama.cpp':
payload = {
"prompt": query,
"n_predict": 128
}
LLAMA_SERVER_URL = f"http://"\
f"{LLAMA_IP}"\
f":"\
f"{LLAMA_PORT}"\
f"/completion"
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(
LLAMA_SERVER_URL,
json=payload,
headers=headers
)
except Exception as e:
error_msg = f"Error: {e}.\n\n"\
f"Is the llama.cpp server running?"
print(error_msg)
erroneous = {
"success": False,
"content": error_msg
}
return erroneous
if response.status_code == 200:
answer = response.json()
successful = {
"success": True,
"content": answer['content']
}
return successful
else:
error_msg = f"Error: {response.status_code}"
print(error_msg)
erroneous = {
"success": False,
"content": error_msg
}
return erroneous
if API_TO_USE == 'ollama':
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
data = json.dumps({
"model": OLLAMA_MODEL,
"prompt": query,
"stream": False
})
try:
response = requests.post(
OLLAMA_URL,
headers=headers,
data=data
)
except Exception as e:
error_msg = f"Error: {e}.\n\nIs the ollama server running?"
print(error_msg)
erroneous = {
"success": False,
"content": error_msg
}
return erroneous
if response.status_code == 200:
answer = response.json()
successful = {
"success": True,
"content": answer['response']
}
return successful
else:
error_msg = f"Error: {response.status_code}"
print(error_msg)
erroneous = {
"success": False,
"content": error_msg
}
return erroneous
def format_llama_request(data: list, data_source: str) -> str:
# Format all the results into neat markdown code blocks.
if data_source == "search":
search_result = "Web search results:\n```\n"
for entry in data:
title = entry['title']
url = entry['href']
meta = entry['body']
search_result = f"{search_result}Page title: {title}\n"
search_result = f"{search_result}URL: {url}\n"
search_result = f"{search_result}Page meta: {meta}\n"
search_result = f"{search_result}\n"
search_result = f"{search_result}```\n"
return search_result
elif data_source == "news":
news_result = "News search results:\n```\n"
for entry in data:
title = entry['title']
url = entry['url']
meta = entry['body']
source = entry['source']
news_result = f"{news_result}Page title: {title}\n"
news_result = f"{news_result}URL: {url}\n"
news_result = f"{news_result}Page meta: {meta}\n"
news_result = f"{news_result}News source: {source}\n"
news_result = f"{news_result}\n"
news_result = f"{news_result}```\n"
return news_result
elif data_source == "wikipedia":
wikipedia_summary = "Wikipedia:\n```\n"
summary_data = data[0]['summary']
wikipedia_summary = f"{wikipedia_summary}{summary_data}\n"
wikipedia_summary = f"{wikipedia_summary}```\n"
return wikipedia_summary
elif data_source == "reddit":
for x in data:
dictionary = x
if 'reply' in dictionary:
dict_reply = dictionary['reply']
print(f"Reply:\n{dict_reply}\n")
if 'op' in dictionary:
dict_op = dictionary['op']
print(f"Original post:\n{dict_op}\n")
else:
print("Error: invalid data source")
def format_sources(collected_source_links: list) -> str:
# format the 'source' links (search result links) depending
# on type of search (cli or web-based)
# set global so we can erase the links after processing
global source_links
# remove any duplicates
collected_source_links = list(dict.fromkeys(collected_source_links))
if SEARCH_TYPE == "web":
sources = (
"<ul id='sources' class='sources'>\n"
) # button to expand/collapse source links
for link in collected_source_links: # create source links
constructed_link = (
f"<li class='source-item'>"
f"<a href='{link}' target='_blank' class='source-link'>"
f"{link}"
f"</a>"
f"</li>"
f"\n"
)
sources = f"{sources} {constructed_link}"
sources = f"{sources} </ul>"
source_links = [] # initialize the links so they don't cache
return sources
if SEARCH_TYPE == "cli":
sources = ""
for link in collected_source_links:
link = f"{link} \n"
sources = f"{sources} {link}"
source_links= [] # initialize the links so they don't cache
return sources
def remove_incomplete_sentence(input_text: str) -> str:
# remove last sentence if incomplete
sentences = re.split(r'(?<=[.!?])\s+', input_text.strip())
if len(sentences) > 0 and not re.search(r'[.!?]$', sentences[-1]):
del sentences[-1]
result = ' '.join(sentences)
return result
def process_search_query(search_query: str) -> str:
# do all the searchy things
search_answers = perform_searches(search_query)
# Extract search results
wikipedia_summary = search_answers.get('wikipedia_summary', "")
search_result = search_answers.get('search_result', "")
news_result = search_answers.get('news_result', "")
search_data = f"{wikipedia_summary}\n"\
f"{search_result}\n"\
f"{news_result}\n"
return search_data
def generate_llamatize_text(search_query: str, search_data: str) -> str:
# the prompt we give to the model with all of our search results.
llamatize = (
f"I performed a web search for `{search_query}`.\n"
f"Formulate a response based upon my search results:\n\n"
f"{search_data}\n"
f"In addition, separately answer my question of "
f"`{search_query}` "
f"directly without considering the information I provided "
f"previously. Finally, "
f"provide a summary which considers both of your answers.\n"
)
return llamatize
def process_and_display_results(search_query: str) -> str:
# if the api/server is online
if _is_llama_online():
search_data = process_search_query(search_query)
llamatize = generate_llamatize_text(search_query, search_data)
print("Feeding the llama... ^°π°^") if not SILENT else None
answer = feed_the_llama(llamatize)
if answer["success"] == False:
# if there was an error, return it
return answer["content"]
else:
# if it was successful, process incomplete sentences
answer = remove_incomplete_sentence(answer["content"])
return answer
else:
error_msg = (
f"{API_TO_USE} server is offline or status is not 'ok'.\n"
f"Please check your {API_TO_USE} settings.\n"
)
print(error_msg)
return error_msg
def web_input(search_query: str) -> str:
# put results into 'answer-response' div with the source links
answer = process_and_display_results(search_query)
sources = format_sources(source_links)
answer_content = (
"<div id='answer-response'>"
f"{answer}"
"</div>\n"
f"{sources}"
)
print("Returning result...")
return answer_content
def web_server() -> None:
print(
f"Starting server at: "
f"http://{BINDING_ADDRESS}:{BINDING_PORT}"
)
# setup Flask
app = Flask(__name__)
app.name = f"{APPNAME} v{VERSION}"
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
@app.route('/search', methods=['POST'])
def web_search():
start_time = time.time() # log the start time for stats
question = request.form['input_text'] # input from web
print(f"━━━━━━━━┫ Received web request: {question}")
# if lock is acquired then this app is currently in use.
# we can currently only handle one request at a time.
if lock.locked():
error_msg = "Sorry, I can only handle one request "\
"at a time and I'm currently busy."
return jsonify({
'result': error_msg
})
# if this app is free to process a request
with lock:
answer = web_input(question)
end_time = time.time()
print(f"Completed in {end_time - start_time:.2f} seconds.")
# finally, return the answer to the web client
return jsonify({'result': answer})
# start Flask
app.run(
host=BINDING_ADDRESS,
port=BINDING_PORT
)
def cli(search_query: str) -> None:
answer = process_and_display_results(search_query)
print("━━━━━━━━┫ ANSWER") if not SILENT else None
print(answer)
sources = format_sources(source_links)
print("\nSOURCES:")
print(sources)
def load_config() -> None:
# load user settings
parser = configparser.ConfigParser()
parser.read('settings.ini')
global BINDING_ADDRESS
global BINDING_PORT
global LLAMA_IP
global LLAMA_PORT
global OLLAMA_BASE_URL
global OLLAMA_URL
global OLLAMA_MODEL
global API_TO_USE
global SILENT
global SEARCH_RESULT_COUNT
global NEWS_RESULT_COUNT
global TRIM_WIKIPEDIA_SUMMARY
global TRIM_WIKIPEDIA_LINES
BINDING_ADDRESS = parser.get(
'laiser',
'BINDING_ADDRESS'
)
BINDING_PORT = parser.get(
'laiser',
'BINDING_PORT'
)
LLAMA_IP = parser.get(
'llamaCPP',
'LLAMA_IP'
)
LLAMA_PORT = parser.get(
'llamaCPP',
'LLAMA_PORT'
)
OLLAMA_BASE_URL = parser.get(
'ollama',
'OLLAMA_BASE_URL'
)
OLLAMA_URL = parser.get(
'ollama',
'OLLAMA_URL'
)
OLLAMA_MODEL = parser.get(
'ollama',
'OLLAMA_MODEL'
)
API_TO_USE = parser.get(
'default_API',
'API_TO_USE'
)
SILENT = parser.getboolean(
'status_messages',
'silent'
)
SEARCH_RESULT_COUNT = parser.getint(
'advanced',
'SEARCH_RESULT_COUNT'
)
NEWS_RESULT_COUNT = parser.getint(
'advanced',
'NEWS_RESULT_COUNT'
)
TRIM_WIKIPEDIA_SUMMARY = parser.getboolean(
'advanced',
'TRIM_WIKIPEDIA_SUMMARY'
)
TRIM_WIKIPEDIA_LINES = parser.getint(
'advanced',
'TRIM_WIKIPEDIA_LINES'
)
def arguments() -> str:
# setup the argument parser
parser = argparse.ArgumentParser(
description=f"{APPNAME} v{VERSION} - Local AI Search"
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'--query',
'-q',
type=str,
help='The query to search for'
)
group.add_argument(
'--server',
'-s',
action='store_true',
help='Connect to the server'
)
args = parser.parse_args()
server = False
if args.query:
query = args.query
return query
elif args.server:
return False
else:
parser.error("Either --query or --server must be specified.")
if __name__ == "__main__":
# get command line arguments
get_arguments = arguments()
# load user settings
load_config()
# control access since currently only one instance can run at a time
lock = threading.Lock()
# set some globals
global SEARCH_TYPE
SEARCH_TYPE = "" # holds search type "cli" or "web"
global source_links
source_links = [] # list for holding cited links
global results
results = "" # holds search results
# announce which API the user is using
print(f"Using {API_TO_USE}") if not SILENT else None
# server mode:
if not get_arguments:
SEARCH_TYPE = "web"
web_server()
# cli mode:
else:
SEARCH_TYPE = "cli"
search_query = get_arguments
if not search_query:
sys.exit("Enter a search query enclosed in quotes.")
else:
start_time = time.time()
cli(search_query)
source_links = [] # initialize links so they don't cache
end_time = time.time()
print(
f"Completed in {end_time - start_time:.2f} seconds."
) if not SILENT else None