-
Notifications
You must be signed in to change notification settings - Fork 2
/
wikidata-tool-agent.py
136 lines (107 loc) · 3.64 KB
/
wikidata-tool-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
import os
import requests
import re
import argparse
from langchain.agents import AgentExecutor, create_react_agent
from langchain_openai import OpenAI
from langchain.agents import tool
from langchain.prompts import PromptTemplate
from langchain.globals import set_debug
from langchain_community.llms import Ollama
from langchain_community.tools.wikidata.tool import WikidataAPIWrapper, WikidataQueryRun
from wikibaseintegrator import wbi_helpers
from wikibaseintegrator.wbi_config import config as wbi_config
WB_LANGUAGE = 'en'
WB_LIMIT = 100
WB_USER_AGENT = 'MyWikibaseBot/1.0'
wbi_config['USER_AGENT'] = 'MyWikibaseBot/1.0'
def extract_error_message(response):
pattern = re.compile(r'MalformedQueryException:(.*)\n')
match = pattern.search(response.text)
if match:
return match.group(1).strip()
else:
return None
@tool
def WikidataRetrieval(item: str) -> str:
"""Returns all the information about the input Q item or property from my wikibase."""
wikidata = WikidataQueryRun(api_wrapper=WikidataAPIWrapper())
return wikidata.run(item)
def load_prompt_file(full_path):
with open(full_path, 'r') as f:
txt_prompt = f.read()
prompt = PromptTemplate.from_template(txt_prompt);
return prompt
@tool
def getQItem(name: str) -> str:
"""Returns the Q item from my wikibase."""
name = name.strip("'").strip('"')
data = {
'action': 'wbsearchentities',
'search': name,
'type': 'item',
'language': WB_LANGUAGE,
'limit': WB_LIMIT
}
result = wbi_helpers.mediawiki_api_call_helper(data=data, allow_anonymous=True)
if result['search']:
return result['search'][0]['id']
else:
return 'Item not found by this name, try another name.'
@tool
def getProperty(name: str) -> str:
"""Returns the property from my wikibase."""
name = name.strip("'").strip('"')
data = {
'action': 'wbsearchentities',
'search': name,
'type': 'property',
'language': WB_LANGUAGE,
'limit': WB_LIMIT
}
result = wbi_helpers.mediawiki_api_call_helper(data=data, allow_anonymous=True)
if result['search']:
return result['search'][0]['id']
else:
return 'Property not found by this name, try another name.'
def answer_the_question(question, final_answer):
if 'OPENAI_API_URL' in os.environ:
llm = OpenAI(openai_api_base=os.environ['OPENAI_API_URL'],
openai_api_key="dummy",
temperature=0,
top_p=0,
max_tokens=1024,
model_kwargs={"seed": 42})
else:
llm = Ollama(
model="mixtral:latest",
temperature=0,
top_p=0)
#max_tokens=1024,
#model_kwargs={"seed": 42})
if final_answer:
# tools = [getQItem, getProperty, WikidataRetrieval]
prompt = load_prompt_file('prompts/retrieval-to-answer.prompt')
else:
tools = [getQItem, WikidataRetrieval]
prompt = load_prompt_file('prompts/question-to-retrieval.prompt')
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
handle_parsing_errors=True,
early_stop_method='generate',
max_iteratin=5
)
set_debug(False)
result = agent_executor.invoke({"input": f"{question}"})
return result
def main():
parser = argparse.ArgumentParser(description='Wikibase agent.')
parser.add_argument('--question', type=str, required=True, help='Your question.')
parser.add_argument('--final-answer', action='store_true', help='Try to get an answer in natural language.')
args = parser.parse_args()
print(answer_the_question(args.question, args.final_answer))
if __name__ == '__main__':
main()