Improve AnthropicChatModel handling of empty generations

- Add handling for cases where ChatCompletion has a stop reason but empty generations.
   Creates a Generation with empty content and metadata when this occurs.
 - Update AnthropicChatModelObservationIT to expect "end_turn" finish reason.
 - Update javadoc.
This commit is contained in:
“claudio-code”
2024-09-24 00:49:56 -03:00
committed by Christian Tzolov
parent e63dc6a457
commit b468354dd3
2 changed files with 8 additions and 1 deletions

View File

@@ -70,6 +70,7 @@ import reactor.core.publisher.Mono;
* @author luocongqiu
* @author Mariusz Bernacki
* @author Thomas Vitale
* @author Claudio Silva Junior
* @since 1.0.0
*/
public class AnthropicChatModel extends AbstractToolCallSupport implements ChatModel {
@@ -293,6 +294,12 @@ public class AnthropicChatModel extends AbstractToolCallSupport implements ChatM
List<Generation> allGenerations = new ArrayList<>(generations);
if (chatCompletion.stopReason() != null && generations.isEmpty()) {
Generation generation = new Generation(new AssistantMessage(null, Map.of()),
ChatGenerationMetadata.from(chatCompletion.stopReason(), null));
allGenerations.add(generation);
}
List<ContentBlock> toolToUseList = chatCompletion.content()
.stream()
.filter(c -> c.type() == ContentBlock.Type.TOOL_USE)

View File

@@ -118,7 +118,7 @@ public class AnthropicChatModelObservationIT {
ChatResponseMetadata responseMetadata = lastChatResponse.getMetadata();
assertThat(responseMetadata).isNotNull();
validate(responseMetadata, KeyValue.NONE_VALUE);
validate(responseMetadata, "[\"end_turn\"]");
}
private void validate(ChatResponseMetadata responseMetadata, String finishReasons) {