Skip to content

Commit

Permalink
add prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderLineChan committed Mar 29, 2023
1 parent ff3eea0 commit 3bdd1d2
Show file tree
Hide file tree
Showing 15 changed files with 342 additions and 179 deletions.
20 changes: 12 additions & 8 deletions OSXChatGPT/OSXChatGPT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
CB27656629D1DA9800897E0E /* AIPromptView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656529D1DA9800897E0E /* AIPromptView.swift */; };
CB27656929D1E65100897E0E /* Conversation+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656729D1E65100897E0E /* Conversation+CoreDataClass.swift */; };
CB27656A29D1E65100897E0E /* Conversation+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656829D1E65100897E0E /* Conversation+CoreDataProperties.swift */; };
CB27656D29D1E6A100897E0E /* Prompt+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656B29D1E6A100897E0E /* Prompt+CoreDataClass.swift */; };
CB27656E29D1E6A100897E0E /* Prompt+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656C29D1E6A100897E0E /* Prompt+CoreDataProperties.swift */; };
CB27657329D30F1400897E0E /* AIPromptViewMdoel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27657229D30F1400897E0E /* AIPromptViewMdoel.swift */; };
CB27657529D33D7A00897E0E /* AIPromptInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27657429D33D7A00897E0E /* AIPromptInputView.swift */; };
CB28A52229C07BE500F0286A /* KeyboardMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB28A52129C07BE500F0286A /* KeyboardMonitor.swift */; };
Expand All @@ -43,6 +41,9 @@
CB2F972029CE1ADC004EBD96 /* OSXChatGPT.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = CBC4B0FD29B8BF9600650296 /* OSXChatGPT.xcdatamodeld */; };
CB2F972229CED6AE004EBD96 /* ChatRoomInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB2F972129CED6AE004EBD96 /* ChatRoomInputView.swift */; };
CB2F972829CEFB65004EBD96 /* ChatRoomToolBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB2F972729CEFB65004EBD96 /* ChatRoomToolBar.swift */; };
CB53A3BB29D4795300A5B8FC /* AIPromptDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB53A3BA29D4795300A5B8FC /* AIPromptDataManager.swift */; };
CB53A3BE29D48C8F00A5B8FC /* Prompt+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB53A3BC29D48C8F00A5B8FC /* Prompt+CoreDataClass.swift */; };
CB53A3BF29D48C8F00A5B8FC /* Prompt+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB53A3BD29D48C8F00A5B8FC /* Prompt+CoreDataProperties.swift */; };
CBC4B12329B8D28D00650296 /* Message+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBC4B11D29B8D28D00650296 /* Message+CoreDataClass.swift */; };
CBC4B12429B8D28D00650296 /* Message+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBC4B11E29B8D28D00650296 /* Message+CoreDataProperties.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -74,14 +75,15 @@
CB27656529D1DA9800897E0E /* AIPromptView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIPromptView.swift; sourceTree = "<group>"; };
CB27656729D1E65100897E0E /* Conversation+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Conversation+CoreDataClass.swift"; sourceTree = "<group>"; };
CB27656829D1E65100897E0E /* Conversation+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Conversation+CoreDataProperties.swift"; sourceTree = "<group>"; };
CB27656B29D1E6A100897E0E /* Prompt+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Prompt+CoreDataClass.swift"; sourceTree = "<group>"; };
CB27656C29D1E6A100897E0E /* Prompt+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Prompt+CoreDataProperties.swift"; sourceTree = "<group>"; };
CB27657229D30F1400897E0E /* AIPromptViewMdoel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIPromptViewMdoel.swift; sourceTree = "<group>"; };
CB27657429D33D7A00897E0E /* AIPromptInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIPromptInputView.swift; sourceTree = "<group>"; };
CB28A52129C07BE500F0286A /* KeyboardMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardMonitor.swift; sourceTree = "<group>"; };
CB28A52729C1569900F0286A /* ThinkingAnimationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThinkingAnimationView.swift; sourceTree = "<group>"; };
CB2F972129CED6AE004EBD96 /* ChatRoomInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatRoomInputView.swift; sourceTree = "<group>"; };
CB2F972729CEFB65004EBD96 /* ChatRoomToolBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatRoomToolBar.swift; sourceTree = "<group>"; };
CB53A3BA29D4795300A5B8FC /* AIPromptDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIPromptDataManager.swift; sourceTree = "<group>"; };
CB53A3BC29D48C8F00A5B8FC /* Prompt+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Prompt+CoreDataClass.swift"; sourceTree = "<group>"; };
CB53A3BD29D48C8F00A5B8FC /* Prompt+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Prompt+CoreDataProperties.swift"; sourceTree = "<group>"; };
CBC4B0FE29B8BF9600650296 /* OSXChatGPT.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = OSXChatGPT.xcdatamodel; sourceTree = "<group>"; };
CBC4B11D29B8D28D00650296 /* Message+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Message+CoreDataClass.swift"; sourceTree = "<group>"; };
CBC4B11E29B8D28D00650296 /* Message+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Message+CoreDataProperties.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -155,6 +157,7 @@
182B437029BC5D1B00F06778 /* ViewModel.swift */,
CB28A52129C07BE500F0286A /* KeyboardMonitor.swift */,
CB27657229D30F1400897E0E /* AIPromptViewMdoel.swift */,
CB53A3BA29D4795300A5B8FC /* AIPromptDataManager.swift */,
);
path = DataProvider;
sourceTree = "<group>";
Expand Down Expand Up @@ -214,8 +217,8 @@
CBC4B11429B8CB1B00650296 /* Models */ = {
isa = PBXGroup;
children = (
CB27656B29D1E6A100897E0E /* Prompt+CoreDataClass.swift */,
CB27656C29D1E6A100897E0E /* Prompt+CoreDataProperties.swift */,
CB53A3BC29D48C8F00A5B8FC /* Prompt+CoreDataClass.swift */,
CB53A3BD29D48C8F00A5B8FC /* Prompt+CoreDataProperties.swift */,
CB27656729D1E65100897E0E /* Conversation+CoreDataClass.swift */,
CB27656829D1E65100897E0E /* Conversation+CoreDataProperties.swift */,
CBC4B11D29B8D28D00650296 /* Message+CoreDataClass.swift */,
Expand Down Expand Up @@ -320,15 +323,16 @@
182B437929BC5D6200F06778 /* OSXChatGPTApp.swift in Sources */,
CB28A52229C07BE500F0286A /* KeyboardMonitor.swift in Sources */,
182B437429BC5D1B00F06778 /* ViewModel.swift in Sources */,
CB53A3BE29D48C8F00A5B8FC /* Prompt+CoreDataClass.swift in Sources */,
182B436729BC5C8700F06778 /* ChatRoomView.swift in Sources */,
CB27656929D1E65100897E0E /* Conversation+CoreDataClass.swift in Sources */,
CB27656E29D1E6A100897E0E /* Prompt+CoreDataProperties.swift in Sources */,
CB0F5A6029D059C4005B71D2 /* SplashCodeSyntaxHighlighter.swift in Sources */,
CBC4B12429B8D28D00650296 /* Message+CoreDataProperties.swift in Sources */,
182B436529BC5C8700F06778 /* SessionsView.swift in Sources */,
CB27657529D33D7A00897E0E /* AIPromptInputView.swift in Sources */,
CB27656D29D1E6A100897E0E /* Prompt+CoreDataClass.swift in Sources */,
CB53A3BB29D4795300A5B8FC /* AIPromptDataManager.swift in Sources */,
182B437229BC5D1B00F06778 /* HTTPClient.swift in Sources */,
CB53A3BF29D48C8F00A5B8FC /* Prompt+CoreDataProperties.swift in Sources */,
182B436829BC5C8700F06778 /* MainContentView.swift in Sources */,
CB28A52829C1569900F0286A /* ThinkingAnimationView.swift in Sources */,
182B437529BC5D1B00F06778 /* ChatGPTManager.swift in Sources */,
Expand Down
38 changes: 38 additions & 0 deletions OSXChatGPT/OSXChatGPT/DataProvider/AIPromptDataManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// AIPromptDataManager.swift
// OSXChatGPT
//
// Created by CoderChan on 2023/3/29.
//

import Foundation


class AIPromptDataManager {
static var shared = AIPromptDataManager()

var allPrompts: [Prompt] = []


init() {
fetchAllPrompts()
}


func addPrompt(prompt: Prompt) {

}

func deletePrompt(prompt: Prompt) {

}

}

extension AIPromptDataManager {
private func fetchAllPrompts() {
let completedDateSort = NSSortDescriptor(keyPath: \Prompt.createdDate, ascending: false)
let aa: [Prompt] = CoreDataManager.shared.fetch("Prompt", sorting: [completedDateSort])
allPrompts = aa
}
}
82 changes: 81 additions & 1 deletion OSXChatGPT/OSXChatGPT/DataProvider/AIPromptViewMdoel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,76 @@
import Foundation
import SwiftUI

class AIPromptSessionViewMdoel: ObservableObject {
@Published var allPrompts: [Prompt] = []
@Published var selectedItem :Prompt?

func fetchAllPrompts(session: Conversation) {
let completedDateSort = NSSortDescriptor(keyPath: \Prompt.serial, ascending: false)
var aa: [Prompt] = CoreDataManager.shared.fetch("Prompt", sorting: [completedDateSort])

if (aa.count == 0) {
aa = AIPromptSessionViewMdoel.createDefaultPrompt()
}

if let prompt = session.prompt {
if aa.contains(where: {$0.id == prompt.id}) {
selectedItem = prompt
}
}

aa.removeAll(where: {$0.type == 1})
let prompt = Prompt(context: CoreDataManager.shared.container.viewContext)
prompt.type = 1
prompt.id = UUID()
prompt.createdDate = Date()
prompt.hexColor = "#999999"
aa.insert(prompt, at: 0)
if selectedItem == nil {
selectedItem = prompt
}

CoreDataManager.shared.saveData()
allPrompts = aa
}

func deletePrompt(prompt: Prompt) {
if prompt.type == 1 {
return
}
allPrompts.removeAll(where: {$0.id == prompt.id})
if prompt.id == selectedItem?.id {
selectedItem = allPrompts.first
}
CoreDataManager.shared.delete(object: prompt)
}

static func createDefaultPrompt() -> [Prompt] {
var temp: [Prompt] = []
let prompt1 = Prompt(context: CoreDataManager.shared.container.viewContext)
prompt1.id = UUID()
prompt1.createdDate = Date()
prompt1.title = "翻译"
prompt1.prompt = "翻译我说的任何中文或英文。只返回翻译结果,不解释它"
prompt1.hexColor = NSColor.randomColor().toHexString()
temp.append(prompt1)

let prompt2 = Prompt(context: CoreDataManager.shared.container.viewContext)
prompt2.id = UUID()
prompt2.createdDate = Date()
prompt2.title = "iOS开发者"
prompt2.prompt = "假设你是一名iOS开发者,使用的是swift语言"
prompt2.hexColor = NSColor.randomColor().toHexString()
temp.append(prompt2)


CoreDataManager.shared.saveData()
return temp
}

}

class AIPromptViewMdoel {
class AIPromptViewMdoel: ObservableObject {
let isSession: Bool
@Published var allPrompts: [Prompt] = []
@Published var prompts: [Prompt] = []
Expand All @@ -31,6 +99,18 @@ class AIPromptViewMdoel {
}


func addPrompt(title: String, content: String) {
let prompt = Prompt(context: CoreDataManager.shared.container.viewContext)
prompt.title = title
prompt.prompt = content
prompt.id = UUID()
prompt.createdDate = Date()
CoreDataManager.shared.saveData()

// AIPromptDataManager.shared.addPrompt(prompt: prompt)
}


}

extension AIPromptViewMdoel {
Expand Down
10 changes: 8 additions & 2 deletions OSXChatGPT/OSXChatGPT/DataProvider/ChatGPTManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct ChatGPTRequest {
let messages: [Message]
let answerType: ChatGPTAnswerType//是否流式回答
let contextCount: ChatGPTContext
let systemMsg: String?//修饰语

// static let completions = URL(string: "https://api.openai.com/v1/completions")!
// static let images = URL(string: "https://api.openai.com/v1/images/generations")!
Expand Down Expand Up @@ -62,6 +63,10 @@ struct ChatGPTRequest {
temp.append(["role": msg.role ?? "user", "content": msg.text ?? ""])
}
}
if let system = systemMsg {
let sys = ["role":"system", "content":system]
temp.insert(sys, at: 0)
}
return temp
}
}
Expand Down Expand Up @@ -270,12 +275,13 @@ extension ChatGPTManager {


}
func askChatGPTStream(messages: [Message], complete:((ChatGPTResponse) -> ())?) {
func askChatGPTStream(messages: [Message], prompt: String?, complete:((ChatGPTResponse) -> ())?) {
if chatGPTSpeaking == true {
return
}
chatGPTSpeaking = true
let request = ChatGPTRequest(model: model, messages: messages, answerType: answerType, contextCount: askContextCount, apiKey: apiKey)
let request = ChatGPTRequest(model: model, messages: messages, answerType: answerType, contextCount: askContextCount, systemMsg: prompt, apiKey: apiKey)

Task {
do {
let stream = try await httpClient.postStream(chatRequest: request)
Expand Down
28 changes: 22 additions & 6 deletions OSXChatGPT/OSXChatGPT/DataProvider/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ import Splash
init() {
fetchConversations()
}
func updateConversation(sesstionId: String, prompt: Prompt) {
var con = fetchConversation(sesstionId: sesstionId)
if con == nil {
con = Conversation(context: CoreDataManager.shared.container.viewContext)
con?.sesstionId = sesstionId
con?.id = UUID()
}
con!.updateData = Date()
con?.prompt = prompt
CoreDataManager.shared.saveData()
if let index = conversations.firstIndex(where: { $0.sesstionId == sesstionId}) {
conversations[index] = con!
} else {
conversations.insert(con!, at: 0)
}
}
func updateConversation(sesstionId: String, message: Message?) {
var con = fetchConversation(sesstionId: sesstionId)
if con == nil {
Expand Down Expand Up @@ -192,16 +208,16 @@ extension ViewModel {
CoreDataManager.shared.delete(objects: msg)

}
func resendMessage(sesstionId: String) {
func resendMessage(sesstionId: String, prompt: String?) {
if let lastMsg = messages.last {
messages.removeAll(where: {$0.id == lastMsg.id })
CoreDataManager.shared.delete(objects: [lastMsg])
}
sendMessage(sesstionId: sesstionId, messages: messages) {
sendMessage(sesstionId: sesstionId, messages: messages, prompt:prompt) {

}
}
func addNewMessage(sesstionId: String, text: String, role: String, updateBlock: @escaping(() -> ())) {
func addNewMessage(sesstionId: String, text: String, role: String, prompt: String?, updateBlock: @escaping(() -> ())) {
if sesstionId.isEmpty {
return
}
Expand All @@ -222,7 +238,7 @@ extension ViewModel {
print("发送问题:\(text)")
var sendMsgs = messages
sendMsgs.removeAll(where: {$0.type == 1})
sendMessage(sesstionId: sesstionId, messages: sendMsgs, updateBlock: updateBlock)
sendMessage(sesstionId: sesstionId, messages: sendMsgs, prompt: prompt, updateBlock: updateBlock)
}
func cancel() {
ChatGPTManager.shared.stopResponse()
Expand All @@ -239,13 +255,13 @@ extension ViewModel {
}
}
}
private func sendMessage(sesstionId: String, messages: [Message], updateBlock: @escaping(() -> ())) {
private func sendMessage(sesstionId: String, messages: [Message], prompt: String?, updateBlock: @escaping(() -> ())) {
var isFeedback = false
var newMsg: Message?
if ChatGPTManager.shared.answerType.valueBool {
self.showStopAnswerBtn = true
}
ChatGPTManager.shared.askChatGPTStream(messages: messages) { rsp in
ChatGPTManager.shared.askChatGPTStream(messages: messages, prompt: prompt) { rsp in
if rsp.request.answerType == .stream {
isFeedback = true
//流式请求
Expand Down
27 changes: 27 additions & 0 deletions OSXChatGPT/OSXChatGPT/Models/NSColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,37 @@ extension NSColor {
self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: alpha)
}

convenience init(hex: String, alpha: CGFloat = 1) {
var hexString = hex
if hexString.hasPrefix("#") {
hexString.remove(at: hexString.startIndex)
}
let scanner = Scanner(string: hexString)
scanner.scanLocation = 0
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let red = CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0
let green = CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0
let blue = CGFloat(rgbValue & 0x0000FF) / 255.0
self.init(red: red, green: green, blue: blue, alpha: alpha)
}

static func randomColor() -> NSColor {
let r = CGFloat(arc4random_uniform(256))
let g = CGFloat(arc4random_uniform(256))
let b = CGFloat(arc4random_uniform(256))
return NSColor(r: r, g: g, b: b)
}

func toHexString() -> String {



let redValue = Int(self.redComponent * 255)
let greenValue = Int(self.greenComponent * 255)
let blueValue = Int(self.blueComponent * 255)
let hexString = String(format: "#%02X%02X%02X", redValue, greenValue, blueValue)
return hexString
}

}
15 changes: 13 additions & 2 deletions OSXChatGPT/OSXChatGPT/Models/Prompt+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@
// Prompt+CoreDataClass.swift
// OSXChatGPT
//
// Created by CoderChan on 2023/3/27.
// Created by CoderChan on 2023/3/29.
//
//

import Foundation
import CoreData
import SwiftUI


public class Prompt: NSManagedObject {

var color: Color {
if let hex = hexColor {
return NSColor(hex: hex).toColor()
}else {
let hex = NSColor.randomColor().toHexString()
hexColor = hex
CoreDataManager.shared.saveData()
return NSColor(hex: hex).toColor()
}

}
}
Loading

0 comments on commit 3bdd1d2

Please sign in to comment.