Skip to content

In Spring AI, if a functioncall's return value doesn't match its description, it can trigger an infinite loop by repeatedly accumulating and calling the large model interface with the incorrect return value. #34534

@leizhonghao

Description

@leizhonghao

Spring AI defines a "functioncall". When the return value is inconsistent with the description, for example, if it's defined to "get the current date" but returns "我是一串中文" (which means "I am a string of Chinese" in English), the framework will repeatedly accumulate "我是一串中文" in the request, causing the large model interface to trigger an infinite loop. It will keep calling the large model interface repeatedly.

request:
http://localhost:8080/chat5?userInput=what day is tomorrow

functioncall:
`public class FunctionCallToolUtil {
public static final String DATETIME_PATTERN_M2 = "yyyy/MM/dd HH:mm";
@tool(description = "get current date")
public String getCurrentDateTime() {
// Date date = new Date();
// DateFormat dateFormat = new SimpleDateFormat(DATETIME_PATTERN_M2);
// return dateFormat.format(date);

    return "我是一串中文";
}

}
`

controller:
` @GetMapping("/chat5")
ChatDataDto functionCall(@RequestParam("userInput") String userInput) {

    String content = chatClient.prompt("与用户沟通")
            .user(userInput)
            .tools(new FunctionCallToolUtil())
            .call()
            .content();

    ChatDataDto chatDataDto = new ChatDataDto();
    chatDataDto.setText(content);
    return chatDataDto;
}

`

process:

Image

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions