Skip to content

Commit 1ad181c

Browse files
committed
feat: bedrock implementation
1 parent b47917b commit 1ad181c

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

llm/bedrock/src/client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl BedrockClient {
3535
}
3636
}
3737

38-
pub fn invoke_model(&self, model_id: &str, request: ConverseRequest) -> Result<ConverseResponse, Error> {
38+
pub fn converse(&self, model_id: &str, request: ConverseRequest) -> Result<ConverseResponse, Error> {
3939
trace!("Sending request to Bedrock API: {request:?}");
4040

4141
let body = serde_json::to_string(&request)
@@ -61,7 +61,7 @@ impl BedrockClient {
6161
parse_response(response)
6262
}
6363

64-
pub fn invoke_model_stream(&self, model_id: &str, request: ConverseRequest) -> Result<EventSource, Error> {
64+
pub fn converse_stream(&self, model_id: &str, request: ConverseRequest) -> Result<EventSource, Error> {
6565
trace!("Sending streaming request to Bedrock API: {request:?}");
6666

6767
let body = serde_json::to_string(&request)
@@ -269,7 +269,7 @@ pub enum ImageFormat {
269269
#[derive(Debug, Clone, Serialize, Deserialize)]
270270
#[serde(tag = "bytes")]
271271
pub struct ImageSource {
272-
pub bytes: String, // base64 encoded
272+
pub bytes: String,
273273
}
274274

275275
#[derive(Debug, Clone, Serialize, Deserialize)]

llm/bedrock/src/conversions.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ pub fn tool_results_to_messages(
174174
let mut messages = Vec::new();
175175

176176
for (tool_call, tool_result) in tool_results {
177-
// Add the assistant's tool use message
178177
messages.push(ClientMessage {
179178
content: vec![ContentBlock::ToolUse {
180179
tool_use_id: tool_call.id.clone(),
@@ -184,7 +183,6 @@ pub fn tool_results_to_messages(
184183
role: ClientRole::Assistant,
185184
});
186185

187-
// Add the tool result message
188186
let (content, status) = match tool_result {
189187
ToolResult::Success(success) => (
190188
vec![ToolResultContentBlock::Text {
@@ -280,7 +278,7 @@ fn message_to_system_content(message: &Message) -> Vec<SystemContentBlock> {
280278
ContentPart::Text(text) => result.push(SystemContentBlock::Text {
281279
text: text.clone(),
282280
}),
283-
ContentPart::Image(_) => {} // System messages typically don't contain images
281+
ContentPart::Image(_) => {}
284282
}
285283
}
286284

llm/bedrock/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ impl LlmChatStreamState for BedrockChatStream {
8585
let json: Value = serde_json::from_str(raw)
8686
.map_err(|err| format!("Failed to deserialize stream event: {err}"))?;
8787

88-
// Handle Bedrock streaming format
8988
if let Some(content_block_delta) = json.get("contentBlockDelta") {
9089
if let Some(delta) = content_block_delta.get("delta") {
9190
if let Some(text) = delta.get("text").and_then(|v| v.as_str()) {
@@ -104,7 +103,6 @@ impl LlmChatStreamState for BedrockChatStream {
104103
tool_use.get("toolUseId").and_then(|v| v.as_str()),
105104
tool_use.get("name").and_then(|v| v.as_str()),
106105
) {
107-
// For Bedrock, tool calls are complete when they start
108106
if let Some(input) = tool_use.get("input") {
109107
return Ok(Some(StreamEvent::Delta(StreamDelta {
110108
content: None,
@@ -179,7 +177,7 @@ impl BedrockComponent {
179177
}
180178

181179
fn request(client: BedrockClient, model_id: &str, request: ConverseRequest) -> ChatEvent {
182-
match client.invoke_model(model_id, request) {
180+
match client.converse(model_id, request) {
183181
Ok(response) => process_response(response),
184182
Err(err) => ChatEvent::Error(err),
185183
}
@@ -190,7 +188,7 @@ impl BedrockComponent {
190188
model_id: &str,
191189
request: ConverseRequest,
192190
) -> LlmChatStream<BedrockChatStream> {
193-
match client.invoke_model_stream(model_id, request) {
191+
match client.converse_stream(model_id, request) {
194192
Ok(stream) => BedrockChatStream::new(stream),
195193
Err(err) => BedrockChatStream::failed(err),
196194
}

test/components-rust/test-llm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ grok = []
1515
openai = []
1616
openrouter = []
1717
ollama = []
18+
bedrock = []
1819

1920
[dependencies]
2021
# To use common shared libs, use the following:

test/components-rust/test-llm/golem.yaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@ components:
139139
clean:
140140
- src/bindings.rs
141141

142+
bedrock-debug:
143+
files:
144+
- sourcePath: ../../data/cat.png
145+
targetPath: /data/cat.png
146+
permissions: read-only
147+
build:
148+
- command: cargo component build --no-default-features --features bedrock
149+
sources:
150+
- src
151+
- wit-generated
152+
- ../../common-rust
153+
targets:
154+
- ../../target/wasm32-wasip1/debug/test_llm.wasm
155+
- command: wac plug --plug ../../../target/wasm32-wasip1/debug/golem_llm_bedrock.wasm ../../target/wasm32-wasip1/debug/test_llm.wasm -o ../../target/wasm32-wasip1/debug/test_bedrock_plugged.wasm
156+
sources:
157+
- ../../target/wasm32-wasip1/debug/test_llm.wasm
158+
- ../../../target/wasm32-wasip1/debug/golem_llm_bedrock.wasm
159+
targets:
160+
- ../../target/wasm32-wasip1/debug/test_bedrock_plugged.wasm
161+
sourceWit: wit
162+
generatedWit: wit-generated
163+
componentWasm: ../../target/wasm32-wasip1/debug/test_bedrock_plugged.wasm
164+
linkedWasm: ../../golem-temp/components/test_bedrock_debug.wasm
165+
clean:
166+
- src/bindings.rs
167+
142168
# RELEASE PROFILES
143169
openai-release:
144170
files:
@@ -270,6 +296,34 @@ components:
270296
clean:
271297
- src/bindings.rs
272298

299+
300+
bedrock-release:
301+
files:
302+
- sourcePath: ../../data/cat.png
303+
targetPath: /data/cat.png
304+
permissions: read-only
305+
build:
306+
- command: cargo component build --release --no-default-features --features bedrock
307+
sources:
308+
- src
309+
- wit-generated
310+
- ../../common-rust
311+
targets:
312+
- ../../target/wasm32-wasip1/release/test_llm.wasm
313+
- command: wac plug --plug ../../../target/wasm32-wasip1/release/golem_llm_bedrock.wasm ../../target/wasm32-wasip1/release/test_llm.wasm -o ../../target/wasm32-wasip1/release/test_bedrock_plugged.wasm
314+
sources:
315+
- ../../target/wasm32-wasip1/release/test_llm.wasm
316+
- ../../../target/wasm32-wasip1/release/golem_llm_bedrock.wasm
317+
targets:
318+
- ../../target/wasm32-wasip1/release/test_bedrock_plugged.wasm
319+
sourceWit: wit
320+
generatedWit: wit-generated
321+
componentWasm: ../../target/wasm32-wasip1/release/test_bedrock_plugged.wasm
322+
linkedWasm: ../../golem-temp/components/test_bedrock_release.wasm
323+
clean:
324+
- src/bindings.rs
325+
326+
273327
defaultProfile: openai-debug
274328

275329
dependencies:

0 commit comments

Comments
 (0)