Skip to content

Commit 9d6c09b

Browse files
committed
Add todo
1 parent 7924f21 commit 9d6c09b

File tree

1 file changed

+81
-32
lines changed

1 file changed

+81
-32
lines changed

Tool/Sources/OpenAIService/APIs/ClaudeChatCompletionsService.swift

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import JoinJSON
77
import Logger
88
import Preferences
99

10+
#warning("Update the definitions")
1011
/// https://docs.anthropic.com/claude/reference/messages_post
1112
public actor ClaudeChatCompletionsService: ChatCompletionsStreamAPI, ChatCompletionsAPI {
1213
/// https://docs.anthropic.com/en/docs/about-claude/models
@@ -44,7 +45,7 @@ public actor ClaudeChatCompletionsService: ChatCompletionsStreamAPI, ChatComplet
4445
}
4546
}
4647

47-
enum MessageRole: String, Codable {
48+
public enum MessageRole: String, Codable {
4849
case user
4950
case assistant
5051

@@ -127,7 +128,7 @@ public actor ClaudeChatCompletionsService: ChatCompletionsStreamAPI, ChatComplet
127128
var stop_sequence: String?
128129
}
129130

130-
public struct RequestBody: Encodable, Equatable {
131+
public struct RequestBody: Codable, Equatable {
131132
public struct CacheControl: Codable, Equatable, Sendable {
132133
public enum CacheControlType: String, Codable, Equatable, Sendable {
133134
case ephemeral
@@ -136,33 +137,33 @@ public actor ClaudeChatCompletionsService: ChatCompletionsStreamAPI, ChatComplet
136137
public var type: CacheControlType = .ephemeral
137138
}
138139

139-
struct MessageContent: Encodable, Equatable {
140-
enum MessageContentType: String, Encodable, Equatable {
140+
public struct MessageContent: Codable, Equatable {
141+
public enum MessageContentType: String, Codable, Equatable {
141142
case text
142143
case image
143144
}
144145

145-
struct ImageSource: Encodable, Equatable {
146-
var type: String = "base64"
146+
public struct ImageSource: Codable, Equatable {
147+
public var type: String = "base64"
147148
/// currently support the base64 source type for images,
148149
/// and the image/jpeg, image/png, image/gif, and image/webp media types.
149-
var media_type: String = "image/jpeg"
150-
var data: String
150+
public var media_type: String = "image/jpeg"
151+
public var data: String
151152
}
152153

153-
var type: MessageContentType
154-
var text: String?
155-
var source: ImageSource?
156-
var cache_control: CacheControl?
154+
public var type: MessageContentType
155+
public var text: String?
156+
public var source: ImageSource?
157+
public var cache_control: CacheControl?
157158
}
158159

159-
struct Message: Encodable, Equatable {
160+
public struct Message: Codable, Equatable {
160161
/// The role of the message.
161-
var role: MessageRole
162+
public var role: MessageRole
162163
/// The content of the message.
163-
var content: [MessageContent]
164+
public var content: [MessageContent]
164165

165-
mutating func appendText(_ text: String) {
166+
public mutating func appendText(_ text: String) {
166167
var otherContents = [MessageContent]()
167168
var existedText = ""
168169
for existed in content {
@@ -182,26 +183,26 @@ public actor ClaudeChatCompletionsService: ChatCompletionsStreamAPI, ChatComplet
182183
}
183184
}
184185

185-
struct SystemPrompt: Encodable, Equatable {
186-
let type = "text"
187-
var text: String
188-
var cache_control: CacheControl?
186+
public struct SystemPrompt: Codable, Equatable {
187+
public var type = "text"
188+
public var text: String
189+
public var cache_control: CacheControl?
189190
}
190191

191-
struct Tool: Encodable, Equatable {
192-
var name: String
193-
var description: String
194-
var input_schema: JSONSchemaValue
192+
public struct Tool: Codable, Equatable {
193+
public var name: String
194+
public var description: String
195+
public var input_schema: JSONSchemaValue
195196
}
196197

197-
var model: String
198-
var system: [SystemPrompt]
199-
var messages: [Message]
200-
var temperature: Double?
201-
var stream: Bool?
202-
var stop_sequences: [String]?
203-
var max_tokens: Int
204-
var tools: [RequestBody.Tool]?
198+
public var model: String
199+
public var system: [SystemPrompt]
200+
public var messages: [Message]
201+
public var temperature: Double?
202+
public var stream: Bool?
203+
public var stop_sequences: [String]?
204+
public var max_tokens: Int
205+
public var tools: [RequestBody.Tool]?
205206
}
206207

207208
var apiKey: String
@@ -521,5 +522,53 @@ extension ClaudeChatCompletionsService.RequestBody {
521522
stop_sequences = body.stop
522523
max_tokens = body.maxTokens ?? 4000
523524
}
525+
526+
func formalized() -> ChatCompletionsRequestBody {
527+
return .init(
528+
model: model,
529+
messages: system.map { system in
530+
let convertedMessage = ChatCompletionsRequestBody.Message(
531+
role: .system,
532+
content: system.text,
533+
cacheIfPossible: system.cache_control != nil
534+
)
535+
return convertedMessage
536+
} + messages.map { message in
537+
var convertedMessage = ChatCompletionsRequestBody.Message(
538+
role: message.role == .user ? .user : .assistant,
539+
content: "",
540+
cacheIfPossible: message.content.contains(where: { $0.cache_control != nil })
541+
)
542+
for messageContent in message.content {
543+
switch messageContent.type {
544+
case .text:
545+
if let text = messageContent.text {
546+
convertedMessage.content += text
547+
}
548+
case .image:
549+
if let source = messageContent.source {
550+
convertedMessage.images.append(
551+
.init(
552+
base64EncodeData: source.data,
553+
format: {
554+
switch source.media_type {
555+
case "image/png": return .png
556+
case "image/gif": return .gif
557+
default: return .jpeg
558+
}
559+
}()
560+
)
561+
)
562+
}
563+
}
564+
}
565+
return convertedMessage
566+
},
567+
temperature: temperature,
568+
stream: stream,
569+
stop: stop_sequences,
570+
maxTokens: max_tokens
571+
)
572+
}
524573
}
525574

0 commit comments

Comments
 (0)