@baziapi/ai-tools 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -0
- package/dist/index.cjs +102 -0
- package/dist/index.d.cts +190 -0
- package/dist/index.d.mts +190 -0
- package/dist/index.d.ts +190 -0
- package/dist/index.mjs +96 -0
- package/package.json +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# BaZi API - AI Agents & LLM Function Calling Tools (`@baziapi/ai-tools`)
|
|
2
|
+
|
|
3
|
+
Official pre-built tool integrations for connecting LLMs, AI Agents, and Chatbots directly with the BaZi Four Pillars calculation engine.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 📦 Supported Frameworks
|
|
8
|
+
|
|
9
|
+
- **OpenAI Function Calling / Assistants API**
|
|
10
|
+
- **Vercel AI SDK (`ai`)**
|
|
11
|
+
- **LangChain (TypeScript & Python)**
|
|
12
|
+
- **LlamaIndex / CrewAI / AutoGen**
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 🚀 Usage
|
|
17
|
+
|
|
18
|
+
### 1. OpenAI Assistants / Chat Completions
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import OpenAI from 'openai';
|
|
22
|
+
import { BaziClient } from '@baziapi/sdk';
|
|
23
|
+
import { baziOpenAITool, executeOpenAIBaziCall } from '@baziapi/ai-tools';
|
|
24
|
+
|
|
25
|
+
const openai = new OpenAI();
|
|
26
|
+
const bazi = new BaziClient({ apiKey: process.env.BAZI_API_KEY });
|
|
27
|
+
|
|
28
|
+
const runner = await openai.chat.completions.create({
|
|
29
|
+
model: 'gpt-4o',
|
|
30
|
+
messages: [
|
|
31
|
+
{ role: 'user', content: 'What is the Day Master of someone born 1998-08-12 at 10:30 male?' },
|
|
32
|
+
],
|
|
33
|
+
tools: [baziOpenAITool],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. Vercel AI SDK (`ai`)
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { generateText } from 'ai';
|
|
41
|
+
import { openai } from '@ai-sdk/openai';
|
|
42
|
+
import { BaziClient } from '@baziapi/sdk';
|
|
43
|
+
import { createBaziVercelTool } from '@baziapi/ai-tools';
|
|
44
|
+
|
|
45
|
+
const client = new BaziClient({ apiKey: process.env.BAZI_API_KEY });
|
|
46
|
+
|
|
47
|
+
const { text } = await generateText({
|
|
48
|
+
model: openai('gpt-4o'),
|
|
49
|
+
tools: {
|
|
50
|
+
calculateBazi: createBaziVercelTool(client),
|
|
51
|
+
},
|
|
52
|
+
prompt: 'Calculate BaZi chart for a male born 1998-08-12 10:30 in Dhaka.',
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 3. LangChain TypeScript
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
60
|
+
import { BaziClient } from '@baziapi/sdk';
|
|
61
|
+
import { createBaziLangChainConfig } from '@baziapi/ai-tools';
|
|
62
|
+
|
|
63
|
+
const client = new BaziClient({ apiKey: process.env.BAZI_API_KEY });
|
|
64
|
+
const baziTool = new DynamicStructuredTool(createBaziLangChainConfig(client));
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## 📄 License
|
|
70
|
+
|
|
71
|
+
MIT License.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const zod = require('zod');
|
|
4
|
+
|
|
5
|
+
const baziOpenAITool = {
|
|
6
|
+
type: "function",
|
|
7
|
+
function: {
|
|
8
|
+
name: "calculate_bazi_chart",
|
|
9
|
+
description: "Calculates complete Chinese Four Pillars of Destiny (BaZi / \u516B\u5B57) astrological chart with Heavenly Stems, Earthly Branches, Five Elements, Ten Gods, Hidden Stems, and Luck Pillars from birth date and time.",
|
|
10
|
+
parameters: {
|
|
11
|
+
type: "object",
|
|
12
|
+
properties: {
|
|
13
|
+
birthDate: {
|
|
14
|
+
type: "string",
|
|
15
|
+
description: "Birth date in YYYY-MM-DD format (e.g. '1998-08-12')"
|
|
16
|
+
},
|
|
17
|
+
birthTime: {
|
|
18
|
+
type: "string",
|
|
19
|
+
description: "Birth time in 24-hour HH:mm format (e.g. '10:30'). Defaults to '12:00' if unknown."
|
|
20
|
+
},
|
|
21
|
+
gender: {
|
|
22
|
+
type: "string",
|
|
23
|
+
enum: ["male", "female"],
|
|
24
|
+
description: "Gender of the person. Determines Luck Pillar forward/backward progression direction."
|
|
25
|
+
},
|
|
26
|
+
timezone: {
|
|
27
|
+
type: "string",
|
|
28
|
+
default: "Asia/Shanghai",
|
|
29
|
+
description: "IANA Timezone string (e.g. 'Asia/Dhaka', 'America/New_York', 'Europe/London')"
|
|
30
|
+
},
|
|
31
|
+
language: {
|
|
32
|
+
type: "string",
|
|
33
|
+
enum: ["en", "zh"],
|
|
34
|
+
default: "en",
|
|
35
|
+
description: "Language for terminology and names ('en' for English pinyin/translations, 'zh' for Chinese)"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
required: ["birthDate", "birthTime", "gender"]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
async function executeOpenAIBaziCall(client, toolArgs) {
|
|
43
|
+
const calcFn = client.calculate ? client.calculate.bind(client) : client.bazi?.calculate.bind(client.bazi);
|
|
44
|
+
if (!calcFn) {
|
|
45
|
+
throw new Error("Invalid BaziClient instance provided.");
|
|
46
|
+
}
|
|
47
|
+
return calcFn({
|
|
48
|
+
birthDate: toolArgs.birthDate,
|
|
49
|
+
birthTime: toolArgs.birthTime || "12:00",
|
|
50
|
+
gender: toolArgs.gender,
|
|
51
|
+
timezone: toolArgs.timezone || "Asia/Shanghai",
|
|
52
|
+
language: toolArgs.language || "en"
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const baziToolSchema = zod.z.object({
|
|
57
|
+
birthDate: zod.z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Must be in YYYY-MM-DD format (e.g. 1998-08-12)").describe("Birth date in YYYY-MM-DD format"),
|
|
58
|
+
birthTime: zod.z.string().regex(/^\d{2}:\d{2}$/, "Must be in HH:mm 24-hour format (e.g. 10:30)").default("12:00").describe("Birth time in 24-hour HH:mm format"),
|
|
59
|
+
gender: zod.z.enum(["male", "female"]).describe("Gender of the individual (required for luck pillar calculation)"),
|
|
60
|
+
timezone: zod.z.string().default("Asia/Shanghai").describe("IANA Timezone string (e.g. Asia/Dhaka, America/New_York)"),
|
|
61
|
+
language: zod.z.enum(["en", "zh"]).default("en").describe("Language for chart output")
|
|
62
|
+
});
|
|
63
|
+
function createBaziVercelTool(client) {
|
|
64
|
+
return {
|
|
65
|
+
description: "Calculates Chinese Four Pillars of Destiny (BaZi) astrological chart with Heavenly Stems, Earthly Branches, Ten Gods, and Luck Pillars.",
|
|
66
|
+
parameters: baziToolSchema,
|
|
67
|
+
execute: async (args) => {
|
|
68
|
+
const result = await client.calculate({
|
|
69
|
+
birthDate: args.birthDate,
|
|
70
|
+
birthTime: args.birthTime,
|
|
71
|
+
gender: args.gender,
|
|
72
|
+
timezone: args.timezone,
|
|
73
|
+
language: args.language
|
|
74
|
+
});
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function createBaziLangChainConfig(client, overrides = {}) {
|
|
81
|
+
return {
|
|
82
|
+
name: overrides.name || "calculate_bazi_chart",
|
|
83
|
+
description: overrides.description || "Calculates complete Chinese Four Pillars of Destiny (BaZi) astrological chart with Heavenly Stems, Earthly Branches, Five Elements, and Ten Gods.",
|
|
84
|
+
schema: baziToolSchema,
|
|
85
|
+
func: async (input) => {
|
|
86
|
+
const chart = await client.calculate({
|
|
87
|
+
birthDate: input.birthDate,
|
|
88
|
+
birthTime: input.birthTime,
|
|
89
|
+
gender: input.gender,
|
|
90
|
+
timezone: input.timezone,
|
|
91
|
+
language: input.language
|
|
92
|
+
});
|
|
93
|
+
return JSON.stringify(chart);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
exports.baziOpenAITool = baziOpenAITool;
|
|
99
|
+
exports.baziToolSchema = baziToolSchema;
|
|
100
|
+
exports.createBaziLangChainConfig = createBaziLangChainConfig;
|
|
101
|
+
exports.createBaziVercelTool = createBaziVercelTool;
|
|
102
|
+
exports.executeOpenAIBaziCall = executeOpenAIBaziCall;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import * as _baziapi_sdk from '@baziapi/sdk';
|
|
2
|
+
import { BaziClient } from '@baziapi/sdk';
|
|
3
|
+
import * as zod from 'zod';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @file openai/toolDefinition.ts
|
|
8
|
+
* @description Official OpenAI Function / Tool Calling Schema for BaZi calculations.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* OpenAI Tool Definition for BaZi Four Pillars calculation.
|
|
12
|
+
* Compatible with OpenAI Chat Completions API, Assistant API, and Agents SDK.
|
|
13
|
+
*/
|
|
14
|
+
declare const baziOpenAITool: {
|
|
15
|
+
type: "function";
|
|
16
|
+
function: {
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
parameters: {
|
|
20
|
+
type: string;
|
|
21
|
+
properties: {
|
|
22
|
+
birthDate: {
|
|
23
|
+
type: string;
|
|
24
|
+
description: string;
|
|
25
|
+
};
|
|
26
|
+
birthTime: {
|
|
27
|
+
type: string;
|
|
28
|
+
description: string;
|
|
29
|
+
};
|
|
30
|
+
gender: {
|
|
31
|
+
type: string;
|
|
32
|
+
enum: string[];
|
|
33
|
+
description: string;
|
|
34
|
+
};
|
|
35
|
+
timezone: {
|
|
36
|
+
type: string;
|
|
37
|
+
default: string;
|
|
38
|
+
description: string;
|
|
39
|
+
};
|
|
40
|
+
language: {
|
|
41
|
+
type: string;
|
|
42
|
+
enum: string[];
|
|
43
|
+
default: string;
|
|
44
|
+
description: string;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
required: string[];
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
interface BaziClientLike {
|
|
52
|
+
calculate?: (input: {
|
|
53
|
+
birthDate: string;
|
|
54
|
+
birthTime?: string;
|
|
55
|
+
gender: 'male' | 'female';
|
|
56
|
+
timezone?: string;
|
|
57
|
+
language?: 'en' | 'zh';
|
|
58
|
+
}) => Promise<unknown>;
|
|
59
|
+
bazi?: {
|
|
60
|
+
calculate: (input: {
|
|
61
|
+
birthDate: string;
|
|
62
|
+
birthTime?: string;
|
|
63
|
+
gender: 'male' | 'female';
|
|
64
|
+
timezone?: string;
|
|
65
|
+
language?: 'en' | 'zh';
|
|
66
|
+
}) => Promise<unknown>;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Helper to execute an OpenAI tool call with a BaziClient instance.
|
|
71
|
+
*/
|
|
72
|
+
declare function executeOpenAIBaziCall(client: BaziClientLike, toolArgs: {
|
|
73
|
+
birthDate: string;
|
|
74
|
+
birthTime?: string;
|
|
75
|
+
gender: 'male' | 'female';
|
|
76
|
+
timezone?: string;
|
|
77
|
+
language?: 'en' | 'zh';
|
|
78
|
+
}): Promise<unknown>;
|
|
79
|
+
|
|
80
|
+
declare const baziToolSchema: z.ZodObject<{
|
|
81
|
+
birthDate: z.ZodString;
|
|
82
|
+
birthTime: z.ZodDefault<z.ZodString>;
|
|
83
|
+
gender: z.ZodEnum<["male", "female"]>;
|
|
84
|
+
timezone: z.ZodDefault<z.ZodString>;
|
|
85
|
+
language: z.ZodDefault<z.ZodEnum<["en", "zh"]>>;
|
|
86
|
+
}, "strip", z.ZodTypeAny, {
|
|
87
|
+
birthDate: string;
|
|
88
|
+
birthTime: string;
|
|
89
|
+
gender: "male" | "female";
|
|
90
|
+
timezone: string;
|
|
91
|
+
language: "en" | "zh";
|
|
92
|
+
}, {
|
|
93
|
+
birthDate: string;
|
|
94
|
+
gender: "male" | "female";
|
|
95
|
+
birthTime?: string | undefined;
|
|
96
|
+
timezone?: string | undefined;
|
|
97
|
+
language?: "en" | "zh" | undefined;
|
|
98
|
+
}>;
|
|
99
|
+
type BaziToolInput = z.infer<typeof baziToolSchema>;
|
|
100
|
+
/**
|
|
101
|
+
* Creates a Vercel AI SDK tool definition.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* import { createBaziVercelTool } from '@baziapi/ai-tools';
|
|
106
|
+
* import { BaziClient } from '@baziapi/sdk';
|
|
107
|
+
* import { generateText } from 'ai';
|
|
108
|
+
* import { openai } from '@ai-sdk/openai';
|
|
109
|
+
*
|
|
110
|
+
* const client = new BaziClient({ apiKey: process.env.BAZI_API_KEY! });
|
|
111
|
+
*
|
|
112
|
+
* const { text } = await generateText({
|
|
113
|
+
* model: openai('gpt-4o'),
|
|
114
|
+
* tools: {
|
|
115
|
+
* calculateBazi: createBaziVercelTool(client),
|
|
116
|
+
* },
|
|
117
|
+
* prompt: 'Calculate BaZi chart for a male born on August 12, 1998 at 10:30 AM in Dhaka.',
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
declare function createBaziVercelTool(client: BaziClient): {
|
|
122
|
+
description: string;
|
|
123
|
+
parameters: z.ZodObject<{
|
|
124
|
+
birthDate: z.ZodString;
|
|
125
|
+
birthTime: z.ZodDefault<z.ZodString>;
|
|
126
|
+
gender: z.ZodEnum<["male", "female"]>;
|
|
127
|
+
timezone: z.ZodDefault<z.ZodString>;
|
|
128
|
+
language: z.ZodDefault<z.ZodEnum<["en", "zh"]>>;
|
|
129
|
+
}, "strip", z.ZodTypeAny, {
|
|
130
|
+
birthDate: string;
|
|
131
|
+
birthTime: string;
|
|
132
|
+
gender: "male" | "female";
|
|
133
|
+
timezone: string;
|
|
134
|
+
language: "en" | "zh";
|
|
135
|
+
}, {
|
|
136
|
+
birthDate: string;
|
|
137
|
+
gender: "male" | "female";
|
|
138
|
+
birthTime?: string | undefined;
|
|
139
|
+
timezone?: string | undefined;
|
|
140
|
+
language?: "en" | "zh" | undefined;
|
|
141
|
+
}>;
|
|
142
|
+
execute: (args: BaziToolInput) => Promise<_baziapi_sdk.BaziCalculateResponse>;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
interface LangChainToolConfig {
|
|
146
|
+
name?: string;
|
|
147
|
+
description?: string;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Creates a LangChain structured tool configuration for BaZi calculations.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
155
|
+
* import { createBaziLangChainConfig } from '@baziapi/ai-tools';
|
|
156
|
+
* import { BaziClient } from '@baziapi/sdk';
|
|
157
|
+
*
|
|
158
|
+
* const client = new BaziClient({ apiKey: 'bazi_xxxx' });
|
|
159
|
+
* const config = createBaziLangChainConfig(client);
|
|
160
|
+
*
|
|
161
|
+
* const baziTool = new DynamicStructuredTool(config);
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
declare function createBaziLangChainConfig(client: BaziClient, overrides?: LangChainToolConfig): {
|
|
165
|
+
name: string;
|
|
166
|
+
description: string;
|
|
167
|
+
schema: zod.ZodObject<{
|
|
168
|
+
birthDate: zod.ZodString;
|
|
169
|
+
birthTime: zod.ZodDefault<zod.ZodString>;
|
|
170
|
+
gender: zod.ZodEnum<["male", "female"]>;
|
|
171
|
+
timezone: zod.ZodDefault<zod.ZodString>;
|
|
172
|
+
language: zod.ZodDefault<zod.ZodEnum<["en", "zh"]>>;
|
|
173
|
+
}, "strip", zod.ZodTypeAny, {
|
|
174
|
+
birthDate: string;
|
|
175
|
+
birthTime: string;
|
|
176
|
+
gender: "male" | "female";
|
|
177
|
+
timezone: string;
|
|
178
|
+
language: "en" | "zh";
|
|
179
|
+
}, {
|
|
180
|
+
birthDate: string;
|
|
181
|
+
gender: "male" | "female";
|
|
182
|
+
birthTime?: string | undefined;
|
|
183
|
+
timezone?: string | undefined;
|
|
184
|
+
language?: "en" | "zh" | undefined;
|
|
185
|
+
}>;
|
|
186
|
+
func: (input: BaziToolInput) => Promise<string>;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export { baziOpenAITool, baziToolSchema, createBaziLangChainConfig, createBaziVercelTool, executeOpenAIBaziCall };
|
|
190
|
+
export type { BaziToolInput, LangChainToolConfig };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import * as _baziapi_sdk from '@baziapi/sdk';
|
|
2
|
+
import { BaziClient } from '@baziapi/sdk';
|
|
3
|
+
import * as zod from 'zod';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @file openai/toolDefinition.ts
|
|
8
|
+
* @description Official OpenAI Function / Tool Calling Schema for BaZi calculations.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* OpenAI Tool Definition for BaZi Four Pillars calculation.
|
|
12
|
+
* Compatible with OpenAI Chat Completions API, Assistant API, and Agents SDK.
|
|
13
|
+
*/
|
|
14
|
+
declare const baziOpenAITool: {
|
|
15
|
+
type: "function";
|
|
16
|
+
function: {
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
parameters: {
|
|
20
|
+
type: string;
|
|
21
|
+
properties: {
|
|
22
|
+
birthDate: {
|
|
23
|
+
type: string;
|
|
24
|
+
description: string;
|
|
25
|
+
};
|
|
26
|
+
birthTime: {
|
|
27
|
+
type: string;
|
|
28
|
+
description: string;
|
|
29
|
+
};
|
|
30
|
+
gender: {
|
|
31
|
+
type: string;
|
|
32
|
+
enum: string[];
|
|
33
|
+
description: string;
|
|
34
|
+
};
|
|
35
|
+
timezone: {
|
|
36
|
+
type: string;
|
|
37
|
+
default: string;
|
|
38
|
+
description: string;
|
|
39
|
+
};
|
|
40
|
+
language: {
|
|
41
|
+
type: string;
|
|
42
|
+
enum: string[];
|
|
43
|
+
default: string;
|
|
44
|
+
description: string;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
required: string[];
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
interface BaziClientLike {
|
|
52
|
+
calculate?: (input: {
|
|
53
|
+
birthDate: string;
|
|
54
|
+
birthTime?: string;
|
|
55
|
+
gender: 'male' | 'female';
|
|
56
|
+
timezone?: string;
|
|
57
|
+
language?: 'en' | 'zh';
|
|
58
|
+
}) => Promise<unknown>;
|
|
59
|
+
bazi?: {
|
|
60
|
+
calculate: (input: {
|
|
61
|
+
birthDate: string;
|
|
62
|
+
birthTime?: string;
|
|
63
|
+
gender: 'male' | 'female';
|
|
64
|
+
timezone?: string;
|
|
65
|
+
language?: 'en' | 'zh';
|
|
66
|
+
}) => Promise<unknown>;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Helper to execute an OpenAI tool call with a BaziClient instance.
|
|
71
|
+
*/
|
|
72
|
+
declare function executeOpenAIBaziCall(client: BaziClientLike, toolArgs: {
|
|
73
|
+
birthDate: string;
|
|
74
|
+
birthTime?: string;
|
|
75
|
+
gender: 'male' | 'female';
|
|
76
|
+
timezone?: string;
|
|
77
|
+
language?: 'en' | 'zh';
|
|
78
|
+
}): Promise<unknown>;
|
|
79
|
+
|
|
80
|
+
declare const baziToolSchema: z.ZodObject<{
|
|
81
|
+
birthDate: z.ZodString;
|
|
82
|
+
birthTime: z.ZodDefault<z.ZodString>;
|
|
83
|
+
gender: z.ZodEnum<["male", "female"]>;
|
|
84
|
+
timezone: z.ZodDefault<z.ZodString>;
|
|
85
|
+
language: z.ZodDefault<z.ZodEnum<["en", "zh"]>>;
|
|
86
|
+
}, "strip", z.ZodTypeAny, {
|
|
87
|
+
birthDate: string;
|
|
88
|
+
birthTime: string;
|
|
89
|
+
gender: "male" | "female";
|
|
90
|
+
timezone: string;
|
|
91
|
+
language: "en" | "zh";
|
|
92
|
+
}, {
|
|
93
|
+
birthDate: string;
|
|
94
|
+
gender: "male" | "female";
|
|
95
|
+
birthTime?: string | undefined;
|
|
96
|
+
timezone?: string | undefined;
|
|
97
|
+
language?: "en" | "zh" | undefined;
|
|
98
|
+
}>;
|
|
99
|
+
type BaziToolInput = z.infer<typeof baziToolSchema>;
|
|
100
|
+
/**
|
|
101
|
+
* Creates a Vercel AI SDK tool definition.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* import { createBaziVercelTool } from '@baziapi/ai-tools';
|
|
106
|
+
* import { BaziClient } from '@baziapi/sdk';
|
|
107
|
+
* import { generateText } from 'ai';
|
|
108
|
+
* import { openai } from '@ai-sdk/openai';
|
|
109
|
+
*
|
|
110
|
+
* const client = new BaziClient({ apiKey: process.env.BAZI_API_KEY! });
|
|
111
|
+
*
|
|
112
|
+
* const { text } = await generateText({
|
|
113
|
+
* model: openai('gpt-4o'),
|
|
114
|
+
* tools: {
|
|
115
|
+
* calculateBazi: createBaziVercelTool(client),
|
|
116
|
+
* },
|
|
117
|
+
* prompt: 'Calculate BaZi chart for a male born on August 12, 1998 at 10:30 AM in Dhaka.',
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
declare function createBaziVercelTool(client: BaziClient): {
|
|
122
|
+
description: string;
|
|
123
|
+
parameters: z.ZodObject<{
|
|
124
|
+
birthDate: z.ZodString;
|
|
125
|
+
birthTime: z.ZodDefault<z.ZodString>;
|
|
126
|
+
gender: z.ZodEnum<["male", "female"]>;
|
|
127
|
+
timezone: z.ZodDefault<z.ZodString>;
|
|
128
|
+
language: z.ZodDefault<z.ZodEnum<["en", "zh"]>>;
|
|
129
|
+
}, "strip", z.ZodTypeAny, {
|
|
130
|
+
birthDate: string;
|
|
131
|
+
birthTime: string;
|
|
132
|
+
gender: "male" | "female";
|
|
133
|
+
timezone: string;
|
|
134
|
+
language: "en" | "zh";
|
|
135
|
+
}, {
|
|
136
|
+
birthDate: string;
|
|
137
|
+
gender: "male" | "female";
|
|
138
|
+
birthTime?: string | undefined;
|
|
139
|
+
timezone?: string | undefined;
|
|
140
|
+
language?: "en" | "zh" | undefined;
|
|
141
|
+
}>;
|
|
142
|
+
execute: (args: BaziToolInput) => Promise<_baziapi_sdk.BaziCalculateResponse>;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
interface LangChainToolConfig {
|
|
146
|
+
name?: string;
|
|
147
|
+
description?: string;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Creates a LangChain structured tool configuration for BaZi calculations.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
155
|
+
* import { createBaziLangChainConfig } from '@baziapi/ai-tools';
|
|
156
|
+
* import { BaziClient } from '@baziapi/sdk';
|
|
157
|
+
*
|
|
158
|
+
* const client = new BaziClient({ apiKey: 'bazi_xxxx' });
|
|
159
|
+
* const config = createBaziLangChainConfig(client);
|
|
160
|
+
*
|
|
161
|
+
* const baziTool = new DynamicStructuredTool(config);
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
declare function createBaziLangChainConfig(client: BaziClient, overrides?: LangChainToolConfig): {
|
|
165
|
+
name: string;
|
|
166
|
+
description: string;
|
|
167
|
+
schema: zod.ZodObject<{
|
|
168
|
+
birthDate: zod.ZodString;
|
|
169
|
+
birthTime: zod.ZodDefault<zod.ZodString>;
|
|
170
|
+
gender: zod.ZodEnum<["male", "female"]>;
|
|
171
|
+
timezone: zod.ZodDefault<zod.ZodString>;
|
|
172
|
+
language: zod.ZodDefault<zod.ZodEnum<["en", "zh"]>>;
|
|
173
|
+
}, "strip", zod.ZodTypeAny, {
|
|
174
|
+
birthDate: string;
|
|
175
|
+
birthTime: string;
|
|
176
|
+
gender: "male" | "female";
|
|
177
|
+
timezone: string;
|
|
178
|
+
language: "en" | "zh";
|
|
179
|
+
}, {
|
|
180
|
+
birthDate: string;
|
|
181
|
+
gender: "male" | "female";
|
|
182
|
+
birthTime?: string | undefined;
|
|
183
|
+
timezone?: string | undefined;
|
|
184
|
+
language?: "en" | "zh" | undefined;
|
|
185
|
+
}>;
|
|
186
|
+
func: (input: BaziToolInput) => Promise<string>;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export { baziOpenAITool, baziToolSchema, createBaziLangChainConfig, createBaziVercelTool, executeOpenAIBaziCall };
|
|
190
|
+
export type { BaziToolInput, LangChainToolConfig };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import * as _baziapi_sdk from '@baziapi/sdk';
|
|
2
|
+
import { BaziClient } from '@baziapi/sdk';
|
|
3
|
+
import * as zod from 'zod';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @file openai/toolDefinition.ts
|
|
8
|
+
* @description Official OpenAI Function / Tool Calling Schema for BaZi calculations.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* OpenAI Tool Definition for BaZi Four Pillars calculation.
|
|
12
|
+
* Compatible with OpenAI Chat Completions API, Assistant API, and Agents SDK.
|
|
13
|
+
*/
|
|
14
|
+
declare const baziOpenAITool: {
|
|
15
|
+
type: "function";
|
|
16
|
+
function: {
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
parameters: {
|
|
20
|
+
type: string;
|
|
21
|
+
properties: {
|
|
22
|
+
birthDate: {
|
|
23
|
+
type: string;
|
|
24
|
+
description: string;
|
|
25
|
+
};
|
|
26
|
+
birthTime: {
|
|
27
|
+
type: string;
|
|
28
|
+
description: string;
|
|
29
|
+
};
|
|
30
|
+
gender: {
|
|
31
|
+
type: string;
|
|
32
|
+
enum: string[];
|
|
33
|
+
description: string;
|
|
34
|
+
};
|
|
35
|
+
timezone: {
|
|
36
|
+
type: string;
|
|
37
|
+
default: string;
|
|
38
|
+
description: string;
|
|
39
|
+
};
|
|
40
|
+
language: {
|
|
41
|
+
type: string;
|
|
42
|
+
enum: string[];
|
|
43
|
+
default: string;
|
|
44
|
+
description: string;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
required: string[];
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
interface BaziClientLike {
|
|
52
|
+
calculate?: (input: {
|
|
53
|
+
birthDate: string;
|
|
54
|
+
birthTime?: string;
|
|
55
|
+
gender: 'male' | 'female';
|
|
56
|
+
timezone?: string;
|
|
57
|
+
language?: 'en' | 'zh';
|
|
58
|
+
}) => Promise<unknown>;
|
|
59
|
+
bazi?: {
|
|
60
|
+
calculate: (input: {
|
|
61
|
+
birthDate: string;
|
|
62
|
+
birthTime?: string;
|
|
63
|
+
gender: 'male' | 'female';
|
|
64
|
+
timezone?: string;
|
|
65
|
+
language?: 'en' | 'zh';
|
|
66
|
+
}) => Promise<unknown>;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Helper to execute an OpenAI tool call with a BaziClient instance.
|
|
71
|
+
*/
|
|
72
|
+
declare function executeOpenAIBaziCall(client: BaziClientLike, toolArgs: {
|
|
73
|
+
birthDate: string;
|
|
74
|
+
birthTime?: string;
|
|
75
|
+
gender: 'male' | 'female';
|
|
76
|
+
timezone?: string;
|
|
77
|
+
language?: 'en' | 'zh';
|
|
78
|
+
}): Promise<unknown>;
|
|
79
|
+
|
|
80
|
+
declare const baziToolSchema: z.ZodObject<{
|
|
81
|
+
birthDate: z.ZodString;
|
|
82
|
+
birthTime: z.ZodDefault<z.ZodString>;
|
|
83
|
+
gender: z.ZodEnum<["male", "female"]>;
|
|
84
|
+
timezone: z.ZodDefault<z.ZodString>;
|
|
85
|
+
language: z.ZodDefault<z.ZodEnum<["en", "zh"]>>;
|
|
86
|
+
}, "strip", z.ZodTypeAny, {
|
|
87
|
+
birthDate: string;
|
|
88
|
+
birthTime: string;
|
|
89
|
+
gender: "male" | "female";
|
|
90
|
+
timezone: string;
|
|
91
|
+
language: "en" | "zh";
|
|
92
|
+
}, {
|
|
93
|
+
birthDate: string;
|
|
94
|
+
gender: "male" | "female";
|
|
95
|
+
birthTime?: string | undefined;
|
|
96
|
+
timezone?: string | undefined;
|
|
97
|
+
language?: "en" | "zh" | undefined;
|
|
98
|
+
}>;
|
|
99
|
+
type BaziToolInput = z.infer<typeof baziToolSchema>;
|
|
100
|
+
/**
|
|
101
|
+
* Creates a Vercel AI SDK tool definition.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* import { createBaziVercelTool } from '@baziapi/ai-tools';
|
|
106
|
+
* import { BaziClient } from '@baziapi/sdk';
|
|
107
|
+
* import { generateText } from 'ai';
|
|
108
|
+
* import { openai } from '@ai-sdk/openai';
|
|
109
|
+
*
|
|
110
|
+
* const client = new BaziClient({ apiKey: process.env.BAZI_API_KEY! });
|
|
111
|
+
*
|
|
112
|
+
* const { text } = await generateText({
|
|
113
|
+
* model: openai('gpt-4o'),
|
|
114
|
+
* tools: {
|
|
115
|
+
* calculateBazi: createBaziVercelTool(client),
|
|
116
|
+
* },
|
|
117
|
+
* prompt: 'Calculate BaZi chart for a male born on August 12, 1998 at 10:30 AM in Dhaka.',
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
declare function createBaziVercelTool(client: BaziClient): {
|
|
122
|
+
description: string;
|
|
123
|
+
parameters: z.ZodObject<{
|
|
124
|
+
birthDate: z.ZodString;
|
|
125
|
+
birthTime: z.ZodDefault<z.ZodString>;
|
|
126
|
+
gender: z.ZodEnum<["male", "female"]>;
|
|
127
|
+
timezone: z.ZodDefault<z.ZodString>;
|
|
128
|
+
language: z.ZodDefault<z.ZodEnum<["en", "zh"]>>;
|
|
129
|
+
}, "strip", z.ZodTypeAny, {
|
|
130
|
+
birthDate: string;
|
|
131
|
+
birthTime: string;
|
|
132
|
+
gender: "male" | "female";
|
|
133
|
+
timezone: string;
|
|
134
|
+
language: "en" | "zh";
|
|
135
|
+
}, {
|
|
136
|
+
birthDate: string;
|
|
137
|
+
gender: "male" | "female";
|
|
138
|
+
birthTime?: string | undefined;
|
|
139
|
+
timezone?: string | undefined;
|
|
140
|
+
language?: "en" | "zh" | undefined;
|
|
141
|
+
}>;
|
|
142
|
+
execute: (args: BaziToolInput) => Promise<_baziapi_sdk.BaziCalculateResponse>;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
interface LangChainToolConfig {
|
|
146
|
+
name?: string;
|
|
147
|
+
description?: string;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Creates a LangChain structured tool configuration for BaZi calculations.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* import { DynamicStructuredTool } from '@langchain/core/tools';
|
|
155
|
+
* import { createBaziLangChainConfig } from '@baziapi/ai-tools';
|
|
156
|
+
* import { BaziClient } from '@baziapi/sdk';
|
|
157
|
+
*
|
|
158
|
+
* const client = new BaziClient({ apiKey: 'bazi_xxxx' });
|
|
159
|
+
* const config = createBaziLangChainConfig(client);
|
|
160
|
+
*
|
|
161
|
+
* const baziTool = new DynamicStructuredTool(config);
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
declare function createBaziLangChainConfig(client: BaziClient, overrides?: LangChainToolConfig): {
|
|
165
|
+
name: string;
|
|
166
|
+
description: string;
|
|
167
|
+
schema: zod.ZodObject<{
|
|
168
|
+
birthDate: zod.ZodString;
|
|
169
|
+
birthTime: zod.ZodDefault<zod.ZodString>;
|
|
170
|
+
gender: zod.ZodEnum<["male", "female"]>;
|
|
171
|
+
timezone: zod.ZodDefault<zod.ZodString>;
|
|
172
|
+
language: zod.ZodDefault<zod.ZodEnum<["en", "zh"]>>;
|
|
173
|
+
}, "strip", zod.ZodTypeAny, {
|
|
174
|
+
birthDate: string;
|
|
175
|
+
birthTime: string;
|
|
176
|
+
gender: "male" | "female";
|
|
177
|
+
timezone: string;
|
|
178
|
+
language: "en" | "zh";
|
|
179
|
+
}, {
|
|
180
|
+
birthDate: string;
|
|
181
|
+
gender: "male" | "female";
|
|
182
|
+
birthTime?: string | undefined;
|
|
183
|
+
timezone?: string | undefined;
|
|
184
|
+
language?: "en" | "zh" | undefined;
|
|
185
|
+
}>;
|
|
186
|
+
func: (input: BaziToolInput) => Promise<string>;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export { baziOpenAITool, baziToolSchema, createBaziLangChainConfig, createBaziVercelTool, executeOpenAIBaziCall };
|
|
190
|
+
export type { BaziToolInput, LangChainToolConfig };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const baziOpenAITool = {
|
|
4
|
+
type: "function",
|
|
5
|
+
function: {
|
|
6
|
+
name: "calculate_bazi_chart",
|
|
7
|
+
description: "Calculates complete Chinese Four Pillars of Destiny (BaZi / \u516B\u5B57) astrological chart with Heavenly Stems, Earthly Branches, Five Elements, Ten Gods, Hidden Stems, and Luck Pillars from birth date and time.",
|
|
8
|
+
parameters: {
|
|
9
|
+
type: "object",
|
|
10
|
+
properties: {
|
|
11
|
+
birthDate: {
|
|
12
|
+
type: "string",
|
|
13
|
+
description: "Birth date in YYYY-MM-DD format (e.g. '1998-08-12')"
|
|
14
|
+
},
|
|
15
|
+
birthTime: {
|
|
16
|
+
type: "string",
|
|
17
|
+
description: "Birth time in 24-hour HH:mm format (e.g. '10:30'). Defaults to '12:00' if unknown."
|
|
18
|
+
},
|
|
19
|
+
gender: {
|
|
20
|
+
type: "string",
|
|
21
|
+
enum: ["male", "female"],
|
|
22
|
+
description: "Gender of the person. Determines Luck Pillar forward/backward progression direction."
|
|
23
|
+
},
|
|
24
|
+
timezone: {
|
|
25
|
+
type: "string",
|
|
26
|
+
default: "Asia/Shanghai",
|
|
27
|
+
description: "IANA Timezone string (e.g. 'Asia/Dhaka', 'America/New_York', 'Europe/London')"
|
|
28
|
+
},
|
|
29
|
+
language: {
|
|
30
|
+
type: "string",
|
|
31
|
+
enum: ["en", "zh"],
|
|
32
|
+
default: "en",
|
|
33
|
+
description: "Language for terminology and names ('en' for English pinyin/translations, 'zh' for Chinese)"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
required: ["birthDate", "birthTime", "gender"]
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
async function executeOpenAIBaziCall(client, toolArgs) {
|
|
41
|
+
const calcFn = client.calculate ? client.calculate.bind(client) : client.bazi?.calculate.bind(client.bazi);
|
|
42
|
+
if (!calcFn) {
|
|
43
|
+
throw new Error("Invalid BaziClient instance provided.");
|
|
44
|
+
}
|
|
45
|
+
return calcFn({
|
|
46
|
+
birthDate: toolArgs.birthDate,
|
|
47
|
+
birthTime: toolArgs.birthTime || "12:00",
|
|
48
|
+
gender: toolArgs.gender,
|
|
49
|
+
timezone: toolArgs.timezone || "Asia/Shanghai",
|
|
50
|
+
language: toolArgs.language || "en"
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const baziToolSchema = z.object({
|
|
55
|
+
birthDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Must be in YYYY-MM-DD format (e.g. 1998-08-12)").describe("Birth date in YYYY-MM-DD format"),
|
|
56
|
+
birthTime: z.string().regex(/^\d{2}:\d{2}$/, "Must be in HH:mm 24-hour format (e.g. 10:30)").default("12:00").describe("Birth time in 24-hour HH:mm format"),
|
|
57
|
+
gender: z.enum(["male", "female"]).describe("Gender of the individual (required for luck pillar calculation)"),
|
|
58
|
+
timezone: z.string().default("Asia/Shanghai").describe("IANA Timezone string (e.g. Asia/Dhaka, America/New_York)"),
|
|
59
|
+
language: z.enum(["en", "zh"]).default("en").describe("Language for chart output")
|
|
60
|
+
});
|
|
61
|
+
function createBaziVercelTool(client) {
|
|
62
|
+
return {
|
|
63
|
+
description: "Calculates Chinese Four Pillars of Destiny (BaZi) astrological chart with Heavenly Stems, Earthly Branches, Ten Gods, and Luck Pillars.",
|
|
64
|
+
parameters: baziToolSchema,
|
|
65
|
+
execute: async (args) => {
|
|
66
|
+
const result = await client.calculate({
|
|
67
|
+
birthDate: args.birthDate,
|
|
68
|
+
birthTime: args.birthTime,
|
|
69
|
+
gender: args.gender,
|
|
70
|
+
timezone: args.timezone,
|
|
71
|
+
language: args.language
|
|
72
|
+
});
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function createBaziLangChainConfig(client, overrides = {}) {
|
|
79
|
+
return {
|
|
80
|
+
name: overrides.name || "calculate_bazi_chart",
|
|
81
|
+
description: overrides.description || "Calculates complete Chinese Four Pillars of Destiny (BaZi) astrological chart with Heavenly Stems, Earthly Branches, Five Elements, and Ten Gods.",
|
|
82
|
+
schema: baziToolSchema,
|
|
83
|
+
func: async (input) => {
|
|
84
|
+
const chart = await client.calculate({
|
|
85
|
+
birthDate: input.birthDate,
|
|
86
|
+
birthTime: input.birthTime,
|
|
87
|
+
gender: input.gender,
|
|
88
|
+
timezone: input.timezone,
|
|
89
|
+
language: input.language
|
|
90
|
+
});
|
|
91
|
+
return JSON.stringify(chart);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { baziOpenAITool, baziToolSchema, createBaziLangChainConfig, createBaziVercelTool, executeOpenAIBaziCall };
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@baziapi/ai-tools",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official AI Agent and LLM Function Calling Tools for BaZi API (OpenAI, LangChain, Vercel AI SDK)",
|
|
5
|
+
"author": "Md. Nasir Uddin Shoyas",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"require": "./dist/index.cjs",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"main": "./dist/index.cjs",
|
|
19
|
+
"module": "./dist/index.mjs",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@baziapi/sdk": "1.0.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"zod": "^3.0.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependenciesMeta": {
|
|
32
|
+
"zod": {
|
|
33
|
+
"optional": true
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"typescript": "^5.6.3",
|
|
38
|
+
"unbuild": "^3.0.0",
|
|
39
|
+
"zod": "^3.23.8"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "unbuild",
|
|
43
|
+
"dev": "unbuild --stub",
|
|
44
|
+
"lint": "eslint src",
|
|
45
|
+
"type-check": "tsc --noEmit"
|
|
46
|
+
}
|
|
47
|
+
}
|