Skip to content

Commit 82bf8d7

Browse files
authored
Merge pull request #803 from iceljc/master
refine code
2 parents 922fbd3 + ac60061 commit 82bf8d7

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/GeminiChatCompletionProvider.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ public void SetModelName(string model)
102102
aiModel.UseGrounding = googleSettings.Gemini.UseGrounding;
103103

104104
// Assembly messages
105-
var prompt = string.Empty;
106105
var contents = new List<Content>();
107106
var tools = new List<Tool>();
108107
var funcDeclarations = new List<FunctionDeclaration>();
109108

109+
var systemPrompts = new List<string>();
110110
if (!string.IsNullOrEmpty(agent.Instruction))
111111
{
112112
var instruction = agentService.RenderedInstruction(agent);
@@ -115,10 +115,10 @@ public void SetModelName(string model)
115115
Role = AgentRole.User
116116
});
117117

118-
prompt += $"{instruction}\r\n";
118+
systemPrompts.Add(instruction);
119119
}
120120

121-
prompt += "\r\n[FUNCTIONS]\r\n";
121+
var funcPrompts = new List<string>();
122122
foreach (var function in agent.Functions)
123123
{
124124
if (!agentService.RenderFunction(agent, function)) continue;
@@ -137,15 +137,15 @@ public void SetModelName(string model)
137137
}
138138
});
139139

140-
prompt += $"{function.Name}: {function.Description} {def}\r\n\r\n";
140+
funcPrompts.Add($"{function.Name}: {function.Description} {def}");
141141
}
142142

143143
if (!funcDeclarations.IsNullOrEmpty())
144144
{
145145
tools.Add(new Tool { FunctionDeclarations = funcDeclarations });
146146
}
147147

148-
prompt += "\r\n[CONVERSATIONS]\r\n";
148+
var convPrompts = new List<string>();
149149
foreach (var message in conversations)
150150
{
151151
if (message.Role == AgentRole.Function)
@@ -163,7 +163,7 @@ public void SetModelName(string model)
163163
}
164164
});
165165

166-
prompt += $"{AgentRole.Assistant}: Call function {message.FunctionName}({message.FunctionArgs})\r\n";
166+
convPrompts.Add($"{AgentRole.Assistant}: Call function {message.FunctionName}({message.FunctionArgs})");
167167
}
168168
else if (message.Role == AgentRole.User)
169169
{
@@ -172,15 +172,15 @@ public void SetModelName(string model)
172172
{
173173
Role = AgentRole.User
174174
});
175-
prompt += $"{AgentRole.User}: {text}\r\n";
175+
convPrompts.Add($"{AgentRole.User}: {text}");
176176
}
177177
else if (message.Role == AgentRole.Assistant)
178178
{
179179
contents.Add(new Content(message.Content)
180180
{
181181
Role = AgentRole.Model
182182
});
183-
prompt += $"{AgentRole.Assistant}: {message.Content}\r\n";
183+
convPrompts.Add($"{AgentRole.Assistant}: {message.Content}");
184184
}
185185
}
186186

@@ -189,6 +189,29 @@ public void SetModelName(string model)
189189
Contents = contents,
190190
Tools = tools
191191
};
192+
193+
var prompt = GetPrompt(systemPrompts, funcPrompts, convPrompts);
192194
return (prompt, request);
193195
}
196+
197+
private string GetPrompt(IEnumerable<string> systemPrompts, IEnumerable<string> funcPrompts, IEnumerable<string> convPrompts)
198+
{
199+
var prompt = string.Empty;
200+
201+
prompt = string.Join("\r\n\r\n", systemPrompts);
202+
203+
if (!funcPrompts.IsNullOrEmpty())
204+
{
205+
prompt += "\r\n\r\n[FUNCTIONS]\r\n";
206+
prompt += string.Join("\r\n", funcPrompts);
207+
}
208+
209+
if (!convPrompts.IsNullOrEmpty())
210+
{
211+
prompt += "\r\n\r\n[CONVERSATION]\r\n";
212+
prompt += string.Join("\r\n", convPrompts);
213+
}
214+
215+
return prompt;
216+
}
194217
}

0 commit comments

Comments
 (0)