-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.swift
More file actions
115 lines (90 loc) · 3.15 KB
/
src.swift
File metadata and controls
115 lines (90 loc) · 3.15 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
import FoundationModels
import Dispatch
@_silgen_name("runFunction")
func runFunction(_ function : UnsafeRawPointer?, _ text : UnsafeMutablePointer<CChar>?);
public struct generation_options {
public var maximumResponseTokens: Int32;
public var greedy : CBool;
public var temperature : CDouble?;
};
var model = SystemLanguageModel.default;
var initedOnce : Bool = false;
var session : LanguageModelSession? = nil;
var sessionOptions : GenerationOptions?;
var instructions : String = "";
var callback : UnsafeRawPointer?;
var isResponding : CBool?; // session?.isResponding exists, but it didn't work for me.
@_cdecl("start_model_to_swift")
public func start_model_to_swift(_ ptr: UnsafeRawPointer) -> Int32 {
let options = ptr.assumingMemoryBound(to: generation_options.self).pointee;
switch model.availability {
case .unavailable(.deviceNotEligible):
return 1;
case .unavailable(.appleIntelligenceNotEnabled):
return 2;
case .unavailable(.modelNotReady):
return 3;
case .unavailable(let other):
print("Failed to init Apple Intelligence: ", other);
return 4;
// Hey, it works!
default:
initedOnce = true;
let maxTokens : Int = Int(options.maximumResponseTokens);
sessionOptions = GenerationOptions(sampling: (options.greedy ? .greedy : nil), temperature: options.temperature, maximumResponseTokens: maxTokens);
session = nil; session = LanguageModelSession(instructions: instructions);
return 0;
}
}
@_cdecl("set_instructions_to_swift")
public func set_instructions_to_swift(_ ptr: UnsafePointer<CChar>) {
instructions = String(cString: ptr);
}
func handle_request(_ request : String) async {
let errorMessage = "Sorry, we are unable to process that request right now.";
do {
let response = try await session?.respond(to: request);
let str = response?.content;
let cString = strdup(str);
if cString != nil {
runFunction(callback, cString);
} else {
free(cString);
}
} catch let error {
print(error);
let cString = strdup(errorMessage);
runFunction(callback, cString);
}
isResponding = false;
}
@_cdecl("send_to_swift")
public func send_to_swift(_ request: UnsafePointer<CChar>, _ cb : UnsafeRawPointer) {
if isResponding ?? false {return;}
isResponding = true;
callback = cb;
Task {
await handle_request(String(cString: request));
}
// runFunction(callback, strdup("Test"));
}
@_cdecl("is_processing")
public func is_processing() -> CBool {
return isResponding ?? false;
}
@_cdecl("free_swift_string")
public func free_swift_string(_ ptr : UnsafeMutablePointer<CChar>?) {
if ptr != nil {
free(ptr);
}
}
// let instructions = """
// Respond in JSON:
// {
// "content": ...
// }
// """;
// session = LanguageModelSession(instructions: instructions);
// let samplePrompt = "What is the Swift API to create a window called?";
// let response = try await session?.respond(to: samplePrompt)
// print(response);