File size: 1,877 Bytes
1186197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import langchain

from langchain.agents import Tool, ConversationalAgent, AgentExecutor, load_tools, tool
from langchain import OpenAI, LLMChain, LLMMathChain
from langchain.chains.conversation.memory import ConversationBufferMemory, ConversationBufferWindowMemory

from duckduckgo_search import ddg, ddg_answers # ddg search

# define search tool using ddg
@tool ("Current Search") # using ddg
def ddgsearch_api(query: str) -> str:
    """Searches the API for the query."""
    # keywords=query+' site:wikipedia.org' # using wikipedia
    keywords=query
    region = 'wt-wt' # no region
    safesearch = 'off' # safesearch off
    max_results = 5 # max results returned
    results = ddg(keywords, region=region, safesearch=safesearch, max_results=max_results)

    # hukumonline stuffs
    keywords=query+ ' site:hukumonline.com'
    region = 'wt-wt' # no region
    safesearch = 'off' # safesearch off
    max_results = 5 # max results returned
    results_ho = ddg(keywords, region=region, safesearch=safesearch, max_results=max_results)

    results = results_ho + results

    tempstr = ''
    for i in range(len(results)):
      tempstr+=("; " + results[i]['body'][:200]) # limits answer to 200
    return tempstr

ddgsearch_api.description = "useful for when you need to answer questions about current events or the current state of the world"

# define calculator tool
llm_math_chain = LLMMathChain(llm=llm, verbose=True)

#### #### #### ####

# define tools that are available to the agent
tools = [
    ddgsearch_api,
    # load_tools(["llm-math"], llm=llm)[0] # a bit of a hack
    Tool(
        name = "Calculator",
        func=llm_math_chain.run, # use preloaded stuffs
        description="useful for when you need to answer questions about math"
    )
]
# tools

# allowed_tools names (for the agent)
allowed_tools = [tool.name for tool in tools]