Skip to content

Commit 1d8d921

Browse files
committed
Return usage in response
1 parent 31dd401 commit 1d8d921

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

Tool/Sources/Logger/Logger.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public final class Logger {
2323
public static let license = Logger(category: "License")
2424
public static let `extension` = Logger(category: "Extension")
2525
public static let communicationBridge = Logger(category: "CommunicationBridge")
26+
public static let chatProxy = Logger(category: "ChatProxy")
2627
public static let debug = Logger(category: "Debug")
2728
#if DEBUG
2829
/// Use a temp logger to log something temporary. I won't be available in release builds.

Tool/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public enum ChatGPTResponse: Equatable {
6868
case partialText(String)
6969
case partialReasoning(String)
7070
case toolCalls([ChatMessage.ToolCall])
71+
case usage(
72+
promptTokens: Int,
73+
completionTokens: Int,
74+
cachedTokens: Int,
75+
otherUsage: [String: Int]
76+
)
7177
}
7278

7379
public typealias ChatGPTResponseStream = AsyncThrowingStream<ChatGPTResponse, any Error>
@@ -196,7 +202,7 @@ public class ChatGPTService: ChatGPTServiceType {
196202
switch content {
197203
case let .partialText(text):
198204
continuation.yield(ChatGPTResponse.partialText(text))
199-
205+
200206
case let .partialReasoning(text):
201207
continuation.yield(ChatGPTResponse.partialReasoning(text))
202208

@@ -222,6 +228,20 @@ public class ChatGPTService: ChatGPTServiceType {
222228
}
223229
}
224230
}
231+
case let .usage(
232+
promptTokens,
233+
completionTokens,
234+
cachedTokens,
235+
otherUsage
236+
):
237+
continuation.yield(
238+
.usage(
239+
promptTokens: promptTokens,
240+
completionTokens: completionTokens,
241+
cachedTokens: cachedTokens,
242+
otherUsage: otherUsage
243+
)
244+
)
225245
}
226246
}
227247

@@ -257,6 +277,12 @@ extension ChatGPTService {
257277
case partialReasoning(String)
258278
case partialText(String)
259279
case partialToolCalls([Int: ChatMessage.ToolCall])
280+
case usage(
281+
promptTokens: Int,
282+
completionTokens: Int,
283+
cachedTokens: Int,
284+
otherUsage: [String: Int]
285+
)
260286
}
261287

262288
enum FunctionCallResult {
@@ -345,14 +371,19 @@ extension ChatGPTService {
345371
if let content = delta.content {
346372
continuation.yield(.partialText(content))
347373
}
348-
374+
349375
if let reasoning = delta.reasoningContent {
350376
continuation.yield(.partialReasoning(reasoning))
351377
}
352378
}
353379

354380
Logger.service.info("ChatGPT usage: \(usage)")
355-
381+
continuation.yield(.usage(
382+
promptTokens: usage.promptTokens,
383+
completionTokens: usage.completionTokens,
384+
cachedTokens: usage.cachedTokens,
385+
otherUsage: usage.otherUsage
386+
))
356387
continuation.finish()
357388
} catch let error as CancellationError {
358389
continuation.finish(throwing: error)
@@ -554,6 +585,7 @@ extension ChatGPTService {
554585
case .system: .system
555586
case .user: .user
556587
case .assistant: .assistant
588+
case .tool: .tool
557589
}
558590
}(),
559591
content: chatMessage.content ?? "",

Tool/Sources/OpenAIService/Configuration/ChatGPTConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public extension ChatGPTConfiguration {
3939
}
4040
}
4141

42-
public class OverridingChatGPTConfiguration: ChatGPTConfiguration {
42+
public final class OverridingChatGPTConfiguration: ChatGPTConfiguration {
4343
public struct Overriding: Codable {
4444
public var temperature: Double?
4545
public var modelId: String?

0 commit comments

Comments
 (0)