OpenAI.WinRT
0.0.15
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package OpenAI.WinRT --version 0.0.15
NuGet\Install-Package OpenAI.WinRT -Version 0.0.15
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenAI.WinRT" Version="0.0.15" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenAI.WinRT --version 0.0.15
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: OpenAI.WinRT, 0.0.15"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OpenAI.WinRT as a Cake Addin #addin nuget:?package=OpenAI.WinRT&version=0.0.15 // Install OpenAI.WinRT as a Cake Tool #tool nuget:?package=OpenAI.WinRT&version=0.0.15
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
OpenAI.WinRT
WinRT library for interacting with OpenAI APIs
Sample usage
- Create an
OpenAIClient
and configure it - To use OpenAI, specify the
ApiKey
, or it will use theOPENAI_KEY
environment variable. - To use Azure OpenAI, you'll need to specify the
CompletionEndpoint
, theApiKey
(or use the OPENAI_KEY env var), and setUseBearerTokenAuthorization(false)
- Once you have the client object, you can call the GetCompletionAsync API to get completions from it given a prompt. The result is an array of objects that have a Text member you can get the completion text from.
#include <winrt/openai.h>
#include <winrt/builders/OpenAI.h>
#include <winrt/builders/helpers.h>
#ifdef USE_OPENAI // don't send any Windows or user code here
auto openaiEndpoint = winrt::OpenAI::builders::OpenAIClient()
.ApiKey(L"..."); // optional or use OPENAI_KEY
#else // Azure OpenAI
auto openaiEndpoint = winrt::OpenAI::builders::OpenAIClient()
.CompletionUri(winrt::Windows::Foundation::Uri{ L"..." })
.ApiKey(L"...") // optional or use OPENAI_KEY
.UseBearerTokenAuthorization(false);
#endif
auto completion = co_await openaiEndpoint.GetCompletionAsync(
winrt::OpenAI::builders::CompletionRequest{}
.Prompt(L"git clone ")
.NCompletions(5)
.Temperature(0.7f)
.MaxTokens(100)
);
auto i = 0;
for (auto const& c : completion) {
std::wcout << L"Completion #" << i << L"\n";
std::wcout << c.Text() << L"\n";
i++;
}
GetCompletionAsync
#include <iostream>
#include <winrt/base.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/openai.h>
// Reference the CppWinRT.Builders NuGet package to get fluent-style property setters
#include <winrt/builders/OpenAI.CompletionRequest.h>
int main()
{
winrt::init_apartment(/*winrt::apartment_type::multi_threaded*/);
auto openai = winrt::OpenAI::OpenAIClient{};
// configure the API key (otherwise it will use the value from the environment variable OPENAI_KEY)
// openai.ApiKey(L"....");
auto completionTask = openai.GetCompletionAsync(L"git clone ", L"text-davinci-003");
auto completions = completionTask.get(); // you can co_await GetCompletionAsync instead if inside an async method
for (auto const& c : completions) {
std::wcout << c.Text() << L"\n";
}
std::wcout << L"\n\n---\n";
auto completionTask2 = openai.GetCompletionAsync(
winrt::OpenAI::builders::CompletionRequest{} // this uses the CppWinRT.Builders package
.Prompt(L"git clone ")
.Model(L"text-davinci-003")
.NCompletions(5)
.Temperature(0.7f)
.MaxTokens(100)
);
auto completions2 = completionTask2.get();
auto i = 0;
for (auto const& c : completions2) {
std::wcout << L"Completion #" << i << L"\n";
std::wcout << c.Text() << L"\n";
i++;
}
If you'd rather not use the CppWinRT.Builders NuGet package, you can set properties individually on a CompletionRequest
object:
auto cr = winrt::OpenAI::CompletionRequest{};
cr.Prompt(L"git clone ");
cr.Model(L"text-davinci-003");
cr.NCompletions(5);
cr.Temperature(0.7f);
cr.MaxTokens(100);
auto completionTask2 = openai.GetCompletionAsync(cr);
Prompt templates
auto promptTemplate = openai.CreateTemplate(L"Tell me a {adjective} joke about {content}");
auto funnyJokeTask = promptTemplate.FormatAsync({ {L"adjective", L"funny"}, {L"content", L"chickens"} });
auto funnyJoke = funnyJokeTask.get();
std::wcout << L"\n\n" << funnyJoke << L"\n\n\n";
Few-shot example templates
auto example = openai.CreateFewShotTemplate({ L"word", L"antonym" });
auto examples = std::vector {
winrt::multi_threaded_map(std::unordered_map<hstring, hstring> { {L"word", L"happy"}, { L"antonym", L"sad" }}),
winrt::multi_threaded_map(std::unordered_map<hstring, hstring>{ {L"word", L"tall"}, { L"antonym", L"short" }}),
};
example.Examples(std::move(examples));
auto fewshot = example.ExecuteAsync(L"big").get();
std::wcout << L"the opposite of big is " << fewshot.Lookup(L"antonym").begin() << L"\n";
GetEmbeddingAsync
Given a string, returns a vector of doubles representing the embedding of the string.
auto embedding = openai.GetEmbeddingAsync(L"hello world").get();
for (auto const& e : embedding) {
std::wcout << e << L", ";
}
EmbeddingDistance
Given two embeddings, returns a double representing the distance between them.
auto embedding1 = openai.GetEmbeddingAsync(L"hello world").get();
auto embedding2 = openai.GetEmbeddingAsync(L"goodbye world").get();
auto distance = openai.EmbeddingDistance(embedding1.GetView(), embedding2.GetView());
std::wcout << L"distance between hello world and goodbye world is " << distance << L"\n";
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0-windows10.0.17763 is compatible. net6.0-windows10.0.18362 is compatible. net7.0-windows was computed. net8.0-windows was computed. |
native | native is compatible. |
Universal Windows Platform | uap was computed. uap10.0 is compatible. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.0.17 | 238 | 8/24/2023 |
0.0.16 | 167 | 8/18/2023 |
0.0.15 | 267 | 4/19/2023 |
0.0.14 | 240 | 4/19/2023 |
0.0.13 | 228 | 4/13/2023 |
0.0.12 | 235 | 4/4/2023 |
0.0.11 | 251 | 4/4/2023 |
0.0.10 | 249 | 3/30/2023 |
0.0.9 | 297 | 3/6/2023 |
0.0.8 | 309 | 2/17/2023 |
0.0.7 | 321 | 2/16/2023 |
0.0.6 | 336 | 2/13/2023 |
0.0.5 | 324 | 2/4/2023 |
0.0.4 | 285 | 2/4/2023 |
0.0.3 | 333 | 1/25/2023 |
0.0.2 | 326 | 1/19/2023 |
0.0.1 | 345 | 12/19/2022 |