Skip to content

Commit 22f9c70

Browse files
Merge pull request #7 from Compiler-Inc/feat/update-simple-model-calling
feat: update simple model call
2 parents c3e5ce8 + cc07ecc commit 22f9c70

File tree

5 files changed

+102
-18
lines changed

5 files changed

+102
-18
lines changed

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/CompilerSwiftAI/CompilerClient.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,61 @@ public final actor CompilerClient {
6262
public func makeStreamingSession() -> StreamConfiguration {
6363
configuration.streamingChat
6464
}
65+
66+
/// Generate text from a prompt using the specified model
67+
/// - Parameters:
68+
/// - prompt: The input prompt
69+
/// - model: The model configuration to use
70+
/// - systemPrompt: Optional system prompt to set context
71+
/// - Returns: The complete model response including tokens used, finish reason, etc.
72+
public func generateText(
73+
prompt: String,
74+
using model: StreamConfiguration,
75+
systemPrompt: String? = nil
76+
) async throws -> CompletionResponse {
77+
try await makeModelCallWithResponse(
78+
using: model.metadata,
79+
systemPrompt: systemPrompt,
80+
userPrompt: prompt
81+
)
82+
}
83+
84+
/// Stream text generation from a prompt
85+
/// - Parameters:
86+
/// - prompt: The input prompt
87+
/// - model: The model configuration to use
88+
/// - systemPrompt: Optional system prompt to set context
89+
/// - Returns: An async stream of response chunks with metadata
90+
public func streamText(
91+
prompt: String,
92+
using model: StreamConfiguration,
93+
systemPrompt: String? = nil
94+
) async -> AsyncThrowingStream<String, Error> {
95+
let message = Message(role: .user, content: prompt)
96+
let messages = systemPrompt.map { [Message(role: .system, content: $0), message] } ?? [message]
97+
return makeStreamingModelCall(using: model.metadata, messages: messages)
98+
}
99+
100+
/// Process a natural language command into structured function calls
101+
/// - Parameters:
102+
/// - command: The natural language command to process
103+
/// - Returns: Array of functions with their parameters
104+
/// - Note: You must specify the Parameters type when calling this function, either through type annotation or explicit generic parameter:
105+
/// ```swift
106+
/// // Option 1: Type annotation
107+
/// let functions: [Function<MyParameters>] = try await client.processFunctionCall("Add todo")
108+
///
109+
/// // Option 2: Explicit generic
110+
/// let functions = try await client.processFunctionCall<MyParameters>("Add todo")
111+
/// ```
112+
public func processFunctionCall<Parameters: Decodable & Sendable>(
113+
_ command: String
114+
) async throws -> [Function<Parameters>] {
115+
// We use an empty state since this is the simplified version
116+
try await processFunction(command, for: EmptyState(), using: "")
117+
}
118+
}
119+
120+
private struct EmptyState: Encodable, Sendable {
121+
// Empty state for simplified function calls
65122
}

Sources/CompilerSwiftAI/Model Calling/ModelCall.swift

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,37 @@ struct StreamRequest: ModelCallRequestBase {
7171
}
7272
}
7373

74-
/// Response format for completion calls
75-
struct CompletionResponse: Codable, Sendable {
76-
let content: String
74+
public struct CompletionResponse: Codable, Sendable {
75+
private struct Choice: Codable {
76+
struct Message: Codable {
77+
let content: String
78+
}
79+
80+
let message: Message
81+
}
82+
83+
private let choices: [Choice]
84+
85+
/// The generated text content
86+
public var content: String {
87+
choices.first?.message.content ?? ""
88+
}
7789
}
7890

79-
/// Response format for streaming calls - each chunk
80-
struct StreamChunk: Codable, Sendable {
81-
let content: String
91+
/// Response format for streaming chunks
92+
public struct StreamChunk: Codable, Sendable {
93+
private struct Choice: Codable {
94+
struct Message: Codable {
95+
let content: String
96+
}
97+
98+
let message: Message
99+
}
100+
101+
private let choices: [Choice]
102+
103+
/// The generated text content
104+
public var content: String {
105+
choices.first?.message.content ?? ""
106+
}
82107
}

Sources/CompilerSwiftAI/UI/Chat/ChatView/ChatViewModel.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ You are a helpful AI Assistant. Be direct, concise, and friendly. Always format
1414
class ChatViewModel: Transcribable {
1515
public var isRecording = false
1616
public var transcribedText = ""
17+
public var rmsLevel: Float = 0.0
1718
public var authStatus: SFSpeechRecognizerAuthorizationStatus = .notDetermined
1819
public var error: Error?
1920

@@ -105,11 +106,11 @@ class ChatViewModel: Transcribable {
105106
let stream = try await transcriber.startStream()
106107
isRecording = true
107108

108-
for try await partialResult in stream {
109-
switch partialResult {
110-
case let .rms(float):
111-
print("rms: \(float)")
112-
case let .transcription(string):
109+
for try await signal in stream {
110+
switch signal {
111+
case .rms(let float):
112+
self.rmsLevel = float
113+
case .transcription(let string):
113114
self._userInput = string
114115
}
115116
}

Sources/CompilerSwiftAI/UI/Function Chat/FunctionChatViewModel.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Transcriber
99
class FunctionChatViewModel<AppState: Encodable & Sendable, Parameters: Decodable & Sendable>: Transcribable {
1010
public var isRecording = false
1111
public var transcribedText = ""
12+
public var rmsLevel: Float = 0
1213
public var authStatus: SFSpeechRecognizerAuthorizationStatus = .notDetermined
1314
public var error: Error?
1415

@@ -41,12 +42,12 @@ class FunctionChatViewModel<AppState: Encodable & Sendable, Parameters: Decodabl
4142
do {
4243
isRecording = true
4344
let stream = try await transcriber.startStream()
44-
45+
4546
for try await signal in stream {
4647
switch signal {
47-
case let .rms(float):
48-
print("float: \(float)")
49-
case let .transcription(string):
48+
case .rms(let float):
49+
rmsLevel = float
50+
case .transcription(let string):
5051
inputText = string
5152
}
5253
}

0 commit comments

Comments
 (0)