Let us assume this is your schema
const schema = {
"openapi":"3.1.0",
"info":{
"title":"Wolfram",
"version":"v0.1"
},
"servers":[
{
"url":"https://www.wolframalpha.com",
"description":"Wolfram Server for ChatGPT"
}
],
"paths": {
"/api/v1/cloud-plugin": {
"get": {
"operationId": "getWolframCloudResults",
"externalDocs": "https://reference.wolfram.com/language/",
"summary": "Evaluate Wolfram Language code",
"responses": {
"200": {
"description": "The result of the Wolfram Language evaluation",
"content": {
"text/plain": {}
}
},
"500": {
"description": "Wolfram Cloud was unable to generate a result"
},
"400": {
"description": "The request is missing the 'input' parameter"
},
"403": {
"description": "Unauthorized"
},
"503":{
"description":"Service temporarily unavailable. This may be the result of too many requests."
}
},
"parameters": [
{
"name": "input",
"in": "query",
"description": "the input expression",
"required": true,
"schema": {
"type": "string"
}
}
]
}
},
"/api/v1/llm-api": {
"get":{
"operationId":"getWolframAlphaResults",
"externalDocs":"https://products.wolframalpha.com/api",
"summary":"Get Wolfram|Alpha results",
"responses":{
"200":{
"description":"The result of the Wolfram|Alpha query",
"content":{
"text/plain":{
}
}
},
"400":{
"description":"The request is missing the 'input' parameter"
},
"403":{
"description":"Unauthorized"
},
"500":{
"description":"Wolfram|Alpha was unable to generate a result"
},
"501":{
"description":"Wolfram|Alpha was unable to generate a result"
},
"503":{
"description":"Service temporarily unavailable. This may be the result of too many requests."
}
},
"parameters":[
{
"name":"input",
"in":"query",
"description":"the input",
"required":true,
"schema":{
"type":"string"
}
},
{
"name":"assumption",
"in":"query",
"description":"the assumption to use, passed back from a previous query with the same input.",
"required":false,
"explode":true,
"style":"form",
"schema":{
"type":"array",
"items":{
"type":"string"
}
}
}
]
}
}
}
}
Try this updated function
function convertSchema(inputSchema) {
var tools = []
for (var path in inputSchema) {
var methods = inputSchema[path]
for (var method in methods) {
var outputSchema = {}
var operation = methods[method]
outputSchema.name = operation.operationId
outputSchema.description = operation.summary
outputSchema.parameters = {
type: "object",
properties: {},
required: [],
}
operation.parameters.forEach(function(param) {
outputSchema.parameters.properties[param.name] = {
type: param.schema.type,
description: param.description
}
if(param.schema.type === "array") {
outputSchema.parameters.properties[param.name].items = {type: "string"}
}
outputSchema.parameters.required.push(param.name)
})
tools.push(outputSchema)
}
}
return tools
}
To use the function
const tools = convertSchema(schema.paths)
Output:
[
{
"name": "getWolframCloudResults",
"description": "Evaluate Wolfram Language code",
"parameters": {
"type": "object",
"properties": {
"input": {
"type": "string",
"description": "the input expression"
}
},
"required": [
"input"
]
}
},
{
"name": "getWolframAlphaResults",
"description": "Get Wolfram|Alpha results",
"parameters": {
"type": "object",
"properties": {
"input": {
"type": "string",
"description": "the input"
},
"assumption": {
"type": "array",
"description": "the assumption to use, passed back from a previous query with the same input.",
"items": {
"type": "string"
}
}
},
"required": [
"input",
"assumption"
]
}
}
]
Please note that the function does not cover all types so better check the output. It will at least give you an idea how to convert your schema.