forked from asklar/OpenAI.WinRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAIClient.h
75 lines (56 loc) · 3.54 KB
/
OpenAIClient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include "OpenAIClient.g.h"
#include "Choice.h"
#include "CompletionRequest.g.h"
#include "PromptTemplate.g.h"
#include "FewShotTemplate.g.h"
#include "EmbeddingUtils.g.h"
#include "ChatMessage.g.h"
#include <winrt/Windows.Web.Http.h>
namespace winrt::OpenAI::implementation
{
struct OpenAIClient : OpenAIClientT<OpenAIClient>
{
OpenAIClient();
winrt::hstring ApiKey() const noexcept { return m_apiKey; }
void ApiKey(winrt::hstring v) noexcept;
static constexpr std::wstring_view gpt35turboEndpoint = L"https://api.openai.com/v1/chat/completions";
winrt::Windows::Foundation::Uri CompletionUri() const noexcept { return m_completionUri; }
void CompletionUri(winrt::Windows::Foundation::Uri v) noexcept { m_completionUri = v; }
Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVector<winrt::OpenAI::Choice>> GetCompletionAsync(winrt::hstring prompt, winrt::hstring model);
Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVector<winrt::OpenAI::Choice>> GetCompletionAsync(winrt::OpenAI::CompletionRequest request);
Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVector<winrt::OpenAI::Choice>> GetChatResponseAsync(winrt::OpenAI::ChatRequest request);
winrt::OpenAI::PromptTemplate CreateTemplate(winrt::hstring promptTemplateString);
winrt::OpenAI::FewShotTemplate CreateFewShotTemplate(winrt::Windows::Foundation::Collections::IVectorView<winrt::hstring> parameters);
winrt::Windows::Foundation::Uri EmbeddingUri() const noexcept { return m_embeddingUri; }
void EmbeddingUri(winrt::Windows::Foundation::Uri v) noexcept { m_embeddingUri = v; }
Windows::Foundation::IAsyncOperation<winrt::Windows::Foundation::Collections::IVector<double>> GetEmbeddingAsync(winrt::hstring prompt);
bool UseBearerTokenAuthorization() const noexcept { return m_useBearerTokenAuthorization; }
void UseBearerTokenAuthorization(bool v) { m_useBearerTokenAuthorization = v; SetAuth(); }
private:
winrt::hstring m_apiKey;
winrt::Windows::Foundation::Uri m_completionUri{ L"https://api.openai.com/v1/completions" };
winrt::Windows::Foundation::Uri m_embeddingUri{ L"https://api.openai.com/v1/embeddings" };
winrt::Windows::Web::Http::HttpClient m_client;
bool m_useBearerTokenAuthorization = true;
void SetAuth();
};
struct ChatMessage : ChatMessageT<ChatMessage>
{
Property<winrt::OpenAI::ChatRole> Role;
Property<winrt::hstring> Content;
ChatMessage(winrt::OpenAI::ChatRole role, winrt::hstring content) : Role(role), Content(content) {}
};
struct EmbeddingUtils : EmbeddingUtilsT<EmbeddingUtils>
{
EmbeddingUtils() = default;
static double EmbeddingDistance(winrt::Windows::Foundation::Collections::IVectorView<double> const& v1, winrt::Windows::Foundation::Collections::IVectorView<double> const& v2, winrt::OpenAI::Similarity const& similarity);
static double EmbeddingDistance(winrt::Windows::Foundation::Collections::IVectorView<double> const& v1, winrt::Windows::Foundation::Collections::IVectorView<double> const& v2);
};
}
namespace winrt::OpenAI::factory_implementation
{
struct OpenAIClient : OpenAIClientT<OpenAIClient, implementation::OpenAIClient>{};
struct EmbeddingUtils : EmbeddingUtilsT<EmbeddingUtils, implementation::EmbeddingUtils> {};
struct ChatMessage : ChatMessageT<ChatMessage, implementation::ChatMessage> {};
}