From 4f329db72f469f70ed3af02f437406cc1134ac55 Mon Sep 17 00:00:00 2001 From: Kevin Hermawan <84965338+kevinhermawan@users.noreply.github.com> Date: Thu, 3 Oct 2024 20:52:19 +0700 Subject: [PATCH] feat: adds prompt caching usage (#4) --- Package.resolved | 4 +-- Package.swift | 2 +- Sources/LLMChatOpenAI/ChatCompletion.swift | 15 +++++++++- .../LLMChatOpenAI/ChatCompletionChunk.swift | 28 ++++++++++++++----- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Package.resolved b/Package.resolved index be83d95..c51a73a 100644 --- a/Package.resolved +++ b/Package.resolved @@ -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" } } ], diff --git a/Package.swift b/Package.swift index 8a539bd..c50855c 100644 --- a/Package.swift +++ b/Package.swift @@ -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: [ diff --git a/Sources/LLMChatOpenAI/ChatCompletion.swift b/Sources/LLMChatOpenAI/ChatCompletion.swift index 637d8b4..d962a7d 100644 --- a/Sources/LLMChatOpenAI/ChatCompletion.swift +++ b/Sources/LLMChatOpenAI/ChatCompletion.swift @@ -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 @@ -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" } } diff --git a/Sources/LLMChatOpenAI/ChatCompletionChunk.swift b/Sources/LLMChatOpenAI/ChatCompletionChunk.swift index 12d3989..92726b6 100644 --- a/Sources/LLMChatOpenAI/ChatCompletionChunk.swift +++ b/Sources/LLMChatOpenAI/ChatCompletionChunk.swift @@ -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 {