b09a98db48
统一系统消息、工具结果与异常响应的消息构造。 补充无效响应纠正、批量工具调用和待确认状态处理。 精简图片上下文提示并完善越界引用回退规则。 补齐运行循环和提示词行为测试。
89 lines
2.3 KiB
Rust
89 lines
2.3 KiB
Rust
use crate::error::PromptError;
|
|
use crate::hook::Hook;
|
|
use crate::memory::AgentMemory;
|
|
use crate::run::PromptRequest;
|
|
use crate::tool::{Tool, ToolDyn};
|
|
|
|
pub struct Agent<M: LlmApiAdaptor<Message>, Message> {
|
|
pub model: M,
|
|
pub tools: Vec<Box<dyn ToolDyn>>,
|
|
pub hooks: Vec<Box<dyn Hook>>,
|
|
pub default_max_turns: usize,
|
|
pub system_prompt: Option<Message>,
|
|
pub memory: Option<Box<dyn AgentMemory<Message>>>,
|
|
}
|
|
|
|
impl<M, Message> Agent<M, Message>
|
|
where
|
|
M: LlmApiAdaptor<Message> + 'static,
|
|
Message: Send + 'static,
|
|
{
|
|
pub fn new(model: M) -> Self {
|
|
Self {
|
|
model,
|
|
tools: Vec::new(),
|
|
hooks: Vec::new(),
|
|
default_max_turns: 10,
|
|
system_prompt: None,
|
|
memory: None,
|
|
}
|
|
}
|
|
|
|
pub fn tool(mut self, tool: impl Tool + Send + Sync + 'static) -> Self {
|
|
self.tools.push(Box::new(tool));
|
|
self
|
|
}
|
|
|
|
pub fn system_prompt(mut self, msg: Message) -> Self {
|
|
self.system_prompt = Some(msg);
|
|
self
|
|
}
|
|
|
|
pub fn memory(mut self, mem: impl AgentMemory<Message> + 'static) -> Self {
|
|
self.memory = Some(Box::new(mem));
|
|
self
|
|
}
|
|
|
|
pub fn hook(mut self, hook: impl Hook + 'static) -> Self {
|
|
self.hooks.push(Box::new(hook));
|
|
self
|
|
}
|
|
|
|
pub fn max_turns(mut self, n: usize) -> Self {
|
|
self.default_max_turns = n;
|
|
self
|
|
}
|
|
|
|
pub fn tools(&self) -> &[Box<dyn ToolDyn>] {
|
|
&self.tools
|
|
}
|
|
|
|
pub fn prompt(&mut self, message: impl Into<Message> + Send) -> PromptRequest<'_, M, Message>
|
|
where
|
|
Message: 'static,
|
|
{
|
|
PromptRequest::new(self, message.into())
|
|
}
|
|
}
|
|
|
|
pub trait LlmApiAdaptor<Message>: Send + Sync {
|
|
fn complete<'a>(
|
|
&self,
|
|
messages: impl Iterator<Item = &'a Message> + Send,
|
|
) -> impl Future<Output = Result<String, PromptError>> + Send
|
|
where
|
|
Message: 'a;
|
|
|
|
fn build_system_message(&self, text: &str) -> Message;
|
|
|
|
fn tool_result_message(&self, tool_name: &str, output: &str) -> Message {
|
|
self.build_system_message(&format!("Tool '{tool_name}' returned: {output}"))
|
|
}
|
|
|
|
fn build_assistant_message(&self, text: &str) -> Message;
|
|
|
|
fn build_error_message(&self, error: &PromptError) -> Message {
|
|
self.tool_result_message("agent-error", &error.to_string())
|
|
}
|
|
}
|