Skip to content

Commit d261ee9

Browse files
committed
feat: ran llm-test and clippy
1 parent 6236dc4 commit d261ee9

File tree

13 files changed

+201
-883
lines changed

13 files changed

+201
-883
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ openapi
44
target
55
tmp
66
components
7+
run.sh

llm-anthropic/src/bindings.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen` 0.36.0. DO NOT EDIT!
22
// Options used:
33
// * runtime_path: "wit_bindgen_rt"
44
// * with "golem:llm/llm@1.0.0" = "golem_llm::golem::llm::llm"
55
// * generate_unused_types
66
use golem_llm::golem::llm::llm as __with_name0;
77
#[cfg(target_arch = "wasm32")]
8-
#[unsafe(
9-
link_section = "component-type:wit-bindgen:0.41.0:golem:llm-anthropic@1.0.0:llm-library:encoded world"
10-
)]
8+
#[link_section = "component-type:wit-bindgen:0.36.0:golem:llm-anthropic@1.0.0:llm-library:encoded world"]
119
#[doc(hidden)]
12-
#[allow(clippy::octal_escapes)]
1310
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 1762] = *b"\
1411
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xe0\x0c\x01A\x02\x01\
1512
A\x02\x01BO\x01m\x04\x04user\x09assistant\x06system\x04tool\x04\0\x04role\x03\0\0\
@@ -46,8 +43,8 @@ ng-get-next\x01B\x01p\x15\x01@\x02\x08messages\xc3\0\x06config)\06\x04\0\x04send
4643
\0\x06config)\06\x04\0\x08continue\x01G\x01i=\x01@\x02\x08messages\xc3\0\x06conf\
4744
ig)\0\xc8\0\x04\0\x06stream\x01I\x04\0\x13golem:llm/llm@1.0.0\x05\0\x04\0%golem:\
4845
llm-anthropic/llm-library@1.0.0\x04\0\x0b\x11\x01\0\x0bllm-library\x03\0\0\0G\x09\
49-
producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rus\
50-
t\x060.41.0";
46+
producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rus\
47+
t\x060.36.0";
5148
#[inline(never)]
5249
#[doc(hidden)]
5350
pub fn __link_custom_section_describing_imports() {

llm-bedrock/src/client.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::{
2-
conversions::{self, from_sdk_error, BedrockInput},
2+
conversions::{self, from_converse_sdk_error, from_converse_stream_sdk_error, BedrockInput},
33
stream::BedrockChatStream,
44
};
55
use aws_config::BehaviorVersion;
66
use aws_sdk_bedrockruntime::{
77
self as bedrock,
8+
config::{AsyncSleep, Sleep},
89
operation::{
910
converse::builders::ConverseFluentBuilder,
1011
converse_stream::builders::ConverseStreamFluentBuilder,
@@ -16,6 +17,7 @@ use golem_llm::{
1617
config::{get_config_key, get_config_key_or_none},
1718
golem::llm::llm,
1819
};
20+
use log::trace;
1921

2022
#[derive(Debug)]
2123
pub struct Bedrock {
@@ -35,6 +37,7 @@ impl Bedrock {
3537
.region(environment.aws_region())
3638
.http_client(wasi_http)
3739
.credentials_provider(environment.aws_credentials())
40+
.sleep_impl(TokioSleep)
3841
.load()
3942
.await;
4043
let client = bedrock::Client::new(&sdk_config);
@@ -57,13 +60,14 @@ impl Bedrock {
5760

5861
let runtime = get_async_runtime();
5962

63+
trace!("Sending request to AWS Bedrock: {input:?}");
6064
runtime.block_on(async {
6165
let model_id = input.model_id.clone();
6266
let response = self
6367
.init_converse(input)
6468
.send()
6569
.await
66-
.map_err(|e| from_sdk_error(model_id, e));
70+
.map_err(|e| from_converse_sdk_error(model_id, e));
6771

6872
if let Err(err) = response {
6973
return llm::ChatEvent::Error(err);
@@ -95,15 +99,16 @@ impl Bedrock {
9599
let input = bedrock_input.unwrap();
96100

97101
let runtime = get_async_runtime();
98-
102+
trace!("Sending request to AWS Bedrock: {input:?}");
99103
runtime.block_on(async {
100104
let model_id = input.model_id.clone();
101105
let response = self
102106
.init_converse_stream(input)
103107
.send()
104108
.await
105-
.map_err(|e| from_sdk_error(model_id, e));
109+
.map_err(|e| from_converse_stream_sdk_error(model_id, e));
106110

111+
trace!("Creating AWS Bedrock event stream");
107112
match response {
108113
Ok(response) => BedrockChatStream::new(response.stream),
109114
Err(error) => BedrockChatStream::failed(error),
@@ -172,6 +177,17 @@ impl BedrockEnvironment {
172177

173178
pub fn get_async_runtime() -> tokio::runtime::Runtime {
174179
tokio::runtime::Builder::new_current_thread()
180+
.enable_time()
175181
.build()
176182
.unwrap()
177183
}
184+
185+
#[derive(Debug, Clone)]
186+
struct TokioSleep;
187+
impl AsyncSleep for TokioSleep {
188+
fn sleep(&self, duration: std::time::Duration) -> Sleep {
189+
Sleep::new(Box::pin(async move {
190+
tokio::time::sleep(duration).await;
191+
}))
192+
}
193+
}

llm-bedrock/src/conversions.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashMap;
44
use aws_sdk_bedrockruntime::{
55
self as bedrock,
66
error::SdkError,
7-
operation::converse,
7+
operation::{converse, converse_stream},
88
types::{
99
ContentBlockDeltaEvent, ContentBlockStartEvent, ConversationRole,
1010
ConverseStreamMetadataEvent, ConverseStreamOutput, ImageBlock, ImageFormat,
@@ -34,7 +34,7 @@ impl BedrockInput {
3434
messages_to_bedrock_message_groups(messages)?;
3535

3636
if let Some(tool_results) = tool_results {
37-
user_messages.push(tool_call_results_to_bedrock_tool_results(tool_results));
37+
user_messages.extend(tool_call_results_to_bedrock_tools(tool_results)?);
3838
}
3939

4040
let options = config
@@ -62,12 +62,22 @@ impl BedrockInput {
6262
}
6363
}
6464

65-
fn tool_call_results_to_bedrock_tool_results(
65+
fn tool_call_results_to_bedrock_tools(
6666
results: Vec<(llm::ToolCall, llm::ToolResult)>,
67-
) -> bedrock::types::Message {
67+
) -> Result<Vec<bedrock::types::Message>, llm::Error> {
68+
let mut tool_calls: Vec<bedrock::types::ContentBlock> = vec![];
6869
let mut tool_results: Vec<bedrock::types::ContentBlock> = vec![];
6970

7071
for (tool_call, tool_result) in results {
72+
tool_calls.push(bedrock::types::ContentBlock::ToolUse(
73+
bedrock::types::ToolUseBlock::builder()
74+
.tool_use_id(tool_call.id.clone())
75+
.name(tool_call.name)
76+
.input(json_str_to_smithy_document(&tool_call.arguments_json)?)
77+
.build()
78+
.unwrap(),
79+
));
80+
7181
tool_results.push(bedrock::types::ContentBlock::ToolResult(
7282
bedrock::types::ToolResultBlock::builder()
7383
.tool_use_id(tool_call.id)
@@ -82,11 +92,18 @@ fn tool_call_results_to_bedrock_tool_results(
8292
));
8393
}
8494

85-
bedrock::types::Message::builder()
86-
.role(ConversationRole::User)
87-
.set_content(Some(tool_results))
88-
.build()
89-
.unwrap()
95+
Ok(vec![
96+
bedrock::types::Message::builder()
97+
.role(ConversationRole::Assistant)
98+
.set_content(Some(tool_calls))
99+
.build()
100+
.unwrap(),
101+
bedrock::types::Message::builder()
102+
.role(ConversationRole::User)
103+
.set_content(Some(tool_results))
104+
.build()
105+
.unwrap(),
106+
])
90107
}
91108

92109
fn tool_defs_to_bedrock_tool_config(
@@ -549,10 +566,24 @@ fn serde_json_to_smithy_document(value: serde_json::Value) -> Document {
549566
}
550567
}
551568

552-
pub fn from_sdk_error<U, V>(model_id: String, sdk_error: SdkError<U, V>) -> llm::Error {
569+
pub fn from_converse_sdk_error(
570+
model_id: String,
571+
sdk_error: SdkError<converse::ConverseError>,
572+
) -> llm::Error {
573+
llm::Error {
574+
code: llm::ErrorCode::InternalError,
575+
message: format!("Error calling Bedrock model {model_id}: {sdk_error:?}",),
576+
provider_error_json: None,
577+
}
578+
}
579+
580+
pub fn from_converse_stream_sdk_error(
581+
model_id: String,
582+
sdk_error: SdkError<converse_stream::ConverseStreamError>,
583+
) -> llm::Error {
553584
llm::Error {
554585
code: llm::ErrorCode::InternalError,
555-
message: format!("Error calling Bedrock model {}: {}", model_id, sdk_error),
586+
message: format!("Error calling Bedrock model {model_id}: {sdk_error:?}",),
556587
provider_error_json: None,
557588
}
558589
}

llm-bedrock/src/stream.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use std::cell::{RefCell, RefMut};
2-
31
use aws_sdk_bedrockruntime::{
42
self as bedrock, primitives::event_stream::EventReceiver,
53
types::error::ConverseStreamOutputError,
64
};
75
use golem_llm::golem::llm::llm;
8-
use golem_rust::wasm_rpc::Pollable;
6+
use golem_rust::wasm_rpc::wasi::io::poll::Pollable;
7+
use std::cell::{RefCell, RefMut};
98

109
use crate::{
1110
client::get_async_runtime,
@@ -72,7 +71,10 @@ impl llm::GuestChatStream for BedrockChatStream {
7271
let token = stream.recv().await;
7372

7473
match token {
75-
Ok(Some(output)) => converse_stream_output_to_stream_event(output),
74+
Ok(Some(output)) => {
75+
log::trace!("Processing bedrock stream event: {output:?}");
76+
converse_stream_output_to_stream_event(output)
77+
}
7678
Ok(None) => {
7779
self.set_finished();
7880
Some(vec![])

llm-grok/src/bindings.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen` 0.36.0. DO NOT EDIT!
22
// Options used:
33
// * runtime_path: "wit_bindgen_rt"
44
// * with "golem:llm/llm@1.0.0" = "golem_llm::golem::llm::llm"
55
// * generate_unused_types
66
use golem_llm::golem::llm::llm as __with_name0;
77
#[cfg(target_arch = "wasm32")]
8-
#[unsafe(
9-
link_section = "component-type:wit-bindgen:0.41.0:golem:llm-grok@1.0.0:llm-library:encoded world"
10-
)]
8+
#[link_section = "component-type:wit-bindgen:0.36.0:golem:llm-grok@1.0.0:llm-library:encoded world"]
119
#[doc(hidden)]
12-
#[allow(clippy::octal_escapes)]
1310
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 1757] = *b"\
1411
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xdb\x0c\x01A\x02\x01\
1512
A\x02\x01BO\x01m\x04\x04user\x09assistant\x06system\x04tool\x04\0\x04role\x03\0\0\
@@ -46,8 +43,8 @@ ng-get-next\x01B\x01p\x15\x01@\x02\x08messages\xc3\0\x06config)\06\x04\0\x04send
4643
\0\x06config)\06\x04\0\x08continue\x01G\x01i=\x01@\x02\x08messages\xc3\0\x06conf\
4744
ig)\0\xc8\0\x04\0\x06stream\x01I\x04\0\x13golem:llm/llm@1.0.0\x05\0\x04\0\x20gol\
4845
em:llm-grok/llm-library@1.0.0\x04\0\x0b\x11\x01\0\x0bllm-library\x03\0\0\0G\x09p\
49-
roducers\x01\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rust\
50-
\x060.41.0";
46+
roducers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rust\
47+
\x060.36.0";
5148
#[inline(never)]
5249
#[doc(hidden)]
5350
pub fn __link_custom_section_describing_imports() {

llm-ollama/src/bindings.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen` 0.36.0. DO NOT EDIT!
22
// Options used:
33
// * runtime_path: "wit_bindgen_rt"
44
// * with "golem:llm/llm@1.0.0" = "golem_llm::golem::llm::llm"
55
// * generate_unused_types
66
use golem_llm::golem::llm::llm as __with_name0;
77
#[cfg(target_arch = "wasm32")]
8-
#[unsafe(
9-
link_section = "component-type:wit-bindgen:0.41.0:golem:llm-ollama@1.0.0:llm-library:encoded world"
10-
)]
8+
#[link_section = "component-type:wit-bindgen:0.36.0:golem:llm-ollama@1.0.0:llm-library:encoded world"]
119
#[doc(hidden)]
12-
#[allow(clippy::octal_escapes)]
1310
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 1759] = *b"\
1411
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xdd\x0c\x01A\x02\x01\
1512
A\x02\x01BO\x01m\x04\x04user\x09assistant\x06system\x04tool\x04\0\x04role\x03\0\0\
@@ -46,8 +43,8 @@ ng-get-next\x01B\x01p\x15\x01@\x02\x08messages\xc3\0\x06config)\06\x04\0\x04send
4643
\0\x06config)\06\x04\0\x08continue\x01G\x01i=\x01@\x02\x08messages\xc3\0\x06conf\
4744
ig)\0\xc8\0\x04\0\x06stream\x01I\x04\0\x13golem:llm/llm@1.0.0\x05\0\x04\0\"golem\
4845
:llm-ollama/llm-library@1.0.0\x04\0\x0b\x11\x01\0\x0bllm-library\x03\0\0\0G\x09p\
49-
roducers\x01\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rust\
50-
\x060.41.0";
46+
roducers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rust\
47+
\x060.36.0";
5148
#[inline(never)]
5249
#[doc(hidden)]
5350
pub fn __link_custom_section_describing_imports() {

llm-openai/src/bindings.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen` 0.36.0. DO NOT EDIT!
22
// Options used:
33
// * runtime_path: "wit_bindgen_rt"
44
// * with "golem:llm/llm@1.0.0" = "golem_llm::golem::llm::llm"
55
// * generate_unused_types
66
use golem_llm::golem::llm::llm as __with_name0;
77
#[cfg(target_arch = "wasm32")]
8-
#[unsafe(
9-
link_section = "component-type:wit-bindgen:0.41.0:golem:llm-openai@1.0.0:llm-library:encoded world"
10-
)]
8+
#[link_section = "component-type:wit-bindgen:0.36.0:golem:llm-openai@1.0.0:llm-library:encoded world"]
119
#[doc(hidden)]
12-
#[allow(clippy::octal_escapes)]
1310
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 1759] = *b"\
1411
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xdd\x0c\x01A\x02\x01\
1512
A\x02\x01BO\x01m\x04\x04user\x09assistant\x06system\x04tool\x04\0\x04role\x03\0\0\
@@ -46,8 +43,8 @@ ng-get-next\x01B\x01p\x15\x01@\x02\x08messages\xc3\0\x06config)\06\x04\0\x04send
4643
\0\x06config)\06\x04\0\x08continue\x01G\x01i=\x01@\x02\x08messages\xc3\0\x06conf\
4744
ig)\0\xc8\0\x04\0\x06stream\x01I\x04\0\x13golem:llm/llm@1.0.0\x05\0\x04\0\"golem\
4845
:llm-openai/llm-library@1.0.0\x04\0\x0b\x11\x01\0\x0bllm-library\x03\0\0\0G\x09p\
49-
roducers\x01\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rust\
50-
\x060.41.0";
46+
roducers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rust\
47+
\x060.36.0";
5148
#[inline(never)]
5249
#[doc(hidden)]
5350
pub fn __link_custom_section_describing_imports() {

llm-openrouter/src/bindings.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen` 0.36.0. DO NOT EDIT!
22
// Options used:
33
// * runtime_path: "wit_bindgen_rt"
44
// * with "golem:llm/llm@1.0.0" = "golem_llm::golem::llm::llm"
55
// * generate_unused_types
66
use golem_llm::golem::llm::llm as __with_name0;
77
#[cfg(target_arch = "wasm32")]
8-
#[unsafe(
9-
link_section = "component-type:wit-bindgen:0.41.0:golem:llm-openrouter@1.0.0:llm-library:encoded world"
10-
)]
8+
#[link_section = "component-type:wit-bindgen:0.36.0:golem:llm-openrouter@1.0.0:llm-library:encoded world"]
119
#[doc(hidden)]
12-
#[allow(clippy::octal_escapes)]
1310
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 1763] = *b"\
1411
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xe1\x0c\x01A\x02\x01\
1512
A\x02\x01BO\x01m\x04\x04user\x09assistant\x06system\x04tool\x04\0\x04role\x03\0\0\
@@ -46,8 +43,8 @@ ng-get-next\x01B\x01p\x15\x01@\x02\x08messages\xc3\0\x06config)\06\x04\0\x04send
4643
\0\x06config)\06\x04\0\x08continue\x01G\x01i=\x01@\x02\x08messages\xc3\0\x06conf\
4744
ig)\0\xc8\0\x04\0\x06stream\x01I\x04\0\x13golem:llm/llm@1.0.0\x05\0\x04\0&golem:\
4845
llm-openrouter/llm-library@1.0.0\x04\0\x0b\x11\x01\0\x0bllm-library\x03\0\0\0G\x09\
49-
producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rus\
50-
t\x060.41.0";
46+
producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.220.0\x10wit-bindgen-rus\
47+
t\x060.36.0";
5148
#[inline(never)]
5249
#[doc(hidden)]
5350
pub fn __link_custom_section_describing_imports() {

0 commit comments

Comments
 (0)