WeAviate fetch similar vector #5656
Unanswered
lakshya29154
asked this question in
Q&A
Replies: 1 comment 5 replies
-
To properly query Weaviate to retrieve similar embeddings and send them to OpenAI, you can follow these steps:
Here is an example code snippet that demonstrates these steps: import weaviate, { ApiKey } from "weaviate-ts-client";
import { OpenAIEmbeddings, OpenAI } from "@langchain/openai";
import { SelfQueryRetriever } from "langchain/retrievers/self_query";
import { WeaviateStore, WeaviateTranslator } from "@langchain/weaviate";
import { Document } from "@langchain/core/documents";
import { AttributeInfo } from "langchain/chains/query_constructor";
// Create documents
const docs = [
new Document({
pageContent: "A bunch of scientists bring back dinosaurs and mayhem breaks loose",
metadata: { year: 1993, rating: 7.7, genre: "science fiction" },
}),
new Document({
pageContent: "Leo DiCaprio gets lost in a dream within a dream within a dream within a ...",
metadata: { year: 2010, director: "Christopher Nolan", rating: 8.2 },
}),
new Document({
pageContent: "A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea",
metadata: { year: 2006, director: "Satoshi Kon", rating: 8.6 },
}),
new Document({
pageContent: "A bunch of normal-sized women are supremely wholesome and some men pine after them",
metadata: { year: 9.3, director: "Greta Gerwig", rating: 8.3 },
}),
new Document({
pageContent: "Toys come alive and have a blast doing so",
metadata: { year: 1995, genre: "animated" },
}),
new Document({
pageContent: "Three men walk into the Zone, three men walk out of the Zone",
metadata: {
year: 1979,
director: "Andrei Tarkovsky",
genre: "science fiction",
rating: 9.9,
},
}),
];
// Define attributes
const attributeInfo: AttributeInfo[] = [
{ name: "genre", description: "The genre of the movie", type: "string or array of strings" },
{ name: "year", description: "The year the movie was released", type: "number" },
{ name: "director", description: "The director of the movie", type: "string" },
{ name: "rating", description: "The rating of the movie (1-10)", type: "number" },
{ name: "length", description: "The length of the movie in minutes", type: "number" },
];
// Instantiate vector store
const embeddings = new OpenAIEmbeddings();
const llm = new OpenAI({ modelName: "gpt-3.5-turbo" });
const client = (weaviate as any).client({
scheme: process.env.WEAVIATE_SCHEME || "https",
host: process.env.WEAVIATE_HOST || "localhost",
apiKey: new ApiKey(process.env.WEAVIATE_API_KEY || "default"),
});
const vectorStore = await WeaviateStore.fromDocuments(docs, embeddings, {
client,
indexName: "Test",
textKey: "text",
metadataKeys: ["year", "director", "rating", "genre"],
});
// Create SelfQueryRetriever
const selfQueryRetriever = SelfQueryRetriever.fromLLM({
llm,
vectorStore,
documentContents: "Brief summary of a movie",
attributeInfo,
structuredQueryTranslator: new WeaviateTranslator(),
});
// Query the vector store
const query1 = await selfQueryRetriever.getRelevantDocuments("Which movies are rated higher than 8.5?");
const query2 = await selfQueryRetriever.getRelevantDocuments("Which movies are directed by Greta Gerwig?");
console.log(query1, query2); This code sets up the documents, defines the attributes, initializes the vector store, creates a |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checked other resources
Commit to Help
Example Code
Description
In this code I have stored my data in weaviate now I want to fetch the similar embedding I dont know how to query it is not giving similar embedding how to get it so I can send it to openAI
System Info
all good
Beta Was this translation helpful? Give feedback.
All reactions