langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.43k stars 2.1k forks source link

Attempt to connect to Azure openai through @langchain/azure-openai failed #5030

Closed racso-dev closed 5 days ago

racso-dev commented 6 months ago

Checked other resources

Example Code

const { AzureOpenAI } = require("@langchain/azure-openai");
const { HumanMessage } = require("@langchain/core/messages");

const model = new AzureOpenAI({
  azureOpenAIEndpoint: 'https://someendpoint.openai.azure.com/',
  azureOpenAIApiKey: 'somekey',
  azureOpenAIApiDeploymentName: 'gpt-4-32k',
  modelName: 'gpt-4-32k'
});

async function doSomething() {
  try {
    console.log(new HumanMessage("Hello world!"));
    const response = await model.invoke(new HumanMessage("Hello world!"));
    console.log(response);
    return response

  } catch (error) {
    console.log(error);
    throw error;
  }
}

module.exports = {
  doSomething
};

Error Message and Stack Trace (if applicable)

"error":{"message":"[object Object]","stack":"Error: [object Object]\n at /app/node_modules/.pnpm/@langchain+core@0.1.27/node_modules/@langchain/core/dist/utils/async_caller.cjs:101:23\n at processTicksAndRejections (node:internal/process/task_queues:95:5)\n at RetryOperation._fn (/app/node_modules/.pnpm/p-retry@4.6.2/node_modules/p-retry/index.js:50:12)"}

Description

It's a duplicate of 4955 except that I'm using import { AzureOpenAI } from '@langchain/azure-openai' instead of import { AzureChatOpenAI } from '@langchain/azure-openai'

As a quick fix I'm using AzureChatOpenAI but just creating the issue so you can fix both

System Info

yarn info v1.22.19

