Guides

Retrieve Knowledge Base Data

Query against your knowledge base by retrieving answers from your custom data.

Overview

Before using this guide, it is recommended that you check out how to Ingest Custom Data to create a knowledge base and ingest your own data. Once your data is done being ingested, this guide will show you how you can perform retrieval by asking queries or questions against your knowledge base.

SGP APIs used in this guide

Prerequisites

PrerequisiteDescriptionInstructions
API KeyRequired for SGP API requestsFollow instructions from the API Key Authentication Section
Knowledge Base IDRequired to ask a query againstFollow instructions from Ingest Custom Data or see Create Knowledge Base

Query a Knowledge Base

Once you have created a knowledge base and ingested your data, you are now able to query against the knowledge base to answer questions about your data. You should have a Scale-generated knowledge_base_id that was returned to you upon knowledge base creation. If you do not have this ID but remember the knowledge base name, you can always call List Knowledge Bases to retrieve the relevant ID.

To query against your knowledge base, you can use the following script:

# Fill in your own API key
API_KEY = "[Your Spellbook API Key here]"

# The ID of your knowledge base
KNOWLEDGE_BASE_ID = "[Your knowledge base ID here]"

# Query against your knowledge base
print(f"Querying against knowledge base with ID {KNOWLEDGE_BASE_ID}...")
url = "https://api.spellbook.scale.com/egp/v1/knowledge-bases/" + KNOWLEDGE_BASE_ID + "/query"

payload = {
  "query": "[Your question to be answered here]",
}

headers = {
    "accept": "application/json",
	  "content-type": "application/json",
    "x-api-key": API_KEY
}
response = requests.post(url, json=payload, headers=headers)
print(f"Response: {response.text}")

What's Next?

Now that you can retrieve and answer queries on your knowledge base data, you might want to explore:

  • Trying out different embedding models to see if one performs better for your use case.
  • Building LangChain QA agents that answer questions based on your knowledge base.