-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
162 lines (121 loc) · 4.33 KB
/
ai.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
from langchain.vectorstores import Pinecone
from sqlalchemy import inspect
import pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
import re
import ast
import json
from reddit import Post, Comment, initialize_db
from config import config
from llm_vm.client import client_build
# extract json from GPT-4 output, or return original text
def extract_json(text):
lines = text.split("\n")
json_content = ""
collect = False
for line in lines:
if "```json" in line:
collect = True
continue
elif "```" in line and collect:
collect = False
if collect:
json_content += line
try:
obj = ast.literal_eval(json_content)
return obj
except (ValueError, SyntaxError):
return json_content
def object_as_dict(obj):
return {column.key: getattr(obj, column.key) for column in inspect(obj).mapper.column_attrs}
def create_prompt(all_post, comment_content):
succinct_content = "COMMENT: {} POST: {}".format(
comment_content,
all_post
)
return succinct_content
def clean_str(content):
s = content.replace("\n", "")
s = re.sub(' +', ' ', s)
s = s.strip()
return s
def augment_prompt(vectorstore, query: str, ignore_subreddits=[], time_cutoff=0):
session = initialize_db()
if not (isinstance(time_cutoff, int)):
time_cutoff = 0
print("Using K={} for similariy search".format(config["rag"]["k"]))
results = vectorstore.similarity_search(query, k=config["rag"]["k"], filter={
"subreddit": {
"$nin": ignore_subreddits
},
"time": {
"$gt": time_cutoff
}
})
# get the text from the results
source_knowledge = ""
i = 1
for x in results:
comment_id = x.page_content
unvectorized_comments = session.query(
Comment).filter_by(id=comment_id).all()
comment_dict = [object_as_dict(comment)
for comment in unvectorized_comments][0]
post = session.query(Post).filter_by(
id=comment_dict["post_id"]).first()
comment_dict["postData"] = object_as_dict(post)
post_title = clean_str(comment_dict["postData"]["title"])
post_content = clean_str(comment_dict["postData"]["selftext"])
comment_content = clean_str(comment_dict["comment"])
all_post = "{}. {}".format(post_title, post_content)
content = create_prompt(all_post, comment_content)
url = clean_str(comment_dict["url"])
if i != len(results) - 1:
source_knowledge += "{}) {} URL: {}\n\n".format(i, content, url)
else:
source_knowledge += "{}) {} URL: {}".format(i, content, url)
i += 1
# feed into an augmented prompt
augmented_prompt = f"""
Given the Reddit Posts, Comment, & URLs below:
```
{source_knowledge}
```
And the description of the product/project:
```
{query}
```
Identify the comments where the user demonstrates a strong interest or need that aligns closely with the described product/project. The match should be precise; avoid broad interpretations.
Output the results in the following JSON format: an array of objects, where each object contains the comment, post, and url.
```json
[
{{
"comment": "Sample Comment",
"post": "Sample Post Title or Content",
"url": "Sample URL"
}},
...
]
```
"""
return augmented_prompt
def get_good_comments(product_description, ignore_subreddits, time_cutoff_seconds):
embed_model = OpenAIEmbeddings(model=config["embedding"]["name"])
pinecone.init(
api_key=config["pinecone_db"]["api_key"],
environment=config["pinecone_db"]["environment"]
)
client = client_build(
type="inference", openai_key=config["rag"]["openai_api_key"], big_model=config["rag"]["main_model"])
index = pinecone.Index(config["pinecone_db"]["index"])
# initialize the vector store object
vectorstore = Pinecone(
index, embed_model.embed_query, "comment"
)
prompt = augment_prompt(vectorstore, product_description,
ignore_subreddits, time_cutoff_seconds)
print("\n")
print("===> Prompt generated. Now requesting from LLM (this might take a few minutes)...")
print("\n")
res = client.complete(prompt=prompt, max_len=1000)
return extract_json(res)