Custom Tools
Custom Tools Quickstart
const ReiCoreSdk = require('reicore-sdk');
// Initialize the SDK
const apiKey = 'your_unit_secret_token';
const reiAgent = new ReiCoreSdk({ agentSecretKey: apiKey });
// Define your custom functions
const getWeather = (location) => {
// Implement your weather API call here
return `Weather in ${location}: Sunny, 22°C`;
};
const searchDatabase = (query) => {
// Implement your database search here
return `Found 3 results for: ${query}`;
};
// Define function schemas
const functions = [
{
name: "get_weather",
description: "Get the current weather for a location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA"
}
},
required: ["location"]
}
},
{
name: "search_database",
description: "Search the database for specific information",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "The search query"
}
},
required: ["query"]
}
}
];
// Function mapping
const functionMap = {
get_weather: getWeather,
search_database: searchDatabase
};
async function processWithFunctions(query) {
try {
// First call to get function details
const response = await reiAgent.chatCompletions(query, { functions });
const message = response.choices[0].message;
// Check if the agent wants to call a function
if (message.function_call) {
const functionName = message.function_call.name;
const functionArgs = JSON.parse(message.function_call.arguments);
// Call the function
const functionResponse = functionMap[functionName](...Object.values(functionArgs));
// Send the function response back to the agent
const secondResponse = await reiAgent.chatCompletions([
{ role: "user", content: query },
{ role: "function", name: functionName, content: functionResponse }
], { functions });
return secondResponse.choices[0].message.content;
}
return message.content;
} catch (error) {
console.error('Error:', error);
return null;
}
}
// Example usage
processWithFunctions("What's the weather in Tokyo?")
.then(response => console.log(response))
.catch(error => console.error(error));Advanced Custom Tools
Multiple Function Calls
Function Calling with Context
Last updated