genkitx-weaviate is a community plugin for using Weaviate vector database with
Genkit. Built by Xavier Portilla Edo.
- Table of Contents
- Installation
- Configuration
- Usage
- Examples
- Features
- API Reference
- Contributing
- Need Support?
- Credits
- License
Install the plugin in your project with your favorite package manager:
npm install genkitx-weaviateor
pnpm add genkitx-weaviateTo use the plugin, you need to configure it with your Weaviate instance details:
import { genkit } from 'genkit';
import { weaviate } from 'genkitx-weaviate';
import { textEmbedding004 } from '@genkit-ai/vertexai';
const ai = genkit({
plugins: [
weaviate({
clientParams: {
host: 'localhost', // or 'your-cluster.weaviate.network' for Weaviate Cloud
port: 8080, // optional, defaults to 8080 for local, 443 for cloud
grpcPort: 50051, // optional, defaults to 50051 for local
apiKey: process.env.WEAVIATE_API_KEY, // optional, for Weaviate Cloud
},
collections: [
{
collectionName: 'Documents',
embedder: textEmbedding004,
},
],
}),
],
});-
clientParams: Configuration for the Weaviate client
host: Hostname of your Weaviate instance (without protocol) - e.g.,'localhost'or'my-cluster.weaviate.network'port: Optional HTTP port (default: 8080 for local, 443 for cloud)grpcPort: Optional gRPC port (default: 50051 for local)secure: Optional boolean for secure connection (default: false for local, true for cloud)apiKey: Optional API key for authentication (required for Weaviate Cloud)headers: Optional custom headerstimeout: Optional timeout in milliseconds (default: 30000)
-
collections: Array of collection configurations
collectionName: Name of the Weaviate collectionembedder: Optional embedder to use for this collectionembedderOptions: Optional embedder configurationcreateCollectionIfMissing: Whether to create the collection if it doesn't exist (default: true)collectionConfig: Optional collection configuration
The plugin provides two main functionalities: index and retrieve. You can use them directly or within a Genkit flow.
The indexer stores documents and their embeddings in a Weaviate collection.
import { weaviateIndexerRef } from 'genkitx-weaviate';
const indexer = weaviateIndexerRef({
collectionName: 'Documents',
});
const documents = [
{
text: 'Weaviate is a vector database',
metadata: { source: 'docs' }
},
{
text: 'Genkit is an AI framework',
metadata: { source: 'docs' }
},
];
await ai.index({ indexer, documents });export const indexerFlow = ai.defineFlow(
{
name: 'indexerFlow',
inputSchema: z.array(z.object({
text: z.string(),
metadata: z.record(z.any()).optional(),
})),
outputSchema: z.string(),
},
async (documents) => {
const indexer = weaviateIndexerRef({
collectionName: 'Documents',
});
await ai.index({ indexer, documents });
return 'Documents indexed successfully';
}
);The retriever searches for similar documents in a Weaviate collection based on vector similarity.
import { weaviateRetrieverRef } from 'genkitx-weaviate';
const retriever = weaviateRetrieverRef({
collectionName: 'Documents',
});
const results = await ai.retrieve({
retriever,
query: 'What is a vector database?',
options: {
k: 5, // Number of results to return
distance: 0.7, // Optional distance threshold
}
});export const retrieverFlow = ai.defineFlow(
{
name: 'retrieverFlow',
inputSchema: z.string(),
outputSchema: z.string(),
},
async (query) => {
const retriever = weaviateRetrieverRef({
collectionName: 'Documents',
});
const docs = await ai.retrieve({
retriever,
query,
options: { k: 5 }
});
const llmResponse = await ai.generate({
prompt: `Answer the question based on the following context:\n\nContext: ${docs.map(d => d.text).join('\n\n')}\n\nQuestion: ${query}`,
});
return llmResponse.text;
}
);You can find more examples in the examples folder.
- Easy Integration: Simple setup with Genkit
- Multiple Collections: Support for multiple Weaviate collections
- Flexible Configuration: Customizable collection settings
- Type Safety: Full TypeScript support
- Latest Weaviate Client: Uses weaviate-client v3.10.0
- Auto-Collection Creation: Automatically creates collections if they don't exist
- Metadata Support: Store and retrieve document metadata
- Distance Filtering: Filter results by similarity distance
- Batch Operations: Efficient batch insertion and retrieval
For detailed API documentation, please refer to the TypeScript definitions in the source code.
weaviate(params): Main plugin functionweaviateIndexerRef(params): Create an indexer referenceweaviateRetrieverRef(params): Create a retriever referenceWeaviateClientWrapper: Client wrapper class- Types:
WeaviatePluginParams,WeaviateClientParams,CollectionConfig
Want to contribute to the project? That's awesome! Head over to our Contribution Guidelines.
Note
This repository depends on Google's Genkit. For issues and questions related to Genkit, please refer to instructions available in Genkit's repository.
Reach out by opening a discussion on GitHub Discussions.
This plugin is proudly maintained by Xavier Portilla Edo.
This project is licensed under the Apache 2.0 License.