/**
* Function to generate embeddings using a specified model.
*
* @param {Object} args - Arguments for the embedding generation.
* @param {string} args.model - The model to use for generating embeddings.
* @param {string} args.input - The input text for which embeddings are to be generated.
* @returns {Promise<Object>} - The result of the embedding generation.
*/
const executeFunction = async ({ model, input }) => {
const url = 'http://127.0.0.1:1234/v1/embeddings';
const headers = {
'Content-Type': 'application/json'
};
const body = JSON.stringify({ model, input });
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 embeddings:', error);
return { error: 'An error occurred while generating embeddings.' };
}
};
/**
* Tool configuration for generating embeddings.
* @type {Object}
*/
const apiTool = {
function: executeFunction,
definition: {
type: 'function',
function: {
name: 'generate_embeddings',
description: 'Generate embeddings for a given input using a specified model.',
parameters: {
type: 'object',
properties: {
model: {
type: 'string',
description: 'The model to use for generating embeddings.'
},
input: {
type: 'string',
description: 'The input text for which embeddings are to be generated.'
}
},
required: ['model', 'input']
}
}
}
};
export { apiTool };