simple/
simple.rs

1use std::convert::Infallible;
2
3use llama_link::*;
4use llmtoolbox::{tool, ToolBox};
5
6#[derive(Debug)]
7struct ConversationTool {
8    user_name: String,
9}
10
11#[tool]
12impl ConversationTool {
13    fn new(user_name: String) -> Self {
14        Self { user_name }
15    }
16
17    /// Give a negative opinion about the topic
18    /// `topic` - What the opinion is about
19    #[tool_part]
20    fn give_negative_opinion(&self, topic: ConverstationTopic) -> String {
21        format!(
22            "Hello {}, I don't like `{}`, because `{}`",
23            self.user_name, topic.topic, topic.opinion
24        )
25    }
26
27    /// Give a positive opinion about the topic
28    /// `topic` - What the opinion is about
29    #[tool_part]
30    async fn give_positive_opinion(&self, topic: ConverstationTopic) -> String {
31        format!(
32            "Hello {}, I like `{}`, because `{}`",
33            self.user_name, topic.topic, topic.opinion
34        )
35    }
36}
37
38#[derive(serde::Deserialize, schemars::JsonSchema)]
39pub struct ConverstationTopic {
40    /// The topic being discussed
41    pub topic: String,
42    /// The opinion about the topic
43    pub opinion: String,
44}
45
46#[tokio::main]
47async fn main() {
48    let mut toolbox: ToolBox<String, Infallible> = ToolBox::new();
49    let tool = ConversationTool::new("Dave".to_owned());
50    toolbox.add_tool(tool).unwrap();
51
52    let link = LlamaLink::new("http://127.0.0.1:3756", Config::builder().build());
53    let result = link
54        .call_function(format_prompt("What do you think about canadians", &toolbox), &toolbox)
55        .await;
56    match result {
57        Ok(Ok(call_result)) => println!("{}", call_result),
58        Err(error) => panic!("{}", error),
59    }
60}
61
62// Out: "Hello Dave, I don't like `Canadians`, because `Canadians are too boring`"
63// Out: "Hello Dave, I like `Canadians`, because `I love Canadians for their kindness and respect for other cultures`"
64// Out: "Hello Dave, I don't like `Canadians`, because `Canadians are very rude to tourists.`"
65
66fn format_prompt<O, E>(user: &str, toolbox: &ToolBox<O, E>) -> String {
67    format!(
68        r#"<|begin_of_text|><|start_header_id|>system<|end_header_id|>
69You are a helpful AI assistant. Respond to the user in this json function calling format:
70    {}<|eot_id|><|start_header_id|>user<|end_header_id|>
71    {}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
72    "#,
73        serde_json::to_string(toolbox.schema()).unwrap(),
74        user
75    )
76}