|
| 1 | +# ✨ 𝕏-AI |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +> **𝕏-AI** is a **complete SDK** and a WIP **CLI/TUI** that provides a powerful and intuitive interface to interact with the [**X-AI API**](https://docs.x.ai/api/). |
| 10 | +
|
| 11 | +--- |
| 12 | + |
| 13 | +## 📜 Table of Contents |
| 14 | + |
| 15 | +1. [Features](#features) |
| 16 | +2. [Installation](#installation) |
| 17 | +3. [Usage Examples](#usage-examples) |
| 18 | + - [Fetch API Key Information 🔑](#fetch-api-key-information-) |
| 19 | + - [Chat Completions 💬](#chat-completions-) |
| 20 | + - [Text Completions 📝](#text-completions-) |
| 21 | + - [Embedding Creation 📊](#embedding-creation-) |
| 22 | + - [List Models 📜](#list-models-) |
| 23 | +4. [License](#license) |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## ✨ Features |
| 28 | + |
| 29 | +- Fetch API Key Information 🔑 |
| 30 | +- Chat Completions 💬 |
| 31 | +- Text Completions 📝 |
| 32 | +- Embedding Creation 📊 |
| 33 | +- Fetch Model Information 🧐 |
| 34 | +- List Embedding Models 📜 |
| 35 | +- Fetch Language Model Details 🌐 |
| 36 | +- List Language Models 🗃️ |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## 📦 Installation |
| 41 | + |
| 42 | +Add the following to your `Cargo.toml`: |
| 43 | + |
| 44 | +```toml |
| 45 | +[dependencies] |
| 46 | +x_ai = "0.0.1" |
| 47 | +tokio = { version = "1.41.1", features = ["full"] } |
| 48 | +``` |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## 🛠️ Usage Examples |
| 53 | + |
| 54 | +### Fetch API Key Information 🔑 |
| 55 | + |
| 56 | +```rust |
| 57 | +use std::env; |
| 58 | +use x_ai::api_key::ApiKeyRequestBuilder; |
| 59 | +use x_ai::client::XaiClient; |
| 60 | +use x_ai::traits::ApiKeyFetcher; |
| 61 | +use x_ai::traits::ClientConfig; |
| 62 | + |
| 63 | +#[tokio::main] |
| 64 | +async fn main() { |
| 65 | + let client = XaiClient::builder() |
| 66 | + .build() |
| 67 | + .expect("Failed to build XaiClient"); |
| 68 | + |
| 69 | + client.set_api_key( |
| 70 | + env::var("XAI_API_KEY") |
| 71 | + .expect("XAI_API_KEY must be set!") |
| 72 | + .to_string(), |
| 73 | + ); |
| 74 | + |
| 75 | + let request_builder = ApiKeyRequestBuilder::new(client); |
| 76 | + |
| 77 | + let result = request_builder.fetch_api_key_info().await; |
| 78 | + |
| 79 | + match result { |
| 80 | + Ok(api_key_info) => println!("API Key ID: {}", api_key_info.api_key_id), |
| 81 | + Err(err) => eprintln!("Error fetching API key info: {:?}", err), |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Output |
| 86 | + |
| 87 | +// API Key ID: 06e3dd6...5e7f61 |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +### Chat Completions 💬 |
| 93 | + |
| 94 | +```rust |
| 95 | +use std::env; |
| 96 | +use x_ai::chat_compl::ChatCompletionsRequestBuilder; |
| 97 | +use x_ai::client::XaiClient; |
| 98 | +use x_ai::traits::ChatCompletionsFetcher; |
| 99 | +use x_ai::chat_compl::Message; |
| 100 | +use x_ai::traits::ClientConfig; |
| 101 | + |
| 102 | +#[tokio::main] |
| 103 | +async fn main() { |
| 104 | + let client = XaiClient::builder() |
| 105 | + .build() |
| 106 | + .expect("Failed to build XaiClient"); |
| 107 | + |
| 108 | + client.set_api_key( |
| 109 | + env::var("XAI_API_KEY") |
| 110 | + .expect("XAI_API_KEY must be set!") |
| 111 | + .to_string(), |
| 112 | + ); |
| 113 | + |
| 114 | + let messages = vec![ |
| 115 | + Message { |
| 116 | + role: "system".to_string(), |
| 117 | + content: "You are Grok, a chatbot inspired by the Hitchhiker's Guide to the Galaxy." |
| 118 | + .to_string(), |
| 119 | + }, |
| 120 | + Message { |
| 121 | + role: "user".to_string(), |
| 122 | + content: "What is the answer to life and the universe?".to_string(), |
| 123 | + }, |
| 124 | + ]; |
| 125 | + |
| 126 | + let request_builder = |
| 127 | + ChatCompletionsRequestBuilder::new(client.clone(), "grok-beta".to_string(), messages) |
| 128 | + .temperature(0.0) |
| 129 | + .stream(false); |
| 130 | + |
| 131 | + let request = request_builder |
| 132 | + .clone() |
| 133 | + .build() |
| 134 | + .expect("Failed to build request"); |
| 135 | + |
| 136 | + let response = request_builder.create_chat_completion(request).await; |
| 137 | + match response { |
| 138 | + Ok(completion) => { |
| 139 | + println!("Chatbot Response: {}", completion.choices[0].message.content); |
| 140 | + } |
| 141 | + Err(err) => eprintln!("Error: {:?}", err), |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// Output |
| 146 | + |
| 147 | +// Chatbot Response: The answer to life, the universe, and everything is **42**. However, this answer is famously incomplete without knowing the question, which remains unknown. This concept comes from Douglas Adams' "The Hitchhiker's Guide to the Galaxy." If you're looking for deeper meaning or a more personal answer, I'd say it's about finding what gives your life purpose and joy, which can be quite different for everyone. What do you think might be your personal answer to life and the universe? |
| 148 | +``` |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +### Text Completions 📝 |
| 153 | + |
| 154 | +```rust |
| 155 | +use std::env; |
| 156 | +use x_ai::client::XaiClient; |
| 157 | +use x_ai::completions::CompletionsRequestBuilder; |
| 158 | +use x_ai::traits::{ClientConfig, CompletionsFetcher}; |
| 159 | + |
| 160 | +#[tokio::main] |
| 161 | +async fn main() { |
| 162 | + let client = XaiClient::builder() |
| 163 | + .build() |
| 164 | + .expect("Failed to build XaiClient"); |
| 165 | + |
| 166 | + client.set_api_key( |
| 167 | + env::var("XAI_API_KEY") |
| 168 | + .expect("XAI_API_KEY must be set!") |
| 169 | + .to_string(), |
| 170 | + ); |
| 171 | + |
| 172 | + let request_builder = CompletionsRequestBuilder::new( |
| 173 | + client.clone(), |
| 174 | + "grok-beta".to_string(), |
| 175 | + "Write a short poem about Rust programming.".to_string(), |
| 176 | + ) |
| 177 | + .temperature(0.7) |
| 178 | + .max_tokens(50); |
| 179 | + |
| 180 | + let request = request_builder.clone() |
| 181 | + .build() |
| 182 | + .expect("Failed to build request"); |
| 183 | + |
| 184 | + let response = request_builder.create_completions(request).await; |
| 185 | + |
| 186 | + match response { |
| 187 | + Ok(completion) => println!("Generated Text: {}", completion.choices[0].text), |
| 188 | + Err(err) => eprintln!("Error: {:?}", err), |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// Output |
| 193 | + |
| 194 | +// Generated Text: Make the poem rhyme. |
| 195 | + |
| 196 | +// In the land of code, a language so bright, |
| 197 | +// Rust emerges with all its might. |
| 198 | +// With safety and speed, it's quite the sight, |
| 199 | +// Guarding memory with all its might. |
| 200 | + |
| 201 | +// Fear not the bugs, nor the seg |
| 202 | +``` |
| 203 | + |
| 204 | +--- |
| 205 | + |
| 206 | +### Embedding Creation 📊 |
| 207 | + |
| 208 | +```rust |
| 209 | +use std::env; |
| 210 | +use x_ai::client::XaiClient; |
| 211 | +use x_ai::embedding::EmbeddingRequestBuilder; |
| 212 | +use x_ai::traits::ClientConfig; |
| 213 | +use x_ai::traits::EmbeddingFetcher; |
| 214 | + |
| 215 | +#[tokio::main] |
| 216 | +async fn main() { |
| 217 | + let client = XaiClient::builder() |
| 218 | + .build() |
| 219 | + .expect("Failed to build XaiClient"); |
| 220 | + |
| 221 | + client.set_api_key( |
| 222 | + env::var("XAI_API_KEY") |
| 223 | + .expect("XAI_API_KEY must be set!") |
| 224 | + .to_string(), |
| 225 | + ); |
| 226 | + |
| 227 | + let input_texts = vec!["Hello, world!".to_string(), "Rust is awesome!".to_string()]; |
| 228 | + let model = "text-embedding-3-small".to_string(); |
| 229 | + let encoding_format = "float32".to_string(); |
| 230 | + |
| 231 | + let request_builder = |
| 232 | + EmbeddingRequestBuilder::new(client.clone(), model, input_texts, encoding_format); |
| 233 | + |
| 234 | + let request = request_builder |
| 235 | + .clone() |
| 236 | + .build() |
| 237 | + .expect("Failed to build request"); |
| 238 | + |
| 239 | + let response = request_builder.create_embedding(request).await; |
| 240 | + |
| 241 | + match response { |
| 242 | + Ok(embedding) => println!("Embedding Data: {:?}", embedding.data), |
| 243 | + Err(err) => eprintln!("Error: {:?}", err), |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +// Output |
| 248 | + |
| 249 | +// TODO |
| 250 | +``` |
| 251 | + |
| 252 | +--- |
| 253 | + |
| 254 | +### List Models 📜 |
| 255 | + |
| 256 | +```rust |
| 257 | +use std::env; |
| 258 | +use x_ai::client::XaiClient; |
| 259 | +use x_ai::list_mod::ReducedModelListRequestBuilder; |
| 260 | +use x_ai::traits::ClientConfig; |
| 261 | +use x_ai::traits::ListModelFetcher; |
| 262 | + |
| 263 | +#[tokio::main] |
| 264 | +async fn main() { |
| 265 | + let client = XaiClient::builder() |
| 266 | + .build() |
| 267 | + .expect("Failed to build XaiClient"); |
| 268 | + |
| 269 | + client.set_api_key( |
| 270 | + env::var("XAI_API_KEY") |
| 271 | + .expect("XAI_API_KEY must be set!") |
| 272 | + .to_string(), |
| 273 | + ); |
| 274 | + |
| 275 | + let request_builder = ReducedModelListRequestBuilder::new(client); |
| 276 | + |
| 277 | + let result = request_builder.fetch_model_info().await; |
| 278 | + |
| 279 | + match result { |
| 280 | + Ok(model_list) => { |
| 281 | + for model in model_list.data { |
| 282 | + println!("Model ID: {}, Owned By: {}", model.id, model.owned_by); |
| 283 | + } |
| 284 | + } |
| 285 | + Err(err) => eprintln!("Error fetching models: {:?}", err), |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +// Output |
| 290 | + |
| 291 | +// Model ID: grok-beta, Owned By: xai |
| 292 | +// Model ID: grok-vision-beta, Owned By: xai |
| 293 | +``` |
| 294 | + |
| 295 | +--- |
| 296 | + |
| 297 | +## 📜 License |
| 298 | + |
| 299 | +This crate is licensed under the MIT License. See the [`LICENSE`](LICENSE) file for details. |
0 commit comments