@@ -54,6 +54,71 @@ impl OpenAIClient {
54
54
55
55
Ok ( Self { model, client } )
56
56
}
57
+
58
+ async fn get_tools_if_supported ( & self , state : & SharedState ) -> Vec < FunctionTool > {
59
+ let mut tools = vec ! [ ] ;
60
+
61
+ // if native tool calls are supported (and XML was not forced)
62
+ if state. lock ( ) . await . native_tools_support {
63
+ // for every namespace available to the model
64
+ for group in state. lock ( ) . await . get_namespaces ( ) {
65
+ // for every action of the namespace
66
+ for action in & group. actions {
67
+ let mut required = vec ! [ ] ;
68
+ let mut properties = HashMap :: new ( ) ;
69
+
70
+ if let Some ( example) = action. example_payload ( ) {
71
+ required. push ( "payload" . to_string ( ) ) ;
72
+ properties. insert (
73
+ "payload" . to_string ( ) ,
74
+ OpenAiToolFunctionParameterProperty {
75
+ the_type : "string" . to_string ( ) ,
76
+ description : format ! (
77
+ "The main function argument, use this as a template: {}" ,
78
+ example
79
+ ) ,
80
+ } ,
81
+ ) ;
82
+ }
83
+
84
+ if let Some ( attrs) = action. example_attributes ( ) {
85
+ for name in attrs. keys ( ) {
86
+ required. push ( name. to_string ( ) ) ;
87
+ properties. insert (
88
+ name. to_string ( ) ,
89
+ OpenAiToolFunctionParameterProperty {
90
+ the_type : "string" . to_string ( ) ,
91
+ description : name. to_string ( ) ,
92
+ } ,
93
+ ) ;
94
+ }
95
+ }
96
+
97
+ let function = FunctionDefinition {
98
+ name : action. name ( ) . to_string ( ) ,
99
+ description : Some ( action. description ( ) . to_string ( ) ) ,
100
+ parameters : Some ( serde_json:: json!( OpenAiToolFunctionParameters {
101
+ the_type: "object" . to_string( ) ,
102
+ required,
103
+ properties,
104
+ } ) ) ,
105
+ } ;
106
+
107
+ tools. push ( FunctionTool {
108
+ the_type : "function" . to_string ( ) ,
109
+ function,
110
+ } ) ;
111
+ }
112
+ }
113
+
114
+ log:: trace!( "openai.tools={:?}" , & tools) ;
115
+
116
+ // let j = serde_json::to_string_pretty(&tools).unwrap();
117
+ // log::info!("{j}");
118
+ }
119
+
120
+ tools
121
+ }
57
122
}
58
123
59
124
#[ async_trait]
@@ -74,7 +139,7 @@ impl Client for OpenAIClient {
74
139
} ,
75
140
openai_api_rust:: Message {
76
141
role: Role :: User ,
77
- content: Some ( "Call the test function." . to_string( ) ) ,
142
+ content: Some ( "Execute the test function." . to_string( ) ) ,
78
143
tool_calls: None ,
79
144
} ,
80
145
] ;
@@ -154,62 +219,7 @@ impl Client for OpenAIClient {
154
219
} ) ;
155
220
}
156
221
157
- let mut tools = vec ! [ ] ;
158
- if state. lock ( ) . await . native_tools_support {
159
- for group in state. lock ( ) . await . get_namespaces ( ) {
160
- for action in & group. actions {
161
- let mut required = vec ! [ ] ;
162
- let mut properties = HashMap :: new ( ) ;
163
-
164
- if let Some ( example) = action. example_payload ( ) {
165
- required. push ( "payload" . to_string ( ) ) ;
166
- properties. insert (
167
- "payload" . to_string ( ) ,
168
- OpenAiToolFunctionParameterProperty {
169
- the_type : "string" . to_string ( ) ,
170
- description : format ! (
171
- "The main function argument, use this as a template: {}" ,
172
- example
173
- ) ,
174
- } ,
175
- ) ;
176
- }
177
-
178
- if let Some ( attrs) = action. example_attributes ( ) {
179
- for name in attrs. keys ( ) {
180
- required. push ( name. to_string ( ) ) ;
181
- properties. insert (
182
- name. to_string ( ) ,
183
- OpenAiToolFunctionParameterProperty {
184
- the_type : "string" . to_string ( ) ,
185
- description : name. to_string ( ) ,
186
- } ,
187
- ) ;
188
- }
189
- }
190
-
191
- let function = FunctionDefinition {
192
- name : action. name ( ) . to_string ( ) ,
193
- description : Some ( action. description ( ) . to_string ( ) ) ,
194
- parameters : Some ( serde_json:: json!( OpenAiToolFunctionParameters {
195
- the_type: "object" . to_string( ) ,
196
- required,
197
- properties,
198
- } ) ) ,
199
- } ;
200
-
201
- tools. push ( FunctionTool {
202
- the_type : "function" . to_string ( ) ,
203
- function,
204
- } ) ;
205
- }
206
- }
207
-
208
- log:: trace!( "openai.tools={:?}" , & tools) ;
209
-
210
- // let j = serde_json::to_string_pretty(&tools).unwrap();
211
- // log::info!("{j}");
212
- }
222
+ let tools = self . get_tools_if_supported ( & state) . await ;
213
223
214
224
let body = ChatBody {
215
225
model : self . model . to_string ( ) ,
0 commit comments