{
  time: {
    '0.0.1': '2020-01-03T03:05:43.024Z',
    created: '2021-06-30T09:29:32.055Z',
    '0.1.1': '2021-06-30T09:29:32.190Z',
    modified: '2022-05-10T12:54:14.830Z',
    '0.1.2': '2021-07-01T08:07:28.558Z',
    '0.1.3': '2021-07-01T12:21:21.837Z',
    '0.1.4': '2021-07-07T14:16:33.407Z',
    '0.1.5': '2021-10-01T14:22:16.935Z'
  },
  name: 'nestjs-starter',
  'dist-tags': {
    latest: '0.1.5'
  },
  versions: [
    '0.1.2',
    '0.1.3',
    '0.1.4',
    '0.1.5'
  ],
  maintainers: [
    {
      name: 'pktai.iot',
      email: 'js.phkht@gmail.com'
    }
  ],
  description: 'Get started for beginners with Nestjs',
  keywords: [
    'nest',
    'nest starter',
    'nestjs starter',
    'nestjs-starter',
    'typescript nest'
  ],
  author: {
    name: 'TaiPham',
    url: 'https://github.com/pktai'
  },
  license: 'MIT',
  readmeFilename: 'README.md',
  homepage: 'https://github.com/pktai/nestjs-starter#readme',
  repository: {
    type: 'git',
    url: 'git+https://github.com/pktai/nestjs-starter.git'
  },
  contributors: [
    {
      name: 'TaiPham',
      url: 'https://github.com/pktai'
    }
  ],
  bugs: {
    url: 'https://github.com/pktai/nestjs-starter/issues'
  },
  version: '0.1.5',
  scripts: {
    'check-types': 'tsc',
    build: 'nest build',
    format: 'prettier --write "src/**/*.ts" "tests/**/*.ts"',
    start: 'nest start',
    'start:dev': 'nest start --watch',
    'start:debug': 'nest start --debug --watch',
    'start:prod': 'node dist/src/main.js',
    lint: 'eslint "{src,scripts,test}/**/*.ts" --fix',
    test: 'jest',
    'test:watch': 'jest --watch',
    'test:cov': 'jest --coverage',
    'test:debug': 'node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand',
    'test:e2e': 'jest --config ./tests/jest-e2e.json',
    prebuild: 'rimraf dist',
    prepare: 'husky install',
    'pre-commit-lint': 'yarn check-types && yarn lint-staged && yarn lint',
    'db:migrate': 'sequelize db:migrate',
    'db:migrate:undo': 'sequelize db:migrate:undo',
    'db:seed': 'sequelize db:seed:all',
    'heroku-postbuild': 'yarn build'
  },
  dependencies: {
    '@nestjs/common': '~7.6.18',
    '@nestjs/config': '~0.6.3',
    '@nestjs/core': '~7.6.18',
    '@nestjs/jwt': '~7.2.0',
    '@nestjs/mongoose': '^8.0.1',
    '@nestjs/passport': '~7.1.5',
    '@nestjs/platform-express': '~7.6.18',
    '@nestjs/platform-fastify': '~7.6.18',
    '@nestjs/platform-socket.io': '~8.0.8',
    '@nestjs/sequelize': '~0.2.0',
    '@nestjs/swagger': '~4.8.0',
    '@nestjs/websockets': '~8.0.8',
    '@types/bluebird': '~3.5.35',
    '@types/cls-hooked': '~4.3.3',
    '@types/validator': '~13.1.4',
    'class-transformer': '~0.4.0',
    'class-validator': '~0.13.1',
    'cls-hooked': '~4.2.2',
    'cookie-parser': '~1.4.5',
    dotenv: '~10.0.0',
    'fastify-helmet': '~5.3.1',
    'fastify-rate-limit': '~5.5.0',
    'fastify-swagger': '~4.7.0',
    lodash: '~4.17.21',
    mongoose: '^6.0.3',
    passport: '~0.4.1',
    'passport-jwt': '~4.0.0',
    pg: '~8.6.0',
    'reflect-metadata': '~0.1.13',
    sequelize: '~6.6.2',
    'sequelize-typescript': '~2.1.0',
    'socket.io': '~4.2.0'
  },
  devDependencies: {
    '@commitlint/cli': '~12.1.4',
    '@commitlint/config-conventional': '~12.1.4',
    '@nestjs/cli': '~7.6.0',
    '@nestjs/schematics': '~7.3.1',
    '@nestjs/testing': '~7.6.18',
    '@types/cookie-parser': '~1.4.2',
    '@types/express': '~4.17.12',
    '@types/jest': '~26.0.23',
    '@types/node': '~15.12.2',
    '@types/passport-jwt': '~3.0.5',
    '@types/sequelize': '~4.28.9',
    '@types/supertest': '~2.0.11',
    '@typescript-eslint/eslint-plugin': '~4.27.0',
    '@typescript-eslint/parser': '~4.27.0',
    axios: '~0.21.1',
    commitlint: '~12.1.4',
    'cross-env': '~7.0.3',
    eslint: '~7.28.0',
    'eslint-config-prettier': '~8.3.0',
    'eslint-plugin-import': '~2.23.4',
    'eslint-plugin-prettier': '~3.4.0',
    husky: '~6.0.0',
    jest: '~27.0.4',
    'lint-staged': '~11.0.0',
    prettier: '~2.3.1',
    rimraf: '~3.0.2',
    'sequelize-cli': '~6.2.0',
    superagent: '~6.1.0',
    supertest: '~6.1.3',
    'ts-jest': '~27.0.3',
    'ts-loader': '~9.2.3',
    'ts-node': '~10.0.0',
    'tsconfig-paths': '~3.9.0',
    typescript: '~4.3.4'
  },
  jest: {
    moduleFileExtensions: [
      'js',
      'json',
      'ts'
    ],
    rootDir: '.',
    testMatch: [
      '**/tests/**/*.e2e-spec.ts'
    ],
    transform: {
      '^.+\\.(t|j)s$': 'ts-jest'
    },
    coverageDirectory: '../coverage',
    testEnvironment: 'node'
  },
  'lint-staged': {
    '*.{js,ts}': [
      'cross-env NODE_ENV=development eslint --cache'
    ],
    '{*.json,.{babelrc,eslintrc,prettierrc}}': [
      'prettier --ignore-path .eslintignore --parser json --write'
    ]
  },
  gitHead: 'c9f49738f1ef9c7ff6da0955ce391038b510fd52',
  dist: {
    integrity: 'sha512-vXQemRjozOaIne1SwPByov16vnddOEYXU1vML3cK9DGu4+OiV/idGcdn0FyqYBvNZpq5XBKMaGI3HO2RkKdMKQ==',
    shasum: '4b67a0c104333a09c7c9ed5e19371dfbcc269b4d',
    tarball: 'https://registry.npmjs.org/nestjs-starter/-/nestjs-starter-0.1.5.tgz',
    fileCount: 99,
    unpackedSize: 89069,
    'npm-signature': '-----BEGIN PGP SIGNATURE-----\r\n' +
      'Version: OpenPGP.js v3.0.13\r\n' +
      'Comment: https://openpgpjs.org\r\n' +
      '\r\n' +
      'wsFcBAEBCAAQBQJh20W6CRA9TVsSAnZWagAAeKsP/iKO2BKgCBISqLAbRo4V\n' +
      'GMmpZbUy5hde7Xy7pE8qXqgFhQbLZyoNt2T7Q6Ad05UYM2Tb96+uZ9l3jPA4\n' +
      'ccHqy0goowQ+EXrVK7QUOPRfY9pNS2VfOPxoR9/TBaGgEUhPpkGy77fQPcuO\n' +
      'Nnv/l83G2djtqmMzdJwvF8QaMKiCARpR2uZnk1bePVpz2DktSvfg/OerChGZ\n' +
      'KcP/B8P0xeFxySJ+N6tmLETe2C6cTxbBuvQorIAp1lpEzM912eLRHYsCRcq7\n' +
      'etW2bZF+/Nm5Hj5QhAyXDqBw/kJX3VtrktQihTFB/McSo8y4QRV3FSw5lmpF\n' +
      '42jFxEf8zEyIIY4axKnt9eZ4etKbcofqlhUbZSTJKL5cv64hy6UdQjhmFNeW\n' +
      'yCMFPdp43B6LFGAVYxboWoV7y1Lg/TDf7F2d1LnTnpnDmBzzv05nQq3WbsyP\n' +
      'M4c5xv2PbL9+dHFxM5XbJcmrz8+mMORhiT6w/GmXpH3DxzdrQrcZGGQsEYpn\n' +
      'vG3ndMnUHMB/Ne3mfo+iBEhx8vus+bjZBeTnA2duMAalfJ6uH0pdNm2NyHE2\n' +
      'dcPD1Ceu0L5UVWW5OKoBKEWRftGyFKmvDzDmEzivvdOOl1mYZ15PQBw3SZXR\n' +
      'MGu2Z2GP9KuXwXKRVEYtdNuCPeIOBGi64snKuITVS8Kiw4ErvAEYA+omzy2I\n' +
      'lAjW\r\n' +
      '=mgc+\r\n' +
      '-----END PGP SIGNATURE-----\r\n',
    signatures: [
      {
        keyid: 'SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA',
        sig: 'MEYCIQCJUuJRjNWvZuZKNkeLuwFmvgQNvCNqV9w+u+kKbFt3jwIhAJs1uScDJNq4AawRvOWymzn/7aYiovKjmlbztALH9yPc'
      }
    ]
  },
  directories: {}
}

