Skip to content

Commit 1256614

Browse files
committed
dev: add support for tools
1 parent cf7aa27 commit 1256614

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/backend/src/modules/puterai/AIInterfaceService.js

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class AIInterfaceService extends BaseService {
7575
description: 'Get completions for a chat log.',
7676
parameters: {
7777
messages: { type: 'json' },
78+
tools: { type: 'json' },
7879
vision: { type: 'flag' },
7980
stream: { type: 'flag' },
8081
model: { type: 'string' },

src/backend/src/modules/puterai/OpenAICompletionService.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class OpenAICompletionService extends BaseService {
152152
* AI Chat completion method.
153153
* See AIChatService for more details.
154154
*/
155-
async complete ({ messages, test_mode, stream, model }) {
155+
async complete ({ messages, test_mode, stream, model, tools }) {
156156

157157
// for now this code (also in AIChatService.js) needs to be
158158
// duplicated because this hasn't been moved to be under
@@ -195,6 +195,7 @@ class OpenAICompletionService extends BaseService {
195195

196196
return await this.complete(messages, {
197197
model: model,
198+
tools,
198199
moderation: true,
199200
stream,
200201
});
@@ -242,7 +243,7 @@ class OpenAICompletionService extends BaseService {
242243
* @returns {Promise<Object>} The completion response containing message and usage info
243244
* @throws {Error} If messages are invalid or content is flagged by moderation
244245
*/
245-
async complete (messages, { stream, moderation, model }) {
246+
async complete (messages, { stream, moderation, model, tools }) {
246247
// Validate messages
247248
if ( ! Array.isArray(messages) ) {
248249
throw new Error('`messages` must be an array');
@@ -360,6 +361,7 @@ class OpenAICompletionService extends BaseService {
360361
user: user_private_uid,
361362
messages: messages,
362363
model: model,
364+
...(tools ? { tools } : {}),
363365
// max_tokens,
364366
stream,
365367
...(stream ? {
@@ -449,9 +451,9 @@ class OpenAICompletionService extends BaseService {
449451
}
450452

451453
// We need to moderate the completion too
452-
if ( moderation ) {
453-
const text = completion.choices[0].message.content;
454-
const moderation_result = await this.check_moderation(text);
454+
const mod_text = completion.choices[0].message.content;
455+
if ( moderation && mod_text !== null ) {
456+
const moderation_result = await this.check_moderation(mod_text);
455457
if ( moderation_result.flagged ) {
456458
throw new Error('message is not allowed');
457459
}

src/puter-js/src/modules/AI.js

+4
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ class AI{
276276
options.stream = settings.stream;
277277
}
278278

279+
if ( settings.tools ) {
280+
options.tools = settings.tools;
281+
}
282+
279283
// Call the original chat.complete method
280284
return await utils.make_driver_method(['messages'], 'puter-chat-completion', driver, 'complete', {
281285
test_mode: testMode ?? false,

0 commit comments

Comments
 (0)