Created
March 6, 2024 16:41
-
-
Save dfeldman/a0e5144d334c86b086cfbe7d4a8e1dc6 to your computer and use it in GitHub Desktop.
ChatGPT->Wikidata connector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.client | |
from urllib.parse import urlencode | |
app = Flask(__name__) | |
# Set up logging | |
logging.basicConfig(level=logging.INFO) | |
WIKIDATA_ENDPOINT = "query.wikidata.org" | |
SPARQL_PATH = "/sparql" | |
@app.route('/proxy', methods=['GET']) | |
def proxy(): | |
query = request.args.get('query') | |
if not query: | |
return jsonify({'error': 'No query provided'}), 400 | |
logging.info(f"Received query: {query}") | |
try: | |
conn = http.client.HTTPSConnection(WIKIDATA_ENDPOINT) | |
headers = {"Accept": "text/csv"} | |
params = urlencode({'query': query}) | |
conn.request("GET", SPARQL_PATH + "?" + params, headers=headers) | |
response = conn.getresponse() | |
data = response.read() | |
print (data) | |
# Close the connection | |
conn.close() | |
return Response(data, status=response.status, headers=dict(response.getheaders())) | |
except Exception as e: | |
logging.error(f"Error: {e}") | |
return jsonify({'error': 'An error occurred'}), 500 | |
def test_query(): | |
"""Function to test the proxy with a sample query after a delay.""" | |
time.sleep(5) # Wait for the Flask server to start | |
test_query_string = ''' | |
SELECT DISTINCT ?iso2 ?qid ?osm_relid ?itemLabel | |
WHERE { | |
?item wdt:P297 _:b0. | |
BIND(strafter(STR(?item),"http://www.wikidata.org/entity/") as ?qid). | |
OPTIONAL { ?item wdt:P1448 ?name .} | |
OPTIONAL { ?item wdt:P297 ?iso2 .} | |
OPTIONAL { ?item wdt:P402 ?osm_relid .} | |
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,[AUTO_LANGUAGE]" . } | |
} | |
ORDER BY ?iso2 | |
''' | |
response = requests.get("http://127.0.0.1:5000/proxy", params={"query": test_query_string}) | |
if response.ok: | |
print("Test query successful.") | |
else: | |
print(f"Test query failed. Status code: {response.status_code}") | |
if __name__ == '__main__': | |
# Start the test query in a separate thread | |
test_thread = threading.Thread(target=test_query) | |
test_thread.start() | |
# Start the Flask server | |
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You are a Wikidata query helper. The user inputs queries in English, and you translate them to SPARQL and pass them to Wikidata using the included Action. ALWAYS do research using Wikidata if there is any doubt about the answer to a question. When possible, output a table including several of the interesting properties of the Wikidata output. Feel free to do multiple Wikidata queries if the first does not provide sufficient information. Do as many queries as needed to provide a satisfactory result. If there are no rows, always refine the query and improve it. Feel free to do initial queries to determine property names and item identifiers. Think step by step and be careful. If a query returns no results, it is probably incorrect. When returning numerical results, always think through the units used. Always remember whether to query based on subclasses, instances, or some other relationship. You can always do sample queries on known objects to see what the properties are likely to be. REmember that Wikidata includes data on fictional people, places and things -- and the user might not want those to be in the query results. Feel free to ask clarifying questions to the user. Your job is to be helpful, thoughtful, and return correct results. |
Author
dfeldman
commented
Mar 6, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment