Klu Context

Context is a library of Documents. Klu automatically creates a vector index and performs retrieval augmented generation via similarity search. Klu appends the most relevant documents to the Action prompt at runtime.

Context and Document model

Create Context libraries in Klu Studio and via the API or SDK. Documents are automatically created via Klu Studio (crawler, file upload, or integrations) or programmatically via the API or SDK (app data or file upload).

Documents can be added to a Context library at any time. Klu automatically transcribes audio and video content. Klu supports the following file types:

CategoryFile Type Extensions
Audio / Video.mp3, .mp4
Documents.pdf, .rtf, .txt
eBooks.epub
Email Files.eml, .msg
Images.png, .jpg
Markup/Structured Text.md, .html, .rst, .org, .xml
Office Documents.doc, .docx, .xlsx, .xls, .ppt, .pptx, .odt
Structured Data.csv, .tsv

Additionally, Klu supports non-file Context

CategorySupported Formats
CollaborationGoogle, MS Teams, Slack, Zoom
Customer PlatformsIntercom, Salesforce, Zendesk
ProjectsAirtable, Asana, Atlassian, Github, Notion
Websites (Crawling)HTML, Sitemap
SQL DatabaseMySQL, PostgreSQL, SQLite, Oracle, SQL Server
RedisAll data types (string, list, set, zset, hash)
ElasticAll data types
SnowflakeAll data types
YoutubeAll video formats

All Context libraries and Documents are automatically associated with the connected Workspace via your API Key.


Context Examples

Here are common examples for how to use Klu Context in your applications.

Create Context from App Data

Create Context from app data in two steps. First, create a custom type Context. Then, add Documents to the Context library. Add specific metadata to each Document to improve retrieval augmented generation results.

Create Context

POST
/v1/context
from klu import Klu

klu = Klu("YOUR_TOKEN")

contect = await klu.context.create(
    name="A beautiful library",
    description="A library of secret documents",
    type="api"
)

print(context)

Add Docs

POST
/v1/context/{guid}/documents
from klu import Klu

klu = Klu("YOUR_TOKEN")

docs = klu.context.add_doc(
    context=context.guid,
    content="John McAfee is a great guy. He works at Acme Inc. and lives in New York. He loves tacos.",
    meta_data={
      "organization_id": "3a38-223-8191",
      "organization_name": "Acme Inc.",
      "user_id": "1c877-069-4201",
      "user_name": "[email protected]"
    }
)

Create Context from Files

When creating Context from files, you will need to pre-sign the file URL and then upload the file to the pre-signed URL. This is a two-step process for each file. Once finished, simply add the file names to the Context creation request.

The Klu SDKs greatly simplify this process.

Pre-sign URL

POST
/v1/context/presign
from klu import Klu

klu = Klu("YOUR_TOKEN")

presigned_url = await klu.context.presign(
    file_name="taco.csv"
)

print(presigned_url)

Upload File

POST
AWS S3 Bucket
from klu import Klu
from klu.context.models import FileData

klu = Klu("YOUR_TOKEN")

file_upload = klu.context.upload_file(
  FileData(file_name="taco.csv, file_path="~/taco.csv")
)
print(file_upload.object_url)

Create Context

POST
/v1/context
from klu import Klu

klu = Klu("YOUR_TOKEN")

context = klu.context.create(
    name="A beautiful library",
    description="A library of secret documents",
    type="files",
    files=["taco.csv", "master_plan_final1.pdf", "unlimited_power.txt"]
)

print(context)

POST/v1/context

Create Context

Create a new Context library, optimizing it for files or app data (custom). Files must be pre-signed and uploaded to AWS S3 bucket.

Required attributes

  • Name
    name
    Type
    string
    Description

    Name

  • Name
    description
    Type
    string
    Description

    Description

  • Name
    type
    Type
    string
    Description

    Files or Custom

Optional attributes

  • Name
    files
    Type
    Array
    Description

    An array of file names already uploaded to Klu

Create Context

POST
/v1/context
from klu import Klu

klu = Klu("YOUR_TOKEN")

context = klu.context.create(
    name="A beautiful library",
    description="A library of secret documents",
    type="files",
    files=["taco.csv", "master_plan_final1.pdf", "unlimited_power.txt"]
)

print(context)

Response

{
  "name":"A beautiful library",
  "description":"A library of secret documents",
  "typeId":4,
  "loaderId":12,
  "workspaceId":1,
  "createdAt":"2023-07-30T03:47:22.601000",
  "updatedAt":"2023-07-30T03:47:23.002000",
  "createdById":"zzz0a0bb694207z22zll69zphp",
  "processed":true,
  "meta_data":null,
  "guid":"11b4e91c-80be-4edc-96fd-a482c7f263bb"
}

POST/v1/context/{guid}/documents

Add App Data

An easy way to convert app data into Context documents.

Required attributes

  • Name
    text
    Type
    string
    Description

    Data sent from your app

Optional attributes

  • Name
    meta_data
    Type
    JSON
    Description

    Key-value pairs of meta data to add to the document

Add Documents

POST
/v1/context/{guid}/documents
from klu import Klu

klu = Klu("YOUR_TOKEN")

doc = klu.context.add_doc(
    context="context_guid",
    text="John McAfee is a great guy. He works at Acme Inc. and lives in New York. He loves tacos.",
    meta_data={
      "organization_id": "3a38-223-8191",
      "organization_name": "Acme Inc.",
      "user_id": "1c877-069-4201",
      "user_name": "[email protected]"
    }
)

Response

{
  "guid": "123e4567-e89b-12d3-a456-426614174000",
  "status": "success"
}

POST/v1/context/{guid}/add_files

Add Files

A simple endpoint to add files to a Context library. Files must be pre-signed and uploaded to AWS S3 bucket.

Required attributes

  • Name
    files
    Type
    array
    Description

    An array of file names

Add Files

POST
/v1/context/{guid}/add_files
from klu import Klu

klu = Klu("YOUR_TOKEN")

context_updated = klu.context.add_files(
    context="context_guid",
    files=["taco.csv", "master_plan_final1.pdf", "unlimited_power.txt"]
)

print(context_updated)

Response

{
  "name": "A beautiful library",
  "description": "A library of secret documents",
  "createdAt": "2023-01-01T12:00:00Z",
  "updatedAt": "2023-01-01T12:01:00Z",
  "processed": true,
  "meta_data": {
    "organization_id": "3a38-223-8191",
    "organization_name": "Acme Inc.",
    "user_id": "1c877-069-4201",
    "user_name": "[email protected]"
  },
  "guid": "123e4567-e89b-12d3-a456-426614174000"
}