Skip to content

Commit ce3f412

Browse files
committed
Make system prompt more obvious to Gemini
1 parent 9faf7b0 commit ce3f412

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

Tool/Sources/OpenAIService/Memory/AutoManagedChatGPTMemoryStrategy/AutoManagedChatGPTMemoryGoogleAIStrategy.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ extension AutoManagedChatGPTMemory {
77
let configuration: ChatGPTConfiguration
88

99
func countToken(_ message: ChatMessage) async -> Int {
10-
(await OpenAIStrategy().countToken(message)) + 10
10+
(await OpenAIStrategy().countToken(message)) + 20
1111
// Using local tiktoken instead until I find a faster solution.
1212
// The official solution requires sending a lot of requests when adjusting the prompt.
13-
// adding 10 just incase.
14-
13+
// adding 20 just incase.
14+
1515
// guard let model = configuration.model else {
1616
// return 0
1717
// }
18-
// let aiModel = GenerativeModel(name: model.info.modelName, apiKey: configuration.apiKey)
18+
// let aiModel = GenerativeModel(name: model.info.modelName, apiKey:
19+
// configuration.apiKey)
1920
// if message.isEmpty { return 0 }
2021
// let modelMessage = ModelContent(message)
2122
// return (try? await aiModel.countTokens([modelMessage]).totalTokens) ?? 0
@@ -41,7 +42,19 @@ extension AutoManagedChatGPTMemory {
4142

4243
for message in history {
4344
let lastIndex = reformattedHistory.endIndex - 1
44-
guard lastIndex >= 0 else {
45+
guard lastIndex >= 0 else { // first message
46+
if message.role == .system {
47+
reformattedHistory.append(.init(
48+
role: .user,
49+
content: ModelContent.convertContent(of: message)
50+
))
51+
reformattedHistory.append(.init(
52+
role: .assistant,
53+
content: "Got it. Let's start our conversation."
54+
))
55+
continue
56+
}
57+
4558
reformattedHistory.append(message)
4659
continue
4760
}
@@ -103,16 +116,18 @@ extension ModelContent {
103116

104117
static func convertContent(of message: ChatMessage) -> String {
105118
switch message.role {
106-
case .user, .system, .function:
107-
return message.content ?? ""
119+
case .system:
120+
return "System Prompt: \n\(message.content ?? " ")"
121+
case .user, .function:
122+
return message.content ?? " "
108123
case .assistant:
109124
if let functionCall = message.functionCall {
110125
return """
111126
call function: \(functionCall.name)
112127
arguments: \(functionCall.arguments)
113128
"""
114129
} else {
115-
return message.content ?? ""
130+
return message.content ?? " "
116131
}
117132
}
118133
}

0 commit comments

Comments
 (0)