|
1 | 1 | # OpenAI with .NET 10 - Getting Started Guide |
2 | | -This readme shows you how to run each OpenAI based sample (.cs) file in this folder directly without a project or additional setup using the latest .NET 10 Preview features. |
| 2 | + |
| 3 | +This readme shows you how to run each OpenAI based sample (.cs) file in this folder directly without a project or additional setup using the latest .NET 10 features. |
3 | 4 |
|
4 | 5 | ## Prerequisites |
5 | 6 |
|
6 | | -### 1. Install .NET 10 Preview |
| 7 | +### 1. Install .NET 10 |
7 | 8 |
|
| 9 | +#### Option A: Using package manager - Recommended |
8 | 10 |
|
9 | | -#### Option A: Using Windows Package Manager (winget) - Recommended |
10 | | -```powershell |
11 | | -# Install .NET 10 SDK Preview |
12 | | -winget install Microsoft.DotNet.SDK.Preview |
13 | | -``` |
| 11 | +- Windows |
| 12 | + |
| 13 | + ```powershell |
| 14 | + # Install .NET 10 SDK Preview |
| 15 | + winget install Microsoft.DotNet.SDK.Preview |
| 16 | + ``` |
| 17 | +
|
| 18 | +- Mac OS |
| 19 | +
|
| 20 | + ```bash |
| 21 | + # Install .NET 10 SDK Preview |
| 22 | + brew tap isen-ng/dotnet-sdk-versions |
| 23 | + brew install --cask dotnet-sdk10-preview |
| 24 | + ``` |
| 25 | +
|
| 26 | +#### Option B: Manual download |
14 | 27 |
|
15 | | -#### Option B: Manual Download |
16 | 28 | 1. Visit the [.NET 10 Download Page](https://dotnet.microsoft.com/download/dotnet/10.0) |
17 | | -2. Download and install: **.NET SDK 10.0 Preview** (required for development and `dotnet run`) |
| 29 | +1. Download and install: **.NET SDK 10.0 Preview** (required for development and `dotnet run`) |
18 | 30 |
|
19 | | -### 2. Verify Installation |
| 31 | +### 2. Verify installation |
20 | 32 |
|
21 | 33 | After installation, verify you have the correct versions: |
22 | 34 |
|
23 | 35 | ```powershell |
24 | 36 | # Check installed SDKs |
25 | 37 | dotnet --list-sdks |
26 | 38 |
|
27 | | -# Check version from the guides directory (should show 10.x) |
28 | | -cd docs/guides |
| 39 | +# Check version from the docs directory (should show 10.x) |
| 40 | +cd docs |
29 | 41 | dotnet --version |
30 | 42 | ``` |
31 | 43 |
|
32 | 44 | You should see output similar to: |
33 | | -``` |
34 | | -10.0.100-preview.5.25277.114 |
| 45 | + |
| 46 | +```text |
| 47 | +10.0.100-rc.1.25451.107 |
35 | 48 | ``` |
36 | 49 |
|
37 | 50 | ## Setup |
38 | 51 |
|
39 | | -### 1. Clone the Repository |
| 52 | +### 1. Clone the repository |
| 53 | + |
40 | 54 | ```powershell |
41 | 55 | git clone https://github.com/openai/openai-dotnet.git |
42 | 56 | cd openai-dotnet |
43 | 57 | ``` |
44 | 58 |
|
45 | | -### 2. Set Your OpenAI API Key |
| 59 | +### 2. Set your OpenAI API key |
46 | 60 |
|
47 | 61 | You need an OpenAI API key to run the samples. Get one from [OpenAI's API platform](https://platform.openai.com/api-keys). |
48 | 62 |
|
49 | | -#### Temporary (Current Session Only) |
| 63 | +#### Temporary (Current session only) |
| 64 | + |
| 65 | +```bash |
| 66 | +# bash/zsh |
| 67 | +export OPENAI_API_KEY="your-api-key-here" |
| 68 | +``` |
| 69 | + |
50 | 70 | ```powershell |
| 71 | +# PowerShell |
51 | 72 | $env:OPENAI_API_KEY = "your-api-key-here" |
52 | 73 | ``` |
53 | 74 |
|
54 | | -#### Permanent Options |
| 75 | +#### Permanent options |
55 | 76 |
|
56 | 77 | **Option A: Using System Properties (GUI)** |
| 78 | + |
57 | 79 | 1. Press `Win + R`, type `sysdm.cpl`, press Enter |
58 | 80 | 2. Click "Environment Variables" |
59 | 81 | 3. Under "User variables", click "New" |
60 | 82 | 4. Variable name: `OPENAI_API_KEY` |
61 | 83 | 5. Variable value: Your API key |
62 | 84 |
|
63 | 85 | **Option B: Using PowerShell (Permanent)** |
| 86 | + |
64 | 87 | ```powershell |
65 | 88 | [Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "your-api-key-here", "User") |
66 | 89 | ``` |
67 | 90 |
|
68 | 91 | **Option C: Using Command Prompt as Administrator** |
| 92 | + |
69 | 93 | ```cmd |
70 | 94 | setx OPENAI_API_KEY "your-api-key-here" |
71 | 95 | ``` |
72 | 96 |
|
73 | | -### 3. Verify Environment Variable |
| 97 | +**Option D: Using bash/zsh** |
| 98 | + |
| 99 | +```bash |
| 100 | +# bash |
| 101 | +echo 'export OPENAI_API_KEY=\"your-api-key-here\"' >> ~/.bashrc |
| 102 | +source ~/.bashrc |
| 103 | +``` |
| 104 | + |
| 105 | +```bash |
| 106 | +# zsh |
| 107 | +echo 'export OPENAI_API_KEY=\"your-api-key-here\"' >> ~/.zshrc |
| 108 | +source ~/.zshrc |
| 109 | +``` |
| 110 | + |
| 111 | +### 3. Verify environment variable |
| 112 | + |
| 113 | +```bash |
| 114 | +# bash/zsh |
| 115 | +echo $OPENAI_API_KEY |
| 116 | +``` |
| 117 | + |
74 | 118 | ```powershell |
| 119 | +# PowerShell |
75 | 120 | echo $env:OPENAI_API_KEY |
76 | 121 | ``` |
77 | 122 |
|
78 | | -## Running the Samples |
| 123 | +## Running the samples |
79 | 124 |
|
80 | 125 | The samples use .NET 10's new single-file application feature. Each `.cs` file in the guides folder is a standalone application. |
81 | 126 |
|
82 | | -### Navigate to the Guides Directory |
| 127 | +### 1. Navigate to the docs directory |
| 128 | + |
83 | 129 | ```powershell |
84 | | -cd docs/guides |
| 130 | +cd docs |
85 | 131 | ``` |
86 | 132 |
|
87 | | -### Run a Sample |
| 133 | +### 2. Run a sample |
| 134 | + |
88 | 135 | ```powershell |
89 | 136 | # Example: Run the simple chat prompt sample |
90 | | -dotnet run text/chat/chat_simpleprompt.cs |
| 137 | +dotnet run quickstart/responses/developer_quickstart.cs |
91 | 138 |
|
92 | 139 | # Run other samples |
93 | | -dotnet run text/chat/chat_instructions.cs |
94 | | -dotnet run text/chat/chat_roles.cs |
| 140 | +dotnet run guides/text/responses/responses_simpleprompt.cs |
| 141 | +dotnet run guides/text/responses/responses_roles.cs |
95 | 142 | ``` |
96 | 143 |
|
97 | | -### Expected Output |
98 | | -When you run `chat_simpleprompt.cs`, you should see output similar to: |
99 | | -``` |
| 144 | +### 3. Expected output |
| 145 | + |
| 146 | +When you run `developer_quickstart.cs`, you should see output similar to: |
| 147 | + |
| 148 | +```text |
100 | 149 | Under a velvet-purple sky, a gentle unicorn named Luna sprinkled stardust over the dreaming forest, filling every heart with peaceful, magical dreams. |
101 | 150 | ``` |
102 | 151 |
|
103 | | -## Sample File Structure |
| 152 | +## Sample file structure |
104 | 153 |
|
105 | 154 | The samples are organized as follows: |
106 | | -``` |
| 155 | + |
| 156 | +```text |
107 | 157 | docs/ |
| 158 | +├── global.json # Specifies .NET 10 preview SDK |
| 159 | +├── README.MD # Basic usage instructions |
108 | 160 | ├── guides/ |
109 | | -│ ├── global.json # Specifies .NET 10 preview SDK |
110 | | -│ ├── README.MD # Basic usage instructions |
111 | 161 | │ └── text/ |
112 | 162 | │ ├── chat/ |
113 | | -│ │ ├── chat_simpleprompt.cs # Basic chat completion |
114 | | -│ │ ├── chat_instructions.cs # Chat with system instructions |
115 | | -│ │ └── chat_roles.cs # Chat with different roles |
| 163 | +│ └── ... # Chat handling samples |
116 | 164 | │ └── responses/ |
117 | | -│ └── ... # Response handling samples |
| 165 | +│ └── ... # Response handling samples |
| 166 | +├── quickstart/ |
| 167 | +│ └── responses/ |
| 168 | +│ └── ... # Response handling samples |
118 | 169 | ``` |
119 | 170 |
|
120 | | -## Understanding the Single-File Format |
| 171 | +## Understanding the single-file format |
121 | 172 |
|
122 | 173 | Each sample file contains special directives at the top: |
123 | 174 |
|
124 | 175 | ```csharp |
125 | 176 | // SAMPLE: Description of what this sample does |
126 | | -#:package OpenAI@2.2.*-* // NuGet package reference |
127 | | -#:property PublishAot false // Build properties |
| 177 | +#:package OpenAI@2.* // NuGet package reference |
| 178 | +#:property PublishAot=false // Build properties |
128 | 179 |
|
129 | | -using OpenAI.Chat; // Regular C# code follows |
| 180 | +using OpenAI.Responses; // Regular C# code follows |
130 | 181 |
|
131 | 182 | // Your application code here... |
132 | 183 | ``` |
133 | 184 |
|
134 | 185 | ## Troubleshooting |
135 | 186 |
|
136 | 187 | ### Problem: "No package found matching input criteria" |
| 188 | + |
137 | 189 | - **Solution**: The .NET 10 preview packages might not be available yet. Try installing from the official Microsoft download page instead. |
138 | 190 |
|
139 | 191 | ### Problem: `dotnet --version` shows 9.x instead of 10.x |
| 192 | + |
140 | 193 | - **Solution**: You need to install the .NET 10 **SDK** (not just the runtime). The `global.json` file in the guides directory requires the SDK. |
141 | 194 |
|
142 | 195 | ### Problem: "Couldn't find a project to run" |
| 196 | + |
143 | 197 | - **Solution**: Make sure you're running the command from the `docs/guides` directory and providing the correct path to the `.cs` file. |
144 | 198 |
|
145 | 199 | ### Problem: "The property directive needs to have two parts" |
| 200 | + |
146 | 201 | - **Solution**: The property directive format should be `#:property PropertyName PropertyValue` (space-separated, not equals sign). |
147 | 202 |
|
148 | 203 | ### Problem: API errors |
149 | | -- **Solution**: |
| 204 | + |
| 205 | +- **Solution**: |
150 | 206 | - Verify your `OPENAI_API_KEY` environment variable is set correctly |
151 | 207 | - Check that your API key is valid and has sufficient credits |
152 | 208 | - Ensure you're using a valid model name (e.g., "gpt-4", "gpt-3.5-turbo") |
153 | 209 |
|
154 | 210 | ### Problem: Build errors about missing packages |
| 211 | + |
155 | 212 | - **Solution**: The package directives should automatically download dependencies. If not, try: |
| 213 | + |
156 | 214 | ```powershell |
157 | 215 | dotnet restore |
158 | 216 | ``` |
159 | 217 |
|
160 | | -## Additional Resources |
| 218 | +## Additional resources |
161 | 219 |
|
162 | 220 | - [OpenAI .NET SDK Documentation](https://github.com/openai/openai-dotnet) |
163 | | -- [.NET 10 Preview Documentation](https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-10) |
| 221 | +- [.NET 10 Preview Documentation](https://docs.microsoft.com/dotnet/core/whats-new/dotnet-10) |
164 | 222 | - [OpenAI API Documentation](https://platform.openai.com/docs) |
165 | 223 | - [Single-File Applications in .NET 10](https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/) |
166 | 224 |
|
167 | | -## Next Steps |
| 225 | +## Next steps |
168 | 226 |
|
169 | 227 | Once you have the basic samples working, you can: |
170 | 228 |
|
|
0 commit comments