瀏覽代碼

fix: o3 tools with array parameters

Apparently the o3 model things items is a required property for array
types in JSON schema. This is not the case, but we fixed it anyway.
KernelDeimos 2 月之前
父節點
當前提交
2ee380ec16
共有 1 個文件被更改,包括 30 次插入1 次删除
  1. 30 1
      src/backend/src/modules/puterai/lib/FunctionCalling.js

+ 30 - 1
src/backend/src/modules/puterai/lib/FunctionCalling.js

@@ -20,7 +20,7 @@ module.exports = class FunctionCalling {
 
             const normalize_function = fn => {
                 const normal_fn = {};
-                const parameters =
+                let parameters =
                     fn.parameters ||
                     fn.input_schema;
                 
@@ -28,6 +28,10 @@ module.exports = class FunctionCalling {
                     type: 'object',
                 };
 
+                if ( parameters.properties ) {
+                    parameters = this.normalize_json_schema(parameters);
+                }
+
                 if ( fn.name ) {
                     normal_fn.name = fn.name;
                 }
@@ -61,6 +65,31 @@ module.exports = class FunctionCalling {
         return tools;
     }
 
+    static normalize_json_schema (schema) {
+        if ( ! schema ) return schema;
+
+        if ( schema.type === 'object' ) {
+            if ( ! schema.properties ) {
+                return schema;
+            }
+
+            const keys = Object.keys(schema.properties);
+            for ( const key of keys ) {
+                schema.properties[key] = this.normalize_json_schema(schema.properties[key]);
+            }
+        }
+
+        if ( schema.type === 'array' ) {
+            if ( ! schema.items ) {
+                schema.items = {};
+            } else {
+                schema.items = this.normalize_json_schema(schema.items);
+            }
+        }
+
+        return schema;
+    }
+
     /**
      * This function will convert a normalized tools object to the
      * format expected by OpenAI.