-
Notifications
You must be signed in to change notification settings - Fork 2
/
wikibase-ollama-agent.py
185 lines (145 loc) · 5.06 KB
/
wikibase-ollama-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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import requests
import re
import argparse
from langchain.agents import AgentExecutor, create_react_agent, create_openai_tools_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 wikibaseintegrator import wbi_helpers
from wikibaseintegrator.wbi_config import config as wbi_config
#WB_LANGUAGE = 'en'
WB_LANGUAGE = 'pt-br'
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
def performSparqlQuery(query: str) -> str:
url = "https://query.wikidata.org/sparql"
user_agent_header = WB_USER_AGENT
query = query.lstrip('sql').lstrip('less').lstrip('ruby').lstrip('sparql').strip("'").strip('"').strip('`')
headers = {"Accept": "application/json"}
if user_agent_header is not None:
headers["User-Agent"] = user_agent_header
return requests.get(
url, headers=headers, params={"query": query, "format": "json"}
)
@tool
def checkSparql(query: str) -> str:
"""Given a SPARQL query check if is valid."""
response = performSparqlQuery(query)
if response.status_code != 200:
error_message = extract_error_message(response)
if error_message:
return f'Query failed with this syntax error: {error_message}, try to fix it with another one.'
else:
return 'Query failed, try another one.'
# print(f"Sparql results: {response.json()}")
return 'Query is valid'
@tool
def runSparql(query: str) -> str:
"""Given a SPARQL query returns the results."""
response = performSparqlQuery(query)
if response.status_code != 200:
error_message = extract_error_message(response)
if error_message:
return f'Query failed with this syntax error: {error_message}, try to fix it with another one.'
else:
return 'Query failed, try another one.'
return response.json()
@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.'
@tool
def runSparqlQuery(query: str) -> str:
"""Given a SPARQL query returns the results."""
try:
results = wbi_helpers.execute_sparql_query(query, max_retries=1)
return results
except Exception as e:
return 'Query is not working, try another one.'
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
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 = [runSparql]
prompt = load_prompt_file('prompts/question-to-answer.prompt')
else:
tools = [getQItem, getProperty, checkSparql, runSparql]
prompt = load_prompt_file('prompts/question-to-sparql.prompt')
agent = create_react_agent(llm, tools, prompt)
#agent = create_openai_tools_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
handle_parsing_errors=True,
early_stop_method='generate',
return_intermediate_steps=False,
max_iteration=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()