-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm.py
executable file
·99 lines (89 loc) · 3.21 KB
/
llm.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
#!/usr/bin/env python
import sys
import os
import re
from pprint import pprint
import json
import requests
from urllib.parse import urlencode, quote
import cohere
def get_bron_documents(query):
params = {
'query': query,
#'filter': 'source:openbesluitvorming,woo,poliflw',
'filter': 'source:openbesluitvorming,woo,poliflw',
'excludes': '',
'limit': 350,
'default_operator': 'and'
}
query_string = urlencode(params)
url = 'https://api.bron.live/documents/search?' + query_string
resp = requests.get(url)
resp.raise_for_status()
results = []
for i in resp.json()['hits']['hits']:
results += [{
'title': i['_source']['title'],
'snippet': d
} for d in i['highlight'].get('description', [])]
# results = [
# {'title': i['_source']['title'], 'snippet': "\n".join(
# i['highlight'].get('description') or
# i['highlight'].get('title', ''))
# } for i in resp.json()['hits']['hits']
# ]
return results
def main(argv):
#qst = "Hoe ziet het bestuurlijke apparaat van NL er uit?"
#qst = "Hoe worden vluchtelingen opgevangen de respectievelijke gemeenten?"
#qst = "Hoe gaan gemeenten om met windmolens en andere vormen van schone energie?"
#qst = "hoe staat het met de parken in almelo?"
qst = 'x'
chat_history = []
chat_history_incl = []
while qst != 'exit':
qst = input('Geef een vraag warop je het antwoord wil weten : ')
if qst.strip() == '':
qst = "Om vluchtelingen beter op te vangen worden er in gemeenten lokale voorzieningen getroffen om vluchtelingen te helpen. Welke voorzieningen voor vluchtelingen zijn er in almelo?"
co = cohere.Client(api_key=os.getenv("COHERE_API_KEY"))
print(f"Vraag: {qst}")
if qst.strip() == 'exit':
continue
resp = co.chat(
model="command-r-plus",
message=qst,
chat_history=chat_history,
search_queries_only=True
)
#pprint(dict(resp))
all_documents = []
print("Genegereerde queries:")
for q in resp.search_queries:
print(q.text)
all_documents += get_bron_documents(q.text)
print("%d documenten meegestuurd" % (len(all_documents,)))
pprint(all_documents)
#all_documents=[]
for event in co.chat_stream(
model="command-r-plus",
message=qst,
chat_history=chat_history,
documents=all_documents,
prompt_truncation='AUTO'
):
if event.event_type == "text-generation":
sys.stdout.write(event.text)
elif event.event_type == "stream-end":
print()
chat_history = [dict(e) for e in event.response.chat_history]
elif event.event_type == 'citation-generation':
sys.stdout.write(str(event) + "\n")
else:
print(event.event_type)
print("Chat history:")
print("----------------------------------")
print(chat_history)
print("----------------------------------")
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))