2023-03-17 15:55:15 +00:00
import requests
2023-03-19 06:17:36 +00:00
from bs4 import BeautifulSoup
2023-04-03 07:43:34 +00:00
from llama_index import GPTSimpleVectorIndex
from llama_index . readers . database import DatabaseReader
2023-03-17 15:55:15 +00:00
2023-04-03 07:43:34 +00:00
from env import settings
2023-03-22 00:34:52 +00:00
from logger import logger
2023-03-18 09:20:18 +00:00
2023-04-03 07:43:34 +00:00
from . base import BaseToolSet , SessionGetter , ToolScope , tool
2023-03-18 12:26:19 +00:00
class RequestsGet ( BaseToolSet ) :
@tool (
2023-03-22 03:21:29 +00:00
name = " Requests Get " ,
2023-03-17 15:55:15 +00:00
description = " A portal to the internet. "
" Use this when you need to get specific content from a website. "
" Input should be a url (i.e. https://www.google.com). "
" The output will be the text response of the GET request. " ,
)
2023-03-22 00:34:52 +00:00
def get ( self , url : str ) - > str :
2023-03-17 15:55:15 +00:00
""" Run the tool. """
2023-03-19 06:17:36 +00:00
html = requests . get ( url ) . text
soup = BeautifulSoup ( html )
non_readable_tags = soup . find_all (
[ " script " , " style " , " header " , " footer " , " form " ]
)
2023-03-17 15:55:15 +00:00
2023-03-19 06:17:36 +00:00
for non_readable_tag in non_readable_tags :
non_readable_tag . extract ( )
content = soup . get_text ( " \n " , strip = True )
if len ( content ) > 300 :
content = content [ : 300 ] + " ... "
2023-03-20 08:27:20 +00:00
2023-03-22 00:34:52 +00:00
logger . debug (
2023-03-20 08:27:20 +00:00
f " \n Processed RequestsGet, Input Url: { url } " f " Output Contents: { content } "
)
2023-03-19 06:17:36 +00:00
return content
2023-03-17 15:55:15 +00:00
2023-03-18 12:26:19 +00:00
class WineDB ( BaseToolSet ) :
2023-03-17 15:55:15 +00:00
def __init__ ( self ) :
db = DatabaseReader (
scheme = " postgresql " , # Database Scheme
host = settings [ " WINEDB_HOST " ] , # Database Host
port = " 5432 " , # Database Port
user = " alphadom " , # Database User
password = settings [ " WINEDB_PASSWORD " ] , # Database Password
dbname = " postgres " , # Database Name
)
self . columns = [ " nameEn " , " nameKo " , " description " ]
concat_columns = str ( " , ' - ' , " . join ( [ f ' " { i } " ' for i in self . columns ] ) )
query = f """
SELECT
Concat ( { concat_columns } )
FROM wine
"""
documents = db . load_data ( query = query )
self . index = GPTSimpleVectorIndex ( documents )
2023-03-18 12:26:19 +00:00
@tool (
2023-03-21 12:20:15 +00:00
name = " Wine Recommendation " ,
2023-03-17 15:55:15 +00:00
description = " A tool to recommend wines based on a user ' s input. "
" Inputs are necessary factors for wine recommendations, such as the user ' s mood today, side dishes to eat with wine, people to drink wine with, what things you want to do, the scent and taste of their favorite wine. "
" The output will be a list of recommended wines. "
" The tool is based on a database of wine reviews, which is stored in a database. " ,
)
2023-03-22 00:34:52 +00:00
def recommend ( self , query : str ) - > str :
2023-03-17 15:55:15 +00:00
""" Run the tool. """
results = self . index . query ( query )
wine = " \n " . join (
[
f " { i } : { j } "
for i , j in zip (
self . columns , results . source_nodes [ 0 ] . source_text . split ( " - " )
)
]
)
2023-03-20 08:27:20 +00:00
output = results . response + " \n \n " + wine
2023-03-22 00:34:52 +00:00
logger . debug (
f " \n Processed WineDB, Input Query: { query } " f " Output Wine: { wine } "
)
2023-03-20 08:27:20 +00:00
return output
2023-03-17 15:55:15 +00:00
2023-03-18 12:26:19 +00:00
class ExitConversation ( BaseToolSet ) :
@tool (
2023-03-21 12:49:47 +00:00
name = " Exit Conversation " ,
2023-03-17 15:55:15 +00:00
description = " A tool to exit the conversation. "
2023-03-21 12:49:47 +00:00
" Use this when you want to exit the conversation. "
2023-03-22 04:49:57 +00:00
" The input should be a message that the conversation is over. " ,
2023-03-22 00:57:38 +00:00
scope = ToolScope . SESSION ,
2023-03-17 15:55:15 +00:00
)
2023-03-22 04:49:57 +00:00
def exit ( self , message : str , get_session : SessionGetter ) - > str :
2023-03-17 15:55:15 +00:00
""" Run the tool. """
2023-03-22 04:48:27 +00:00
_ , executor = get_session ( )
del executor
2023-03-18 09:20:18 +00:00
2023-03-22 00:34:52 +00:00
logger . debug ( f " \n Processed ExitConversation. " )
2023-03-20 08:27:20 +00:00
2023-03-22 04:49:57 +00:00
return message