Skip to content

Commit

Permalink
Merge pull request #247 from dounan/feat/add-developer-chat-completio…
Browse files Browse the repository at this point in the history
…n-message-role

Feat: Add developer role
  • Loading branch information
nezhyborets authored Jan 29, 2025
2 parents dbaf347 + a5cd09f commit 989df7b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Demo/DemoChat/Sources/UI/DetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ struct ChatBubble: View {
Spacer(minLength: 24)
case .system:
EmptyView()
case .developer:
EmptyView()
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions Sources/OpenAI/Public/Models/ChatQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public struct ChatQuery: Equatable, Codable, Streamable {
public enum ChatCompletionMessageParam: Codable, Equatable {

case system(Self.SystemMessageParam)
case developer(Self.DeveloperMessageParam)
case user(Self.UserMessageParam)
case assistant(Self.AssistantMessageParam)
case tool(Self.ToolMessageParam)
Expand All @@ -119,6 +120,8 @@ public struct ChatQuery: Equatable, Codable, Streamable {
switch self {
case .system(let systemMessage):
return Self.UserMessageParam.Content.string(systemMessage.content)
case .developer(let developerMessage):
return Self.UserMessageParam.Content.string(developerMessage.content)
case .user(let userMessage):
return userMessage.content
case .assistant(let assistantMessage):
Expand All @@ -135,6 +138,8 @@ public struct ChatQuery: Equatable, Codable, Streamable {
switch self {
case .system(let systemMessage):
return systemMessage.role
case .developer(let developerMessage):
return developerMessage.role
case .user(let userMessage):
return userMessage.role
case .assistant(let assistantMessage):
Expand All @@ -148,6 +153,8 @@ public struct ChatQuery: Equatable, Codable, Streamable {
switch self {
case .system(let systemMessage):
return systemMessage.name
case .developer(let developerMessage):
return developerMessage.name
case .user(let userMessage):
return userMessage.name
case .assistant(let assistantMessage):
Expand Down Expand Up @@ -189,6 +196,12 @@ public struct ChatQuery: Equatable, Codable, Streamable {
} else {
return nil
}
case .developer:
if let content {
self = .developer(.init(content: content, name: name))
} else {
return nil
}
case .user:
if let content {
self = .user(.init(content: .init(string: content), name: name))
Expand Down Expand Up @@ -227,6 +240,8 @@ public struct ChatQuery: Equatable, Codable, Streamable {
) {
if role == .system {
self = .system(.init(content: content, name: name))
} else if role == .developer {
self = .developer(.init(content: content, name: name))
} else {
return nil
}
Expand Down Expand Up @@ -274,6 +289,8 @@ public struct ChatQuery: Equatable, Codable, Streamable {
switch self {
case .system(let a0):
try container.encode(a0)
case .developer(let a0):
try container.encode(a0)
case .user(let a0):
try container.encode(a0)
case .assistant(let a0):
Expand All @@ -285,6 +302,7 @@ public struct ChatQuery: Equatable, Codable, Streamable {

enum CodingKeys: CodingKey {
case system
case developer
case user
case assistant
case tool
Expand Down Expand Up @@ -314,6 +332,31 @@ public struct ChatQuery: Equatable, Codable, Streamable {
case name
}
}

public struct DeveloperMessageParam: Codable, Equatable {
public typealias Role = ChatQuery.ChatCompletionMessageParam.Role

/// The contents of the developer message.
public let content: String
/// The role of the messages author, in this case developer.
public let role: Self.Role = .developer
/// An optional name for the participant. Provides the model information to differentiate between participants of the same role.
public let name: String?

public init(
content: String,
name: String? = nil
) {
self.content = content
self.name = name
}

enum CodingKeys: CodingKey {
case content
case role
case name
}
}

public struct UserMessageParam: Codable, Equatable {
public typealias Role = ChatQuery.ChatCompletionMessageParam.Role
Expand Down Expand Up @@ -570,6 +613,7 @@ public struct ChatQuery: Equatable, Codable, Streamable {

public enum Role: String, Codable, Equatable, CaseIterable {
case system
case developer
case user
case assistant
case tool
Expand Down
2 changes: 2 additions & 0 deletions Sources/OpenAI/Public/Models/ChatResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ extension ChatQuery.ChatCompletionMessageParam {
switch try messageContainer.decode(Role.self, forKey: .role) {
case .system:
self = try .system(.init(from: decoder))
case .developer:
self = try .developer(.init(from: decoder))
case .user:
self = try .user(.init(from: decoder))
case .assistant:
Expand Down

0 comments on commit 989df7b

Please sign in to comment.