Undang-Undang-ChatGPT / anton-agent.py
fawwazanvilen's picture
coba dulu
1186197
raw
history blame
1.88 kB
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]