Skip to content

Commit 9485472

Browse files
authored
Merge pull request #402 from gongxh13/main
Add reasoningContent for model chat
2 parents bc9aab7 + 40258d2 commit 9485472

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

Sources/OpenAI/Public/Models/ChatQuery.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ public struct ChatQuery: Equatable, Codable, Streamable, Sendable {
292292
public init?(
293293
role: Role,
294294
content: String? = nil,
295+
reasoningContent: String? = nil,
295296
imageData: Data? = nil,
296297
name: String? = nil,
297298
toolCalls: [Self.AssistantMessageParam.ToolCallParam]? = nil,
@@ -318,9 +319,9 @@ public struct ChatQuery: Equatable, Codable, Streamable, Sendable {
318319
}
319320
case .assistant:
320321
if let content {
321-
self = .assistant(.init(content: .textContent(content), name: name, toolCalls: toolCalls))
322+
self = .assistant(.init(content: .textContent(content), reasoningContent: reasoningContent, name: name, toolCalls: toolCalls))
322323
} else {
323-
self = .assistant(.init(content: nil, name: name, toolCalls: toolCalls))
324+
self = .assistant(.init(content: nil, reasoningContent: reasoningContent, name: name, toolCalls: toolCalls))
324325
}
325326
case .tool:
326327
if let content, let toolCallId {
@@ -783,6 +784,8 @@ public struct ChatQuery: Equatable, Codable, Streamable, Sendable {
783784
public let role: Self.Role = .assistant
784785
/// The contents of the assistant message. Required unless `tool_calls` is specified.
785786
public let content: TextOrRefusalContent?
787+
/// The reasoning content of the assistant message.
788+
public let reasoningContent: String?
786789
/// Data about a previous audio response from the model.
787790
public let audio: Audio?
788791
/// The name of the author of this message. `name` is required if role is `function`, and it should be the name of the function whose response is in the `content`. May contain a-z, A-Z, 0-9, and underscores, with a maximum length of 64 characters.
@@ -792,11 +795,13 @@ public struct ChatQuery: Equatable, Codable, Streamable, Sendable {
792795

793796
public init(
794797
content: TextOrRefusalContent? = nil,
798+
reasoningContent: String? = nil,
795799
audio: Audio? = nil,
796800
name: String? = nil,
797801
toolCalls: [Self.ToolCallParam]? = nil
798802
) {
799803
self.content = content
804+
self.reasoningContent = reasoningContent
800805
self.audio = audio
801806
self.name = name
802807
self.toolCalls = toolCalls
@@ -806,6 +811,7 @@ public struct ChatQuery: Equatable, Codable, Streamable, Sendable {
806811
case name
807812
case role
808813
case content
814+
case reasoningContent = "reasoning_content"
809815
case audio
810816
case toolCalls = "tool_calls"
811817
}

Tests/OpenAITests/ChatQueryCodingTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@ struct ChatQueryCodingTests {
9494
#expect(try equal(query, expected))
9595
}
9696

97+
@Test func encodeReasoningContent() throws {
98+
let query = ChatQuery(
99+
messages: [
100+
.assistant(.init(content: .textContent("Content"), reasoningContent: "Reasoning"))
101+
],
102+
model: .gpt4_o
103+
)
104+
105+
let expected = """
106+
{
107+
"model": "gpt-4o",
108+
"messages": [
109+
{
110+
"role": "assistant",
111+
"content": "Content",
112+
"reasoning_content": "Reasoning"
113+
}
114+
],
115+
"stream": false
116+
}
117+
"""
118+
119+
#expect(try equal(query, expected))
120+
}
121+
97122
@Test func encodeWebSearchOptions() throws {
98123
let query = ChatQuery(
99124
messages: [],

0 commit comments

Comments
 (0)