Skip to content

Commit 2064b75

Browse files
Merge pull request #39 from AppcentMobile/feature/stream-support
Feature/stream support
2 parents bbcfdcf + 43225df commit 2064b75

File tree

7 files changed

+48
-11
lines changed

7 files changed

+48
-11
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import PackageDescription
55

66
let package = Package(
77
name: "ACMOpenAI",
8+
platforms: [
9+
.iOS(.v13), // Set the minimum iOS version here
10+
],
811
products: [
912
.library(
1013
name: "ACMOpenAI",
1114
targets: ["ACMOpenAI"]
1215
),
1316
],
1417
dependencies: [
15-
.package(url: "https://github.com/AppcentMobile/ACMNetworking.git", from: "1.1.3"),
18+
.package(url: "https://github.com/AppcentMobile/ACMNetworking.git", branch: "main"),
1619
],
1720
targets: [
1821
.target(

Sources/ACMOpenAI/Callbacks/Chat/ChatCallback.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ public struct ChatCallback {}
77

88
public extension ChatCallback {
99
/// Create chat typealias
10-
typealias Create = ((ACMOAIChatResponse.Create?) -> Void)?
10+
typealias Create = (([ACMOAIChatResponse.Create?]) -> Void)?
1111
}

Sources/ACMOpenAI/Constants/ACMOAIConstants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44

55
/// Constants for holding open ai data
6-
struct ACMOAIConstants {
6+
enum ACMOAIConstants {
77
static let organizationHeader = "OpenAI-Organization"
88
static let contentTypeKey = "Content-Type"
99
static let contentTypeValue = "application/json"

Sources/ACMOpenAI/Manager/Chat/ACMOAIChatManager.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,20 @@ public extension ACMOAIChatManager {
6060
to = to.add(param: ACMBodyModel(key: "user", value: user))
6161
}
6262

63-
network.request(to: to.build()) { (response: ACMOAIChatResponse.Create) in
64-
onSuccess?(response)
65-
} onError: { error in
66-
onError?(error)
63+
let buildEndpoint = to.build()
64+
65+
if request.stream == true {
66+
network.request(to: buildEndpoint) { (response: [ACMOAIChatResponse.Create]) in
67+
onSuccess?(response)
68+
} onError: { error in
69+
onError?(error)
70+
}
71+
} else {
72+
network.request(to: buildEndpoint) { (response: ACMOAIChatResponse.Create) in
73+
onSuccess?([response])
74+
} onError: { error in
75+
onError?(error)
76+
}
6777
}
6878
}
6979
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// ACMChatMessageModel.swift
3+
//
4+
//
5+
// Created by burak on 18.12.2023.
6+
//
7+
8+
import UIKit
9+
10+
public final class ACMChatMessageModel: Codable {
11+
public let role: String?
12+
public let content: String?
13+
14+
public init(from decoder: Decoder) throws {
15+
let container = try decoder.container(keyedBy: CodingKeys.self)
16+
role = try container.decodeIfPresent(String.self, forKey: .role)
17+
content = try container.decodeIfPresent(String.self, forKey: .content)
18+
}
19+
20+
public init(role: String?, content: String?) {
21+
self.role = role
22+
self.content = content
23+
}
24+
}

Sources/ACMOpenAI/Requests/Chat/ACMOAIChatRequest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public extension ACMOAIChatRequest {
1111
/// model: required*
1212
public var model: String
1313
/// messages: required*
14-
public var messages: [[String: String]]
14+
public var messages: [ACMChatMessageModel]
1515
/// temperature: optional
1616
public var temperature: Double?
1717
/// top_p: optional
@@ -34,7 +34,7 @@ public extension ACMOAIChatRequest {
3434
public var user: String?
3535

3636
/// Init function for creating request
37-
public init(model: String, messages: [[String: String]], temperature: Double? = nil, top_p: Double? = nil, n: Int? = nil, stream: Bool? = nil, stop: [String]? = nil, max_tokens: Int? = nil, presence_penalty: Double? = nil, frequency_penalty: Double? = nil, logit_bias: [String: Int]? = nil, user: String? = nil) {
37+
public init(model: String, messages: [ACMChatMessageModel], temperature: Double? = nil, top_p: Double? = nil, n: Int? = nil, stream: Bool? = nil, stop: [String]? = nil, max_tokens: Int? = nil, presence_penalty: Double? = nil, frequency_penalty: Double? = nil, logit_bias: [String: Int]? = nil, user: String? = nil) {
3838
self.model = model
3939
self.messages = messages
4040
self.temperature = temperature

0 commit comments

Comments
 (0)