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
// use crate::interface::{Plugin, PluginCategory, PluginInfo};
// use amico::ai::services::CompletionService;
// use amico::core::action_map::ActionMap;
// use amico::core::model::Model;
// use amico_core::errors::ActionSelectorError;
// use amico_core::traits::Action;
// use amico_core::types::AgentEvent;
// use futures::executor::block_on;
// /// A Standard Implementation of the ActionSelector Plugin.
// pub struct ActionSelector<S>
// where
// S: CompletionService,
// {
// // Actions
// pub actions_map: ActionMap,
// pub service: S,
// pub model: Box<dyn Model>,
// }
// // Implement the Plugin trait for the ActionSelector struct
// impl<S> Plugin for ActionSelector<S>
// where
// S: CompletionService,
// {
// fn info(&self) -> &'static PluginInfo {
// &PluginInfo {
// name: "StandardActionSelector",
// category: PluginCategory::ActionSelector,
// }
// }
// }
// // Implement the ActionSelector trait for the ActionSelector struct
// impl<S> amico_core::traits::ActionSelector for ActionSelector<S>
// where
// S: CompletionService,
// {
// // Temporarily ignore the events
// fn select_action(
// &mut self,
// events: Vec<AgentEvent>,
// ) -> Result<(Box<dyn Action>, Vec<u32>), ActionSelectorError> {
// // Update the prompt
// let prompt = format!(
// "Instruction - Return the correct action for the current state \
// and return the id of the events that is going to be solved.\n\
// Context - {}\n\
// Received Events - {}",
// self.model.get_environment_description(),
// serde_json::to_string(&events).unwrap()
// )
// .to_string();
// // Call the asynchronous method to get the response text
// let response = match block_on(self.service.generate_text(prompt)) {
// Ok(res) => res,
// Err(e) => {
// return Err(ActionSelectorError::SelectingActionFailed(format!(
// "Failed to generate text: {}",
// e
// )));
// }
// };
// // Try to parse the response as JSON
// let json_response: serde_json::Value = match serde_json::from_str(&response) {
// Ok(json) => json,
// Err(e) => {
// return Err(ActionSelectorError::SelectingActionFailed(format!(
// "Failed to parse the response as JSON: {}",
// e
// )));
// }
// };
// // Extract the action name and parameters from the JSON response
// if let (Some(action_name), Some(parameters), Some(event_ids)) = (
// json_response["name"].as_str(),
// json_response["parameters"].as_object(),
// json_response["event_ids"].as_array(),
// ) {
// // Check if the action exists in the actions_map
// if let Some(mut action) = self.actions_map.get(action_name) {
// action.set_parameters(serde_json::Value::Object(parameters.clone()));
// // Return the action and the Vec<u32>
// return Ok((
// Box::new(action),
// event_ids
// .iter()
// .map(|id| id.as_u64().unwrap() as u32)
// .collect(),
// ));
// }
// }
// // Return a default action and an empty vector if the JSON response is not valid
// Err(ActionSelectorError::SelectingActionFailed(
// "Failed to select an action from the response".to_string(),
// ))
// }
// }
// /// Implement the ActionSelector struct
// impl<S> ActionSelector<S>
// where
// S: CompletionService,
// {
// /// Create a new instance of the ActionSelector struct.
// pub fn new(actions_map: ActionMap, service: S, model: Box<dyn Model>) -> Self {
// Self {
// actions_map,
// service,
// model,
// }
// }
// // /// Update the system prompt.
// // fn update_system_prompt(&mut self) {
// // // Set the system prompt
// // let prompt = r#"You are an Action Selector to select actions to execute in an agent.
// // You will be provided with information of the environment, the state of the current agent
// // and the events that are received. Make the best decision based on these information.
// // Don't output the reason of choosing the action. Just output the
// // name, the parameters of the action you choose and the event ids you solved."#;
// // // An example output to be shown in the system prompt
// // let example_output = r#"{
// // "name": "clean",
// // "parameters": {
// // "room": "kitchen"
// // },
// // "event_ids": [1, 2, 3]
// // }"#;
// // // The final prompt to be set in the system
// // let final_prompt = format!(
// // "{}\n\
// // Here is an example of the output:{}\n\
// // Here are the available actions:{}",
// // prompt.trim(),
// // example_output,
// // self.actions_map.describe()
// // );
// // // Set the system prompt
// // self.service.mut_ctx().update(move |ctx| {
// // ctx.system_prompt = final_prompt.to_string();
// // });
// // }
// // /// Set the AI service for the ActionSelector.
// // pub fn set_service(&mut self, service: S) {
// // // Set the service
// // self.service = service;
// // // Update the system prompt
// // self.update_system_prompt();
// // }
// }