Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Use ZhipuAiChatModel to input sensitive words #1079

Open
yongxingMa opened this issue May 10, 2024 · 2 comments · May be fixed by #1095
Open

[BUG] Use ZhipuAiChatModel to input sensitive words #1079

yongxingMa opened this issue May 10, 2024 · 2 comments · May be fixed by #1095
Labels
bug Something isn't working P3 Medium priority

Comments

@yongxingMa
Copy link

Describe the bug

Use ZhipuAiChatModel to input some sensitive words,error message not returned properly,Leads to a direct interruption of the programme, I would like to be able to return it normally via Response .

The error message is as follows:
status code: 400; body: {"error":{"code":"1301","message":"系统检测到输入或生成内容可能包含不安全或敏感内容,请您避免输入易产生敏感内容的提示语,感谢您的配合。"}}

Response<AiMessage> resMessage = zhipuAiChatModel.generate(asList(systemMessage,userMessage));

@yongxingMa yongxingMa added the bug Something isn't working label May 10, 2024
Copy link

dosubot bot commented May 10, 2024

To address the issue where ZhipuAiChatModel encounters sensitive words and returns a status code 400, leading to a program interruption, you can modify the error handling to manage this scenario gracefully. Specifically, you can adjust the onFailure method within your EventSourceListener to check for a status code of 400. When this error is detected, you can then log the error and create a custom AiMessage indicating the issue, which allows the program to continue running by returning a response rather than crashing.

Here's a snippet to guide you through handling a 400 error:

@Override
public void onFailure(EventSource eventSource, Throwable t, okhttp3.Response response) {
    if (response != null && response.code() == 400) {
        try {
            String responseBody = response.body().string();
            log.error("Encountered sensitive words: {}", responseBody);
            AiMessage errorMessage = AiMessage.from("Encountered sensitive words. Please adjust your query.");
            Response<AiMessage> errorResponse = Response.from(errorMessage, null, FinishReason.ERROR);
            handler.onComplete(errorResponse);
        } catch (IOException e) {
            log.error("Error handling sensitive words response", e);
            handler.onError(new RuntimeException("Failed to handle sensitive words error.", e));
        }
    } else if (t != null) {
        handler.onError(t);
    } else {
        handler.onError(new RuntimeException(String.format("Unexpected error: status code: %s; body: %s", response.code(), response.body())));
    }
}

This approach ensures that when sensitive words are detected, resulting in a 400 error, your application can handle the situation more elegantly by logging the error and notifying the user through a custom message, thus avoiding an abrupt termination of the program.

Details

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.

To continue the conversation, mention @dosu.

@langchain4j
Copy link
Owner

cc @1402564807

@langchain4j langchain4j added the P3 Medium priority label May 10, 2024
@1402564807 1402564807 linked a pull request May 12, 2024 that will close this issue
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P3 Medium priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants