-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
298 lines (237 loc) · 10.3 KB
/
agent.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
# This script sets up the agent employed by the bot, For demostration, the agent
# in this script acts as a shop assistant at Home Depot.
# Set up the environment
import os
from dotenv import find_dotenv, load_dotenv
import flatdict # flattens nested dict
import re
# packages for web scraping/ searching
import validators
from bs4 import BeautifulSoup
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time # gives extra time for webdriver to load webpage
from serpapi import GoogleSearch
# Language models and embedding models
# from langchain.llms import OpenAI # my OpenAI trial expired :/
# from langchain.embeddings.openai import OpenAIEmbeddings
# from langchain.llms import Cohere
from langchain.llms import AI21
from langchain.embeddings import CohereEmbeddings
# Chains and chain components
from langchain.vectorstores import Chroma
from langchain.chains.question_answering import load_qa_chain
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
from langchain.agents import initialize_agent, Tool, AgentType
from langchain.tools import tool
from langchain.memory import ConversationBufferMemory
# Load api key variables from .env file and set api keys
load_dotenv(find_dotenv('private/.env'))
SERPAPI_API_KEY = os.environ["SERPAPI_API_KEY"]
AI21_API_KEY = os.environ["AI21_API_KEY"]
COHERE_API_KEY = os.environ["COHERE_API_KEY"]
# Set language model
# llm = OpenAI(temperature=0)
llm = AI21(temperature=0.5)
llm_doc = AI21(temperature=0.1) # for queries related to docs
# Rate limit for Cohere model is 5 calls/min
# llm = Cohere(temperature=0.5)
# llm_doc = Cohere(temperature=0) # for queries related to docs
################################################################################
def genius(query,db):
"""
The agent.
"""
# Prompt guidelines
guidelines = """
If the query is not related to services or products offered by Home Depot, say you can't help.
If the query is asking about a specific product available at Home Depot, look for the product.
If the query is not about a specific product, offer the customer some general advice and suggest a few related Home Depot products.
If you don't know the answer, say you don't know and provide contact information for customer service.
"""
# Set prompt template
prompt_template = """
You are a helpful shop assistant at Home Depot. This is a query from a Home Depot customer: {query}
Answer the query following these guidelines: {guidelines}
In your answer:
Explain your answer briefly.
When you mention specific products, use full product names.
If you are asked about one specific product and you found the product, provide a link to that product.
"""
prompt = PromptTemplate(template=prompt_template,
input_variables=["query", "guidelines"])
############################################################################
# Chains and functions
prompt_str = prompt.format(query=query, guidelines=guidelines)
chain = RetrievalQA.from_chain_type(llm=llm_doc, chain_type="stuff",
retriever=db.as_retriever())
# Cheeck url
def check_url(url):
valid=validators.url(url)
if valid==True:
try:
headers = {"User-Agent":
"Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) \
AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/81.0.4044.141 Safari/537.36"}
response = requests.head(url,headers=headers)
if response.status_code == 200:
return True
else:
return False
except requests.ConnectionError as e:
return False
else:
return False
# Create tools from functions
@tool
def is_homedepot(query):
"""
Decide whether the query is asking about a product available at Home Depot
query : customer query
"""
prompt_new = 'Is the following asking about a product available at Home Depot? ' + query
chain_new = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff",
retriever=db.as_retriever())
ans = chain_new.run(prompt_new)
return ans
@tool
def get_keyword(query):
"""
Get the search keyword from the query.
query : customer query
"""
prompt_new = 'What is the product mentioned in the query? ' + query
chain_new = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff",
retriever=db.as_retriever())
ans = chain_new.run(prompt_new)
return ans
@tool
def get_products(keyword):
"""
Searches for the keyword at Home Depot. Add results to vector store.
keyword : Search keyword
"""
params = {
"engine": "home_depot",
"api_key": SERPAPI_API_KEY,
"q": keyword
}
search = GoogleSearch(params)
if "products" in search.get_dict().keys():
products = search.get_dict()["products"]
else:
ans = chain.run(prompt_str)
return ans
########################################################################
# Clean the search results
# drop unused product info
for p in products:
for key in ['position', 'thumbnails', 'serpapi_link', 'collection', 'variants']:
if key in p.keys():
del p[key]
# Embedding models only work on texts or numbers.
# Fix values other than str, int or float.
for p in products:
for k,v in p.items():
# Change value to 'none' if it's not str/int/float
# or list/bool (will take care of these later)
if type(v) not in [str, int, float, list, bool]:
p[k] = 'none'
# Convert list of strings to string
if type(v)==list:
p[k] = '-'.join(v)
# Convert boolean to string
if type(v)==bool:
if p[k]==True:
p[k] = 'true'
else:
p[k] = 'false'
# Vector stores do not accept nested dict as metadata. Need to flatten it.
for p in range(len(products)):
# flatten each dict in products
p_flatdict = flatdict.FlatDict(products[p], delimiter='.')
# convert flatdict back to dict
p_dict = {}
for i in p_flatdict.iteritems():
p_dict.update({i})
products[p] = p_dict
########################################################################
# Add search results to vector store
# Get a list of product names (will be used as the texts for vector stores)
product_names = [p['title'] for p in products]
# Add texts to vector store
nonlocal db # assess db outside the function
db.add_texts(product_names, metadatas=products)
ans = chain.run(prompt_str)
return ans
@tool
def get_details(link):
"""
Get product details from the link. Add results to vector store.
link : link to product detail page
"""
# Validate link url
if not check_url(link): return "Please provide more information."
# Use webdriver to load the page
driver = webdriver.Chrome('./chromedriver')
driver.get(link)
time.sleep(20) # give extra time to ensure that the page is fully rendered
page = driver.page_source
# Scrape texts on page
soup = BeautifulSoup(page, "html.parser")
name = soup.find('div', {'class': 'product-details__badge-title--wrapper'}).text
ids = soup.find('div', {'class': 'sui-flex sui-text-xs sui-flex-wrap'}).text
overview = soup.find('section', {'id': 'product-section-product-overview'}).text
specs = soup.find('section', {'id': 'specifications-desktop'}).text
# Close webdriver after scraping
driver.close()
# clean the data
overview = re.sub(r"(\w)([A-Z])", r"\1 \2", overview)
specs = re.sub(r"(\w)([A-Z])", r"\1 \2", specs)
specs = re.sub('See Similar Items', ' ', specs)
ids = re.sub('Internet', ' Internet:', ids)
ids = re.sub('Model', ' Model:', ids)
ids = re.sub('Store SKU', ' Store SKU:', ids)
ids = re.sub('Store SO SKU', ' Store SO SKU:', ids)
# Concatenate scraped texts into a string
pdp = 'Product Name: ' + name + '\nProduct IDs: ' + ids
+ '\nProduct Overview: ' + overview + '\nSpecifications: ' + specs
# Add text to vector store
nonlocal db # assess db outside the function
db.add_texts(pdp)
ans = chain.run(prompt_str)
return ans
############################################################################
# Create an agent with tools
tools = [
Tool(
name = "is_homedepot",
func=is_homedepot,
description="Use this tool if you are not sure the query is asking \
about a specific product available at Home Depot. \
Input is the query",
),
Tool(
name = "get_products",
func=get_products,
description="Look for the products on Home Depot's website. \
Input is a string.",
return_direct=True
),
Tool(
name = "get_details",
func=get_details,
description="Find the product details about a specific product. \
The input is the link to the product. You can get this link \
from the search results you got from the function 'get_products'",
return_direct=True
)
]
memory = ConversationBufferMemory(memory_key="chat_history")
agent = initialize_agent(tools, llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=False, memory=memory)
return agent.run(query)