1
1
using BotSharp . Abstraction . Agents ;
2
2
using BotSharp . Abstraction . Agents . Enums ;
3
- using BotSharp . Abstraction . Conversations ;
4
3
using BotSharp . Abstraction . Loggers ;
5
4
using BotSharp . Abstraction . Functions . Models ;
6
5
using BotSharp . Abstraction . Routing ;
7
6
using BotSharp . Plugin . GoogleAI . Settings ;
8
7
using LLMSharp . Google . Palm ;
9
8
using Microsoft . Extensions . Logging ;
10
- using System . Diagnostics . Metrics ;
11
- using static System . Net . Mime . MediaTypeNames ;
9
+ using LLMSharp . Google . Palm . DiscussService ;
12
10
13
11
namespace BotSharp . Plugin . GoogleAI . Providers ;
14
12
@@ -39,19 +37,25 @@ public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> con
39
37
40
38
var client = new GooglePalmClient ( apiKey : _settings . PaLM . ApiKey ) ;
41
39
42
- var ( prompt , messages ) = PrepareOptions ( agent , conversations ) ;
40
+ var ( prompt , messages , hasFunctions ) = PrepareOptions ( agent , conversations ) ;
43
41
44
42
RoleDialogModel msg ;
45
43
46
- if ( messages == null )
44
+ if ( hasFunctions )
47
45
{
48
46
// use text completion
49
- var response = client . GenerateTextAsync ( prompt , null ) . Result ;
47
+ // var response = client.GenerateTextAsync(prompt, null).Result;
48
+ var response = client . ChatAsync ( new PalmChatCompletionRequest
49
+ {
50
+ Context = prompt ,
51
+ Messages = messages ,
52
+ Temperature = 0.1f
53
+ } ) . Result ;
50
54
51
55
var message = response . Candidates . First ( ) ;
52
56
53
57
// check if returns function calling
54
- var llmResponse = message . Output . JsonContent < FunctionCallingResponse > ( ) ;
58
+ var llmResponse = message . Content . JsonContent < FunctionCallingResponse > ( ) ;
55
59
56
60
msg = new RoleDialogModel ( llmResponse . Role , llmResponse . Content )
57
61
{
@@ -79,13 +83,14 @@ public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> con
79
83
Task . WaitAll ( hooks . Select ( hook =>
80
84
hook . AfterGenerated ( msg , new TokenStatsModel
81
85
{
86
+ Prompt = prompt ,
82
87
Model = _model
83
88
} ) ) . ToArray ( ) ) ;
84
89
85
90
return msg ;
86
91
}
87
92
88
- private ( string , List < PalmChatMessage > ) PrepareOptions ( Agent agent , List < RoleDialogModel > conversations )
93
+ private ( string , List < PalmChatMessage > , bool ) PrepareOptions ( Agent agent , List < RoleDialogModel > conversations )
89
94
{
90
95
var prompt = "" ;
91
96
@@ -99,6 +104,9 @@ public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> con
99
104
var routing = _services . GetRequiredService < IRoutingService > ( ) ;
100
105
var router = routing . Router ;
101
106
107
+ var messages = conversations . Select ( c => new PalmChatMessage ( c . Content , c . Role == AgentRole . User ? "user" : "AI" ) )
108
+ . ToList ( ) ;
109
+
102
110
if ( agent . Functions != null && agent . Functions . Count > 0 )
103
111
{
104
112
prompt += "\r \n \r \n [Functions] defined in JSON Schema:\r \n " ;
@@ -118,13 +126,13 @@ public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> con
118
126
119
127
prompt += "\r \n \r \n " + router . Templates . FirstOrDefault ( x => x . Name == "response_with_function" ) . Content ;
120
128
121
- return ( prompt , null ) ;
129
+ return ( prompt , new List < PalmChatMessage >
130
+ {
131
+ new PalmChatMessage ( "Which function should be used for the next step based on latest user or function response, output your response in JSON:" , AgentRole . User ) ,
132
+ } , true ) ;
122
133
}
123
134
124
- var messages = conversations . Select ( c => new PalmChatMessage ( c . Content , c . Role == AgentRole . User ? "user" : "AI" ) )
125
- . ToList ( ) ;
126
-
127
- return ( prompt , messages ) ;
135
+ return ( prompt , messages , false ) ;
128
136
}
129
137
130
138
public Task < bool > GetChatCompletionsAsync ( Agent agent , List < RoleDialogModel > conversations , Func < RoleDialogModel , Task > onMessageReceived , Func < RoleDialogModel , Task > onFunctionExecuting )
0 commit comments