I am trying to deploy this function using langchain to firebase functions but I keep getting this error when I deploy it.
node_modules/@langchain/openai/node_modules/@langchain/core/dist/language_models/chat_models.d.ts:64:129 - error TS2304: Cannot find name 'this'.
64 _generateCached({ messages, cache, llmStringKey, parsedOptions, handledOptions, }: ChatModelGenerateCachedParameters<typeof this>): Promise<LLMResult & {
~~~~
node_modules/@langchain/openai/node_modules/@langchain/core/dist/language_models/llms.d.ts:66:122 - error TS2304: Cannot find name 'this'.
66 _generateCached({ prompts, cache, llmStringKey, parsedOptions, handledOptions, }: LLMGenerateCachedParameters<typeof this>): Promise<LLMResult & {
~~~~
src/index.ts:36:31 - error TS2345: Argument of type 'ChatOpenAI<ChatOpenAICallOptions>' is not assignable to parameter of type 'RunnableLike<ChatPromptValueInterface, BaseMessageChunk>'.
Type 'ChatOpenAI<ChatOpenAICallOptions>' is missing the following properties from type 'RunnableMapLike<ChatPromptValueInterface, BaseMessageChunk>': concat, text, content, additional_kwargs, and 3 more.
36 const chain = prompt.pipe(chatModel).pipe(outputParser);
~~~~~~~~~
Found 3 errors in 3 files.
Errors Files
1 node_modules/@langchain/openai/node_modules/@langchain/core/dist/language_models/chat_models.d.ts:64
1 node_modules/@langchain/openai/node_modules/@langchain/core/dist/language_models/llms.d.ts:66
1 src/index.ts:36
Error: functions predeploy error: Command terminated with non-zero exit code 2
This is my tsconfig.json
{
"compilerOptions": {
"module": "NodeNext",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "ES2020",
"esModuleInterop": true,
"moduleResolution": "node"
},
"compileOnSave": true,
"include": ["src"]
}
My package.json
{
"name": "functions",
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "20"
},
"main": "lib/index.js",
"dependencies": {
"@langchain/openai": "^0.0.14",
"firebase-admin": "^11.8.0",
"firebase-functions": "^4.3.1",
"langchain": "^0.1.18",
"stream-chat": "^8.16.0"
},
"overrides": {
"@langchain/core": "0.1.5"
},
"devDependencies": {
"firebase-functions-test": "^3.1.0",
"typescript": "^4.9.0"
},
"private": true
}
This is the code for my firebase function. It is expected to trigger the AI using langchain and then send the AI message back to the user.
import { onRequest } from "firebase-functions/v2/https";
import * as logger from "firebase-functions/logger";
import { StreamChat } from "stream-chat";
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
export const myFunction = onRequest(
{ timeoutSeconds: 60, region: ["europe-west2"] },
(request, response) => {
const client = StreamChat.getInstance(
"api_key",
"api_secret"
);
const signature = request.headers["x-signature"] as string;
const valid = client.verifyWebhook(request.rawBody, signature);
if (!valid) response.status(204).end();
const chatModel = new ChatOpenAI({
openAIApiKey: "openai_key",
});
const outputParser = new StringOutputParser();
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant",
],
["user", "{input}"],
]);
const chain = prompt.pipe(chatModel).pipe(outputParser);
if (request.body.user.id !== "system_user") {
chain.invoke({ input: request.body.message.text }).then((aiRes) => {
const channel = client.channel(
"messaging",
"channel_id"
);
return channel.sendMessage({
user_id: "my-app",
text: aiRes,
});
});
}
response.status(200).end();
}
);
Update Changed to typescript 5 but got rewarded with new errors
node_modules/@langchain/core/dist/utils/stream.d.ts:1:18 - error TS2320: Interface 'IterableReadableStreamInterface<T>' cannot simultaneously extend types 'ReadableStream<T>' and 'AsyncIterable<T>'.
Named property '[Symbol.asyncIterator]' of types 'ReadableStream<T>' and 'AsyncIterable<T>' are not identical.
1 export interface IterableReadableStreamInterface<T> extends ReadableStream<T>, AsyncIterable<T> {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/@langchain/core/dist/utils/stream.d.ts:9:5 - error TS2425: Class 'ReadableStream<T>' defines instance member property '[Symbol.asyncIterator]', but extended class 'IterableReadableStream<T>' defines it as instance member function.
9 [Symbol.asyncIterator](): this;
~~~~~~~~~~~~~~~~~~~~~~
Found 2 errors in the same file, starting at: node_modules/@langchain/core/dist/utils/stream.d.ts:1
Error: functions predeploy error: Command terminated with non-zero exit code 2
Any idea why this is happening? I am using langchain "^0.1.18". Thanks in advance!
After a long ordeal, this finally worked for me. Thank you Ilya Tkachou for the tip on upgrading to Typescript 5. That partially solved the problem. Another very important thing was to add
"esModuleInterop": true
to tsconfig.json. I also had to downgrade from Node 20 to Node 18.
Sometimes you need to restart your IDE for the changes to take effect.