Skip to content

Commit 0abca3c

Browse files
author
Your Name
committed
openai chat/audio api
1 parent 24eb623 commit 0abca3c

19 files changed

+357
-251
lines changed

sdk/cpp/include/configuration.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
14
#pragma once
25
#include <string>
36
#include <optional>

sdk/cpp/include/foundry_local.h

Lines changed: 19 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#pragma once
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
25
#include <string>
36
#include <string_view>
47
#include <vector>
@@ -14,9 +17,13 @@
1417
#include <gsl/span>
1518

1619
#include "configuration.h"
17-
1820
#include "logger.h"
1921

22+
// OpenAI-based API types and clients are in a separate directory to keep the
23+
// OpenAI surface well-separated from the core SDK (mirrors the C# layout).
24+
#include "openai/openai_chat_client.h"
25+
#include "openai/openai_audio_client.h"
26+
2027
namespace FoundryLocal::Internal {
2128
struct IFoundryLocalCore;
2229
}
@@ -35,15 +42,6 @@ namespace FoundryLocal {
3542
NPU
3643
};
3744

38-
/// Reason the model stopped generating tokens.
39-
enum class FinishReason {
40-
None,
41-
Stop,
42-
Length,
43-
ToolCalls,
44-
ContentFilter
45-
};
46-
4745
struct Runtime {
4846
DeviceType device_type = DeviceType::Invalid;
4947
std::string execution_provider;
@@ -56,99 +54,6 @@ namespace FoundryLocal {
5654
std::string prompt;
5755
};
5856

59-
struct AudioCreateTranscriptionResponse {
60-
std::string text;
61-
};
62-
63-
/// JSON Schema property definition used to describe tool function parameters.
64-
struct PropertyDefinition {
65-
std::string type;
66-
std::optional<std::string> description;
67-
std::optional<std::unordered_map<std::string, PropertyDefinition>> properties;
68-
std::optional<std::vector<std::string>> required;
69-
};
70-
71-
/// Describes a function that a model may call.
72-
struct FunctionDefinition {
73-
std::string name;
74-
std::optional<std::string> description;
75-
std::optional<PropertyDefinition> parameters;
76-
};
77-
78-
/// A tool definition following the OpenAI tool calling spec.
79-
struct ToolDefinition {
80-
std::string type = "function";
81-
FunctionDefinition function;
82-
};
83-
84-
/// A parsed function call returned by the model.
85-
struct FunctionCall {
86-
std::string name;
87-
std::string arguments; ///< JSON string of the arguments
88-
};
89-
90-
/// A tool call returned by the model in a chat completion response.
91-
struct ToolCall {
92-
std::string id;
93-
std::string type;
94-
std::optional<FunctionCall> function_call;
95-
};
96-
97-
/// Controls whether and how the model calls tools.
98-
enum class ToolChoiceKind {
99-
Auto,
100-
None,
101-
Required
102-
};
103-
104-
struct ChatMessage {
105-
std::string role;
106-
std::string content;
107-
std::optional<std::string> tool_call_id; ///< For role="tool" responses
108-
std::vector<ToolCall> tool_calls;
109-
};
110-
111-
struct ChatChoice {
112-
int index = 0;
113-
FinishReason finish_reason = FinishReason::None;
114-
115-
// non-streaming
116-
std::optional<ChatMessage> message;
117-
118-
// streaming
119-
std::optional<ChatMessage> delta;
120-
};
121-
122-
struct ChatCompletionCreateResponse {
123-
int64_t created = 0;
124-
std::string id;
125-
126-
bool is_delta = false;
127-
bool successful = false;
128-
int http_status_code = 0;
129-
130-
std::vector<ChatChoice> choices;
131-
132-
/// Returns the object type string. Derived from is_delta — no allocation.
133-
const char* GetObject() const noexcept { return is_delta ? "chat.completion.chunk" : "chat.completion"; }
134-
135-
/// Returns the created timestamp as an ISO 8601 string.
136-
/// Computed lazilym only allocates when called.
137-
std::string GetCreatedAtIso() const;
138-
};
139-
140-
struct ChatSettings {
141-
std::optional<float> frequency_penalty;
142-
std::optional<int> max_tokens;
143-
std::optional<int> n;
144-
std::optional<float> temperature;
145-
std::optional<float> presence_penalty;
146-
std::optional<int> random_seed;
147-
std::optional<int> top_k;
148-
std::optional<float> top_p;
149-
std::optional<ToolChoiceKind> tool_choice;
150-
};
151-
15257
using DownloadProgressCallback = std::function<void(float percentage)>;
15358

15459
// Forward declarations
@@ -187,64 +92,6 @@ namespace FoundryLocal {
18792
int64_t created_at_unix = 0;
18893
};
18994

190-
class AudioClient final {
191-
public:
192-
explicit AudioClient(gsl::not_null<const ModelVariant*> model);
193-
194-
/// Returns the model ID this client was created for.
195-
const std::string& GetModelId() const noexcept { return modelId_; }
196-
197-
AudioCreateTranscriptionResponse TranscribeAudio(const std::filesystem::path& audioFilePath) const;
198-
199-
using StreamCallback = std::function<void(const AudioCreateTranscriptionResponse& chunk)>;
200-
void TranscribeAudioStreaming(const std::filesystem::path& audioFilePath, const StreamCallback& onChunk) const;
201-
202-
private:
203-
AudioClient(gsl::not_null<FoundryLocal::Internal::IFoundryLocalCore*> core, std::string_view modelId,
204-
gsl::not_null<ILogger*> logger);
205-
206-
std::string modelId_;
207-
gsl::not_null<FoundryLocal::Internal::IFoundryLocalCore*> core_;
208-
gsl::not_null<ILogger*> logger_;
209-
210-
friend class ModelVariant;
211-
};
212-
213-
class ChatClient final {
214-
public:
215-
explicit ChatClient(gsl::not_null<const ModelVariant*> model);
216-
217-
/// Returns the model ID this client was created for.
218-
const std::string& GetModelId() const noexcept { return modelId_; }
219-
220-
ChatCompletionCreateResponse CompleteChat(gsl::span<const ChatMessage> messages,
221-
const ChatSettings& settings) const;
222-
223-
ChatCompletionCreateResponse CompleteChat(gsl::span<const ChatMessage> messages,
224-
gsl::span<const ToolDefinition> tools,
225-
const ChatSettings& settings) const;
226-
227-
using StreamCallback = std::function<void(const ChatCompletionCreateResponse& chunk)>;
228-
void CompleteChatStreaming(gsl::span<const ChatMessage> messages, const ChatSettings& settings,
229-
const StreamCallback& onChunk) const;
230-
231-
void CompleteChatStreaming(gsl::span<const ChatMessage> messages, gsl::span<const ToolDefinition> tools,
232-
const ChatSettings& settings, const StreamCallback& onChunk) const;
233-
234-
private:
235-
ChatClient(gsl::not_null<FoundryLocal::Internal::IFoundryLocalCore*> core, std::string_view modelId,
236-
gsl::not_null<ILogger*> logger);
237-
238-
std::string BuildChatRequestJson(gsl::span<const ChatMessage> messages, gsl::span<const ToolDefinition> tools,
239-
const ChatSettings& settings, bool stream) const;
240-
241-
std::string modelId_;
242-
gsl::not_null<FoundryLocal::Internal::IFoundryLocalCore*> core_;
243-
gsl::not_null<ILogger*> logger_;
244-
245-
friend class ModelVariant;
246-
};
247-
24895
class ModelVariant final {
24996
public:
25097
const ModelInfo& GetInfo() const;
@@ -257,11 +104,11 @@ namespace FoundryLocal {
257104
void Unload() const;
258105
void RemoveFromCache();
259106

260-
[[deprecated("Use AudioClient(model) constructor instead")]]
261-
AudioClient GetAudioClient() const;
107+
[[deprecated("Use OpenAIAudioClient(model) constructor instead")]]
108+
OpenAIAudioClient GetAudioClient() const;
262109

263-
[[deprecated("Use ChatClient(model) constructor instead")]]
264-
ChatClient GetChatClient() const;
110+
[[deprecated("Use OpenAIChatClient(model) constructor instead")]]
111+
OpenAIChatClient GetChatClient() const;
265112

266113
const std::string& GetId() const noexcept;
267114
const std::string& GetAlias() const noexcept;
@@ -278,8 +125,8 @@ namespace FoundryLocal {
278125
gsl::not_null<ILogger*> logger_;
279126

280127
friend class Catalog;
281-
friend class AudioClient;
282-
friend class ChatClient;
128+
friend class OpenAIAudioClient;
129+
friend class OpenAIChatClient;
283130
#ifdef FL_TESTS
284131
friend struct Testing::MockObjectFactory;
285132
#endif
@@ -299,13 +146,13 @@ namespace FoundryLocal {
299146
void Load() const { SelectedVariant().Load(); }
300147
void Unload() const { SelectedVariant().Unload(); }
301148
void RemoveFromCache() { SelectedVariant().RemoveFromCache(); }
302-
[[deprecated("Use AudioClient(model) constructor instead")]]
303-
AudioClient GetAudioClient() const {
149+
[[deprecated("Use OpenAIAudioClient(model) constructor instead")]]
150+
OpenAIAudioClient GetAudioClient() const {
304151
return SelectedVariant().GetAudioClient();
305152
}
306153

307-
[[deprecated("Use ChatClient(model) constructor instead")]]
308-
ChatClient GetChatClient() const {
154+
[[deprecated("Use OpenAIChatClient(model) constructor instead")]]
155+
OpenAIChatClient GetChatClient() const {
309156
return SelectedVariant().GetChatClient();
310157
}
311158

sdk/cpp/include/foundry_local_exception.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
14
#pragma once
25

36
#include <stdexcept>

sdk/cpp/include/log_level.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
14
#pragma once
25

36
#include <string_view>

sdk/cpp/include/logger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
14
#pragma once
25
#include <string_view>
36
#include "log_level.h"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
#include <string>
7+
#include <string_view>
8+
#include <functional>
9+
#include <filesystem>
10+
11+
#include <gsl/pointers>
12+
13+
namespace FoundryLocal::Internal {
14+
struct IFoundryLocalCore;
15+
}
16+
17+
namespace FoundryLocal {
18+
class ILogger;
19+
class ModelVariant;
20+
21+
struct AudioCreateTranscriptionResponse {
22+
std::string text;
23+
};
24+
25+
class OpenAIAudioClient final {
26+
public:
27+
explicit OpenAIAudioClient(gsl::not_null<const ModelVariant*> model);
28+
29+
/// Returns the model ID this client was created for.
30+
const std::string& GetModelId() const noexcept { return modelId_; }
31+
32+
AudioCreateTranscriptionResponse TranscribeAudio(const std::filesystem::path& audioFilePath) const;
33+
34+
using StreamCallback = std::function<void(const AudioCreateTranscriptionResponse& chunk)>;
35+
void TranscribeAudioStreaming(const std::filesystem::path& audioFilePath, const StreamCallback& onChunk) const;
36+
37+
private:
38+
OpenAIAudioClient(gsl::not_null<FoundryLocal::Internal::IFoundryLocalCore*> core, std::string_view modelId,
39+
gsl::not_null<ILogger*> logger);
40+
41+
std::string modelId_;
42+
gsl::not_null<FoundryLocal::Internal::IFoundryLocalCore*> core_;
43+
gsl::not_null<ILogger*> logger_;
44+
45+
friend class ModelVariant;
46+
};
47+
48+
/// Backward-compatible alias.
49+
using AudioClient = OpenAIAudioClient;
50+
51+
} // namespace FoundryLocal

0 commit comments

Comments
 (0)