rest_query.py
This is an example of using the platform via REST. For more sample code, including any dependencies this file has, please have a look at our GitHub examples repository. This file can be found in that repo at python/vectara-rest/rest_query.py
python/vectara-rest/rest_query.py
"""Simple example of using the Vectara REST API for searching a corpus.
"""
import json
import logging
import requests
def _get_query_json(customer_id: int, corpus_id: int, query_value: str):
""" Returns a query json. """
query = {}
query_obj = {}
query_obj["query"] = query_value
query_obj["num_results"] = 10
corpus_key = {}
corpus_key["customer_id"] = customer_id
corpus_key["corpus_id"] = corpus_id
query_obj["corpus_key"] = [ corpus_key ]
query["query"] = [ query_obj ]
return json.dumps(query)
def query(customer_id: int, corpus_id: int, query_address: str, jwt_token: str, query: str):
"""This method queries the data.
Args:
customer_id: Unique customer ID in vectara platform.
corpus_id: ID of the corpus to which data needs to be indexed.
query_address: Address of the querying server. e.g., api.vectara.io
jwt_token: A valid Auth token.
Returns:
(response, True) in case of success and returns (error, False) in case of failure.
"""
post_headers = {
"customer-id": f"{customer_id}",
"Authorization": f"Bearer {jwt_token}"
}
response = requests.post(
f"https://{query_address}/v1/query",
data=_get_query_json(customer_id, corpus_id, query),
verify=True,
headers=post_headers)
if response.status_code != 200:
logging.error("Query failed with code %d, reason %s, text %s",
response.status_code,
response.reason,
response.text)
return response, False
return response, True