forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoManagedChatGPTMemoryGoogleAIStrategy.swift
More file actions
66 lines (58 loc) · 2.13 KB
/
AutoManagedChatGPTMemoryGoogleAIStrategy.swift
File metadata and controls
66 lines (58 loc) · 2.13 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
import Foundation
import GoogleGenerativeAI
import Logger
extension AutoManagedChatGPTMemory {
struct GoogleAIStrategy: AutoManagedChatGPTMemoryStrategy {
let configuration: ChatGPTConfiguration
func countToken(_ message: ChatMessage) async -> Int {
(await OpenAIStrategy().countToken(message)) + 20
// Using local tiktoken instead until I find a faster solution.
// The official solution requires sending a lot of requests when adjusting the prompt.
// adding 20 just incase.
// guard let model = configuration.model else {
// return 0
// }
// let aiModel = GenerativeModel(name: model.info.modelName, apiKey:
// configuration.apiKey)
// if message.isEmpty { return 0 }
// let modelMessage = ModelContent(message)
// return (try? await aiModel.countTokens([modelMessage]).totalTokens) ?? 0
}
func countToken<F>(_: F) async -> Int where F: ChatGPTFunction {
// function is not supported.
return 0
}
}
}
extension ModelContent {
static func convertRole(_ role: ChatMessage.Role) -> String {
switch role {
case .user, .system, .function:
return "user"
case .assistant:
return "model"
}
}
static func convertContent(of message: ChatMessage) -> String {
switch message.role {
case .system:
return "System Prompt: \n\(message.content ?? " ")"
case .user, .function:
return message.content ?? " "
case .assistant:
if let functionCall = message.functionCall {
return """
call function: \(functionCall.name)
arguments: \(functionCall.arguments)
"""
} else {
return message.content ?? " "
}
}
}
init(_ message: ChatMessage) {
let role = Self.convertRole(message.role)
let parts = [ModelContent.Part.text(Self.convertContent(of: message))]
self = .init(role: role, parts: parts)
}
}