KDB.AI
KDB.AI is a powerful knowledge-based vector database and search engine that allows you to build scalable, reliable AI applications, using real-time data, by providing advanced search, recommendation and personalization.
This example demonstrates how to use KDB.AI to run semantic search on unstructured text documents.
To access your end point and API keys, sign up to KDB.AI here.
To set up your development environment, follow the instructions on the KDB.AI pre-requisites page.
The following examples demonstrate some of the ways you can interact with KDB.AI through LangChain.
You'll need to install langchain-community
with pip install -qU langchain-community
to use this integration
Import required packages
import os
import time
from getpass import getpass
import kdbai_client as kdbai
import pandas as pd
import requests
from langchain.chains import RetrievalQA
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import KDBAI
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
KDBAI_ENDPOINT = input("KDB.AI endpoint: ")
KDBAI_API_KEY = getpass("KDB.AI API key: ")
if "OPENAI_API_KEY" not in os.environ:
os.environ["OPENAI_API_KEY"] = getpass("OpenAI API Key: ")
KDB.AI endpoint: https://ui.qa.cld.kx.com/instance/pcnvlmi860
KDB.AI API key: ········
OpenAI API Key: ········
TEMP = 0.0
K = 3
Create a KBD.AI Session
print("Create a KDB.AI session...")
session = kdbai.Session(endpoint=KDBAI_ENDPOINT, api_key=KDBAI_API_KEY)
Create a KDB.AI session...
Create a table
print('Create table "documents"...')
schema = {
"columns": [
{"name": "id", "pytype": "str"},
{"name": "text", "pytype": "bytes"},
{
"name": "embeddings",
"pytype": "float32",
"vectorIndex": {"dims": 1536, "metric": "L2", "type": "hnsw"},
},
{"name": "tag", "pytype": "str"},
{"name": "title", "pytype": "bytes"},
]
}
table = session.create_table("documents", schema)
Create table "documents"...
%%time
URL = "https://www.conseil-constitutionnel.fr/node/3850/pdf"
PDF = "Déclaration_des_droits_de_l_homme_et_du_citoyen.pdf"
open(PDF, "wb").write(requests.get(URL).content)
CPU times: user 44.1 ms, sys: 6.04 ms, total: 50.2 ms
Wall time: 213 ms
562978