Couchbase Lite C++
Couchbase Lite C++ API
Loading...
Searching...
No Matches
Prediction.hh
Go to the documentation of this file.
1//
2// Prediction.hh
3//
4// Copyright (c) 2024 Couchbase, Inc All rights reserved.
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17//
18
19
20#ifdef COUCHBASE_ENTERPRISE
21
22#pragma once
23#include "cbl++/Base.hh"
24#include "cbl/CBLPrediction.h"
25#include <string_view>
26
27
29
30namespace cbl {
38 using PredictiveModel = std::function<fleece::MutableDict(fleece::Dict)>;
39
42 class Prediction {
43 public:
47 static void registerModel(std::string_view name, PredictiveModel model) {
48 auto* holder = new PredictiveModel(std::move(model));
49 CBLPredictiveModel config{};
50 config.context = holder;
51 config.prediction = [](void* ctx, FLDict input) -> FLMutableDict {
52 try {
53 auto& fn = *static_cast<PredictiveModel*>(ctx);
54 return FLMutableDict_Retain((FLMutableDict)fn(fleece::Dict(input)));
55 } catch (const cbl::Error& error) {
56 CBL_Log(kCBLLogDomainDatabase, kCBLLogError, "Prediction function throws error %d/%d: %s",
57 error.domain, error.code, error.what());
58 } catch (const std::exception& error) {
59 CBL_Log(kCBLLogDomainDatabase, kCBLLogError, "Prediction function throws error %s",
60 error.what());
61 } catch (...) {
62 CBL_Log(kCBLLogDomainDatabase, kCBLLogError, "Prediction function throws unknown exception");
63 }
64 return FLMutableDict_New();
65 };
66 config.unregistered = [](void* ctx) {
67 delete static_cast<PredictiveModel*>(ctx);
68 };
70 }
71
73 static void unregisterModel(std::string_view name) {
75 }
76 };
77}
78
80
81#endif
#define CBL_ASSUME_NONNULL_END
#define CBL_ASSUME_NONNULL_BEGIN
Registers/unregisters predictive models by name.
Definition Prediction.hh:42
static void registerModel(std::string_view name, PredictiveModel model)
Registers a predictive model with the given name.
Definition Prediction.hh:47
static void unregisterModel(std::string_view name)
Unregisters the model; LiteCore fires unregistered which frees the model.
Definition Prediction.hh:73
kCBLLogDomainDatabase
kCBLLogError
FLEECE_PUBLIC FLMutableDict FL_NULLABLE FLMutableDict_New(void)
FLMutableDict FL_NULLABLE FLMutableDict_Retain(FLMutableDict FL_NULLABLE d)
void CBL_Log(CBLLogDomain domain, CBLLogLevel level, const char *format,...) __printflike(3
void CBL_RegisterPredictiveModel(FLString name, CBLPredictiveModel model)
void CBL_UnregisterPredictiveModel(FLString name)
struct _FLDict * FLMutableDict
const struct _FLDict * FLDict
Definition Base.hh:46
fleece::slice slice
Convenience alias for fleece::slice, a non-owning view of a byte range.
Definition Base.hh:49
std::function< fleece::MutableDict(fleece::Dict)> PredictiveModel
A predictive model callable that integrates a machine learning model into queries,...
Definition Prediction.hh:38
void *_cbl_nullable context
FLMutableDict _cbl_nullable(* prediction)(void *_cbl_nullable context, FLDict input)
The exception thrown by the Couchbase Lite C++ API to report a Couchbase Lite failure.
Definition Base.hh:97
int code
Error code, specific to the domain. 0 always means no error.
Definition Base.hh:119
CBLErrorDomain domain
Domain of errors.
Definition Base.hh:118