platform: ubuntu 22.04 node version : v20.12.1 pnpm version : 8.15.6

dosubot[bot] commented 6 months ago

Thank you for providing detailed information about the issue you're facing. Based on the error message you've shared, it seems like the error is being thrown from the async_caller.cjs file in the @langchain/core package. However, without more specific information about the error object, it's difficult to pinpoint the exact cause of the issue.

From the context provided, it appears that there have been similar issues reported in the past. For instance, issue #4955 seems to be a duplicate of your issue but with the use of AzureChatOpenAI instead of AzureOpenAI. This issue was resolved by a pull request: azure-openai[patch]: fix azureOpenAIApiKey not working. It might be worth checking if the changes made in this pull request are applicable to your case.

Additionally, there are a few other closed issues that might be relevant:

  1. AzureOpenAI Content Filter Error cause unhandled error: This issue suggests that the problem might be resolved when integrating the new OpenAI SDK.

  2. Azure OpenAI call gets Bad Authorization header: This issue suggests ensuring that the AZURE_OPENAI_API_KEY and other Azure OpenAI related environment variables are correctly set in your environment or passed in the fields object when creating a new ChatOpenAI instance.

  3. Langchain and Azure ChatOpenAI is not working: This issue suggests setting the environment variables directly.

Given the information you've provided, I would recommend the following steps:

  1. Double-check your environment variables and the parameters you're passing to the AzureOpenAI constructor. Make sure they match the expected format and type as described in the LangChain.js documentation.

  2. Try to get more information about the error object. The current error message is not very informative as it only shows [object Object]. You could try logging error.message or JSON.stringify(error) to get more details about the error.

  3. If the error persists, please provide more information about the error object and any other relevant details. This will help us better understand the issue and provide a more accurate solution.

I hope this helps! If you have any other questions or need further clarification, feel free to ask. Dosu

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
LakshK7 commented 5 months ago

Any updates on this? Facing the same issue With error - Error: [object Object]      at /app/server/node_modules/@langchain/core/dist/utils/async_caller.cjs:98:23     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)     at async RetryOperation._fn (/app/server/node_modules/p-retry/index.js:50:12) {    attemptNumber: 7,    retriesLeft: 0 }

On using both AzureOpenAI as well as AzureChatOpenAI. Already tried the fix suggested in #4955 but that did not fix the issue

racso-dev commented 4 months ago

Up

richardeee commented 4 months ago

I'm facing the same error.

When I use @langchain/openai, the error is:

Error: OpenAI or Azure OpenAI API key or Token Provider not found
    at new ChatOpenAI (/Users/zouang/code/LangchainJSTest/node_modules/@langchain/openai/dist/chat_models.cjs:426:19)
    at new AzureChatOpenAI (/Users/zouang/code/LangchainJSTest/node_modules/@langchain/openai/dist/azure/chat_models.cjs:25:9)
    at Object.<anonymous> (/Users/zouang/code/LangchainJSTest/index.js:13:19)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49

When I use @langchain/azure-openai, the error is:

The sample encountered an error: Error: [object Object]
    at /Users/zouang/code/LangchainJSTest/node_modules/@langchain/core/dist/utils/async_caller.cjs:98:23
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async RetryOperation._fn (/Users/zouang/code/LangchainJSTest/node_modules/p-retry/index.js:50:12) {
  attemptNumber: 7,
  retriesLeft: 0
racso-dev commented 3 months ago

up