How to retrieve DB Embedding from We Aviate #5724
Unanswered
lakshya29154
asked this question in
Q&A
Replies: 1 comment
-
To retrieve similar embeddings from Weaviate using the provided code, you can use the Here is an example of how you can modify your code to use the import * as fs from "fs";
import { config } from 'dotenv';
import { createHash } from 'crypto';
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { OpenAIEmbeddings } from "@langchain/openai";
import weaviate, { ApiKey } from "weaviate-ts-client";
import { WeaviateStore } from "@langchain/weaviate";
import { OpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
config();
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const model = new OpenAI({ temperature: 0, apiKey: OPENAI_API_KEY });
const client = (weaviate as any).client({
scheme: process.env.WEAVIATE_SCHEME || "https",
host: "swagger-as4wyy7e.weaviate.network",
apiKey: new ApiKey('yEhKKW4MLmbvVJT2yw7mlTYutuz5zl22U2p1'),
});
const API_URL_PROMPT_TEMPLATE1 = `You are given the below the embedding fo some swagger Documentation:
{api_docs}
Using this embeddings, find the relevant API URLs there can be multiple url so give all the url step by step give the method also like get, post , put , delete and give the answer in step1 , step 2 like this.
You should build the API URLs in order to get a response that is as short as possible, while still getting the necessary information to answer the question. Pay attention to deliberately exclude any unnecessary pieces of data in the API call.
Question:{question}
API url:`;
const API_URL_PROMPT_TEMPLATE = /* #__PURE__ */ new PromptTemplate({
inputVariables: ["api_docs", "question"],
template: API_URL_PROMPT_TEMPLATE1,
});
async function generateHash(text) {
return createHash('sha256').update(text).digest('hex');
}
async function splitTextIntoChunks(text) {
const splitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000, chunkOverlap: 200 });
const documents = await splitter.createDocuments([text]);
return documents.map(doc => doc.pageContent);
}
async function storeEmbedding(textChunks) {
const hashes = textChunks.map(chunk => generateHash(chunk));
await WeaviateStore.fromTexts(
textChunks,
textChunks.map((chunk, index) => ({ hash: hashes[index] })),
new OpenAIEmbeddings(),
{
client,
indexName: "Test",
textKey: "text",
metadataKeys: ["hash"],
}
);
}
async function queryWeaviate(query) {
try {
const store = await WeaviateStore.fromExistingIndex(new OpenAIEmbeddings(), {
client,
indexName: "Test",
metadataKeys: ["hash"],
});
const results = await store.similaritySearch(query, 2);
console.log(results);
} catch (error) {
console.error("Error querying Weaviate:", error);
}
}
async function main() {
try {
const specFilePath = './swagger.json';
const jsonData = fs.readFileSync(specFilePath, 'utf8');
const textChunks = await splitTextIntoChunks(jsonData);
await storeEmbedding(textChunks);
console.log("Embedding has been stored successfully");
let query = "Can you give me the endpoint to place an order for a pet?";
await queryWeaviate(query);
} catch (error) {
console.error('Error:', error);
}
}
main(); In this example, the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I am getting error in retrieving similar embedding Can you please help how to get relevant embedding
System Info
all ok
Beta Was this translation helpful? Give feedback.
All reactions