@@ -61,23 +61,33 @@ public FunctionResult functionCall(String question, ResultFormat resultFormat) {
6161 if (!this .activeProfile .contains ("ollama" )) {
6262 return new FunctionResult (" " , null );
6363 }
64+ var result = new FunctionResult (" " , null );
6465
65- FunctionResult result = switch (resultFormat ) {
66- case ResultFormat .Text -> this .functionCallText (question );
67- case ResultFormat .Json -> this .functionCallJson (question );
68- };
66+ int i = 0 ;
67+ while (i < 3 && (result .jsonResult () == null && result .result () == " " )) {
68+ try {
69+ result = switch (resultFormat ) {
70+ case ResultFormat .Text -> this .functionCallText (question );
71+ case ResultFormat .Json -> this .functionCallJson (question );
72+ };
73+ } catch (Exception e ) {
74+ LOGGER .warn ("AI Call failed." , e );
75+ }
76+ i ++;
77+ }
6978 return result ;
7079 }
7180
7281 private FunctionResult functionCallText (String question ) {
73- var result = this .chatClient .prompt ().user (this .promptStr + question ).tools (FunctionConfig .OPEN_LIBRARY_CLIENT ). call ()
74- .content ();
82+ var result = this .chatClient .prompt ().user (this .promptStr + question ).tools (FunctionConfig .OPEN_LIBRARY_CLIENT )
83+ .call (). content ();
7584 return new FunctionResult (result , null );
7685 }
7786
7887 private FunctionResult functionCallJson (String question ) {
79- var result = this .chatClient .prompt ().user (this .promptStr + question ).tools (FunctionConfig .OPEN_LIBRARY_CLIENT ).call ()
80- .entity (new ParameterizedTypeReference <List <JsonResult >>() {});
88+ var result = this .chatClient .prompt ().user (this .promptStr + question ).tools (FunctionConfig .OPEN_LIBRARY_CLIENT )
89+ .call ().entity (new ParameterizedTypeReference <List <JsonResult >>() {
90+ });
8191 return new FunctionResult (null , result );
8292 }
8393}
0 commit comments