|
2 | 2 | title: OpenAI |
3 | 3 | --- |
4 | 4 |
|
5 | | -AISDK supports OpenAI as a provider. You can use it to generate text, images, and other data. |
| 5 | +AISDK includes first-class support for the OpenAI. |
6 | 6 |
|
7 | | -### Quick Installation |
| 7 | +## Installation |
8 | 8 |
|
9 | | -To add the OpenAI provider, you can use the following command: |
| 9 | +Enable the OpenAI provider feature: |
10 | 10 |
|
11 | 11 | ```bash |
12 | 12 | cargo add aisdk --features openai |
13 | 13 | ``` |
14 | 14 |
|
15 | | -### Manual Installation |
| 15 | +This installs AISDK with the OpenAI provider enabled. |
16 | 16 |
|
17 | | -To add the OpenAI provider manually, you can enable the `openai` feature in your `Cargo.toml` file: |
| 17 | +Once you have enabled the OpenAI provider, you can use all aisdk <Link href="/docs#core-features">features</Link> with it. |
18 | 18 |
|
19 | | -```toml |
20 | | -[dependencies] |
21 | | -aisdk = { version = "0.1.0", features = ["openai"] } |
22 | | -``` |
| 19 | +### Quick Start |
| 20 | + |
| 21 | +Create an OpenAI provider instance with default settings: |
| 22 | + |
| 23 | +```rust |
| 24 | +use aisdk::providers::openai::OpenAI; |
23 | 25 |
|
24 | | -This will install the OpenAI provider and its dependencies. |
| 26 | +let openai = OpenAI::new("gpt-5"); |
| 27 | +``` |
25 | 28 |
|
26 | | -### Usage |
| 29 | +This initializes the provider with: |
27 | 30 |
|
28 | | -Once you have installed the OpenAI provider, you can use all aisdk <Link href="/docs#core-features">features</Link> with it. |
| 31 | +* Model: `"gpt-5"` |
| 32 | +* API key from environment (if set with `OPENAI_API_KEY`) |
| 33 | +* OpenAI’s default base URL |
29 | 34 |
|
30 | | -#### Example Generating Text |
| 35 | +## Basic Text Generation |
31 | 36 |
|
32 | 37 | ```rust |
33 | 38 | use aisdk::{ |
34 | | - core::{LanguageModelRequest}, |
| 39 | + core::LanguageModelRequest, |
35 | 40 | providers::openai::OpenAI, |
36 | 41 | }; |
37 | 42 |
|
38 | 43 | #[tokio::main] |
39 | 44 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
40 | 45 |
|
41 | | - // with default openai provider settings |
| 46 | + // Initialize provider with default settings. |
42 | 47 | let openai = OpenAI::new("gpt-5"); |
43 | 48 |
|
44 | | - let result = LanguageModelRequest::builder() |
| 49 | + let response = LanguageModelRequest::builder() |
45 | 50 | .model(openai) |
46 | | - .prompt("hello world") |
| 51 | + .prompt("Write a short poem about Rust.") |
47 | 52 | .build() |
48 | 53 | .generate_text() |
49 | | - .await?; |
| 54 | + .await? |
| 55 | + .text()?; |
50 | 56 |
|
51 | | - println!("{}", result.text.unwrap()); |
| 57 | + println!("Model output: {}", response); |
52 | 58 | Ok(()) |
53 | 59 | } |
54 | 60 | ``` |
55 | 61 |
|
56 | | -This will generate a response from the OpenAI provider. |
57 | | - |
58 | 62 | ## Provider Settings |
59 | 63 |
|
60 | | -You can customize the provider settings by passing them as arguments to the `OpenAI::builder()` method. |
| 64 | +You can customize provider configuration using `OpenAI::builder()` |
61 | 65 |
|
62 | | -#### API Key |
63 | | - |
64 | | -To set the API key, you can use the `api_key` method: |
| 66 | +### API Key |
65 | 67 |
|
66 | 68 | ```rust |
67 | 69 | let openai = OpenAI::builder() |
68 | 70 | .api_key("your-api-key") |
69 | 71 | .build()?; |
70 | 72 | ``` |
71 | 73 |
|
72 | | -#### Base URL |
| 74 | +If not specified, AISDK uses the `OPENAI_API_KEY` environment variable. |
| 75 | + |
| 76 | +### Base URL |
73 | 77 |
|
74 | | -To set the base URL, you can use the `base_url` method: |
| 78 | +Useful when routing through a proxy, gateway, or self-hosted compatible endpoint. |
75 | 79 |
|
76 | 80 | ```rust |
77 | 81 | let openai = OpenAI::builder() |
78 | 82 | .base_url("https://api.openai.com/v1") |
79 | 83 | .build()?; |
80 | 84 | ``` |
81 | 85 |
|
82 | | -#### Provider Name |
| 86 | +### Provider Name |
83 | 87 |
|
84 | | -To set the provider name, you can use the `provider_name` method: |
85 | | -This is used for observability and logging purposes. |
| 88 | +For logging, analytics, and observability. |
86 | 89 |
|
87 | 90 | ```rust |
88 | 91 | let openai = OpenAI::builder() |
89 | 92 | .provider_name("OpenAI") |
90 | 93 | .build()?; |
91 | 94 | ``` |
92 | 95 |
|
93 | | -#### Model Name |
| 96 | +### Model Name |
94 | 97 |
|
95 | | -To set the model name, you can use the `model_name` method: |
| 98 | +Set a default model for all requests using this provider instance: |
96 | 99 |
|
97 | 100 | ```rust |
98 | 101 | let openai = OpenAI::builder() |
99 | 102 | .model_name("gpt-4o") |
100 | 103 | .build()?; |
101 | 104 | ``` |
102 | 105 |
|
| 106 | +### Full Custom Configuration Example |
103 | 107 |
|
| 108 | +```rust |
| 109 | +let openai = OpenAI::builder() |
| 110 | + .api_key("your-api-key") |
| 111 | + .base_url("https://api.openai.com/v1") |
| 112 | + .provider_name("OpenAI") |
| 113 | + .model_name("gpt-4o") |
| 114 | + .build()?; |
| 115 | +``` |
0 commit comments