Skip to content

Commit 8b757c7

Browse files
author
Suresh Kumar Durairaj
committed
Replaced DialogFlow api(s) with openai(chatGPT) api
1 parent 511cc31 commit 8b757c7

File tree

13 files changed

+215
-88
lines changed

13 files changed

+215
-88
lines changed

MessageKit/MessageKit.xcodeproj/project.pbxproj

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
F9232D65233C56D9005B6C2F /* Chat.swift in Sources */ = {isa = PBXBuildFile; fileRef = F922A69B2337975B00CB35B5 /* Chat.swift */; };
6767
F9232D67233C5BC9005B6C2F /* ChatViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9232D66233C5BC9005B6C2F /* ChatViewTests.swift */; };
6868
F9232D69233F6E33005B6C2F /* UIImageView+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9232D68233F6E33005B6C2F /* UIImageView+Extension.swift */; };
69+
F926BBB629FA7CF300859629 /* screen-shot-1.png in Resources */ = {isa = PBXBuildFile; fileRef = F926BBB529FA7CF300859629 /* screen-shot-1.png */; };
70+
F926BBB829FA7D0200859629 /* screen-shot-2.png in Resources */ = {isa = PBXBuildFile; fileRef = F926BBB729FA7D0200859629 /* screen-shot-2.png */; };
6971
F93BA97B2344764B00B4B622 /* ParameterEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = F93BA9782344764B00B4B622 /* ParameterEncoding.swift */; };
7072
F93BA97C2344764B00B4B622 /* URLParameterEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = F93BA9792344764B00B4B622 /* URLParameterEncoding.swift */; };
7173
F93BA97D2344764B00B4B622 /* JSONParameterEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = F93BA97A2344764B00B4B622 /* JSONParameterEncoder.swift */; };
@@ -134,6 +136,8 @@
134136
F9232D4C233C4F99005B6C2F /* HomeViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HomeViewTests.swift; sourceTree = "<group>"; };
135137
F9232D66233C5BC9005B6C2F /* ChatViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatViewTests.swift; sourceTree = "<group>"; };
136138
F9232D68233F6E33005B6C2F /* UIImageView+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+Extension.swift"; sourceTree = "<group>"; };
139+
F926BBB529FA7CF300859629 /* screen-shot-1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "screen-shot-1.png"; sourceTree = "<group>"; };
140+
F926BBB729FA7D0200859629 /* screen-shot-2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "screen-shot-2.png"; sourceTree = "<group>"; };
137141
F93BA9782344764B00B4B622 /* ParameterEncoding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParameterEncoding.swift; sourceTree = "<group>"; };
138142
F93BA9792344764B00B4B622 /* URLParameterEncoding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLParameterEncoding.swift; sourceTree = "<group>"; };
139143
F93BA97A2344764B00B4B622 /* JSONParameterEncoder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONParameterEncoder.swift; sourceTree = "<group>"; };
@@ -286,6 +290,7 @@
286290
F922A6502336027200CB35B5 /* Resources */ = {
287291
isa = PBXGroup;
288292
children = (
293+
F926BBB429FA7CD900859629 /* Screenshots */,
289294
F922A61A2335F3A200CB35B5 /* Info.plist */,
290295
F922A6152335F3A200CB35B5 /* Assets.xcassets */,
291296
F922A6172335F3A200CB35B5 /* LaunchScreen.storyboard */,
@@ -392,6 +397,15 @@
392397
path = Network;
393398
sourceTree = "<group>";
394399
};
400+
F926BBB429FA7CD900859629 /* Screenshots */ = {
401+
isa = PBXGroup;
402+
children = (
403+
F926BBB529FA7CF300859629 /* screen-shot-1.png */,
404+
F926BBB729FA7D0200859629 /* screen-shot-2.png */,
405+
);
406+
path = Screenshots;
407+
sourceTree = "<group>";
408+
};
395409
F93BA9772344764B00B4B622 /* Encoding */ = {
396410
isa = PBXGroup;
397411
children = (
@@ -465,7 +479,7 @@
465479
isa = PBXProject;
466480
attributes = {
467481
LastSwiftUpdateCheck = 1030;
468-
LastUpgradeCheck = 1030;
482+
LastUpgradeCheck = 1420;
469483
ORGANIZATIONNAME = self;
470484
TargetAttributes = {
471485
F922A60A2335F3A100CB35B5 = {
@@ -507,6 +521,8 @@
507521
buildActionMask = 2147483647;
508522
files = (
509523
F922A68B233696C000CB35B5 /* userList.json in Resources */,
524+
F926BBB629FA7CF300859629 /* screen-shot-1.png in Resources */,
525+
F926BBB829FA7D0200859629 /* screen-shot-2.png in Resources */,
510526
F922A65C2336033400CB35B5 /* AppTheme.plist in Resources */,
511527
F922A6192335F3A200CB35B5 /* LaunchScreen.storyboard in Resources */,
512528
F922A6A02337E79500CB35B5 /* sendMessage.json in Resources */,
@@ -662,6 +678,7 @@
662678
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
663679
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
664680
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
681+
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
665682
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
666683
CLANG_WARN_STRICT_PROTOTYPES = YES;
667684
CLANG_WARN_SUSPICIOUS_MOVE = YES;
@@ -687,7 +704,7 @@
687704
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
688705
GCC_WARN_UNUSED_FUNCTION = YES;
689706
GCC_WARN_UNUSED_VARIABLE = YES;
690-
IPHONEOS_DEPLOYMENT_TARGET = 12.4;
707+
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
691708
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
692709
MTL_FAST_MATH = YES;
693710
ONLY_ACTIVE_ARCH = YES;
@@ -723,6 +740,7 @@
723740
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
724741
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
725742
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
743+
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
726744
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
727745
CLANG_WARN_STRICT_PROTOTYPES = YES;
728746
CLANG_WARN_SUSPICIOUS_MOVE = YES;
@@ -742,7 +760,7 @@
742760
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
743761
GCC_WARN_UNUSED_FUNCTION = YES;
744762
GCC_WARN_UNUSED_VARIABLE = YES;
745-
IPHONEOS_DEPLOYMENT_TARGET = 12.4;
763+
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
746764
MTL_ENABLE_DEBUG_INFO = NO;
747765
MTL_FAST_MATH = YES;
748766
SDKROOT = iphoneos;
@@ -759,6 +777,7 @@
759777
CODE_SIGN_STYLE = Automatic;
760778
DEVELOPMENT_TEAM = 4L453G6266;
761779
INFOPLIST_FILE = MessageKit/Resources/Info.plist;
780+
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
762781
LD_RUNPATH_SEARCH_PATHS = (
763782
"$(inherited)",
764783
"@executable_path/Frameworks",
@@ -777,6 +796,7 @@
777796
CODE_SIGN_STYLE = Automatic;
778797
DEVELOPMENT_TEAM = 4L453G6266;
779798
INFOPLIST_FILE = MessageKit/Resources/Info.plist;
799+
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
780800
LD_RUNPATH_SEARCH_PATHS = (
781801
"$(inherited)",
782802
"@executable_path/Frameworks",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
uuid = "9517D377-A779-477A-A9E6-99015CCB469B"
4+
type = "1"
5+
version = "2.0">
6+
<Breakpoints>
7+
<BreakpointProxy
8+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
9+
<BreakpointContent
10+
uuid = "F401A9A8-2EF1-4C34-969C-71856FE7B502"
11+
shouldBeEnabled = "Yes"
12+
ignoreCount = "0"
13+
continueAfterRunningActions = "No"
14+
filePath = "MessageKit/Modules/Chat/View/ChatViewController.swift"
15+
startingColumnNumber = "9223372036854775807"
16+
endingColumnNumber = "9223372036854775807"
17+
startingLineNumber = "229"
18+
endingLineNumber = "229"
19+
landmarkName = "textViewDidChangeSelection(_:)"
20+
landmarkType = "7">
21+
</BreakpointContent>
22+
</BreakpointProxy>
23+
<BreakpointProxy
24+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
25+
<BreakpointContent
26+
uuid = "08F5A31C-F318-4F8F-A03B-7498E6210318"
27+
shouldBeEnabled = "Yes"
28+
ignoreCount = "0"
29+
continueAfterRunningActions = "No"
30+
filePath = "MessageKit/Modules/Chat/View/ChatViewController.swift"
31+
startingColumnNumber = "9223372036854775807"
32+
endingColumnNumber = "9223372036854775807"
33+
startingLineNumber = "210"
34+
endingLineNumber = "210"
35+
landmarkName = "textView(_:shouldChangeTextIn:replacementText:)"
36+
landmarkType = "7">
37+
</BreakpointContent>
38+
</BreakpointProxy>
39+
</Breakpoints>
40+
</Bucket>

MessageKit/MessageKit/Base/BaseController.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ enum ViewState {
1717
final class Spinner {
1818

1919
static var spinner: UIActivityIndicatorView?
20-
static var style: UIActivityIndicatorView.Style = .whiteLarge
20+
static var style: UIActivityIndicatorView.Style = UIActivityIndicatorView.Style.large
2121
static var baseBackColor = UIColor.white
2222
static var baseColor = UIColor.black
2323

2424
static func show(style: UIActivityIndicatorView.Style = style, backColor: UIColor = baseBackColor, baseColor: UIColor = baseColor) {
25-
if spinner == nil, let window = UIApplication.shared.keyWindow {
25+
if spinner == nil, let window = UIApplication.shared.connectedScenes
26+
.filter({$0.activationState == .foregroundActive})
27+
.compactMap({$0 as? UIWindowScene})
28+
.first?.windows
29+
.filter({$0.isKeyWindow}).first {
2630
let frame = UIScreen.main.bounds
2731
spinner = UIActivityIndicatorView(frame: frame)
2832
spinner!.backgroundColor = backColor
@@ -94,7 +98,7 @@ class BaseController: UIViewController {
9498
let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
9599
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
96100
let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
97-
let statusBarHeight = UIApplication.shared.statusBarFrame.height
101+
let statusBarHeight = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
98102
let navigationBarHeight = self.navigationController?.navigationBar.frame.size.height ?? 0
99103
let topSectionHeight = statusBarHeight + navigationBarHeight
100104
if endFrameY >= UIScreen.main.bounds.size.height {

MessageKit/MessageKit/Modules/Chat/Model/Chat.swift

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,99 @@
66
// Copyright © 2019 self. All rights reserved.
77
//
88

9-
import UIKit
9+
import Foundation
1010

1111
final class Chat: Decodable {
12-
13-
var isReceiver : Bool = true
14-
var timeStampString : String?
15-
var result : ChatResult?
16-
17-
lazy var payload : ChatPayload? = {
18-
self.isReceiver = true
19-
let message = result?.fullFillMent?.messages?.filter{ $0.payload != nil }.first
20-
return message?.payload
12+
var id: String?
13+
var object: String?
14+
var created: TimeInterval?
15+
var model: String?
16+
var usage: Usage?
17+
var choices: [Choice]?
18+
var isReceived: Bool = true
19+
init(isReceived: Bool) {
20+
self.isReceived = isReceived
2121

22-
}()
22+
}
23+
24+
2325

2426
enum CodingKeys : String, CodingKey {
25-
case timeStampString = "timestamp"
26-
case result = "result"
27+
case id, object, created, model, usage, choices
28+
}
29+
30+
31+
init(from decoder: Decoder) throws {
32+
let container = try decoder.container(keyedBy: CodingKeys.self)
33+
id = try? container.decode(String.self, forKey: .id)
34+
object = try? container.decode(String.self, forKey: .object)
35+
created = try? container.decode(TimeInterval.self, forKey: .created)
36+
model = try? container.decode(String.self, forKey: .model)
37+
usage = try? container.decode(Usage.self, forKey: .usage)
38+
choices = try container.decode([Choice].self, forKey: .choices)
39+
2740
}
41+
2842

2943
}
3044

31-
final class ChatResult : Decodable {
32-
var action : String?
33-
var fullFillMent : ChatFullFillMent?
34-
enum CodingKeys : String, CodingKey {
35-
case action = "action"
36-
case fullFillMent = "fulfillment"
45+
final class Choice: Decodable {
46+
var message: Message?
47+
var finishReason: String?
48+
var index : Int?
49+
50+
init() {
3751
}
38-
}
3952

40-
final class ChatFullFillMent : Decodable {
41-
var messages : [Message]?
42-
enum CodingKeys : String, CodingKey {
43-
case messages = "messages"
53+
enum CodingKeys: String, CodingKey {
54+
case message
55+
case finishReason = "finish_reason"
56+
case index
4457
}
4558

59+
init(from decoder: Decoder) throws {
60+
let container = try decoder.container(keyedBy: CodingKeys.self)
61+
message = try? container.decode(Message.self, forKey: .message)
62+
finishReason = try? container.decode(String.self, forKey: .finishReason)
63+
index = try? container.decode(Int.self, forKey: .index)
64+
}
65+
4666
}
4767

48-
49-
final class Message: Decodable {
50-
var payload : ChatPayload?
51-
enum CodingKeys : String, CodingKey {
52-
case payload = "payload"
68+
final class Usage: Decodable {
69+
var totalTokens, completionTokens, promptTokens: Int?
70+
enum CodingKeys: String, CodingKey {
71+
case totkenTokens = "total_tokens"
72+
case completionTokens = "completion_tokens"
73+
case promptTokens = "prompt_tokens"
74+
}
75+
76+
init(from decoder: Decoder) throws {
77+
let container = try decoder.container(keyedBy: CodingKeys.self)
78+
totalTokens = try? container.decode(Int.self, forKey: .totkenTokens)
79+
completionTokens = try? container.decode(Int.self, forKey: .completionTokens)
80+
promptTokens = try? container.decode(Int.self, forKey: .promptTokens)
5381
}
82+
5483
}
5584

56-
57-
open class ChatPayload: Decodable {
58-
var chatMessage : String?
59-
enum CodingKeys : String, CodingKey {
60-
case chatMessage = "defaultReply"
85+
final class Message: Decodable {
86+
var content, role: String?
87+
88+
init(content: String?, role: String?) {
89+
self.content = content
90+
self.role = role
91+
}
92+
93+
enum CodingKeys: String, CodingKey {
94+
case content, role
6195
}
96+
97+
init(from decoder: Decoder) throws {
98+
let container = try decoder.container(keyedBy: CodingKeys.self)
99+
content = try? container.decode(String.self, forKey: .content)
100+
role = try? container.decode(String.self, forKey: .role)
101+
}
102+
62103
}
104+

MessageKit/MessageKit/Modules/Chat/Network/ChatEndPoint.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ extension ChatEndPoint: EndPointType {
1717
var path: String {
1818
switch self {
1919
case .sendMessage:
20-
return "query"
20+
return "v1/chat/completions"
2121
}
2222
}
2323

2424
var baseURL: URL {
25-
return URL(string: "https://api.dialogflow.com/v1/")!
25+
return URL(string: "https://api.openai.com")!
2626
}
2727

2828
var headers: HTTPHeaders? {
29-
return ["Authorization" : "Bearer ea840b32ce0d4f28b162d62d8808bed4",
29+
return ["Authorization" : "Bearer <your-open-ai-api-key>",
3030
"Content-Type" : "application/json"]
3131
}
3232

MessageKit/MessageKit/Modules/Chat/View/ChatCell.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ extension ChatCell {
5656

5757
//ConfigureCell
5858
func configure(chat: Chat?, estimatedSize: CGSize, screenWidth: CGFloat) {
59-
messageTextView.text = chat?.payload?.chatMessage ?? ""
60-
if chat?.isReceiver == false {
59+
messageTextView.text = chat?.choices?.first?.message?.content ?? ""
60+
if chat?.isReceived == false {
6161
bubbleImageView.image = rightBubbleImage
6262
messageTextView.frame = CGRect(x: screenWidth - estimatedSize.width - 32 - 32, y: 32, width: estimatedSize.width + 32, height: estimatedSize.height + 16)
6363
bubbleImageView.frame = CGRect(x: screenWidth - estimatedSize.width - 32 - 32 - 16, y: 16, width: estimatedSize.width + 32 + 32, height: estimatedSize.height + 16 + 32)

0 commit comments

Comments
 (0)