[Python] Querying via Rest API

Hi Vectara team,

I’m currently querying an index I created. When I query via the web UI the summary is generated as expected (yay!). However, I’m trying to query via the Python Rest API and the summary is not returned. I see the responseSet returned but the summary itself is not returned. For reference, this is the code example that I am using to query my index:

Please let me know if you would be able to assist me with the above.

Thanks in advance!

Hey there,

I do not work for Vectara so someone can probably give you a better answer but I believe you have to include something to this effect to return the summary:

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]

    # Add the "summary" section
    summary = {
        "summarizerPromptName": "vectara-summary-ext-v1.2.0",
        "maxSummarizedResults": 5,
        "responseLang": "eng"
    }
    query_obj["summary"] = [summary]

    query["query"] = [query_obj]

    return query

Hope this is helpful!

Awesome. Thanks Ed. I did try adding the Summary section earlier in the day. I end up getting an error:

'summary': [{'text': 'The returned results did not contain sufficient information to be summarized into a useful answer for your query. Please try a different search or restate your query differently.', 'lang': 'eng', 'prompt': '', 'status': [], 'futureId': 2}], 'futureId': 1}]

I don’t get this error when I query from the UI. So I find it odd. Thanks for replying! (Hopefully, you have useful insights for the new error as well haha).

Does it respond with all of the results? This response would suggest there is nothing for it to use as a summary. Obviously when you use the UI, it handles all the retrieval and then summarising of the documents so it would suggest that your python script is not getting any results back OR the results it is getting back are not relevant to your query which could be the case if you are asking something super specific (or for me sometimes super broad).

If you can share the full response you get I might be able to assist.

Personally, I like to test these things first in Postman before moving to writing python scripts as it is much easier to solve issues fast so that might be worth trying!

Yep responds with all the results. I’m using the same query in both cases. I’m using a curl command and still have the same issue haha. I think I must be missing something the query parameters.

Also, the results are in the same order and with the same similarity scores as those in the UI which confuses me further.

I just tried it in Postman and my first question came back with the same response you got about there not being enough info to make a summary, but I asked it a very specific question and it got a result so I guess it is potentially an issue with the API (but I think you already had worked that out!), sorry I couldn’t be more help!

Please try the following with context_config. Without context, the response didn’t have enough information to summarize.

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
    query_obj["start"] = 0

    corpus_key = {}
    corpus_key["customer_id"] = customer_id
    corpus_key["corpus_id"] = corpus_id
        
    query_obj["context_config"] = {
        "charsBefore": 0,
        "charsAfter": 0,
        "sentencesBefore": 2,
        "sentencesAfter": 2,
        "startTag": "%START_SNIPPET%",
        "endTag": "%END_SNIPPET%"
    }
    query_obj["corpus_key"] = [ corpus_key ]
    summary = {
        "summarizerPromptName": "vectara-summary-ext-v1.2.0",
        "maxSummarizedResults": 5,
        "responseLang": "eng"
    }
    query_obj["summary"] = [ summary ]
    query["query"] = [ query_obj ]
    return json.dumps(query)

The API Playground is a nice place to test out these things as well. It also provides the default values for most of the things.

Hey Talip,

Thanks! This was the solution to the problem. All results are now essentially identical to the web UI! Do you know if there is any way to know the exact query parameters that are used when querying from the UI?

1 Like

Thanks for the resource aamir. Will def use this in the future for any further modification on the query side of things.

yes, on the console, after you query, hit “Inspect” button.

Fantastic! Thanks, Talip.