This guide provides a quickstart tutorial for using the colbertdb-ts
package to integrate ColbertDB with LlamaIndex, leveraging OpenAI's GPT-4 model for processing and querying documents.
Ensure you have the following installed and configured:
- Node.js and npm
- Docker (for running ColbertDB)
- An OpenAI API key
- Environment variables configured for ColbertDB
-
Install the package
npm install colbertdb-ts
-
Set up environment variables
Create a
.env
file in the root directory of your project and add the following:OPENAI_API_KEY=your_openai_api_key COLBERT_URL=your_colbertdb_url COLBERT_API_KEY=your_colbertdb_api_key COLBERT_STORE_NAME=your_colbertdb_store_name
Below is an example of how to use the colbertdb-ts
package to fetch, process, and query documents.
The code begins by importing necessary dependencies.
import axios from "axios";
import "dotenv/config";
import { HTMLReader, TextQaPrompt, OpenAI, Settings } from "llamaindex";
import { ColbertDB } from "colbertdb-ts";
import { CreateCollectionDocument } from "colbertdb-ts/dist/models";
Set the OpenAI model configuration with your API key.
Settings.llm = new OpenAI({
model: "gpt-4-turbo",
temperature: 0,
apiKey: process.env.OPENAI_API_KEY as string,
});
Define a prompt template for generating responses in the style of a Sherlock Holmes detective novel.
const newTextQaPrompt: TextQaPrompt = ({ context, query }) => {
return `Context information is below.
---------------------
${context}
---------------------
Given the context information and not prior knowledge, answer the query.
Answer the query in the style of a Sherlock Holmes detective novel.
Query: ${query}
Answer:`;
};
The main function contains the core logic for connecting to ColbertDB, fetching HTML content, creating a collection, and querying the data.
const main = async () => {
// Connect to ColbertDB
const client = await new ColbertDB(
process.env.COLBERT_URL as string,
process.env.COLBERT_API_KEY as string,
process.env.COLBERT_STORE_NAME as string
).connect();
// Fetch HTML content from Wikipedia
const html = await axios.get("https://en.wikipedia.org/wiki/Onigiri");
const docs = [
{
content: await new HTMLReader().parseContent(html.data),
metadata: { type: "food" },
} as CreateCollectionDocument,
];
// Create a new collection in ColbertDB
const collection = await client.createCollection("onigiri", docs, {
force_create: true,
});
// Search the collection
const response = await collection.search("What are onigiri?", 3);
let context = "";
for (const doc of response.documents) {
context += doc.content + "\n";
}
// Generate a response using OpenAI
const prompt = newTextQaPrompt({ context, query: "What are onigiri?" });
const answer = await new OpenAI().complete({ prompt });
console.log(answer);
};
// Execute the main function
main();
To execute the script, run:
ts-node path/to/your/script.ts
This will connect to ColbertDB, fetch and process the HTML content from the specified URL, create a document collection, perform a search query, and generate a response using OpenAI's GPT-4 model.
This guide provides a quickstart overview of using the colbertdb-ts
package for document processing and querying. Customize the prompt and collection as needed for your specific use case.