Functions

Now lets add GIF support via function calling...

import { ChatPrompt } from '@teams.sdk/ai';
import { OpenAIChatModel } from '@teams.sdk/openai';

interface GifySearchArgs {
    readonly text: string;
    readonly limit?: number;
}

const prompt = new ChatPrompt({
    instructions: [
        'you are an assistant that helps find the perfect emoji to use for a given situation.',
        'you will only respond with emojis.',
    ].join('\n'),
    model: new OpenAIChatModel({
        model: 'gpt-4o',
        apiKey: process.OPENAI_API_KEY,
        stream: true,
    }),
}).function(
    'gify',
    'search for gifs',
    {
        type: 'object',
        properties: {
            text: {
                type: 'string',
                description: 'the text to search for',
            },
            limit: {
                type: 'number',
                description: 'the maximum number of gifs to return',
            },
        },
        required: ['text'],
    },
    async ({ text, limit }: GifySearchArgs) => {
        const { GiphyFetch } = await import('@giphy/js-fetch-api');
        const giphy = new GiphyFetch(process.env.GIPHY_API_KEY || '');
        const res = await giphy.search(text, { limit, sort: 'relevant' });
        return res.data[0].images.original.url;
    }
);

(async () => {
    await prompt.chat('having a great day!', (chunk) => {
        process.stdout.write(chunk); // gif and/or 😄🌞🎉...
    });

    process.stdout.write('\n');
})();