Commit
·
1186197
1
Parent(s):
540c811
coba dulu
Browse files- .gitignore +1 -0
- anton-agent.py +55 -0
- app.py +10 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
anton-agent.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import langchain
|
3 |
+
|
4 |
+
from langchain.agents import Tool, ConversationalAgent, AgentExecutor, load_tools, tool
|
5 |
+
from langchain import OpenAI, LLMChain, LLMMathChain
|
6 |
+
from langchain.chains.conversation.memory import ConversationBufferMemory, ConversationBufferWindowMemory
|
7 |
+
|
8 |
+
from duckduckgo_search import ddg, ddg_answers # ddg search
|
9 |
+
|
10 |
+
# define search tool using ddg
|
11 |
+
@tool ("Current Search") # using ddg
|
12 |
+
def ddgsearch_api(query: str) -> str:
|
13 |
+
"""Searches the API for the query."""
|
14 |
+
# keywords=query+' site:wikipedia.org' # using wikipedia
|
15 |
+
keywords=query
|
16 |
+
region = 'wt-wt' # no region
|
17 |
+
safesearch = 'off' # safesearch off
|
18 |
+
max_results = 5 # max results returned
|
19 |
+
results = ddg(keywords, region=region, safesearch=safesearch, max_results=max_results)
|
20 |
+
|
21 |
+
# hukumonline stuffs
|
22 |
+
keywords=query+ ' site:hukumonline.com'
|
23 |
+
region = 'wt-wt' # no region
|
24 |
+
safesearch = 'off' # safesearch off
|
25 |
+
max_results = 5 # max results returned
|
26 |
+
results_ho = ddg(keywords, region=region, safesearch=safesearch, max_results=max_results)
|
27 |
+
|
28 |
+
results = results_ho + results
|
29 |
+
|
30 |
+
tempstr = ''
|
31 |
+
for i in range(len(results)):
|
32 |
+
tempstr+=("; " + results[i]['body'][:200]) # limits answer to 200
|
33 |
+
return tempstr
|
34 |
+
|
35 |
+
ddgsearch_api.description = "useful for when you need to answer questions about current events or the current state of the world"
|
36 |
+
|
37 |
+
# define calculator tool
|
38 |
+
llm_math_chain = LLMMathChain(llm=llm, verbose=True)
|
39 |
+
|
40 |
+
#### #### #### ####
|
41 |
+
|
42 |
+
# define tools that are available to the agent
|
43 |
+
tools = [
|
44 |
+
ddgsearch_api,
|
45 |
+
# load_tools(["llm-math"], llm=llm)[0] # a bit of a hack
|
46 |
+
Tool(
|
47 |
+
name = "Calculator",
|
48 |
+
func=llm_math_chain.run, # use preloaded stuffs
|
49 |
+
description="useful for when you need to answer questions about math"
|
50 |
+
)
|
51 |
+
]
|
52 |
+
# tools
|
53 |
+
|
54 |
+
# allowed_tools names (for the agent)
|
55 |
+
allowed_tools = [tool.name for tool in tools]
|
app.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv, find_dotenv
|
3 |
+
import openai
|
4 |
+
import langchain
|
5 |
+
|
6 |
+
# load environment
|
7 |
+
load_dotenv(find_dotenv())
|
8 |
+
|
9 |
+
# secrets
|
10 |
+
OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]
|