forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLegacyChatGPTService.swift
More file actions
108 lines (94 loc) · 3.44 KB
/
LegacyChatGPTService.swift
File metadata and controls
108 lines (94 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import AIModel
import AsyncAlgorithms
import ChatBasic
import Dependencies
import Foundation
import IdentifiedCollections
import Preferences
@available(*, deprecated, message: "Use ChatGPTServiceType instead.")
public protocol LegacyChatGPTServiceType {
var memory: ChatGPTMemory { get set }
var configuration: ChatGPTConfiguration { get set }
func send(content: String, summary: String?) async throws -> AsyncThrowingStream<String, Error>
func stopReceivingMessage() async
}
@available(*, deprecated, message: "Use ChatGPTServiceType instead.")
public class LegacyChatGPTService: LegacyChatGPTServiceType {
public var memory: ChatGPTMemory
public var configuration: ChatGPTConfiguration
public var functionProvider: ChatGPTFunctionProvider
var runningTask: Task<AsyncThrowingStream<String, any Error>, Never>?
public init(
memory: ChatGPTMemory = AutoManagedChatGPTMemory(
systemPrompt: "",
configuration: UserPreferenceChatGPTConfiguration(),
functionProvider: NoChatGPTFunctionProvider(),
maxNumberOfMessages: .max
),
configuration: ChatGPTConfiguration = UserPreferenceChatGPTConfiguration(),
functionProvider: ChatGPTFunctionProvider = NoChatGPTFunctionProvider()
) {
self.memory = memory
self.configuration = configuration
self.functionProvider = functionProvider
}
@Dependency(\.uuid) var uuid
@Dependency(\.date) var date
@Dependency(\.chatCompletionsAPIBuilder) var chatCompletionsAPIBuilder
/// Send a message and stream the reply.
public func send(
content: String,
summary: String? = nil
) async throws -> AsyncThrowingStream<String, Error> {
let task = Task {
if !content.isEmpty || summary != nil {
let newMessage = ChatMessage(
id: uuid().uuidString,
role: .user,
content: content,
name: nil,
toolCalls: nil,
summary: summary,
references: []
)
await memory.appendMessage(newMessage)
}
let service = ChatGPTService(
configuration: configuration,
functionProvider: functionProvider
)
let responses = service.send(memory)
return responses.compactMap { response in
switch response {
case let .partialText(token): return token
default: return nil
}
}.eraseToThrowingStream()
}
runningTask = task
return await task.value
}
/// Send a message and get the reply in return.
public func sendAndWait(
content: String,
summary: String? = nil
) async throws -> String? {
if !content.isEmpty || summary != nil {
let newMessage = ChatMessage(
id: uuid().uuidString,
role: .user,
content: content,
summary: summary
)
await memory.appendMessage(newMessage)
}
let service = ChatGPTService(
configuration: configuration,
functionProvider: functionProvider
)
return try await service.send(memory).asText()
}
public func stopReceivingMessage() {
runningTask?.cancel()
}
}