-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrag-json.py
78 lines (65 loc) · 2.64 KB
/
rag-json.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
import requests
import json
input_file = open('/Users/ap2x07/Desktop/post-content.json')
corpus_of_documents = json.load(input_file)
# print(corpus_of_documents)
# corpus_of_documents = []
# corpus_of_documents = [
# "Take a leisurely walk in the park and enjoy the fresh air.",
# "Visit a local museum and discover something new.",
# "Attend a live music concert and feel the rhythm.",
# "Go for a hike and admire the natural scenery.",
# "Have a picnic with friends and share some laughs.",
# "Explore a new cuisine by dining at an ethnic restaurant.",
# "Take a yoga class and stretch your body and mind.",
# "Join a local sports league and enjoy some friendly competition.",
# "Attend a workshop or lecture on a topic you're interested in.",
# "Visit an amusement park and ride the roller coasters."
# ]
def jaccard_similarity(query, document):
query = query.lower().split(" ")
document = document.lower().split(" ")
intersection = set(query).intersection(set(document))
union = set(query).union(set(document))
return len(intersection)/len(union)
def return_response(query, corpus):
similarities = []
for doc in corpus:
similarity = jaccard_similarity(user_input, doc)
similarities.append(similarity)
return corpus_of_documents[similarities.index(max(similarities))]
# Get user input for both user preference and prompt text
user_input = input("Enter your preference / question: ")
prompt_text = input("Enter the prompt text to use: ")
# user_input = "I like to hike"
relevant_document = return_response(user_input, corpus_of_documents)
# https://github.com/jmorganca/ollama/blob/main/docs/api.md
# Construct the prompt
prompt = """
{}
This is the recommended activity: {}
The user input is: {}
Compile a recommendation to the user based on the recommended activity and the user input.
""".format(prompt_text, relevant_document, user_input)
url = 'http://localhost:11434/api/generate'
data = {
"model": "llama2",
"prompt": prompt.format(user_input=user_input, relevant_document=relevant_document)
}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, data=json.dumps(data),
headers=headers, stream=True)
full_response = []
try:
count = 0
for line in response.iter_lines():
# filter out keep-alive new lines
# count += 1
# if count % 5== 0:
# print(decoded_line['response']) # print every fifth token
if line:
decoded_line = json.loads(line.decode('utf-8'))
full_response.append(decoded_line['response'])
finally:
response.close()
print(''.join(full_response))