ai.muna.muna 0.0.43 → 0.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Editor/MunaMenu.cs +11 -1
- package/Plugins/Android/Muna.aar +0 -0
- package/Plugins/Linux/arm64/libFunction.so +0 -0
- package/Plugins/Linux/x86_64/libFunction.so +0 -0
- package/Plugins/Windows/arm64/Function.dll +0 -0
- package/Plugins/Windows/x86_64/Function.dll +0 -0
- package/Plugins/iOS/Function.xcframework/Info.plist +11 -11
- package/Plugins/iOS/Function.xcframework/ios-arm64/Function.framework/Function +0 -0
- package/Plugins/iOS/Function.xcframework/ios-arm64/Function.framework/Headers/FXNVersion.h +1 -1
- package/Plugins/iOS/Function.xcframework/ios-arm64/Function.framework/Info.plist +0 -0
- package/Plugins/iOS/Function.xcframework/ios-arm64/Function.framework/_CodeSignature/CodeResources +3 -3
- package/Plugins/iOS/Function.xcframework/ios-arm64_x86_64-simulator/Function.framework/Function +0 -0
- package/Plugins/iOS/Function.xcframework/ios-arm64_x86_64-simulator/Function.framework/Headers/FXNVersion.h +1 -1
- package/Plugins/iOS/Function.xcframework/ios-arm64_x86_64-simulator/Function.framework/Info.plist +0 -0
- package/Plugins/iOS/Function.xcframework/ios-arm64_x86_64-simulator/Function.framework/_CodeSignature/CodeResources +3 -3
- package/Plugins/iOS/Function.xcframework/xros-arm64/Function.framework/Function +0 -0
- package/Plugins/iOS/Function.xcframework/xros-arm64/Function.framework/Headers/FXNVersion.h +1 -1
- package/Plugins/iOS/Function.xcframework/xros-arm64/Function.framework/Info.plist +0 -0
- package/Plugins/iOS/Function.xcframework/xros-arm64/Function.framework/_CodeSignature/CodeResources +3 -3
- package/Plugins/iOS/Function.xcframework/xros-arm64_x86_64-simulator/Function.framework/Function +0 -0
- package/Plugins/iOS/Function.xcframework/xros-arm64_x86_64-simulator/Function.framework/Headers/FXNVersion.h +1 -1
- package/Plugins/iOS/Function.xcframework/xros-arm64_x86_64-simulator/Function.framework/Info.plist +0 -0
- package/Plugins/iOS/Function.xcframework/xros-arm64_x86_64-simulator/Function.framework/_CodeSignature/CodeResources +3 -3
- package/Plugins/macOS/Function.dylib +0 -0
- package/README.md +1 -1
- package/Runtime/API/DotNetClient.cs +0 -3
- package/Runtime/Beta/BetaClient.cs +14 -1
- package/Runtime/Beta/OpenAI/AudioService.cs +38 -0
- package/Runtime/Beta/OpenAI/AudioService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/ChatService.cs +38 -0
- package/Runtime/Beta/OpenAI/ChatService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/CompletionService.cs +117 -0
- package/Runtime/Beta/OpenAI/CompletionService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/EmbeddingService.cs +252 -0
- package/Runtime/Beta/OpenAI/EmbeddingService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/OpenAIClient.cs +50 -0
- package/Runtime/Beta/OpenAI/OpenAIClient.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/SpeechService.cs +250 -0
- package/Runtime/Beta/OpenAI/SpeechService.cs.meta +11 -0
- package/Runtime/Beta/OpenAI/Types.cs +365 -0
- package/Runtime/Beta/OpenAI/Types.cs.meta +11 -0
- package/Runtime/Beta/OpenAI.meta +8 -0
- package/Runtime/Beta/Remote/RemotePredictionService.cs +45 -66
- package/Runtime/Beta/{Value.cs → Remote/Value.cs} +3 -4
- package/Runtime/C/Configuration.cs +1 -1
- package/Runtime/C/Function.cs +1 -1
- package/Runtime/C/Prediction.cs +1 -1
- package/Runtime/C/PredictionStream.cs +1 -1
- package/Runtime/C/Predictor.cs +1 -1
- package/Runtime/C/Value.cs +3 -2
- package/Runtime/C/ValueMap.cs +1 -1
- package/Runtime/Muna.cs +2 -2
- package/Runtime/Types/Parameter.cs +75 -0
- package/Runtime/Types/Parameter.cs.meta +11 -0
- package/Runtime/Types/Predictor.cs +0 -53
- package/Unity/API/PredictionCacheClient.cs +1 -1
- package/Unity/MunaUnity.cs +0 -1
- package/package.json +1 -1
- /package/Runtime/Beta/{Value.cs.meta → Remote/Value.cs.meta} +0 -0
package/Editor/MunaMenu.cs
CHANGED
|
@@ -5,12 +5,13 @@
|
|
|
5
5
|
|
|
6
6
|
namespace Muna.Editor {
|
|
7
7
|
|
|
8
|
+
using System.IO;
|
|
8
9
|
using UnityEditor;
|
|
9
10
|
|
|
10
11
|
internal static class MunaMenu {
|
|
11
12
|
|
|
12
13
|
private const int BasePriority = -50;
|
|
13
|
-
|
|
14
|
+
|
|
14
15
|
[MenuItem(@"Muna/Muna " + Muna.Version, false, BasePriority)]
|
|
15
16
|
private static void Version() { }
|
|
16
17
|
|
|
@@ -28,5 +29,14 @@ namespace Muna.Editor {
|
|
|
28
29
|
|
|
29
30
|
[MenuItem(@"Muna/Report an Issue", false, BasePriority + 4)]
|
|
30
31
|
private static void ReportIssue() => Help.BrowseURL(@"https://github.com/muna-ai/muna-unity");
|
|
32
|
+
|
|
33
|
+
[MenuItem(@"Muna/Clear Predictor Cache", false, BasePriority + 5)]
|
|
34
|
+
private static void ClearPredictorCache() {
|
|
35
|
+
Directory.Delete(
|
|
36
|
+
global::Muna.API.PredictionCacheClient.PredictorCachePath,
|
|
37
|
+
true
|
|
38
|
+
);
|
|
39
|
+
UnityEngine.Debug.Log("Muna: Cleared predictor cache.");
|
|
40
|
+
}
|
|
31
41
|
}
|
|
32
42
|
}
|
package/Plugins/Android/Muna.aar
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
<key>BinaryPath</key>
|
|
9
9
|
<string>Function.framework/Function</string>
|
|
10
10
|
<key>LibraryIdentifier</key>
|
|
11
|
-
<string>
|
|
11
|
+
<string>xros-arm64</string>
|
|
12
12
|
<key>LibraryPath</key>
|
|
13
13
|
<string>Function.framework</string>
|
|
14
14
|
<key>SupportedArchitectures</key>
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
<string>arm64</string>
|
|
17
17
|
</array>
|
|
18
18
|
<key>SupportedPlatform</key>
|
|
19
|
-
<string>
|
|
19
|
+
<string>xros</string>
|
|
20
20
|
</dict>
|
|
21
21
|
<dict>
|
|
22
22
|
<key>BinaryPath</key>
|
|
23
23
|
<string>Function.framework/Function</string>
|
|
24
24
|
<key>LibraryIdentifier</key>
|
|
25
|
-
<string>
|
|
25
|
+
<string>xros-arm64_x86_64-simulator</string>
|
|
26
26
|
<key>LibraryPath</key>
|
|
27
27
|
<string>Function.framework</string>
|
|
28
28
|
<key>SupportedArchitectures</key>
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
<string>x86_64</string>
|
|
32
32
|
</array>
|
|
33
33
|
<key>SupportedPlatform</key>
|
|
34
|
-
<string>
|
|
34
|
+
<string>xros</string>
|
|
35
35
|
<key>SupportedPlatformVariant</key>
|
|
36
36
|
<string>simulator</string>
|
|
37
37
|
</dict>
|
|
@@ -39,32 +39,32 @@
|
|
|
39
39
|
<key>BinaryPath</key>
|
|
40
40
|
<string>Function.framework/Function</string>
|
|
41
41
|
<key>LibraryIdentifier</key>
|
|
42
|
-
<string>
|
|
42
|
+
<string>ios-arm64_x86_64-simulator</string>
|
|
43
43
|
<key>LibraryPath</key>
|
|
44
44
|
<string>Function.framework</string>
|
|
45
45
|
<key>SupportedArchitectures</key>
|
|
46
46
|
<array>
|
|
47
47
|
<string>arm64</string>
|
|
48
|
+
<string>x86_64</string>
|
|
48
49
|
</array>
|
|
49
50
|
<key>SupportedPlatform</key>
|
|
50
|
-
<string>
|
|
51
|
+
<string>ios</string>
|
|
52
|
+
<key>SupportedPlatformVariant</key>
|
|
53
|
+
<string>simulator</string>
|
|
51
54
|
</dict>
|
|
52
55
|
<dict>
|
|
53
56
|
<key>BinaryPath</key>
|
|
54
57
|
<string>Function.framework/Function</string>
|
|
55
58
|
<key>LibraryIdentifier</key>
|
|
56
|
-
<string>
|
|
59
|
+
<string>ios-arm64</string>
|
|
57
60
|
<key>LibraryPath</key>
|
|
58
61
|
<string>Function.framework</string>
|
|
59
62
|
<key>SupportedArchitectures</key>
|
|
60
63
|
<array>
|
|
61
64
|
<string>arm64</string>
|
|
62
|
-
<string>x86_64</string>
|
|
63
65
|
</array>
|
|
64
66
|
<key>SupportedPlatform</key>
|
|
65
|
-
<string>
|
|
66
|
-
<key>SupportedPlatformVariant</key>
|
|
67
|
-
<string>simulator</string>
|
|
67
|
+
<string>ios</string>
|
|
68
68
|
</dict>
|
|
69
69
|
</array>
|
|
70
70
|
<key>CFBundlePackageType</key>
|
|
Binary file
|
|
Binary file
|
package/Plugins/iOS/Function.xcframework/ios-arm64/Function.framework/_CodeSignature/CodeResources
CHANGED
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
</data>
|
|
35
35
|
<key>Headers/FXNVersion.h</key>
|
|
36
36
|
<data>
|
|
37
|
-
|
|
37
|
+
tE+esu7ATFoktEMmOOH9UR5LeSI=
|
|
38
38
|
</data>
|
|
39
39
|
<key>Headers/Function.h</key>
|
|
40
40
|
<data>
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
</data>
|
|
43
43
|
<key>Info.plist</key>
|
|
44
44
|
<data>
|
|
45
|
-
|
|
45
|
+
77Lcbm4ZxdMrLYBhzOzt2LpOHwU=
|
|
46
46
|
</data>
|
|
47
47
|
<key>Modules/module.modulemap</key>
|
|
48
48
|
<data>
|
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
<dict>
|
|
109
109
|
<key>hash2</key>
|
|
110
110
|
<data>
|
|
111
|
-
|
|
111
|
+
oUXKLIwYEgu8xek+vLL9pW76/6GkUgEiNk9ksD+DWC4=
|
|
112
112
|
</data>
|
|
113
113
|
</dict>
|
|
114
114
|
<key>Headers/Function.h</key>
|
package/Plugins/iOS/Function.xcframework/ios-arm64_x86_64-simulator/Function.framework/Function
CHANGED
|
Binary file
|
package/Plugins/iOS/Function.xcframework/ios-arm64_x86_64-simulator/Function.framework/Info.plist
CHANGED
|
Binary file
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
</data>
|
|
35
35
|
<key>Headers/FXNVersion.h</key>
|
|
36
36
|
<data>
|
|
37
|
-
|
|
37
|
+
tE+esu7ATFoktEMmOOH9UR5LeSI=
|
|
38
38
|
</data>
|
|
39
39
|
<key>Headers/Function.h</key>
|
|
40
40
|
<data>
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
</data>
|
|
43
43
|
<key>Info.plist</key>
|
|
44
44
|
<data>
|
|
45
|
-
|
|
45
|
+
JFYHkPYWryCGBETjbMnlGcFkSyo=
|
|
46
46
|
</data>
|
|
47
47
|
<key>Modules/module.modulemap</key>
|
|
48
48
|
<data>
|
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
<dict>
|
|
109
109
|
<key>hash2</key>
|
|
110
110
|
<data>
|
|
111
|
-
|
|
111
|
+
oUXKLIwYEgu8xek+vLL9pW76/6GkUgEiNk9ksD+DWC4=
|
|
112
112
|
</data>
|
|
113
113
|
</dict>
|
|
114
114
|
<key>Headers/Function.h</key>
|
|
Binary file
|
|
Binary file
|
package/Plugins/iOS/Function.xcframework/xros-arm64/Function.framework/_CodeSignature/CodeResources
CHANGED
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
</data>
|
|
35
35
|
<key>Headers/FXNVersion.h</key>
|
|
36
36
|
<data>
|
|
37
|
-
|
|
37
|
+
tE+esu7ATFoktEMmOOH9UR5LeSI=
|
|
38
38
|
</data>
|
|
39
39
|
<key>Headers/Function.h</key>
|
|
40
40
|
<data>
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
</data>
|
|
43
43
|
<key>Info.plist</key>
|
|
44
44
|
<data>
|
|
45
|
-
|
|
45
|
+
ZD+CUPNSOoIqYqzW7OmDKpTV2jM=
|
|
46
46
|
</data>
|
|
47
47
|
<key>Modules/module.modulemap</key>
|
|
48
48
|
<data>
|
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
<dict>
|
|
109
109
|
<key>hash2</key>
|
|
110
110
|
<data>
|
|
111
|
-
|
|
111
|
+
oUXKLIwYEgu8xek+vLL9pW76/6GkUgEiNk9ksD+DWC4=
|
|
112
112
|
</data>
|
|
113
113
|
</dict>
|
|
114
114
|
<key>Headers/Function.h</key>
|
package/Plugins/iOS/Function.xcframework/xros-arm64_x86_64-simulator/Function.framework/Function
CHANGED
|
Binary file
|
package/Plugins/iOS/Function.xcframework/xros-arm64_x86_64-simulator/Function.framework/Info.plist
CHANGED
|
Binary file
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
</data>
|
|
35
35
|
<key>Headers/FXNVersion.h</key>
|
|
36
36
|
<data>
|
|
37
|
-
|
|
37
|
+
tE+esu7ATFoktEMmOOH9UR5LeSI=
|
|
38
38
|
</data>
|
|
39
39
|
<key>Headers/Function.h</key>
|
|
40
40
|
<data>
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
</data>
|
|
43
43
|
<key>Info.plist</key>
|
|
44
44
|
<data>
|
|
45
|
-
|
|
45
|
+
HMv7yGfcwMOfT2pXw3UR2fTFFqo=
|
|
46
46
|
</data>
|
|
47
47
|
<key>Modules/module.modulemap</key>
|
|
48
48
|
<data>
|
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
<dict>
|
|
109
109
|
<key>hash2</key>
|
|
110
110
|
<data>
|
|
111
|
-
|
|
111
|
+
oUXKLIwYEgu8xek+vLL9pW76/6GkUgEiNk9ksD+DWC4=
|
|
112
112
|
</data>
|
|
113
113
|
</dict>
|
|
114
114
|
<key>Headers/Function.h</key>
|
|
Binary file
|
package/README.md
CHANGED
|
@@ -26,9 +26,6 @@ namespace Muna.API {
|
|
|
26
26
|
/// </summary>
|
|
27
27
|
/// <param name="url">Muna API URL.</param>
|
|
28
28
|
/// <param name="accessKey">Muna access key.</param>
|
|
29
|
-
/// <param name="clientId">Client identifier.</param>
|
|
30
|
-
/// <param name="deviceId">Device model identifier.</param>
|
|
31
|
-
/// <param name="cachePath">Prediction resource cache path.</param>
|
|
32
29
|
public DotNetClient(
|
|
33
30
|
string url,
|
|
34
31
|
string? accessKey = default
|
|
@@ -8,7 +8,10 @@
|
|
|
8
8
|
namespace Muna.Beta {
|
|
9
9
|
|
|
10
10
|
using API;
|
|
11
|
+
using OpenAI;
|
|
11
12
|
using Services;
|
|
13
|
+
using PredictorService = global::Muna.Services.PredictorService;
|
|
14
|
+
using EdgePredictionService = global::Muna.Services.PredictionService;
|
|
12
15
|
|
|
13
16
|
/// <summary>
|
|
14
17
|
/// Client for incubating features.
|
|
@@ -20,13 +23,23 @@ namespace Muna.Beta {
|
|
|
20
23
|
/// Make predictions.
|
|
21
24
|
/// </summary>
|
|
22
25
|
public readonly PredictionService Predictions;
|
|
26
|
+
|
|
27
|
+
/// <summary>
|
|
28
|
+
/// OpenAI client.
|
|
29
|
+
/// </summary>
|
|
30
|
+
public readonly OpenAIClient OpenAI;
|
|
23
31
|
#endregion
|
|
24
32
|
|
|
25
33
|
|
|
26
34
|
#region --Operations--
|
|
27
35
|
|
|
28
|
-
internal BetaClient(
|
|
36
|
+
internal BetaClient(
|
|
37
|
+
MunaClient client,
|
|
38
|
+
PredictorService predictors,
|
|
39
|
+
EdgePredictionService predictions
|
|
40
|
+
) {
|
|
29
41
|
this.Predictions = new PredictionService(client);
|
|
42
|
+
this.OpenAI = new OpenAIClient(predictors, predictions, Predictions.Remote);
|
|
30
43
|
}
|
|
31
44
|
#endregion
|
|
32
45
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Muna
|
|
3
|
+
* Copyright © 2025 NatML Inc. All rights reserved.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#nullable enable
|
|
7
|
+
|
|
8
|
+
namespace Muna.Beta.OpenAI {
|
|
9
|
+
|
|
10
|
+
using Services;
|
|
11
|
+
using PredictorService = global::Muna.Services.PredictorService;
|
|
12
|
+
using EdgePredictionService = global::Muna.Services.PredictionService;
|
|
13
|
+
|
|
14
|
+
/// <summary>
|
|
15
|
+
/// Create speech and transcriptions.
|
|
16
|
+
/// </summary>
|
|
17
|
+
public sealed class AudioService {
|
|
18
|
+
|
|
19
|
+
#region --Client API--
|
|
20
|
+
/// <summary>
|
|
21
|
+
/// Create speech.
|
|
22
|
+
/// </summary>
|
|
23
|
+
public readonly SpeechService Speech;
|
|
24
|
+
#endregion
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
#region --Operations--
|
|
28
|
+
|
|
29
|
+
internal AudioService(
|
|
30
|
+
PredictorService predictors,
|
|
31
|
+
EdgePredictionService predictions,
|
|
32
|
+
RemotePredictionService remotePredictions
|
|
33
|
+
) {
|
|
34
|
+
Speech = new SpeechService(predictors, predictions, remotePredictions);
|
|
35
|
+
}
|
|
36
|
+
#endregion
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Muna
|
|
3
|
+
* Copyright © 2025 NatML Inc. All rights reserved.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#nullable enable
|
|
7
|
+
|
|
8
|
+
namespace Muna.Beta.OpenAI {
|
|
9
|
+
|
|
10
|
+
using Services;
|
|
11
|
+
using PredictorService = global::Muna.Services.PredictorService;
|
|
12
|
+
using EdgePredictionService = global::Muna.Services.PredictionService;
|
|
13
|
+
|
|
14
|
+
/// <summary>
|
|
15
|
+
/// Create chat conversations.
|
|
16
|
+
/// </summary>
|
|
17
|
+
public sealed class ChatService {
|
|
18
|
+
|
|
19
|
+
#region --Client API--
|
|
20
|
+
/// <summary>
|
|
21
|
+
/// Create completions.
|
|
22
|
+
/// </summary>
|
|
23
|
+
public readonly ChatCompletionService Completions;
|
|
24
|
+
#endregion
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
#region --Operations--
|
|
28
|
+
|
|
29
|
+
internal ChatService(
|
|
30
|
+
PredictorService predictors,
|
|
31
|
+
EdgePredictionService predictions,
|
|
32
|
+
RemotePredictionService remotePredictions
|
|
33
|
+
) {
|
|
34
|
+
Completions = new ChatCompletionService(predictors, predictions, remotePredictions);
|
|
35
|
+
}
|
|
36
|
+
#endregion
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Muna
|
|
3
|
+
* Copyright © 2025 NatML Inc. All rights reserved.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#nullable enable
|
|
7
|
+
|
|
8
|
+
namespace Muna.Beta.OpenAI {
|
|
9
|
+
|
|
10
|
+
using System;
|
|
11
|
+
using System.Collections.Generic;
|
|
12
|
+
using System.Threading.Tasks;
|
|
13
|
+
using Newtonsoft.Json.Linq;
|
|
14
|
+
using Services;
|
|
15
|
+
using PredictorService = global::Muna.Services.PredictorService;
|
|
16
|
+
using EdgePredictionService = global::Muna.Services.PredictionService;
|
|
17
|
+
|
|
18
|
+
/// <summary>
|
|
19
|
+
/// Create chat conversations.
|
|
20
|
+
/// </summary>
|
|
21
|
+
public sealed class ChatCompletionService {
|
|
22
|
+
|
|
23
|
+
#region --Client API--
|
|
24
|
+
/// <summary>
|
|
25
|
+
/// Create a chat completion.
|
|
26
|
+
/// </summary>
|
|
27
|
+
/// <param name="model">Chat model predictor tag.</param>
|
|
28
|
+
/// <param name="messages">Messages comprising the conversation so far.</param>
|
|
29
|
+
/// <param name="maxTokens">Maximum output tokens.</param>
|
|
30
|
+
/// <param name="acceleration">Prediction acceleration.</param>
|
|
31
|
+
public async Task<ChatCompletion> Create(
|
|
32
|
+
string model,
|
|
33
|
+
ChatMessage[] messages,
|
|
34
|
+
int? maxTokens = null,
|
|
35
|
+
object? acceleration = null
|
|
36
|
+
) {
|
|
37
|
+
var prediction = await CreatePrediction(
|
|
38
|
+
model,
|
|
39
|
+
new() {
|
|
40
|
+
[@"messages"] = messages,
|
|
41
|
+
[@"max_tokens"] = maxTokens
|
|
42
|
+
},
|
|
43
|
+
acceleration: acceleration ?? Acceleration.Auto
|
|
44
|
+
);
|
|
45
|
+
if (prediction.error != null)
|
|
46
|
+
throw new InvalidOperationException(prediction.error);
|
|
47
|
+
var completion = (prediction.results![0] as JObject)!.ToObject<ChatCompletion>()!;
|
|
48
|
+
return completion;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// <summary>
|
|
52
|
+
/// Stream a chat completion.
|
|
53
|
+
/// </summary>
|
|
54
|
+
/// <param name="model">Chat model predictor tag.</param>
|
|
55
|
+
/// <param name="messages">Messages comprising the conversation so far.</param>
|
|
56
|
+
/// <param name="maxTokens">Maximum output tokens.</param>
|
|
57
|
+
/// <param name="acceleration">Prediction acceleration.</param>
|
|
58
|
+
public async IAsyncEnumerable<ChatCompletionChunk> Stream(
|
|
59
|
+
string model,
|
|
60
|
+
ChatMessage[] messages,
|
|
61
|
+
int? maxTokens = null,
|
|
62
|
+
object? acceleration = null
|
|
63
|
+
) {
|
|
64
|
+
var stream = StreamPrediction(
|
|
65
|
+
model,
|
|
66
|
+
new() {
|
|
67
|
+
[@"messages"] = messages,
|
|
68
|
+
[@"max_tokens"] = maxTokens
|
|
69
|
+
},
|
|
70
|
+
acceleration: acceleration ?? Acceleration.Auto
|
|
71
|
+
);
|
|
72
|
+
await foreach (var prediction in stream) {
|
|
73
|
+
if (prediction.error != null)
|
|
74
|
+
throw new InvalidOperationException(prediction.error);
|
|
75
|
+
var chunk = (prediction.results![0] as JObject)!.ToObject<ChatCompletionChunk>()!;
|
|
76
|
+
yield return chunk;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
#endregion
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
#region --Operations--
|
|
83
|
+
private readonly PredictorService predictors;
|
|
84
|
+
private readonly EdgePredictionService predictions;
|
|
85
|
+
private readonly RemotePredictionService remotePredictions;
|
|
86
|
+
|
|
87
|
+
internal ChatCompletionService(
|
|
88
|
+
PredictorService predictors,
|
|
89
|
+
EdgePredictionService predictions,
|
|
90
|
+
RemotePredictionService remotePredictions
|
|
91
|
+
) {
|
|
92
|
+
this.predictors = predictors;
|
|
93
|
+
this.predictions = predictions;
|
|
94
|
+
this.remotePredictions = remotePredictions;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private Task<Prediction> CreatePrediction(
|
|
98
|
+
string tag,
|
|
99
|
+
Dictionary<string, object?> inputs,
|
|
100
|
+
object acceleration
|
|
101
|
+
) => acceleration switch {
|
|
102
|
+
Acceleration acc => predictions.Create(tag, inputs, acc),
|
|
103
|
+
RemoteAcceleration acc => remotePredictions.Create(tag, inputs, acc),
|
|
104
|
+
_ => throw new InvalidOperationException($"Cannot create {tag} prediction because acceleration is invalid: {acceleration}")
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
private IAsyncEnumerable<Prediction> StreamPrediction(
|
|
108
|
+
string tag,
|
|
109
|
+
Dictionary<string, object?> inputs,
|
|
110
|
+
object acceleration
|
|
111
|
+
) => acceleration switch {
|
|
112
|
+
Acceleration acc => predictions.Stream(tag, inputs, acc),
|
|
113
|
+
_ => throw new InvalidOperationException($"Cannot stream {tag} prediction because acceleration is invalid: {acceleration}")
|
|
114
|
+
};
|
|
115
|
+
#endregion
|
|
116
|
+
}
|
|
117
|
+
}
|