style(tools, llm): 统一导入顺序与代码格式
This commit is contained in:
+17
-15
@@ -48,7 +48,11 @@ impl PromptComposer {
|
||||
|
||||
/// 添加一条 Tool 消息(工具执行结果回传)。
|
||||
pub fn tool(mut self, tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
|
||||
self.push_message(Message::tool_result(tool_call_id.into(), content.into(), false));
|
||||
self.push_message(Message::tool_result(
|
||||
tool_call_id.into(),
|
||||
content.into(),
|
||||
false,
|
||||
));
|
||||
self
|
||||
}
|
||||
|
||||
@@ -133,11 +137,7 @@ impl PromptComposer {
|
||||
}
|
||||
|
||||
/// 添加一条含指定 ContentBlock 的 Tool 消息。
|
||||
pub fn tool_content(
|
||||
mut self,
|
||||
tool_call_id: impl Into<String>,
|
||||
block: ContentBlock,
|
||||
) -> Self {
|
||||
pub fn tool_content(mut self, tool_call_id: impl Into<String>, block: ContentBlock) -> Self {
|
||||
self.push_message(Message::ToolResult {
|
||||
tool_call_id: tool_call_id.into(),
|
||||
content: vec![block],
|
||||
@@ -187,9 +187,7 @@ impl PromptComposer {
|
||||
/// 验证消息序列是否符合 LLM API 要求(Tool 消息必须紧跟含 tool_calls 的 Assistant)。
|
||||
pub fn validate_messages(messages: &[Message]) -> Result<(), PromptError> {
|
||||
if messages.is_empty() {
|
||||
return Err(PromptError::InvalidSequence(
|
||||
"消息列表不能为空".to_string(),
|
||||
));
|
||||
return Err(PromptError::InvalidSequence("消息列表不能为空".to_string()));
|
||||
}
|
||||
|
||||
let mut last_tool_call_ids: Vec<String> = Vec::new();
|
||||
@@ -297,7 +295,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_template_if() {
|
||||
let tpl = PromptTemplate::compile("Hello {{#if name}}{{name}}{{else}}Guest{{/if}}").unwrap();
|
||||
let tpl =
|
||||
PromptTemplate::compile("Hello {{#if name}}{{name}}{{else}}Guest{{/if}}").unwrap();
|
||||
let mut ctx = TemplateContext::new();
|
||||
ctx.insert("name", "Bob");
|
||||
|
||||
@@ -312,11 +311,14 @@ mod tests {
|
||||
fn test_template_each() {
|
||||
let tpl = PromptTemplate::compile("Items: {{#each items}}{{item}}, {{/each}}").unwrap();
|
||||
let mut ctx = TemplateContext::new();
|
||||
ctx.insert("items", TemplateValue::Array(vec![
|
||||
TemplateValue::String("a".to_string()),
|
||||
TemplateValue::String("b".to_string()),
|
||||
TemplateValue::String("c".to_string()),
|
||||
]));
|
||||
ctx.insert(
|
||||
"items",
|
||||
TemplateValue::Array(vec![
|
||||
TemplateValue::String("a".to_string()),
|
||||
TemplateValue::String("b".to_string()),
|
||||
TemplateValue::String("c".to_string()),
|
||||
]),
|
||||
);
|
||||
|
||||
let result = tpl.render(&ctx).unwrap();
|
||||
assert_eq!(result, "Items: a, b, c, ");
|
||||
|
||||
Reference in New Issue
Block a user