Harnessing the power of OpenAI's API within .NET applications just got simpler. The ConnectingApps.Refit.OpenAI NuGet package offers developers an intuitive way to seamlessly integrate OpenAI’s capabilities, such as ChatGPT, into their applications. This article delves into the package's features, its integration with the Refit library for efficient REST calls, and provides a step-by-step guide on how to employ it for effective OpenAI interactions.
Introduction
This article introduces the ConnectingApps.Refit.OpenAI
NuGet package, a valuable tool for developers looking to efficiently interact with OpenAI's API within .NET applications. Found on NuGet and sourced from GitHub, this package simplifies OpenAI's API calls while granting precise control over the HttpClient
. This can be useful for various purposes, including request logging and status code handling.
Background
OpenAI’s API grants access to advanced AI models like ChatGPT, which can understand and generate text. This capability proves invaluable for developers building applications that require conversational agents, simulated characters for games, or any functionality that involves dynamic text generation and understanding. The API abstracts the complexity of machine learning and natural language processing, offering a straightforward interface for prompt-response interactions.
The ConnectingApps.Refit.OpenAI
package, which is accessible on NuGet and whose source code is available on GitHub, acts as a client wrapper for OpenAI’s API. It utilizes Refit, a type-safe REST library for .NET platforms. Refit allows for automatic interface implementations for API clients, thereby simplifying the declaration and use of API endpoints. For more on Refit, consult its official documentation.
Using the Code
Start by setting up your OpenAI API key as an environment variable:
var apiKey = Environment.GetEnvironmentVariable("OPENAI_KEY");
Then, instantiate the ICompletion
interface using the RestService.For<ICompletion>
method:
using ConnectingApps.Refit.OpenAI;
using ConnectingApps.Refit.OpenAI.Completions;
using ConnectingApps.Refit.OpenAI.Completions.Request;
using Refit;
var completionApi = RestService.For<icompletion>(new HttpClient
{
BaseAddress = new Uri("https://api.openai.com")
}, OpenAiRefitSettings.RefitSettings);
Create and dispatch a request to OpenAI’s API like so:
var response = await completionApi.CreateCompletionAsync(new ChatRequest
{
Model = "gpt-3.5-turbo",
Temperature = 0.7,
Messages = new List<message>
{
new()
{
Role = "user",
Content = "What is the capital of the France?",
}
}
}, $"Bearer {apiKey}");
Console.WriteLine($"Returned response status code {response.StatusCode}");
Console.WriteLine(response.Content!.Choices!.First().Message!.Content);
This streamlined code snippet communicates with OpenAI’s API and retrieves the status code and content of the response, exemplifying the efficiency and simplicity of the ConnectingApps.Refit.OpenAI
package in action.
Points of Interest
Developing and working with the ConnectingApps.Refit.OpenAI
package illuminated the powerful synergy between OpenAI’s capabilities and Refit’s user-friendly REST functionalities. This harmonious integration fosters the crafting of code that is not only efficient but is also elegant and readable, making the development process smooth and enjoyable for those looking to leverage advanced AI in their projects.
History
- 11th October, 2023: Initial version
The ConnectingApps.Refit.OpenAI
package is a recent addition to the .NET developer’s toolkit for interacting with OpenAI's APIs. With its introduction, we expect continual improvements and updates based on the feedback and usage within the developer community.