This repository provides a simple introduction to using the OpenAI SDK for .NET through a console application. You will learn how to set up the OpenAI ChatClient, use the completitions API and implement chat history to recall context from previous turns.
Before you begin, make sure you have the following:
- .NET 8.0 SDK or later
- An API key from OpenAI. You can obtain this by creating an account at OpenAI's platform.
-
Clone this repository:
git clone https://github.com/ndamulelonemakh/openai-net-starter.git
-
Install the OpenAI SDK for .NET using NuGet:
cd openai-net-starter dotnet add package OpenAI
To interact with OpenAI, you need an API key. Set it up in your environment as follows:
Windows
setx OPENAI_API_KEY "YOUR_API_KEY_HERE"
Linux/macOS
export OPENAI_API_KEY="YOUR_API_KEY_HERE"
-
How to Run
-
Open your terminal, navigate to the directory containing the project, and run:
dotnet run
-
You will be prompted to enter a message. Enter any message you want, and the assistant will respond.
-
Type
exit
to end the session.
-
- The application uses a
ChatClient
to interact with the OpenAI model (e.g., GPT-4). - User input is taken from the console and added to a conversation history (
chatHistory
) to maintain context over multiple turns. - Each input is passed to the OpenAI REST API, and the response is displayed back to the user.
- The conversation continues until the user types "exit".
- Remember to use a valid API key and handle your credentials securely.
- This project is intended as an introductory example, suitable for expanding into more sophisticated .NET applications.
Feel free to open issues or pull requests for suggestions and improvements. Contributions are always welcome!
This project is licensed under the MIT License. See the LICENSE file for details.
If you have any questions, reach out via the Issues tab