Skip to content

Commit 70a0146

Browse files
committed
fix: change retry_prompt impl for bedrock to make message resumption more fluid
1 parent a286788 commit 70a0146

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

llm/bedrock/src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,58 @@ impl ExtendedGuest for BedrockComponent {
6363
}
6464
}
6565

66+
fn retry_prompt(
67+
original_messages: &[Message],
68+
partial_result: &[llm::StreamDelta],
69+
) -> Vec<Message> {
70+
let mut extended_messages = Vec::new();
71+
extended_messages.push(Message {
72+
role: llm::Role::System,
73+
name: None,
74+
content: vec![
75+
llm::ContentPart::Text(
76+
"You were asked the same question previously, but the response was interrupted before completion. \
77+
Please continue your response from where you left off. \
78+
Do not include the part of the response that was already seen. If the response starts with a new word and no punctuation then add a space to the beginning".to_string()),
79+
],
80+
});
81+
extended_messages.push(Message {
82+
role: llm::Role::User,
83+
name: None,
84+
content: vec![llm::ContentPart::Text(
85+
"Here is the original question:".to_string(),
86+
)],
87+
});
88+
extended_messages.extend_from_slice(original_messages);
89+
90+
let mut partial_result_as_content = Vec::new();
91+
for delta in partial_result {
92+
if let Some(contents) = &delta.content {
93+
partial_result_as_content.extend_from_slice(contents);
94+
}
95+
if let Some(tool_calls) = &delta.tool_calls {
96+
for tool_call in tool_calls {
97+
partial_result_as_content.push(llm::ContentPart::Text(format!(
98+
"<tool-call id=\"{}\" name=\"{}\" arguments=\"{}\"/>",
99+
tool_call.id, tool_call.name, tool_call.arguments_json,
100+
)));
101+
}
102+
}
103+
}
104+
105+
extended_messages.push(Message {
106+
role: llm::Role::User,
107+
name: None,
108+
content: vec![llm::ContentPart::Text(
109+
"Here is the partial response that was successfully received:".to_string(),
110+
)]
111+
.into_iter()
112+
.chain(partial_result_as_content)
113+
.collect(),
114+
});
115+
extended_messages
116+
}
117+
66118
fn subscribe(_stream: &Self::ChatStream) -> golem_rust::wasm_rpc::Pollable {
67119
unimplemented!()
68120
}

0 commit comments

Comments
 (0)