File size: 1,651 Bytes
7ec7db5 0b9ebb7 7ec7db5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import { Tool } from 'openai-function-calling-tools';
import { z } from 'zod';
function createCoinMarketCapApi({ apiKey }: { apiKey: string }) {
const paramsSchema = z.object({
input: z.string(), // Assuming this is used to pass query parameters, though you might want to define these more specifically
});
const name = 'coinmarketcap';
const description = 'A realtime cryptocurrency API. Useful for when you need to answer questions about latest crypto';
const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
try {
// Parse the input to get parameters, or define them directly if not using 'input' for this
// For demonstration, I'm using static parameters that you might want to customize
const queryParams = new URLSearchParams({
start: '1',
limit: '10',
convert: 'USD'
});
const response = await fetch(`https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?${queryParams}`, {
method: 'GET', // Method is GET by default for fetch, specified here for clarity
headers: {
'X-CMC_PRO_API_KEY': apiKey, // Use the passed apiKey for authentication
'Accept': 'application/json',
},
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return JSON.stringify(data);
} catch (error) {
throw new Error(`Error in coinMarketCapApi: ${error}`);
}
};
return new Tool(paramsSchema, name, description, execute).tool;
}
export { createCoinMarketCapApi };
|