Langchain 未部署到 Firebase 功能

问题描述 投票:0回答:1

我正在尝试使用 langchain 将此函数部署到 firebase 函数,但在部署它时我不断收到此错误。

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

这是我的 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"]
}

我的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
}

这是我的 firebase 函数的代码。预计使用langchain触发AI,然后将AI消息发送回用户。

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();
  }
);

更新 更改为 Typescript 5 但得到了新错误的奖励

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

知道为什么会发生这种情况吗?我正在使用langchain“^0.1.18”。预先感谢!

node.js typescript google-cloud-functions es6-modules langchain
1个回答
1
投票

今天也遇到同样的问题。尝试将您的打字稿版本至少提高到 5。这是来自 langchainjs github 的相关问题:https://github.com/langchain-ai/langchainjs/issues/4043.

© www.soinside.com 2019 - 2024. All rights reserved.