Skip to main content

use_xai_tools/
use_xai_tools.rs

1use anyhow::Result;
2use schemars::JsonSchema;
3use serde::Deserialize;
4use serde::Serialize;
5
6use allms::{
7    llm::{
8        tools::{LLMTools, XAIWebSearchConfig, XAIXSearchConfig},
9        XAIModels,
10    },
11    Completions,
12};
13
14// Example 1: Web search
15#[derive(Deserialize, Serialize, JsonSchema, Debug, Clone)]
16struct AINewsArticles {
17    pub articles: Vec<AINewsArticle>,
18}
19
20#[derive(Deserialize, Serialize, JsonSchema, Debug, Clone)]
21struct AINewsArticle {
22    pub title: String,
23    pub url: String,
24    pub description: String,
25}
26
27// Example 2: X Search
28#[derive(Deserialize, Serialize, JsonSchema, Debug, Clone)]
29struct XPosts {
30    pub posts: Vec<XPost>,
31}
32
33#[derive(Deserialize, Serialize, JsonSchema, Debug, Clone)]
34struct XPost {
35    pub author: String,
36    pub content: String,
37    pub url: Option<String>,
38    pub date: Option<String>,
39}
40
41#[tokio::main]
42async fn main() -> Result<()> {
43    env_logger::init();
44
45    let xai_api_key: String = std::env::var("XAI_API_KEY").expect("XAI_API_KEY not set");
46
47    // Example 1: Web search example with domain filters
48    let web_search_config = XAIWebSearchConfig::new()
49        .add_allowed_domains(&["techcrunch.com".to_string(), "wired.com".to_string()])
50        .with_enable_image_understanding(true);
51
52    let web_search_tool = LLMTools::XAIWebSearch(web_search_config);
53    let xai_responses =
54        Completions::new(XAIModels::Grok4_1FastNonReasoning, &xai_api_key, None, None)
55            .add_tool(web_search_tool);
56
57    match xai_responses
58        .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models. 
59        For each news item, provide the title, url, and a short description.")
60        .await
61    {
62        Ok(response) => println!("AI news articles:\n{:#?}", response),
63        Err(e) => eprintln!("Error: {:?}", e),
64    }
65
66    // Example 2: X Search example with date range and handle filters
67    let x_search_config = XAIXSearchConfig::new()
68        .from_date("2025-01-01".to_string())
69        .to_date("2025-12-31".to_string())
70        .add_allowed_x_handles(&["@elonmusk".to_string(), "@OpenAI".to_string()])
71        .enable_image_understanding(true)
72        .enable_video_understanding(true);
73
74    let x_search_tool = LLMTools::XAIXSearch(x_search_config);
75    let xai_responses_x =
76        Completions::new(XAIModels::Grok4_1FastReasoning, &xai_api_key, None, None)
77            .add_tool(x_search_tool);
78
79    match xai_responses_x
80        .get_answer::<XPosts>(
81            "Find up to 5 recent posts about AI and machine learning from the specified accounts. 
82        For each post, provide the author handle, content, and if available, the URL and date.",
83        )
84        .await
85    {
86        Ok(response) => println!("X posts:\n{:#?}", response),
87        Err(e) => eprintln!("Error: {:?}", e),
88    }
89
90    Ok(())
91}