forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLMChain.swift
More file actions
43 lines (36 loc) · 1.16 KB
/
Copy pathLLMChain.swift
File metadata and controls
43 lines (36 loc) · 1.16 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
import Foundation
public class ChatModelChain<Input>: Chain {
public typealias Output = ChatMessage
public internal(set) var chatModel: ChatModel
public internal(set) var promptTemplate: (Input) -> [ChatMessage]
public internal(set) var stops: [String]
public init(
chatModel: ChatModel,
stops: [String] = [],
promptTemplate: @escaping (Input) -> [ChatMessage]
) {
self.chatModel = chatModel
self.promptTemplate = promptTemplate
self.stops = stops
}
public func callLogic(
_ input: Input,
callbackManagers: [CallbackManager]
) async throws -> Output {
let prompt = promptTemplate(input)
let output = try await chatModel.generate(
prompt: prompt,
stops: stops,
callbackManagers: callbackManagers
)
return output
}
public func parseOutput(_ output: Output) -> String {
if let content = output.content {
return content
} else if let functionCall = output.functionCall {
return "\(functionCall.name): \(functionCall.arguments)"
}
return ""
}
}