Skip to content

Commit 6bfb0d1

Browse files
committed
Always prefer text only content if possible in OpenAI Api
1 parent a9a0729 commit 6bfb0d1

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

Tool/Sources/OpenAIService/APIs/OpenAIChatCompletionsService.swift

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ public actor OpenAIChatCompletionsService: ChatCompletionsStreamAPI, ChatComplet
271271

272272
public struct RequestBody: Codable, Equatable {
273273
public typealias ClaudeCacheControl = ClaudeChatCompletionsService.RequestBody.CacheControl
274+
275+
public struct GitHubCopilotCacheControl: Codable, Equatable, Sendable {
276+
public var type: String
277+
278+
public init(type: String = "ephemeral") {
279+
self.type = type
280+
}
281+
}
274282

275283
public struct Message: Codable, Equatable {
276284
public enum MessageContent: Codable, Equatable {
@@ -457,21 +465,26 @@ public actor OpenAIChatCompletionsService: ChatCompletionsStreamAPI, ChatComplet
457465
///
458466
/// Deprecated.
459467
public var function_call: MessageFunctionCall?
468+
#warning("TODO: when to use it?")
469+
/// Cache control for GitHub Copilot models.
470+
public var copilot_cache_control: GitHubCopilotCacheControl?
460471

461472
public init(
462473
role: MessageRole,
463474
content: MessageContent,
464475
name: String? = nil,
465476
tool_calls: [MessageToolCall]? = nil,
466477
tool_call_id: String? = nil,
467-
function_call: MessageFunctionCall? = nil
478+
function_call: MessageFunctionCall? = nil,
479+
copilot_cache_control: GitHubCopilotCacheControl? = nil
468480
) {
469481
self.role = role
470482
self.content = content
471483
self.name = name
472484
self.tool_calls = tool_calls
473485
self.tool_call_id = tool_call_id
474486
self.function_call = function_call
487+
self.copilot_cache_control = copilot_cache_control
475488
}
476489
}
477490

@@ -623,6 +636,7 @@ public actor OpenAIChatCompletionsService: ChatCompletionsStreamAPI, ChatComplet
623636
Self.setupCustomBody(&request, model: model)
624637
Self.setupAppInformation(&request)
625638
Self.setupAPIKey(&request, model: model, apiKey: apiKey)
639+
Self.setupGitHubCopilotVisionField(&request, model: model)
626640
await Self.setupExtraHeaderFields(&request, model: model, apiKey: apiKey)
627641
requestModifier?(&request)
628642

@@ -771,6 +785,13 @@ public actor OpenAIChatCompletionsService: ChatCompletionsStreamAPI, ChatComplet
771785
}
772786
}
773787
}
788+
789+
static func setupGitHubCopilotVisionField(_ request: inout URLRequest, model: ChatModel) {
790+
guard model.format == .gitHubCopilot else { return }
791+
if model.info.supportsImage {
792+
request.setValue("true", forHTTPHeaderField: "copilot-vision-request")
793+
}
794+
}
774795

775796
static func setupCustomBody(_ request: inout URLRequest, model: ChatModel) {
776797
switch model.format {
@@ -1242,12 +1263,17 @@ extension OpenAIChatCompletionsService.RequestBody {
12421263
}
12431264
}(),
12441265
content: {
1266+
// always prefer text only content if possible.
12451267
if supportsMultipartMessageContent {
1246-
return .contentParts(Self.convertContentPart(
1247-
content: message.content,
1248-
images: supportsImage ? message.images : [],
1249-
audios: supportsAudio ? message.audios : []
1250-
))
1268+
let images = supportsImage ? message.images : []
1269+
let audios = supportsAudio ? message.audios : []
1270+
if !images.isEmpty || !audios.isEmpty {
1271+
return .contentParts(Self.convertContentPart(
1272+
content: message.content,
1273+
images: images,
1274+
audios: audios
1275+
))
1276+
}
12511277
}
12521278
return .text(message.content)
12531279
}(),

0 commit comments

Comments
 (0)