when I try to make my ai gain knowledge using txt , with convex and also langchain , but it always throws the error Type 'RunnableSequence<string, string>' is not assignable to type 'RunnableLike<any, any>'.
#24340
Replies: 1 comment
-
Hey there, @Ninoambaraa! I'm a bot designed to help you with bug fixes, answer questions, and guide you to become a contributor. I'm here to assist you while we wait for a human maintainer to address your issue. To resolve the error Here is an example of how you can use the from langchain_core.runnables import RunnableLambda, RunnableSequence, coerce_to_runnable
def add_one(x: int) -> int:
return x + 1
def mul_two(x: int) -> int:
return x * 2
# Coerce functions to Runnables
runnable_1 = coerce_to_runnable(add_one)
runnable_2 = coerce_to_runnable(mul_two)
# Create a RunnableSequence
sequence = RunnableSequence(runnable_1, runnable_2)
# Invoke the sequence
result = sequence.invoke(1)
print(result) # Should print 4 In your case, you need to ensure that the components in your import { v } from "convex/values";
import * as fs from "node:fs";
import { ChatOpenAI } from "@langchain/openai";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { BufferMemory } from "langchain/memory";
import { ConvexChatMessageHistory } from "langchain/stores/message/convex";
import { ConvexVectorStore } from "langchain/vectorstores/convex";
import { internalAction } from "./_generated/server";
import type { Document } from "@langchain/core/documents";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import {
RunnablePassthrough,
RunnableSequence,
coerce_to_runnable,
} from "@langchain/core/runnables";
import { StringOutputParser } from "langchain/dist/schema/output_parser";
const formatDocumentsAsString = (documents: Document[]) => {
return documents.map((document) => document.pageContent).join("\n\n");
};
const OPENAI_MODEL = "gpt-3.5-turbo";
export const answer = internalAction({
args: {
sessionId: v.string(),
message: v.string(),
},
handler: async (ctx, { sessionId, message }) => {
const text = fs.readFileSync("./convex/docs/software-engineer.txt", "utf8");
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
});
const docs = await textSplitter.createDocuments([text]);
const vectorStore = await ConvexVectorStore.fromDocuments(
docs,
new OpenAIEmbeddings(),
{ ctx }
);
const SYSTEM_TEMPLATE = `Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
----------------
{context}`;
const prompt = ChatPromptTemplate.fromMessages([
["system", SYSTEM_TEMPLATE],
["human", "{question}"],
]);
const model = new ChatOpenAI({
apiKey: 'adskdsdmksamdksm',
model: OPENAI_MODEL,
});
const chatHistory = new ConvexChatMessageHistory({ sessionId, ctx });
const memory = new BufferMemory({
chatHistory: chatHistory,
memoryKey: "chat_history",
outputKey: "text",
returnMessages: true,
});
const VectorStoreRetriever = vectorStore.asRetriever();
const chain = RunnableSequence.from([
coerce_to_runnable({
context: VectorStoreRetriever.pipe(formatDocumentsAsString),
question: new RunnablePassthrough(),
}),
prompt,
model,
new StringOutputParser(),
]);
const res = await chain.invoke("Hello");
console.log(res);
return res;
},
}); By using the |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I tried using langchain with convex and wanted to provide knowledge to my AI, but had a problem where I didn't know what kind of document to use or which one was better to use, so I chose to use txt, but got an error like this
RunnableSequence<string, string> ' is not assignable to type 'RunnableLike<any, any>
when trying to create a chain with runnableSequence , what should I do?System Info
platform Mac
"convex": "^1.5.1",
"convex-helpers": "^0.1.48",
"langchain": "^0.0.189",
"openai": "^4.19.0",
"@langchain/openai": "^0.2.2",
Beta Was this translation helpful? Give feedback.
All reactions