-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Amazon Bedrock Chat adds tool support.
- Loading branch information
wmz7year
committed
Jun 6, 2024
1 parent
49b3326
commit b03129d
Showing
7 changed files
with
811 additions
and
78 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
...k/src/main/java/org/springframework/ai/bedrock/BedrockConverseChatGenerationMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright 2023 - 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.ai.bedrock; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.ai.bedrock.api.BedrockConverseApiUtils; | ||
import org.springframework.ai.chat.metadata.ChatGenerationMetadata; | ||
|
||
import software.amazon.awssdk.services.bedrockruntime.model.ContentBlock; | ||
import software.amazon.awssdk.services.bedrockruntime.model.ContentBlockDeltaEvent; | ||
import software.amazon.awssdk.services.bedrockruntime.model.ContentBlockStartEvent; | ||
import software.amazon.awssdk.services.bedrockruntime.model.ConversationRole; | ||
import software.amazon.awssdk.services.bedrockruntime.model.ConverseResponse; | ||
import software.amazon.awssdk.services.bedrockruntime.model.ConverseStreamOutput; | ||
import software.amazon.awssdk.services.bedrockruntime.model.Message; | ||
import software.amazon.awssdk.services.bedrockruntime.model.MessageStopEvent; | ||
import software.amazon.awssdk.services.bedrockruntime.model.StopReason; | ||
|
||
/** | ||
* Amazon Bedrock Chat model converse interface generation metadata, encapsulating | ||
* information on the completion. | ||
* | ||
* @author Wei Jiang | ||
* @since 1.0.0 | ||
*/ | ||
public class BedrockConverseChatGenerationMetadata implements ChatGenerationMetadata { | ||
|
||
private String stopReason; | ||
|
||
private Message message; | ||
|
||
private ConverseStreamOutput event; | ||
|
||
public BedrockConverseChatGenerationMetadata(String stopReason, ConverseStreamOutput event) { | ||
super(); | ||
|
||
this.stopReason = stopReason; | ||
this.event = event; | ||
} | ||
|
||
public BedrockConverseChatGenerationMetadata(String stopReason, Message message) { | ||
super(); | ||
|
||
this.stopReason = stopReason; | ||
this.message = message; | ||
} | ||
|
||
public static BedrockConverseChatGenerationMetadata from(ConverseResponse response, Message message) { | ||
return new BedrockConverseChatGenerationMetadata(response.stopReasonAsString(), message); | ||
} | ||
|
||
public static BedrockConverseChatGenerationMetadata from(ConverseStreamOutput event) { | ||
String stopReason = null; | ||
|
||
if (event instanceof MessageStopEvent messageStopEvent) { | ||
stopReason = messageStopEvent.stopReasonAsString(); | ||
} | ||
|
||
return new BedrockConverseChatGenerationMetadata(stopReason, event); | ||
} | ||
|
||
@Override | ||
public <T> T getContentFilterMetadata() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getFinishReason() { | ||
return stopReason; | ||
} | ||
|
||
public Message getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(Message message) { | ||
this.message = message; | ||
} | ||
|
||
public ConverseStreamOutput getEvent() { | ||
return event; | ||
} | ||
|
||
public boolean isToolUseEvent() { | ||
if (event instanceof ContentBlockStartEvent startEvent) { | ||
if (startEvent.start().toolUse() != null) { | ||
return false; | ||
} | ||
} | ||
|
||
if (event instanceof ContentBlockDeltaEvent deltaEvent) { | ||
if (deltaEvent.delta().toolUse() != null) { | ||
return false; | ||
} | ||
} | ||
|
||
if (event instanceof MessageStopEvent stopEvent) { | ||
return stopEvent.stopReason() != StopReason.TOOL_USE; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static void generateEventMessage(ChatGenerationMetadata chatGenerationMetadata) { | ||
if (chatGenerationMetadata instanceof BedrockConverseChatGenerationMetadata metadata) { | ||
ConverseStreamOutput event = metadata.getEvent(); | ||
|
||
if (event instanceof ContentBlockDeltaEvent deltaEvent) { | ||
Message message = BedrockConverseApiUtils.createMessage( | ||
List.of(ContentBlock.builder().text(deltaEvent.delta().text()).build()), | ||
ConversationRole.ASSISTANT); | ||
metadata.setMessage(message); | ||
} | ||
else { | ||
metadata.setMessage(BedrockConverseApiUtils.EMPTY_MESSAGE); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.