Commit 00202cfe authored by Steven杜宇's avatar Steven杜宇

// AI对话

parent c591b3e3
......@@ -568,6 +568,8 @@
0493B3DE2BA80C2300AF9393 /* YHCertificateFilterButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0493B3DD2BA80C2300AF9393 /* YHCertificateFilterButton.swift */; };
04943BE82CF0A0B500BF2255 /* submit_page_scroll.gif in Resources */ = {isa = PBXBuildFile; fileRef = 04943BE72CF0A0B500BF2255 /* submit_page_scroll.gif */; };
04943BEA2CF1B44100BF2255 /* YHAIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04943BE92CF1B44100BF2255 /* YHAIViewController.swift */; };
04943BED2CF46B6600BF2255 /* YHAIRequestManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04943BEC2CF46B6600BF2255 /* YHAIRequestManager.swift */; };
04943BF42CF4954800BF2255 /* YHAIMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04943BF32CF4954800BF2255 /* YHAIMessageCell.swift */; };
049537852BA9B28500AF7E59 /* YHCertificateTemplateCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049537842BA9B28500AF7E59 /* YHCertificateTemplateCell.swift */; };
049663BA2BF6105C003C8295 /* YHRefreshHeaderAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049663B92BF6105C003C8295 /* YHRefreshHeaderAnimator.swift */; };
049663C02BF729F6003C8295 /* galaxy_refresher.gif in Resources */ = {isa = PBXBuildFile; fileRef = 049663BF2BF729F5003C8295 /* galaxy_refresher.gif */; };
......@@ -1606,6 +1608,8 @@
0493B3DD2BA80C2300AF9393 /* YHCertificateFilterButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHCertificateFilterButton.swift; sourceTree = "<group>"; };
04943BE72CF0A0B500BF2255 /* submit_page_scroll.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = submit_page_scroll.gif; sourceTree = "<group>"; };
04943BE92CF1B44100BF2255 /* YHAIViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHAIViewController.swift; sourceTree = "<group>"; };
04943BEC2CF46B6600BF2255 /* YHAIRequestManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHAIRequestManager.swift; sourceTree = "<group>"; };
04943BF32CF4954800BF2255 /* YHAIMessageCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHAIMessageCell.swift; sourceTree = "<group>"; };
049537842BA9B28500AF7E59 /* YHCertificateTemplateCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHCertificateTemplateCell.swift; sourceTree = "<group>"; };
049663B92BF6105C003C8295 /* YHRefreshHeaderAnimator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YHRefreshHeaderAnimator.swift; sourceTree = "<group>"; };
049663BF2BF729F5003C8295 /* galaxy_refresher.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = galaxy_refresher.gif; sourceTree = "<group>"; };
......@@ -3908,6 +3912,47 @@
path = VM;
sourceTree = "<group>";
};
04943BEE2CF4945800BF2255 /* AI */ = {
isa = PBXGroup;
children = (
04943BEF2CF4946200BF2255 /* AI-Dialogue(AI对话) */,
);
path = AI;
sourceTree = "<group>";
};
04943BEF2CF4946200BF2255 /* AI-Dialogue(AI对话) */ = {
isa = PBXGroup;
children = (
04943BF22CF4948400BF2255 /* C */,
04943BF12CF4947F00BF2255 /* V */,
04943BF02CF4947A00BF2255 /* M */,
);
path = "AI-Dialogue(AI对话)";
sourceTree = "<group>";
};
04943BF02CF4947A00BF2255 /* M */ = {
isa = PBXGroup;
children = (
);
path = M;
sourceTree = "<group>";
};
04943BF12CF4947F00BF2255 /* V */ = {
isa = PBXGroup;
children = (
04943BF32CF4954800BF2255 /* YHAIMessageCell.swift */,
);
path = V;
sourceTree = "<group>";
};
04943BF22CF4948400BF2255 /* C */ = {
isa = PBXGroup;
children = (
04943BE92CF1B44100BF2255 /* YHAIViewController.swift */,
);
path = C;
sourceTree = "<group>";
};
049663B62BF60BB7003C8295 /* CustomRefreshHeader */ = {
isa = PBXGroup;
children = (
......@@ -5034,6 +5079,7 @@
A5ACE8F42B4564F7002C94D2 /* YHNetRequest.swift */,
A5ACE8F52B4564F7002C94D2 /* YHNetworkStatusManager.swift */,
A5ACE8F62B4564F7002C94D2 /* YHBaseUrlManager.swift */,
04943BEC2CF46B6600BF2255 /* YHAIRequestManager.swift */,
);
path = NetWork;
sourceTree = "<group>";
......@@ -5176,6 +5222,7 @@
A5ACE91B2B4564F7002C94D2 /* Modules */ = {
isa = PBXGroup;
children = (
04943BEE2CF4945800BF2255 /* AI */,
04129E842CD0C8000072F87B /* AliYunLog(阿里云日志) */,
041470892C885EC900B7B688 /* ButlerServiceManager(银河管家) */,
A596CADD2C6B9AF200F85797 /* HomeAlertViewManager(首页弹窗管理) */,
......@@ -5212,7 +5259,6 @@
A5ACE9212B4564F7002C94D2 /* YHNavigationController.swift */,
A5ACE9222B4564F7002C94D2 /* YHTabBarViewController.swift */,
A5551FFD2B4C26CE00510980 /* YHBaseViewModel.swift */,
04943BE92CF1B44100BF2255 /* YHAIViewController.swift */,
);
path = C;
sourceTree = "<group>";
......@@ -6002,6 +6048,7 @@
04CE1AD82C2AD91F001CB80A /* YHActivityListViewController.swift in Sources */,
A5ACE94E2B4564F7002C94D2 /* AlignedCollectionViewFlowLayout.swift in Sources */,
A567E5B32BD7643D00D5D5A0 /* YHServiceOrderAlertView.swift in Sources */,
04943BF42CF4954800BF2255 /* YHAIMessageCell.swift in Sources */,
A592FE932BA6CFFC0062FACA /* YHCertificateEntryBottomView.swift in Sources */,
A5ACE93B2B4564F7002C94D2 /* UIFont+Extension.swift in Sources */,
047F3DD92CE601D1001B2A6D /* YHEducationCertificateConfirmCell.swift in Sources */,
......@@ -6113,6 +6160,7 @@
A5ACE9312B4564F7002C94D2 /* YHPersonalCenterCell.swift in Sources */,
0430E6562C7342AB000511E2 /* YHAdopterIncomeFileCountTableViewCell.swift in Sources */,
0449EEEB2C8EF2B700A397FD /* YHResignMaterialListVC.swift in Sources */,
04943BED2CF46B6600BF2255 /* YHAIRequestManager.swift in Sources */,
044D0C1C2C22C30B00C5CF5E /* YHEditSchoolViewController.swift in Sources */,
044E31C92CBD2AA400500F8E /* YHResignInfoConfirmIViewModel.swift in Sources */,
A5CA3F562C74850200EB22F5 /* YHUploadCertificateDetailVC.swift in Sources */,
......
//
// YHAIViewController.swift
// galaxy
//
// Created by Dufet on 2024/11/23.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
import Alamofire
class YHAIViewController: YHBaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func requestData() {
// AF.request("http://192.168.52.250:9898/stream")
// .responseStream { response in
// switch response.result {
// case .success(let stream):
// for line in stream {
// print("Received line: \(line)")
// }
// case .failure(let error):
// print("Error: \(error)")
// }
// }
}
}
//
// YHAIViewController.swift
// galaxy
//
// Created by Dufet on 2024/11/23.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
import Alamofire
class YHAIViewController: YHBaseViewController {
var messages:[YHAIResponse] = []
lazy var tableView: UITableView = {
let tableView = UITableView(frame:.zero, style:.grouped)
if #available(iOS 11.0, *) {
tableView.contentInsetAdjustmentBehavior = .never
}
tableView.showsVerticalScrollIndicator = false
tableView.estimatedSectionHeaderHeight = 1.0
tableView.estimatedSectionFooterHeight = 1.0
tableView.backgroundColor = UIColor(hexString:"#F8F8F8")
tableView.separatorStyle = .none
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
tableView.register(YHAIMessageCell.self, forCellReuseIdentifier: YHAIMessageCell.cellReuseIdentifier)
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
gk_navTitle = "AI对话"
self.view.addSubview(self.tableView)
tableView.snp.makeConstraints { make in
make.left.right.bottom.equalTo(0)
make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
}
requestData()
}
func requestData() {
YHAIRequestManager.shared.requestAI(query: "今天深圳天气怎么样") {
[weak self] res in
guard let self = self else { return }
self.handleMessage(res)
}
}
func handleMessage(_ res: YHAIResponse) {
var find = false
for msg in messages {
if msg.uuid == res.uuid {
find = true
if msg.getType() == .text {
msg.body.contentText += res.body.contentText
}
}
}
if !find {
messages.append(res)
}
self.tableView.reloadData()
}
}
extension YHAIViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if 0 <= indexPath.row, indexPath.row < messages.count {
let msg = messages[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: YHAIMessageCell.cellReuseIdentifier, for: indexPath) as! YHAIMessageCell
cell.message = msg
return cell
}
let defaultCell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
return defaultCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1.0
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView()
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 1.0
}
}
//
// YHAIMessageCell.swift
// galaxy
//
// Created by Dufet on 2024/11/25.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
class YHAIMessageCell: UITableViewCell {
static let cellReuseIdentifier = "YHAIMessageCell"
var message: YHAIResponse = YHAIResponse() {
didSet {
messageLabel.text = message.body.contentText
}
}
var whiteContentView: UIView = {
let v = UIView()
v.backgroundColor = .white
v.layer.cornerRadius = 4.0
v.clipsToBounds = true
return v
}()
var
lazy var messageLabel:UILabel = {
let lable = UILabel()
lable.textColor = UIColor.mainTextColor
lable.textAlignment = .left
lable.font = UIFont.PFSC_R(ofSize:14)
lable.numberOfLines = 0
return lable
}()
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
}
func setupUI() {
contentView.backgroundColor = .clear
backgroundColor = .clear
contentView.addSubview(whiteContentView)
whiteContentView.addSubview(messageLabel)
whiteContentView.snp.makeConstraints { make in
make.left.equalTo(20)
make.right.lessThanOrEqualTo(-20)
make.top.equalTo(20)
make.bottom.equalTo(-20)
}
messageLabel.snp.makeConstraints { make in
make.left.equalTo(16)
make.right.equalTo(-16)
make.top.equalTo(16)
make.bottom.equalTo(-16)
}
}
}
......@@ -349,6 +349,12 @@ extension YHMyViewController : UITableViewDelegate, UITableViewDataSource {
return
}
if true {
let vc = YHAIViewController()
self.navigationController?.pushViewController(vc)
return
}
let item: PersonalModuleItem = items[indexPath.row]
switch item.type {
case .myCard://我的名片
......
//
// YHAIRequestManager.swift
// galaxy
//
// Created by Dufet on 2024/11/25.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
import Alamofire
import SmartCodable
enum YHAIResponseType: Int {
case none = 0
case text = 1
case card = 2
}
class YHAIResponse: CustomStringConvertible {
var id: String = ""
var event: String = ""
var data: [String : Any] = [:]
var uuid: String = ""
var body = YHAIResponseBody()
func isFinishd() -> Bool {
if event.isEmpty {
return true
}
if event == "conversation.message.completed" {
return true
}
return false
}
func getType() -> YHAIResponseType {
if let type = data["contentType"] as? Int, type == 1 {
return .text
}
return .none
}
func getText() -> String {
if getType() == .text {
if let text = data["contentText"] as? String, !text.isEmpty {
return text
}
}
return ""
}
var description: String {
return "uudi:\(uuid)\n,id: \(id)\n, event: \(event)\n, data: \(data))\n\n"
}
}
class YHAIResponseBody: SmartCodable {
// "contentType":1,"contentText":"如何判断未来深圳的天气走势?","botId":"7439200648601714700","status":"completed"
var contentType: Int = 0
var contentText: String = ""
var botId: String = ""
var status: String = ""
required init() {
}
}
class YHAIRequestManager: NSObject {
static let shared = YHAIRequestManager()
let prefix_id = "id:"
let prefix_event = "event:"
let prefix_data = "data:"
var uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description
func requestAI(query: String = "", completion:((_ : YHAIResponse) ->())?) {
var url = "http://192.168.52.250:9898/example?query=" + query
url = url.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
self.uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description
AF.streamRequest(url).responseStreamString {
[weak self] stream in
guard let self = self else { return }
switch stream.event {
case let .stream(result):
switch result {
case let .success(string):
// print(string)
self.handle(dataString: string) {
[weak self] res in
// guard let self = self else { return }
completion?(res)
}
case let .failure(error):
print("")
}
case let .complete(completion):
print("response done")
}
}
}
// 处理data字符串
func handle(dataString: String, completion:((_ : YHAIResponse) ->())?) {
let arr = dataString.components(separatedBy: "\n\n")
if arr.count <= 0 {
return
}
for text in arr {
if text.isEmpty {
continue
}
let lines = text.components(separatedBy: "\n")
if lines.count > 0 {
let response = YHAIResponse()
response.uuid = uuid
for line in lines {
if line.starts(with: prefix_id) {
if let idRange = line.range(of: prefix_id) {
let id = String(line[idRange.upperBound...])
response.id = id
}
} else if line.starts(with: prefix_event) {
if let eventRange = line.range(of: prefix_event) {
let eventString = String(line[eventRange.upperBound...])
response.event = eventString
}
} else if line.starts(with: prefix_data) {
if let dataRange = line.range(of: prefix_data) {
let jsonString = String(line[dataRange.upperBound...])
do {
if let jsonData = jsonString.data(using: .utf8) {
let jsonDict = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any]
let dic = (jsonDict ?? [:])
response.data = dic
if let body = YHAIResponseBody.deserialize(from: dic) {
response.body = body
}
}
} catch {
}
}
}
}
if response.isFinishd() {
// 一段话结束需要重新生成uuid 来
uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description
}
completion?(response)
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment