Skip to content

Commit 39ba887

Browse files
author
Arun Sekhar
committed
Fix .NET 10 property directive format in sample files
- Updated property directive format from 'PropertyName=Value' to 'PropertyName Value' - Fixed all sample .cs files in docs/guides to work with .NET 10 preview - Added comprehensive getting started documentation in docs/README.md - Enables successful execution of single-file C# applications with dotnet run
1 parent 94608bb commit 39ba887

File tree

9 files changed

+180
-12
lines changed

9 files changed

+180
-12
lines changed

docs/guides/README.MD

Lines changed: 172 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,178 @@
1-
# OpenAI Documentation Examples for .NET
1+
# OpenAI .NET SDK - Getting Started Guide
2+
3+
This guide will help you run the OpenAI .NET SDK based code samples (.cs files) in this folder directly without a project using the latest .NET 10 preview features.
4+
5+
## Prerequisites
6+
7+
### 1. Install .NET 10 Preview
8+
9+
You need both the .NET 10 runtime and SDK to run these samples.
10+
11+
#### Option A: Using Windows Package Manager (winget) - Recommended
12+
```powershell
13+
# Install .NET 10 SDK Preview
14+
winget install Microsoft.DotNet.SDK.Preview
15+
```
16+
17+
#### Option B: Manual Download
18+
1. Visit the [.NET 10 Download Page](https://dotnet.microsoft.com/download/dotnet/10.0)
19+
2. Download and install: **.NET SDK 10.0 Preview** (required for development and `dotnet run`)
20+
21+
### 2. Verify Installation
22+
23+
After installation, verify you have the correct versions:
24+
25+
```powershell
26+
# Check installed SDKs
27+
dotnet --list-sdks
28+
29+
# Check version from the guides directory (should show 10.x)
30+
cd docs/guides
31+
dotnet --version
32+
```
33+
34+
You should see output similar to:
35+
```
36+
10.0.100-preview.5.25277.114
37+
```
38+
39+
## Setup
40+
41+
### 1. Clone the Repository
42+
```powershell
43+
git clone https://github.com/openai/openai-dotnet.git
44+
cd openai-dotnet
45+
```
46+
47+
### 2. Set Your OpenAI API Key
48+
49+
You need an OpenAI API key to run the samples. Get one from [OpenAI's API platform](https://platform.openai.com/api-keys).
50+
51+
#### Temporary (Current Session Only)
52+
```powershell
53+
$env:OPENAI_KEY = "your-api-key-here"
54+
```
55+
56+
#### Permanent Options
57+
58+
**Option A: Using System Properties (GUI)**
59+
1. Press `Win + R`, type `sysdm.cpl`, press Enter
60+
2. Click "Environment Variables"
61+
3. Under "User variables", click "New"
62+
4. Variable name: `OPENAI_KEY`
63+
5. Variable value: Your API key
64+
65+
**Option B: Using PowerShell (Permanent)**
66+
```powershell
67+
[Environment]::SetEnvironmentVariable("OPENAI_KEY", "your-api-key-here", "User")
68+
```
69+
70+
**Option C: Using Command Prompt as Administrator**
71+
```cmd
72+
setx OPENAI_KEY "your-api-key-here"
73+
```
74+
75+
### 3. Verify Environment Variable
76+
```powershell
77+
echo $env:OPENAI_KEY
78+
```
279

380
## Running the Samples
481

5-
The samples use a new .NET 10 feature that allows [running single C# file applications](https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/). Each file in this folder is a standalone application.
82+
The samples use .NET 10's new single-file application feature. Each `.cs` file in the guides folder is a standalone application.
83+
84+
### Navigate to the Guides Directory
85+
```powershell
86+
cd docs/guides
87+
```
88+
89+
### Run a Sample
90+
```powershell
91+
# Example: Run the simple chat prompt sample
92+
dotnet run text/chat/chat_simpleprompt.cs
93+
94+
# Run other samples
95+
dotnet run text/chat/chat_instructions.cs
96+
dotnet run text/chat/chat_roles.cs
97+
```
98+
99+
### Expected Output
100+
When you run `chat_simpleprompt.cs`, you should see output similar to:
101+
```
102+
Under a velvet-purple sky, a gentle unicorn named Luna sprinkled stardust over the dreaming forest, filling every heart with peaceful, magical dreams.
103+
```
104+
105+
## Sample File Structure
106+
107+
The samples are organized as follows:
108+
```
109+
docs/
110+
├── guides/
111+
│ ├── global.json # Specifies .NET 10 preview SDK
112+
│ ├── README.MD # Basic usage instructions
113+
│ └── text/
114+
│ ├── chat/
115+
│ │ ├── chat_simpleprompt.cs # Basic chat completion
116+
│ │ ├── chat_instructions.cs # Chat with system instructions
117+
│ │ └── chat_roles.cs # Chat with different roles
118+
│ └── responses/
119+
│ └── ... # Response handling samples
120+
```
121+
122+
## Understanding the Single-File Format
123+
124+
Each sample file contains special directives at the top:
125+
126+
```csharp
127+
// SAMPLE: Description of what this sample does
128+
#:package [email protected].*-* // NuGet package reference
129+
#:property PublishAot false // Build properties
130+
131+
using OpenAI.Chat; // Regular C# code follows
132+
133+
// Your application code here...
134+
```
135+
136+
## Troubleshooting
137+
138+
### Problem: "No package found matching input criteria"
139+
- **Solution**: The .NET 10 preview packages might not be available yet. Try installing from the official Microsoft download page instead.
140+
141+
### Problem: `dotnet --version` shows 9.x instead of 10.x
142+
- **Solution**: You need to install the .NET 10 **SDK** (not just the runtime). The `global.json` file in the guides directory requires the SDK.
143+
144+
### Problem: "Couldn't find a project to run"
145+
- **Solution**: Make sure you're running the command from the `docs/guides` directory and providing the correct path to the `.cs` file.
146+
147+
### Problem: "The property directive needs to have two parts"
148+
- **Solution**: The property directive format should be `#:property PropertyName PropertyValue` (space-separated, not equals sign).
149+
150+
### Problem: API errors
151+
- **Solution**:
152+
- Verify your `OPENAI_KEY` environment variable is set correctly
153+
- Check that your API key is valid and has sufficient credits
154+
- Ensure you're using a valid model name (e.g., "gpt-4", "gpt-3.5-turbo")
155+
156+
### Problem: Build errors about missing packages
157+
- **Solution**: The package directives should automatically download dependencies. If not, try:
158+
```powershell
159+
dotnet restore
160+
```
161+
162+
## Additional Resources
163+
164+
- [OpenAI .NET SDK Documentation](https://github.com/openai/openai-dotnet)
165+
- [.NET 10 Preview Documentation](https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-10)
166+
- [OpenAI API Documentation](https://platform.openai.com/docs)
167+
- [Single-File Applications in .NET 10](https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/)
168+
169+
## Next Steps
6170

7-
To run them, you need to install the .NET 10 preview.
171+
Once you have the basic samples working, you can:
8172

9-
Then, from the command line, you can run them using the `dotnet run <sample_name>` command, for example: `dotnet run s1-chat_simpleprompt.cs`.
173+
1. **Explore other samples** in the `text/` directory
174+
2. **Modify the prompts** in the sample files to experiment with different outputs
175+
3. **Create your own samples** following the same single-file format
176+
4. **Integrate the OpenAI SDK** into your own .NET applications
10177

178+
Happy coding with OpenAI and .NET 10! 🚀

docs/guides/text/chat/chat_instructions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SAMPLE: Generate text with messages using different roles
22
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
3+
#:property PublishAot false
44

55
using OpenAI.Chat;
66

docs/guides/text/chat/chat_roles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SAMPLE: Generate text with messages using different roles
22
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
3+
#:property PublishAot false
44

55
using OpenAI.Chat;
66

docs/guides/text/chat/chat_simpleprompt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SAMPLE: Generate text from a simple prompt
22
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
3+
#:property PublishAot false
44

55
using OpenAI.Chat;
66

docs/guides/text/responses/responses_fileinput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SAMPLE: Prompt template with file input variable
22
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
3+
#:property PublishAot false
44

55
using OpenAI.Responses;
66
using OpenAI.Files;

docs/guides/text/responses/responses_instructions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SAMPLE: Generate text with instructions
22
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
3+
#:property PublishAot false
44

55
using OpenAI.Responses;
66

docs/guides/text/responses/responses_prompttemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SAMPLE: Generate text with a prompt template
22
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
3+
#:property PublishAot false
44

55
using OpenAI.Responses;
66
using System.ClientModel;

docs/guides/text/responses/responses_roles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SAMPLE: Generate text with messages using different roles
22
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
3+
#:property PublishAot false
44

55
using OpenAI.Responses;
66

docs/guides/text/responses/responses_simpleprompt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SAMPLE: Generate text from a simple prompt
22
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
3+
#:property PublishAot false
44

55
using OpenAI.Responses;
66

0 commit comments

Comments
 (0)