-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathmodel.h
More file actions
193 lines (156 loc) · 5.69 KB
/
model.h
File metadata and controls
193 lines (156 loc) · 5.69 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include <string>
#include <string_view>
#include <vector>
#include <cstdint>
#include <optional>
#include <memory>
#include <functional>
#include <filesystem>
#include <gsl/pointers>
#include <gsl/span>
#include "logger.h"
namespace foundry_local {
class OpenAIChatClient;
class OpenAIAudioClient;
class OpenAIEmbeddingClient;
}
namespace foundry_local::Internal {
struct IFoundryLocalCore;
}
namespace foundry_local {
#ifdef FL_TESTS
namespace Testing {
struct MockObjectFactory;
}
#endif
using DownloadProgressCallback = std::function<void(float percentage)>;
class IModel {
public:
virtual ~IModel() = default;
virtual const std::string& GetId() const = 0;
virtual const std::string& GetAlias() const = 0;
virtual bool IsLoaded() const = 0;
virtual bool IsCached() const = 0;
virtual const std::filesystem::path& GetPath() const = 0;
virtual void Download(DownloadProgressCallback onProgress = nullptr) = 0;
virtual void Load() = 0;
virtual void Unload() = 0;
virtual void RemoveFromCache() = 0;
protected:
struct CoreAccess {
gsl::not_null<Internal::IFoundryLocalCore*> core;
std::string modelName;
gsl::not_null<ILogger*> logger;
};
virtual CoreAccess GetCoreAccess() const = 0;
friend class OpenAIChatClient;
friend class OpenAIAudioClient;
friend class OpenAIEmbeddingClient;
};
enum class DeviceType {
Invalid,
CPU,
GPU,
NPU
};
struct Runtime {
DeviceType device_type = DeviceType::Invalid;
std::string execution_provider;
};
struct PromptTemplate {
std::string system;
std::string user;
std::string assistant;
std::string prompt;
};
// Forward declarations
class ModelVariant;
struct Parameter {
std::string name;
std::optional<std::string> value;
};
struct ModelSettings {
std::vector<Parameter> parameters;
};
struct ModelInfo {
std::string id;
std::string name;
uint32_t version = 0;
std::string alias;
std::optional<std::string> display_name;
std::string provider_type;
std::string uri;
std::string model_type;
std::optional<PromptTemplate> prompt_template;
std::optional<std::string> publisher;
std::optional<ModelSettings> model_settings;
std::optional<std::string> license;
std::optional<std::string> license_description;
bool cached = false;
std::optional<std::string> task;
std::optional<Runtime> runtime;
std::optional<uint32_t> file_size_mb;
std::optional<bool> supports_tool_calling;
std::optional<int64_t> max_output_tokens;
std::optional<std::string> min_fl_version;
int64_t created_at_unix = 0;
};
class ModelVariant final : public IModel {
public:
explicit ModelVariant(gsl::not_null<foundry_local::Internal::IFoundryLocalCore*> core, ModelInfo info,
gsl::not_null<ILogger*> logger);
const ModelInfo& GetInfo() const;
const std::filesystem::path& GetPath() const override;
void Download(DownloadProgressCallback onProgress = nullptr) override;
void Load() override;
bool IsLoaded() const override;
bool IsCached() const override;
void Unload() override;
void RemoveFromCache() override;
const std::string& GetId() const noexcept override;
const std::string& GetAlias() const noexcept override;
uint32_t GetVersion() const noexcept;
protected:
CoreAccess GetCoreAccess() const override;
private:
static std::string MakeModelParamRequest(std::string_view modelId);
ModelInfo info_;
mutable std::filesystem::path cachedPath_;
gsl::not_null<foundry_local::Internal::IFoundryLocalCore*> core_;
gsl::not_null<ILogger*> logger_;
friend class Model;
};
class Model final : public IModel {
public:
explicit Model(gsl::not_null<foundry_local::Internal::IFoundryLocalCore*> core, gsl::not_null<ILogger*> logger);
gsl::span<const ModelVariant> GetAllModelVariants() const;
bool IsLoaded() const override { return SelectedVariant().IsLoaded(); }
bool IsCached() const override { return SelectedVariant().IsCached(); }
const std::filesystem::path& GetPath() const override { return SelectedVariant().GetPath(); }
void Download(DownloadProgressCallback onProgress = nullptr) override {
SelectedVariant().Download(std::move(onProgress));
}
void Load() override { SelectedVariant().Load(); }
void Unload() override { SelectedVariant().Unload(); }
void RemoveFromCache() override { SelectedVariant().RemoveFromCache(); }
const std::string& GetId() const override;
const std::string& GetAlias() const override;
void SelectVariant(const ModelVariant& variant) const;
protected:
CoreAccess GetCoreAccess() const override;
private:
ModelVariant& SelectedVariant();
const ModelVariant& SelectedVariant() const;
gsl::not_null<foundry_local::Internal::IFoundryLocalCore*> core_;
std::vector<ModelVariant> variants_;
mutable const ModelVariant* selectedVariant_ = nullptr;
gsl::not_null<ILogger*> logger_;
friend class Catalog;
#ifdef FL_TESTS
friend struct Testing::MockObjectFactory;
#endif
};
} // namespace foundry_local