Skip to content

Commit

Permalink
feat: adds prompt caching usage (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhermawan authored Oct 3, 2024
1 parent a0df5e0 commit 4f329db
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/kevinhermawan/swift-json-schema.git",
"state" : {
"revision" : "6bf106ad4ef4ee3c85ecab4963296374240b9415",
"version" : "1.0.2"
"revision" : "48cade5fb09c6fdc86adcf9af7e43a2c7582a65a",
"version" : "1.0.3"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
targets: ["LLMChatOpenAI"]),
],
dependencies: [
.package(url: "https://github.com/kevinhermawan/swift-json-schema.git", exact: "1.0.2"),
.package(url: "https://github.com/kevinhermawan/swift-json-schema.git", exact: "1.0.3"),
.package(url: "https://github.com/apple/swift-docc-plugin.git", .upToNextMajor(from: "1.3.0"))
],
targets: [
Expand Down
15 changes: 14 additions & 1 deletion Sources/LLMChatOpenAI/ChatCompletion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ public struct ChatCompletion: Decodable {
/// Breakdown of tokens used in a completion.
public let completionTokensDetails: CompletionTokensDetails?

public struct CompletionTokensDetails: Decodable {
/// Breakdown of tokens used in the prompt.
public let promptTokensDetails: PromptTokensDetails?

public struct CompletionTokensDetails: Decodable {
/// Tokens generated by the model for reasoning.
public let reasoningTokens: Int

Expand All @@ -166,11 +169,21 @@ public struct ChatCompletion: Decodable {
}
}

public struct PromptTokensDetails: Decodable {
/// Cached tokens present in the prompt.
public let cachedTokens: Int

private enum CodingKeys: String, CodingKey {
case cachedTokens = "cached_tokens"
}
}

private enum CodingKeys: String, CodingKey {
case completionTokens = "completion_tokens"
case promptTokens = "prompt_tokens"
case totalTokens = "total_tokens"
case completionTokensDetails = "completion_tokens_details"
case promptTokensDetails = "prompt_tokens_details"
}
}

Expand Down
28 changes: 21 additions & 7 deletions Sources/LLMChatOpenAI/ChatCompletionChunk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,37 @@ public struct ChatCompletionChunk: Decodable {
/// Total number of tokens used in the request (prompt + completion).
public let totalTokens: Int?

/// Additional details about completion tokens.
/// Breakdown of tokens used in a completion.
public let completionTokensDetails: CompletionTokensDetails?

private enum CodingKeys: String, CodingKey {
case completionTokens = "completion_tokens"
case promptTokens = "prompt_tokens"
case totalTokens = "total_tokens"
case completionTokensDetails = "completion_tokens_details"
}
/// Breakdown of tokens used in the prompt.
public let promptTokensDetails: PromptTokensDetails?

public struct CompletionTokensDetails: Decodable {
/// Tokens generated by the model for reasoning.
public let reasoningTokens: Int

private enum CodingKeys: String, CodingKey {
case reasoningTokens = "reasoning_tokens"
}
}

public struct PromptTokensDetails: Decodable {
/// Cached tokens present in the prompt.
public let cachedTokens: Int

private enum CodingKeys: String, CodingKey {
case cachedTokens = "cached_tokens"
}
}

private enum CodingKeys: String, CodingKey {
case completionTokens = "completion_tokens"
case promptTokens = "prompt_tokens"
case totalTokens = "total_tokens"
case completionTokensDetails = "completion_tokens_details"
case promptTokensDetails = "prompt_tokens_details"
}
}

private enum CodingKeys: String, CodingKey {
Expand Down

0 comments on commit 4f329db

Please sign in to comment.