Skip to content

fix: add multi-turn output type for openai component #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ jobs:
golem-cli worker invoke test:llm/ollama-1 test5
golem-cli worker invoke test:llm/ollama-1 test6
golem-cli worker invoke test:llm/ollama-1 test7
golem-cli worker invoke test:llm/ollama-1 test8
publish-all:
needs:
- tests
Expand Down
2 changes: 1 addition & 1 deletion llm/anthropic/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn process_response(response: MessagesResponse) -> ChatEvent {
Err(e) => {
return ChatEvent::Error(Error {
code: ErrorCode::InvalidRequest,
message: format!("Failed to decode base64 image data: {}", e),
message: format!("Failed to decode base64 image data: {e}"),
provider_error_json: None,
});
}
Expand Down
2 changes: 1 addition & 1 deletion llm/grok/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn convert_content_parts(contents: Vec<ContentPart>) -> crate::client::Content {
let media_type = &image_source.mime_type; // This is already a string
result.push(crate::client::ContentPart::ImageInput {
image_url: crate::client::ImageUrl {
url: format!("data:{};base64,{}", media_type, base64_data),
url: format!("data:{media_type};base64,{base64_data}"),
detail: image_source.detail.map(|d| d.into()),
},
});
Expand Down
2 changes: 1 addition & 1 deletion llm/llm/src/event_source/ndjson_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn try_parse_line(
return Ok(None);
}

trace!("Parsed NDJSON line: {}", line);
trace!("Parsed NDJSON line: {line}");

// Create a MessageEvent with the JSON line as data
let event = MessageEvent {
Expand Down
6 changes: 3 additions & 3 deletions llm/llm/src/event_source/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Utf8(err) => f.write_fmt(format_args!("UTF8 error: {}", err)),
Self::Parser(err) => f.write_fmt(format_args!("Parse error: {}", err)),
Self::Transport(err) => f.write_fmt(format_args!("Transport error: {}", err)),
Self::Utf8(err) => f.write_fmt(format_args!("UTF8 error: {err}")),
Self::Parser(err) => f.write_fmt(format_args!("Parse error: {err}")),
Self::Transport(err) => f.write_fmt(format_args!("Transport error: {err}")),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion llm/ollama/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ pub fn image_to_base64(source: &str) -> Result<String, Box<dyn std::error::Error
pub fn from_reqwest_error(context: &str, err: reqwest::Error) -> Error {
Error {
code: ErrorCode::InternalError,
message: format!("{}: {}", context, err),
message: format!("{context}: {err}"),
provider_error_json: None,
}
}
2 changes: 1 addition & 1 deletion llm/ollama/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub fn process_response(response: CompletionsResponse) -> ChatEvent {
};

ChatEvent::Message(CompleteResponse {
id: format!("ollama-{}", timestamp),
id: format!("ollama-{timestamp}"),
content,
tool_calls,
metadata,
Expand Down
2 changes: 2 additions & 0 deletions llm/openai/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ pub enum InnerInput {
pub enum InnerInputItem {
#[serde(rename = "input_text")]
TextInput { text: String },
#[serde(rename = "output_text")]
TextOutput { text: String },
#[serde(rename = "input_image")]
ImageInput {
image_url: String,
Expand Down
68 changes: 36 additions & 32 deletions llm/openai/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,7 @@ pub fn create_request(
pub fn messages_to_input_items(messages: Vec<Message>) -> Vec<InputItem> {
let mut items = Vec::new();
for message in messages {
let role = to_openai_role_name(message.role).to_string();
let mut input_items = Vec::new();
for content_part in message.content {
input_items.push(content_part_to_inner_input_item(content_part));
}

items.push(InputItem::InputMessage {
role,
content: InnerInput::List(input_items),
});
items.push(llm_message_to_openai_message(message));
}
items
}
Expand Down Expand Up @@ -122,35 +113,48 @@ pub fn to_openai_role_name(role: Role) -> &'static str {
}
}

pub fn content_part_to_inner_input_item(content_part: ContentPart) -> InnerInputItem {
match content_part {
ContentPart::Text(msg) => InnerInputItem::TextInput { text: msg },
ContentPart::Image(image_reference) => match image_reference {
ImageReference::Url(image_url) => InnerInputItem::ImageInput {
image_url: image_url.url,
detail: match image_url.detail {
Some(ImageDetail::Auto) => Detail::Auto,
Some(ImageDetail::Low) => Detail::Low,
Some(ImageDetail::High) => Detail::High,
None => Detail::default(),
},
pub fn llm_message_to_openai_message(message: Message) -> InputItem {
let mut items = Vec::new();

for content_part in message.content {
let item = match content_part {
ContentPart::Text(msg) => match message.role {
Role::Assistant => InnerInputItem::TextOutput { text: msg },
_ => InnerInputItem::TextInput { text: msg },
},
ImageReference::Inline(image_source) => {
let base64_data = general_purpose::STANDARD.encode(&image_source.data);
let mime_type = &image_source.mime_type; // This is already a string
let data_url = format!("data:{};base64,{}", mime_type, base64_data);

InnerInputItem::ImageInput {
image_url: data_url,
detail: match image_source.detail {
ContentPart::Image(image_reference) => match image_reference {
ImageReference::Url(image_url) => InnerInputItem::ImageInput {
image_url: image_url.url,
detail: match image_url.detail {
Some(ImageDetail::Auto) => Detail::Auto,
Some(ImageDetail::Low) => Detail::Low,
Some(ImageDetail::High) => Detail::High,
None => Detail::default(),
},
},
ImageReference::Inline(image_source) => {
let base64_data = general_purpose::STANDARD.encode(&image_source.data);
let mime_type = &image_source.mime_type; // This is already a string
let data_url = format!("data:{mime_type};base64,{base64_data}");

InnerInputItem::ImageInput {
image_url: data_url,
detail: match image_source.detail {
Some(ImageDetail::Auto) => Detail::Auto,
Some(ImageDetail::Low) => Detail::Low,
Some(ImageDetail::High) => Detail::High,
None => Detail::default(),
},
}
}
}
},
},
};
items.push(item);
}

InputItem::InputMessage {
role: to_openai_role_name(message.role).to_string(),
content: InnerInput::List(items),
}
}

Expand Down
2 changes: 1 addition & 1 deletion llm/openrouter/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn convert_content_parts(contents: Vec<ContentPart>) -> crate::client::Content {
let media_type = &image_source.mime_type; // This is already a string
result.push(crate::client::ContentPart::ImageInput {
image_url: crate::client::ImageUrl {
url: format!("data:{};base64,{}", media_type, base64_data),
url: format!("data:{media_type};base64,{base64_data}"),
detail: image_source.detail.map(|d| d.into()),
},
});
Expand Down
2 changes: 1 addition & 1 deletion test/components-rust/test-llm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ path = "wit-generated"

[package.metadata.component.target.dependencies]
"golem:llm" = { path = "wit-generated/deps/golem-llm" }
"wasi:clocks" = { path = "wit-generated/deps/clocks" }
"wasi:io" = { path = "wit-generated/deps/io" }
"wasi:clocks" = { path = "wit-generated/deps/clocks" }
"golem:rpc" = { path = "wit-generated/deps/golem-rpc" }
"test:helper-client" = { path = "wit-generated/deps/test_helper-client" }
"test:llm-exports" = { path = "wit-generated/deps/test_llm-exports" }
Expand Down
Loading
Loading