Reputation: 3176
I'm trying to fetch data from my qdrant vector collection but whatever I ask it it just responses something in the line of: "The context provided does not contain any information or content to share. Therefore, I cannot provide any details or answer your question."
I'm really not sure what I'm missing here but I know the following:
Is RunnableSequence the correct thing to use here or is it better to .pipe() requests? How does that work? Any code examples on how to get this to work is VERY much appreciated. 🙏
import { QdrantVectorStore } from "@langchain/qdrant";
import { z } from "zod";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatOpenAI } from "@langchain/openai";
import { embeddings } from "../clients/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { SelfQueryRetriever } from "langchain/retrievers/self_query";
import { QdrantTranslator } from "@langchain/community/structured_query/qdrant";
import type { Document } from "@langchain/core/documents";
import {
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
const completionSchema = z.object({
response: z.string().describe("The completion that was generated"),
profitSentiment: z
.enum(["positive", "negative"])
.describe(
"A string representing if the profit was positive or negative compared to the last report released. "
),
profitExplanation: z
.string()
.describe(
"An explanation of the profit compared to the last report released. "
),
source: z
.string()
.describe("The document that was used to generate the completion"),
});
const attributeInfo = [
{
name: "chunk_index",
description: "a index for the chunk",
type: "number",
},
{
name: "company_id",
description: "A id referencing the company",
type: "number",
},
{
name: "company_ticker",
description: "The stock ticker of the company",
type: "string",
},
{
name: "created_at",
description: "When the piece of content was created",
type: "a date string",
},
{
name: "document_type_id",
description: "a id referencing the document type",
type: "number",
},
{
name: "file_name",
description: "The name of the file that was the source of the information",
type: "string",
},
{
name: "timespan_end",
description:
"a timestamp indicating the end of the period in time where the information is relevant",
type: "a date string",
},
{
name: "timespan_start",
description:
"a timestamp indicating the start of the period in time where the information is relevant",
type: "a date string",
},
];
export const runCompletions = async () => {
const vectorStore = await QdrantVectorStore.fromExistingCollection(
embeddings,
{
url: process.env.QDRANT_API_ENDPOINT!,
collectionName: "documents_yearly_reports",
}
);
const prompt = ChatPromptTemplate.fromTemplate(`
Answer the question based only on the context provided.
Context: {context}
Question: {question}
`);
const formatDocs = (docs: Document[]) => {
console.log({ docs });
return docs.map((doc) => JSON.stringify(doc)).join("\n\n");
};
const llm = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0,
});
const selfQueryRetriever = SelfQueryRetriever.fromLLM({
llm,
vectorStore,
documentContents:
"Information about yearly reports from stock traded companies in sweden",
attributeInfo,
structuredQueryTranslator: new QdrantTranslator(),
searchParams: {
filter: {
must: [
{
key: "metadata.company_id",
match: {
value: 435,
},
},
],
},
},
});
const ragChain = RunnableSequence.from([
{
context: selfQueryRetriever.pipe(formatDocs),
question: new RunnablePassthrough(),
},
prompt,
llm,
new StringOutputParser(),
]);
const response = await ragChain.invoke("tell me everything!");
console.log({ response });
};
## Packages used
"@langchain/community": "^0.3.1",
"@langchain/core": "^0.3.1",
"@langchain/openai": "^0.3.0",
"@langchain/qdrant": "^0.1.0",
"@langchain/textsplitters": "^0.1.0",
"@qdrant/js-client-rest": "^1.11.0",
"@mozilla/readability": "^0.5.0",
"langchain": "^0.3.2",
I've tried for 2-3 days to get this to work. All the examples I can find creates the documents needed and then use them but I already have the documents in the qdrant DB so I dont know how to use them I guess.
Upvotes: 0
Views: 72