llm_kit_xai/
lib.rs

1//! xAI (Grok) provider implementation for the LLM Kit.
2//!
3//! This crate provides a provider implementation for xAI's Grok models,
4//! supporting chat completions, image generation, reasoning modes, and integrated search.
5//!
6//! # Examples
7//!
8//! ## Basic Usage with Client Builder (Recommended)
9//!
10//! ```no_run
11//! use llm_kit_xai::XaiClient;
12//!
13//! // Create a provider using the client builder
14//! let provider = XaiClient::new()
15//!     .api_key("your-api-key")
16//!     .build();
17//!
18//! let model = provider.chat_model("grok-4");
19//! ```
20//!
21//! ## Alternative: Direct Instantiation
22//!
23//! ```no_run
24//! use llm_kit_xai::{XaiProvider, XaiProviderSettings};
25//!
26//! // Create a provider using settings directly
27//! let provider = XaiProvider::new(
28//!     XaiProviderSettings::new()
29//!         .with_api_key("your-api-key")
30//! );
31//!
32//! let model = provider.chat_model("grok-4");
33//! ```
34//!
35//! ## Chained Usage
36//!
37//! ```no_run
38//! use llm_kit_xai::XaiClient;
39//!
40//! let model = XaiClient::new()
41//!     .api_key("your-api-key")
42//!     .build()
43//!     .chat_model("grok-3-fast");
44//! ```
45//!
46//! ## Environment Variable
47//!
48//! ```no_run
49//! use llm_kit_xai::XaiClient;
50//!
51//! // API key will be read from XAI_API_KEY environment variable
52//! let provider = XaiClient::new().build();
53//! let model = provider.chat_model("grok-4-fast-reasoning");
54//! ```
55//!
56//! ## Tool Calling
57//!
58//! ```no_run
59//! use llm_kit_provider::LanguageModel;
60//! use llm_kit_provider::language_model::call_options::LanguageModelCallOptions;
61//! use llm_kit_provider::language_model::prompt::LanguageModelMessage;
62//! use llm_kit_provider::language_model::tool::LanguageModelTool;
63//! use llm_kit_provider::language_model::tool::function_tool::LanguageModelFunctionTool;
64//! use llm_kit_xai::XaiClient;
65//! use serde_json::json;
66//!
67//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
68//! let provider = XaiClient::new()
69//!     .api_key("your-api-key")
70//!     .build();
71//!
72//! let model = provider.chat_model("grok-beta");
73//!
74//! // Define a tool using llm-kit-provider types
75//! let weather_tool = LanguageModelFunctionTool::new(
76//!     "get_weather",
77//!     json!({
78//!         "type": "object",
79//!         "properties": {
80//!             "city": {"type": "string", "description": "The city name"}
81//!         },
82//!         "required": ["city"]
83//!     }),
84//! )
85//! .with_description("Get the current weather");
86//!
87//! let tools = vec![LanguageModelTool::Function(weather_tool)];
88//!
89//! let prompt = vec![LanguageModelMessage::user_text("What's the weather in Tokyo?")];
90//! let options = LanguageModelCallOptions::new(prompt).with_tools(tools);
91//!
92//! let result = model.do_generate(options).await?;
93//! # Ok(())
94//! # }
95//! ```
96//!
97//! For full tool execution with llm-kit-core, see the examples directory.
98//!
99//! ## Image Generation
100//!
101//! ```no_run
102//! use llm_kit_provider::ImageModel;
103//! use llm_kit_provider::image_model::call_options::ImageModelCallOptions;
104//! use llm_kit_xai::XaiClient;
105//!
106//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
107//! let provider = XaiClient::new()
108//!     .api_key("your-api-key")
109//!     .build();
110//!
111//! let model = provider.image_model("grok-2-vision-1212");
112//!
113//! let options = ImageModelCallOptions::new(
114//!     "A futuristic cityscape at sunset".to_string(),
115//!     1
116//! );
117//!
118//! let result = model.do_generate(options).await?;
119//!
120//! println!("Generated {} image(s)", result.images.len());
121//! # Ok(())
122//! # }
123//! ```
124//!
125//! For more image generation examples with llm-kit-core, see the examples directory.
126
127/// Chat completion implementation for xAI models.
128pub mod chat;
129
130/// Client builder for creating xAI providers.
131pub mod client;
132
133/// Error types for xAI provider operations.
134pub mod error;
135
136/// Provider implementation and creation functions.
137pub mod provider;
138
139/// Settings and configuration for xAI providers.
140pub mod settings;
141
142// Re-export main types from chat
143pub use chat::{
144    SearchParameters, SearchSource, XaiChatLanguageModel, XaiChatMessage, XaiChatModelId,
145    XaiProviderOptions, XaiUserContent, convert_to_xai_chat_messages,
146};
147
148pub use client::XaiClient;
149pub use error::{XaiErrorData, XaiErrorDetails};
150pub use provider::XaiProvider;
151pub use settings::XaiProviderSettings;