langchain-ai / langchainjs

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

AzureOpenAI Content Filter Error cause unhandled error #2346

Closed xMHW closed 9 months ago

xMHW commented 1 year ago
api:dev:         return new axios_1.default.AxiosError(message, axios_1.default.AxiosError[code], config, request, response);
api:dev:                ^
api:dev: AxiosError: Request failed with status code 400 and body {"error":{"message":"The response was filtered due to the prompt triggering Azure OpenAI’s content management policy. Please modify your prompt and retry. To learn more about our content filtering policies please read our documentation: https://go.microsoft.com/fwlink/?linkid=2198766","type":null,"param":"prompt","code":"content_filter","status":400}}
api:dev:     at createError (/Users/hwmin414/**/node_modules/.pnpm/langchain@0.0.131_@aws-sdk+client-s3@3.382.0_@supabase+supabase-js@2.31.0_axios@1.4.0_cheerio_esyesgfxlqlyse2qn4tpgidyoy/node_modules/langchain/dist/util/axios-fetch-adapter.cjs:356:16)
api:dev:     at settle (/Users/hwmin414/**/node_modules/.pnpm/langchain@0.0.131_@aws-sdk+client-s3@3.382.0_@supabase+supabase-js@2.31.0_axios@1.4.0_cheerio_esyesgfxlqlyse2qn4tpgidyoy/node_modules/langchain/dist/util/axios-fetch-adapter.cjs:38:16)
api:dev:     at /Users/hwmin414/**/node_modules/.pnpm/langchain@0.0.131_@aws-sdk+client-s3@3.382.0_@supabase+supabase-js@2.31.0_axios@1.4.0_cheerio_esyesgfxlqlyse2qn4tpgidyoy/node_modules/langchain/dist/util/axios-fetch-adapter.cjs:190:19
api:dev:     at new Promise (<anonymous>)
api:dev:     at fetchAdapter (/Users/hwmin414/**/node_modules/.pnpm/langchain@0.0.131_@aws-sdk+client-s3@3.382.0_@supabase+supabase-js@2.31.0_axios@1.4.0_cheerio_esyesgfxlqlyse2qn4tpgidyoy/node_modules/langchain/dist/util/axios-fetch-adapter.cjs:182:12)
api:dev:     at processTicksAndRejections (node:internal/process/task_queues:95:5)
api:dev:     at RetryOperation._fn (/Users/hwmin414/**/node_modules/.pnpm/p-retry@4.6.2/node_modules/p-retry/index.js:50:12)

Langchain version 0.0.131 OpenAI version 3.3.0

AzureOpenAI Chat model keeps faults with the above no matter how I wrap call functions with error handlers(try catch, promise.catch, ...) It keeps causing my NestJS process to die.

I have no clue where to debug this. Just don't recommend me to try with other input(I knew it worked well for the other cases). I need help with how I can catch this error.

dosubot[bot] commented 1 year ago

🤖

Hello,

I understand that you're having trouble with the "AzureOpenAI Content Filter Error" causing your NestJS process to terminate. This error is likely being thrown because the input to the OpenAI API is triggering Azure's content management policy.

To catch and handle this error, you can use a try-catch block around the function that is causing the error. In the LangChain codebase, it seems like the error might be coming from the completionWithRetry function in the ChatOpenAI class.

Here's how you can modify the completionWithRetry function to catch and handle the error:

async completionWithRetry(
  request: CreateChatCompletionRequest,
  options?: StreamingAxiosConfiguration
) {
  try {
    // existing code...
    return this.caller
      .call(
        this.client.createChatCompletion.bind(this.client),
        request,
        axiosOptions
      )
      .then((res) => res.data);
  } catch (error) {
    // Handle the error here
    console.error("Caught AzureOpenAI Content Filter Error: ", error);
    // You can also re-throw the error if you want it to be handled further up the call stack
    throw error;
  }
}

This modification will catch the error and log it to the console. You can replace the console.error line with your own error handling logic. If you want the error to be handled further up the call stack, you can re-throw the error after logging it.

Please let me know if this helps or if you need further assistance.

Sources


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.

xMHW commented 1 year ago
  request: CreateChatCompletionRequest,
  options?: StreamingAxiosConfiguration
) {
    // existing code...
    return this.caller
      .call(
        this.client.createChatCompletion.bind(this.client),
        request,
        axiosOptions
      )
      .then((res) => res.data)
      .catch(e => {
        console.error("Caught AzureOpenAI Content Filter Error: ", error);
        throw e;
      });
}

Actually changing openai-chat.ts to above, does catch the error, but If I don't throw this error back then, the logic stops there and nothing happens at all. So I need to know how I can properly wrap that error so that the SDK consumer can properly handle that error only once. Currently, handleLLMError callback can catch that content-filter error. But even after handling that error with handleLLMError callback, the process faults with the same Axios Error again.

jacoblee93 commented 1 year ago

This will be hopefully resolved when we integrate the new OpenAI SDK (#2389)

xMHW commented 1 year ago
Error: The response was filtered due to the prompt triggering Azure OpenAI’s content management policy. Please modify your prompt and retry. To learn more about our content filtering policies please read our documentation: https://go.microsoft.com/fwlink/?linkid=2198766
at Function.generate (/Users/**/node_modules/.pnpm/openai@4.4.0/node_modules/openai/src/error.ts:75:6)
at OpenAI.makeStatusError (/Users/**/node_modules/.pnpm/openai@4.4.0/node_modules/openai/src/core.ts:444:21)
at OpenAI.makeRequest (/Users/**/node_modules/.pnpm/openai@4.4.0/node_modules/openai/src/core.ts:494:17)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at /Users/**/node_modules/.pnpm/langchain@0.0.142_@aws-sdk+client-s3@3.382.0_@supabase+supabase-js@2.31.0_axios@1.4.0_cheerio_onpdrrau34ml5jctkywfkc6pgq/node_modules/langchain/dist/chat_models/openai.cjs:516:29
at RetryOperation._fn (/Users/**/node_modules/.pnpm/p-retry@4.6.2/node_modules/p-retry/index.js:50:12)

Version upgrade didn't fixed it IMO..

Tested on new version https://github.com/hwchase17/langchainjs/releases/tag/0.0.142 And this version has bug with Chat model of OpenAI raise error when called with azure openai key option.(I solved it through patch and performed test which results above) @jacoblee93

dosubot[bot] commented 9 months ago

Hi, @xMHW,

I'm helping the langchainjs team manage their backlog and am marking this issue as stale. From what I understand, you raised an issue regarding a content filtering error caused by Azure OpenAI, leading to a NestJS process crash. There were attempts to catch the error using a try-catch block, and modifications were suggested to the completionWithRetry function. It was also mentioned that the issue might be resolved when integrating the new OpenAI SDK (#2389), but testing the new version still resulted in the error persisting.

Could you please confirm if this issue is still relevant to the latest version of the langchainjs repository? If it is, kindly let the langchainjs team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days. Thank you!

alexechoi commented 3 weeks ago

Hi @xMHW did you find any method to overcome this issue?

xMHW commented 3 weeks ago

@alexechoi I'm afraid I don't. Whenever this happens, we changed the input values or used other provider.