/**
* Function to generate completions using the specified model and prompt.
*
* @param {Object} args - Arguments for the completion request.
* @param {string} args.model - The model to use for generating completions.
* @param {string} args.prompt - The prompt to generate completions for.
* @param {number} [args.temperature=0.7] - The temperature for sampling.
* @param {number} [args.max_tokens=-1] - The maximum number of tokens to generate.
* @returns {Promise<Object>} - The result of the completion request.
*/
const executeFunction = async ({ model, prompt, temperature = 0.7, max_tokens = -1 }) => {
const url = 'http://127.0.0.1:1234/v1/completions';
const headers = {
'Content-Type': 'application/json'
};
const body = JSON.stringify({
model,
prompt,
temperature,
max_tokens
});
try {
// Perform the fetch request
const response = await fetch(url, {
method: 'POST',
headers,
body
});
// Check if the response was successful
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData);
}
// Parse and return the response data
const data = await response.json();
return data;
} catch (error) {
console.error('Error generating completions:', error);
return { error: 'An error occurred while generating completions.' };
}
};
/**
* Tool configuration for generating completions.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'generate_completions',
description: 'Generate completions using a specified model and prompt.',
parameters: {
type: 'object',
properties: {
model: {
type: 'string',
description: 'The model to use for generating completions.'
},
prompt: {
type: 'string',
description: 'The prompt to generate completions for.'
},
temperature: {
type: 'number',
description: 'The temperature for sampling.'
},
max_tokens: {
type: 'integer',
description: 'The maximum number of tokens to generate.'
}
},
required: ['model', 'prompt']
}
}
}
};
export { apiTool };