Skip to content
Go back

Function Calling in Fastify with OpenAI Functions

Function Calling in Fastify with OpenAI Functions

Introduction

OpenAI function calling allows AI to invoke predefined functions. Integrate with Fastify for AI-driven operations.

Prerequisites

Step 1: Install Dependencies

npm install fastify openai

Step 2: Define Functions

In openai/functions.ts:

export const functions = [
  {
    name: 'get_current_weather',
    description: 'Get current weather for a location',
    parameters: {
      type: 'object',
      properties: {
        location: { type: 'string', description: 'City name' },
        unit: { type: 'string', enum: ['celsius','fahrenheit'] },
      },
      required: ['location'],
    },
  },
];

export async function get_current_weather({ location, unit }: any) {
  // Call real weather API here
  return { location, temperature: 22, unit };
}

Step 3: Fastify Route

In server.js:

import Fastify from 'fastify';
import OpenAI from 'openai';
import { functions, get_current_weather } from './openai/functions';

const fastify = Fastify({ logger: true });
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

fastify.post('/chat', async (req, reply) => {
  const { messages } = req.body;

  const response = await openai.chat.completions.create({
    model: 'gpt-4-0613',
    messages,
    functions,
    function_call: 'auto',
  });

  const message = response.choices[0].message;

  if (message.function_call) {
    const { name, arguments: args } = message.function_call;
    const func = { get_current_weather }[name];
    const result = await func(JSON.parse(args));

    return reply.send({ 
      role: 'function',
      name,
      content: JSON.stringify(result)
    });
  }

  reply.send(message);
});

fastify.listen({ port: 3000 });

Summary

Fastify + OpenAI function calling allows AI to perform actions by invoking defined functions, enabling dynamic, AI-driven endpoints.


Share this post on:

Previous Post
Designing Advanced Chat UX with OpenAI Streaming
Next Post
Automating Lambda Deployments with AWS CDK Pipelines