Skip to main content

Agentic_rs/
lib.rs

1use ollama_rs::{
2    generation::{completion::request::GenerationRequest, options::GenerationOptions},
3    Ollama,
4};
5
6pub struct Cognitive {
7    engine: Ollama,
8    model: String,
9}
10pub struct Agent {
11    name: String,
12    persona: Message,
13    cognitive_resource: Cognitive,
14    memory: Vec<Message>,
15    role: Role,
16}
17
18impl Agent {
19    pub fn new(name: String, persona: Message, talent: Cognitive) -> Self {
20        Self {
21            name,
22            persona,
23            cognitive_resource: talent,
24            memory: Vec::new(),
25            role: Role::AGENT,
26        }
27    }
28    pub async fn execute(&self, task: String) -> String {
29        let text = format!("You are {:#?}.\nBackground of this task is {:#?} Please to following instruction: {:#?}\n ",self.persona,self.memory,task,);
30        let prompt = Message::new(text.to_string(), Role::AGENT);
31        self.cognitive_resource.reflect(prompt).await.to_string()
32    }
33}
34#[derive(Debug)]
35pub struct Message {
36    content: String,
37    role: Role,
38}
39impl Message {
40    pub fn new(prompt: String, role: Role) -> Self {
41        Self {
42            content: prompt,
43            role: role,
44        }
45    }
46}
47#[derive(Debug)]
48pub enum Role {
49    AGENT,
50    USER,
51    SYSTEM,
52}
53impl Cognitive {
54    pub fn new(model: String) -> Self {
55        Self {
56            engine: Ollama::default(),
57            model,
58        }
59    }
60
61    pub async fn reflect(&self, prompt: Message) -> String {
62        let options = GenerationOptions::default()
63            .temperature(0.2)
64            .repeat_penalty(1.5)
65            .top_k(25)
66            .top_p(0.25);
67
68        let res = self
69            .engine
70            .generate(GenerationRequest::new(self.model.clone(), prompt.content).options(options))
71            .await;
72
73        match res {
74            Ok(res) => res.response,
75            Err(_) => "Unknown".to_string(),
76        }
77    }
78}