Function Calling

To perform a function calling:

import PropulsionAI from 'propulsionai';
import {
  ModelChatResponse,
  ModelChatParams
} from 'propulsionai/resources/models';

// Initialize PropulsionAI with bearer token from environment variables
const p8n = new PropulsionAI({
  bearerToken: process.env['PROPULSIONAI_BEARER_TOKEN'] as string,
});

// Example dummy function hardcoded to return the same weather
// In production, this could be your backend API or an external API
interface WeatherParameters {
  location: string;
  unit?: string;
}
function getCurrentWeather(parameters: WeatherParameters): string {
  let { location } = parameters;

  if (location.toLowerCase().includes("tokyo")) {
    return JSON.stringify({ location: "Tokyo", temperature: "10", unit: "celsius" });
  } else if (location.toLowerCase().includes("san francisco")) {
    return JSON.stringify({ location: "San Francisco", temperature: "72", unit: "fahrenheit" });
  } else if (location.toLowerCase().includes("paris")) {
    return JSON.stringify({ location: "Paris", temperature: "22", unit: "fahrenheit" });
  } else {
    return JSON.stringify({ location, temperature: "unknown" });
  }
}

async function main() {
  const tools: Array<ModelChatParams.Tool> = [
    {
      type: "function",
      function: {
        name: "get_current_weather",
        description: "Get the current weather in a given location",
        function: getCurrentWeather, // Optional: Link your actual function
        parameters: {
          type: "object",
          properties: {
            location: {
              type: "string",
              description: "The city and state, e.g., San Francisco, CA",
            },
            unit: {
              type: "string",
              enum: ["celsius", "fahrenheit"]
            },
          },
          required: ["location"],
          example: `get_current_weather("Tokyo", "celsius")`
        },
      },
    },
  ];
  let deployment_tag: string = '<deployment_tag>';
  let params: ModelChatParams = {
    tools: tools,
    messages: [
      {
        role: 'system',
        content: "You are a weatherman.",
      },
      {
        role: 'user',
        content: "What is the weather of Tokyo?",
      },
    ],
    model: 'auto',
    stream: false,
    wait: true,
  };

  let response: ModelChatResponse;
  try {
    response = await p8n.models.epAuto(deployment_tag, params);
  } catch (e) {
    console.log('Error:', e);
    return;
  }
  console.log("Task ID:", response.task_id);
  if (response.choices) {
    response.choices.forEach((choice) => {
      console.log(`${choice.message?.role}: ${choice.message?.content}`);
    });
  }
  if (response.toolCalls) {
    response.toolCalls.forEach((toolCall) => {
      console.log(`${toolCall.function?.name}: ${JSON.stringify(toolCall.function?.parameters)}`);
    });
  }
}

main();

Last updated