1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
use super::*;
use crate::{client::Model, function::Functions};
use anyhow::{Context, Result};
use std::{fs::read_to_string, path::Path};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize)]
pub struct Agent {
name: String,
config: AgentConfig,
definition: AgentDefinition,
#[serde(skip)]
functions: Functions,
#[serde(skip)]
rag: Option<Arc<Rag>>,
#[serde(skip)]
model: Model,
}
impl Agent {
pub async fn init(
config: &GlobalConfig,
name: &str,
abort_signal: AbortSignal,
) -> Result<Self> {
let functions_dir = Config::agent_functions_dir(name)?;
let definition_file_path = functions_dir.join("index.yaml");
let functions_file_path = functions_dir.join("functions.json");
let rag_path = Config::agent_rag_file(name)?;
let definition = AgentDefinition::load(&definition_file_path)?;
let functions = if functions_file_path.exists() {
Functions::init(&functions_file_path)?
} else {
Functions::default()
};
let agent_config = config
.read()
.agents
.iter()
.find(|v| v.name == name)
.cloned()
.unwrap_or_else(|| AgentConfig::new(name));
let model = {
let config = config.read();
match agent_config.model_id.as_ref() {
Some(model_id) => Model::retrieve_chat(&config, model_id)?,
None => config.current_model().clone(),
}
};
let rag = if rag_path.exists() {
Some(Arc::new(Rag::load(config, "rag", &rag_path)?))
} else if !definition.documents.is_empty() {
println!("The agent has the documents, initializing RAG...");
let mut document_paths = vec![];
for path in &definition.documents {
if Rag::is_url_path(path) {
document_paths.push(path.to_string());
} else {
let new_path = safe_join_path(&functions_dir, path)
.ok_or_else(|| anyhow!("Invalid document path: '{path}'"))?;
document_paths.push(new_path.display().to_string())
}
}
Some(Arc::new(
Rag::init(config, "rag", &rag_path, &document_paths, abort_signal).await?,
))
} else {
None
};
Ok(Self {
name: name.to_string(),
config: agent_config,
definition,
functions,
rag,
model,
})
}
pub fn export(&self) -> Result<String> {
let mut value = serde_json::json!(self);
value["functions_dir"] = Config::agent_functions_dir(&self.name)?
.display()
.to_string()
.into();
value["config_dir"] = Config::agent_config_dir(&self.name)?
.display()
.to_string()
.into();
let data = serde_yaml::to_string(&value)?;
Ok(data)
}
pub fn banner(&self) -> String {
self.definition.banner()
}
pub fn name(&self) -> &str {
&self.name
}
pub fn config(&self) -> &AgentConfig {
&self.config
}
pub fn functions(&self) -> &Functions {
&self.functions
}
pub fn definition(&self) -> &AgentDefinition {
&self.definition
}
pub fn rag(&self) -> Option<Arc<Rag>> {
self.rag.clone()
}
pub fn conversation_staters(&self) -> &[String] {
&self.definition.conversation_starters
}
}
impl RoleLike for Agent {
fn to_role(&self) -> Role {
let mut role = Role::new("", &self.definition.instructions);
role.sync(self);
role
}
fn model(&self) -> &Model {
&self.model
}
fn model_mut(&mut self) -> &mut Model {
&mut self.model
}
fn temperature(&self) -> Option<f64> {
self.config.temperature
}
fn top_p(&self) -> Option<f64> {
self.config.top_p
}
fn use_tools(&self) -> Option<String> {
let common_tools = &self.definition.common_tools;
if common_tools.is_empty() {
None
} else {
Some(common_tools.join(","))
}
}
fn set_model(&mut self, model: &Model) {
self.config.model_id = Some(model.id());
self.model = model.clone();
}
fn set_temperature(&mut self, value: Option<f64>) {
self.config.temperature = value;
}
fn set_top_p(&mut self, value: Option<f64>) {
self.config.top_p = value;
}
fn set_use_tools(&mut self, _value: Option<String>) {}
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct AgentConfig {
pub name: String,
#[serde(rename(serialize = "model", deserialize = "model"))]
pub model_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dangerously_functions_filter: Option<String>,
}
impl AgentConfig {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
..Default::default()
}
}
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct AgentDefinition {
pub name: String,
#[serde(default)]
pub description: String,
#[serde(default)]
pub version: String,
pub instructions: String,
#[serde(default)]
pub conversation_starters: Vec<String>,
#[serde(default)]
pub documents: Vec<String>,
#[serde(default)]
pub common_tools: Vec<String>,
}
impl AgentDefinition {
pub fn load(path: &Path) -> Result<Self> {
let contents = read_to_string(path)
.with_context(|| format!("Failed to read agent index file at '{}'", path.display()))?;
let definition: Self = serde_yaml::from_str(&contents)
.with_context(|| format!("Failed to load agent at '{}'", path.display()))?;
Ok(definition)
}
fn banner(&self) -> String {
let AgentDefinition {
name,
description,
version,
conversation_starters,
..
} = self;
let starters = if conversation_starters.is_empty() {
String::new()
} else {
let starters = conversation_starters
.iter()
.map(|v| format!("- {v}"))
.collect::<Vec<_>>()
.join("\n");
format!(
r#"
## Conversation Starters
{starters}"#
)
};
format!(
r#"# {name} {version}
{description}{starters}"#
)
}
}
pub fn list_agents() -> Vec<String> {
list_agents_impl().unwrap_or_default()
}
fn list_agents_impl() -> Result<Vec<String>> {
let base_dir = Config::functions_dir()?;
let contents = read_to_string(base_dir.join("agents.txt"))?;
let agents = contents
.split('\n')
.filter_map(|line| {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
None
} else {
Some(line.to_string())
}
})
.collect();
Ok(agents)
}
|