Skip to main content
All Integrations
AI Agent Tooling

OpenAI Function Calling
for Four Pillars Calculations

Large language models cannot reliably calculate True Solar Time, lunar calendar conversions, or Ten Gods mathematics without hallucinating. Connect BaZi API as an OpenAI tool to provide 100% accurate mathematical inputs for your AI assistants.

The Ground Truth Rule in AI Calendrical Systems

When prompting an LLM to calculate a chart from scratch, it guesses stems and solar terms with high error rates. By providing the official BaZi API tool schema, the LLM delegates the astronomical calculation to our deterministic engine and focuses exclusively on high-value psychological and philosophical analysis.

1. OpenAI Tool Definition (JSON Schema)

Include this tool definition in your tools array when calling the Chat Completions API or configuring an Assistant:

{
  "type": "function",
  "function": {
    "name": "calculate_bazi_chart",
    "description": "Calculate authentic Four Pillars (BaZi) data for a given birth date, time, and timezone.",
    "parameters": {
      "type": "object",
      "properties": {
        "birthDate": {
          "type": "string",
          "description": "ISO 8601 date string, format YYYY-MM-DD"
        },
        "birthTime": {
          "type": "string",
          "description": "24-hour time string, format HH:mm"
        },
        "gender": {
          "type": "string",
          "enum": ["male", "female"]
        },
        "timezone": {
          "type": "string",
          "description": "IANA timezone identifier, e.g. America/New_York or Asia/Shanghai"
        }
      },
      "required": ["birthDate", "birthTime", "gender"]
    }
  }
}

2. Execution Flow with GPT-4o

When the model decides to invoke the tool, forward the parameters to BaZi API and return the output:

import OpenAI from "openai";

const openai = new OpenAI();

// Pass the tool to GPT-4o
const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    { role: "user", content: "What is my Day Master if I was born on 1995-10-24 at 08:30 in New York?" }
  ],
  tools: [BAZI_TOOL_SCHEMA],
  tool_choice: "auto",
});

// When GPT calls 'calculate_bazi_chart', forward arguments to BaZi API:
const toolCall = response.choices[0].message.tool_calls?.[0];
if (toolCall) {
  const args = JSON.parse(toolCall.function.arguments);
  const baziData = await fetch("https://api.baziapi.pro/api/v2/bazi/calculate", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-api-key": process.env.BAZI_API_KEY!,
    },
    body: JSON.stringify(args),
  }).then(r => r.json());

  // Return real deterministic calculation back to the model for explanation
}

Ready to build AI agents?

Get an API key and explore pre-built agent prompts in our docs.