Skip to content

Commit b8d4d6d

Browse files
achandmsftArun Sekhar
andauthored
Fix .NET 10 property directive format in sample files (#497)
* 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 * Update guides README.MD with simplified instructions - Streamlined the getting started guide for better clarity - Focused on essential steps for running .NET 10 samples - Improved readability and user experience * Add guidance comments to all sample files - Added '// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start' to all .cs files - Provides users with quick access to getting started documentation - Consistent guidance across all sample files --------- Co-authored-by: Arun Sekhar <[email protected]>
1 parent 94608bb commit b8d4d6d

File tree

9 files changed

+186
-12
lines changed

9 files changed

+186
-12
lines changed

docs/guides/README.MD

Lines changed: 170 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,176 @@
1-
# OpenAI Documentation Examples for .NET
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.
3+
4+
## Prerequisites
5+
6+
### 1. Install .NET 10 Preview
7+
8+
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+
```
14+
15+
#### Option B: Manual Download
16+
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`)
18+
19+
### 2. Verify Installation
20+
21+
After installation, verify you have the correct versions:
22+
23+
```powershell
24+
# Check installed SDKs
25+
dotnet --list-sdks
26+
27+
# Check version from the guides directory (should show 10.x)
28+
cd docs/guides
29+
dotnet --version
30+
```
31+
32+
You should see output similar to:
33+
```
34+
10.0.100-preview.5.25277.114
35+
```
36+
37+
## Setup
38+
39+
### 1. Clone the Repository
40+
```powershell
41+
git clone https://github.com/openai/openai-dotnet.git
42+
cd openai-dotnet
43+
```
44+
45+
### 2. Set Your OpenAI API Key
46+
47+
You need an OpenAI API key to run the samples. Get one from [OpenAI's API platform](https://platform.openai.com/api-keys).
48+
49+
#### Temporary (Current Session Only)
50+
```powershell
51+
$env:OPENAI_KEY = "your-api-key-here"
52+
```
53+
54+
#### Permanent Options
55+
56+
**Option A: Using System Properties (GUI)**
57+
1. Press `Win + R`, type `sysdm.cpl`, press Enter
58+
2. Click "Environment Variables"
59+
3. Under "User variables", click "New"
60+
4. Variable name: `OPENAI_KEY`
61+
5. Variable value: Your API key
62+
63+
**Option B: Using PowerShell (Permanent)**
64+
```powershell
65+
[Environment]::SetEnvironmentVariable("OPENAI_KEY", "your-api-key-here", "User")
66+
```
67+
68+
**Option C: Using Command Prompt as Administrator**
69+
```cmd
70+
setx OPENAI_KEY "your-api-key-here"
71+
```
72+
73+
### 3. Verify Environment Variable
74+
```powershell
75+
echo $env:OPENAI_KEY
76+
```
277

378
## Running the Samples
479

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

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

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`.
171+
1. **Explore other samples** in the `text/` directory
172+
2. **Modify the prompts** in the sample files to experiment with different outputs
173+
3. **Create your own samples** following the same single-file format
174+
4. **Integrate the OpenAI SDK** into your own .NET applications
10175

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

docs/guides/text/chat/chat_instructions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SAMPLE: Generate text with messages using different roles
2+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
23
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
4+
#:property PublishAot false
45

56
using OpenAI.Chat;
67

docs/guides/text/chat/chat_roles.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SAMPLE: Generate text with messages using different roles
2+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
23
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
4+
#:property PublishAot false
45

56
using OpenAI.Chat;
67

docs/guides/text/chat/chat_simpleprompt.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SAMPLE: Generate text from a simple prompt
2+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
23
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
4+
#:property PublishAot false
45

56
using OpenAI.Chat;
67

docs/guides/text/responses/responses_fileinput.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SAMPLE: Prompt template with file input variable
2+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
23
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
4+
#:property PublishAot false
45

56
using OpenAI.Responses;
67
using OpenAI.Files;

docs/guides/text/responses/responses_instructions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SAMPLE: Generate text with instructions
2+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
23
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
4+
#:property PublishAot false
45

56
using OpenAI.Responses;
67

docs/guides/text/responses/responses_prompttemplate.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SAMPLE: Generate text with a prompt template
2+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
23
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
4+
#:property PublishAot false
45

56
using OpenAI.Responses;
67
using System.ClientModel;

docs/guides/text/responses/responses_roles.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SAMPLE: Generate text with messages using different roles
2+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
23
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
4+
#:property PublishAot false
45

56
using OpenAI.Responses;
67

docs/guides/text/responses/responses_simpleprompt.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SAMPLE: Generate text from a simple prompt
2+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
23
#:package OpenAI@2.2.*-*
3-
#:property PublishAot=false
4+
#:property PublishAot false
45

56
using OpenAI.Responses;
67

0 commit comments

Comments
 (0)