@botonic/plugin-ai-agents 0.37.0 → 0.37.1
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 +148 -6
- package/lib/cjs/types.d.ts +3 -2
- package/lib/esm/types.d.ts +3 -2
- package/package.json +1 -1
- package/src/types.ts +4 -2
package/README.md
CHANGED
|
@@ -2,13 +2,155 @@
|
|
|
2
2
|
|
|
3
3
|
## env variables
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- It is necessary to add the following variables to an `.env` file:
|
|
6
|
+
- `AZURE_OPENAI_API_KEY`
|
|
7
|
+
- `AZURE_OPENAI_API_BASE`
|
|
8
|
+
- `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
9
|
+
- `AZURE_OPENAI_API_VERSION`
|
|
10
|
+
- Add these env variables to build using rspack, for target NODE and DEV.
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
```ts
|
|
13
|
+
...
|
|
14
|
+
import { config } from 'dotenv'
|
|
15
|
+
config()
|
|
16
|
+
...
|
|
17
|
+
const plugins = [
|
|
18
|
+
new rspack.EnvironmentPlugin({
|
|
19
|
+
NODE_ENV: process.env.NODE_ENV,
|
|
20
|
+
HUBTYPE_API_URL: process.env.HUBTYPE_API_URL || HUBTYPE_DEFAULTS.API_URL,
|
|
21
|
+
BOTONIC_TARGET: BotonicTarget.NODE,
|
|
22
|
+
WEBCHAT_PUSHER_KEY:
|
|
23
|
+
process.env.WEBCHAT_PUSHER_KEY || HUBTYPE_DEFAULTS.WEBCHAT_PUSHER_KEY,
|
|
24
|
+
ENVIRONMENT: process.env.ENVIRONMENT,
|
|
25
|
+
AZURE_OPENAI_API_KEY: isNodeOrDev
|
|
26
|
+
? process.env.AZURE_OPENAI_API_KEY
|
|
27
|
+
: undefined,
|
|
28
|
+
AZURE_OPENAI_API_DEPLOYMENT_NAME: isNodeOrDev
|
|
29
|
+
? process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME
|
|
30
|
+
: undefined,
|
|
31
|
+
AZURE_OPENAI_API_VERSION: isNodeOrDev
|
|
32
|
+
? process.env.AZURE_OPENAI_API_VERSION
|
|
33
|
+
: undefined,
|
|
34
|
+
AZURE_OPENAI_API_BASE: isNodeOrDev
|
|
35
|
+
? process.env.AZURE_OPENAI_API_BASE
|
|
36
|
+
: undefined,
|
|
37
|
+
}),
|
|
38
|
+
new rspack.ProgressPlugin(),
|
|
39
|
+
]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Custom Tools
|
|
43
|
+
|
|
44
|
+
- It is possible to add tools from the bot code. To do this:
|
|
45
|
+
- Add the `zod` dependency.
|
|
46
|
+
- Create and export the definition of the tools in a file, for example: `src/server/tools.ts`.
|
|
8
47
|
|
|
48
|
+
```ts
|
|
49
|
+
import { CustomTool } from '@botonic/plugin-ai-agents'
|
|
50
|
+
import axios from 'axios'
|
|
51
|
+
import z from 'zod'
|
|
52
|
+
|
|
53
|
+
export const customTools: CustomTool[] = [
|
|
54
|
+
{
|
|
55
|
+
name: 'get_current_weather',
|
|
56
|
+
description: 'Get current weather for a city',
|
|
57
|
+
schema: z.object({
|
|
58
|
+
cityName: z.string(),
|
|
59
|
+
}),
|
|
60
|
+
func: async (input: { cityName: string }) => {
|
|
61
|
+
const baseURL = 'http://api.weatherapi.com/v1'
|
|
62
|
+
const response = await axios.get(`${baseURL}/forecast.json`, {
|
|
63
|
+
params: {
|
|
64
|
+
key: 'WEATHER_API_TOKEN',
|
|
65
|
+
q: input.cityName,
|
|
66
|
+
},
|
|
67
|
+
})
|
|
68
|
+
const data = response.data
|
|
69
|
+
console.log('get_current_weather', { data })
|
|
70
|
+
return data
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
...
|
|
74
|
+
]
|
|
9
75
|
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
76
|
+
|
|
77
|
+
- Add these tools to:
|
|
78
|
+
- Update the `BotonicPluginFlowBuilderOptions`, as we did with the knowledge base, and add the `customTools`.
|
|
79
|
+
|
|
80
|
+
src/server/config.ts
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { PluginAiAgentOptions } from '@botonic/plugin-ai-agents'
|
|
84
|
+
import { customTools } from './tools'
|
|
85
|
+
|
|
86
|
+
...
|
|
87
|
+
function getFlowBuilderConfig(
|
|
88
|
+
env: ENVIRONMENT
|
|
89
|
+
): BotonicPluginFlowBuilderOptions<BotPlugins, UserData> {
|
|
90
|
+
return {
|
|
91
|
+
...
|
|
92
|
+
getAiAgentResponse: async (
|
|
93
|
+
request: BotRequest,
|
|
94
|
+
aiAgentArgs: {
|
|
95
|
+
name: string
|
|
96
|
+
instructions: string
|
|
97
|
+
}
|
|
98
|
+
) => {
|
|
99
|
+
const aiAgentPlugin = request.plugins.aiAgents
|
|
100
|
+
return await aiAgentPlugin.getInference(request, aiAgentArgs)
|
|
101
|
+
},
|
|
102
|
+
...
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getAiAgentsConfig(): PluginAiAgentOptions {
|
|
106
|
+
return {
|
|
107
|
+
authToken: 'AUTH_TOKEN', // Used locally
|
|
108
|
+
customTools: customTools,
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
...
|
|
112
|
+
|
|
113
|
+
interface Config {
|
|
114
|
+
// Use any for infer type for TPlugins and TExtraData
|
|
115
|
+
// to avoid type errors with circular dependencies
|
|
116
|
+
flowBuilder: BotonicPluginFlowBuilderOptions<any, any>
|
|
117
|
+
aiAgents: PluginAiAgentOptions
|
|
118
|
+
knowledgeBases: PluginKnowledgeBaseOptions
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const CONFIG: Record<ENVIRONMENT, Config> = {
|
|
122
|
+
[ENVIRONMENT.LOCAL]: {
|
|
123
|
+
flowBuilder: getFlowBuilderConfig(ENVIRONMENT.LOCAL),
|
|
124
|
+
aiAgents: getAiAgentsConfig(),
|
|
125
|
+
knowledgeBases: getKnowledgeBasesConfig(),
|
|
126
|
+
},
|
|
127
|
+
...
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Share customTools with flow builder frontend
|
|
132
|
+
|
|
133
|
+
- Create new `src/bot-config.ts` file and add customTools. This file is used so that every time a bot is deployed, the Flow Builder frontend can know which tools are in the deployed bot and enable or disable tools for each AI Agent node.
|
|
134
|
+
|
|
135
|
+
src/bot-config.ts
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { BotConfigJSON } from '@botonic/core'
|
|
139
|
+
|
|
140
|
+
import { customTools } from './server/tools'
|
|
141
|
+
|
|
142
|
+
const aiAgentTools =
|
|
143
|
+
customTools?.map(customTool => ({
|
|
144
|
+
name: customTool.name,
|
|
145
|
+
description: customTool.description,
|
|
146
|
+
})) || []
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* This is the configuration is shared with flow-builder-frontend.
|
|
150
|
+
*/
|
|
151
|
+
export const botConfig: BotConfigJSON = {
|
|
152
|
+
tools: aiAgentTools,
|
|
153
|
+
payloads: [],
|
|
154
|
+
webviews: [],
|
|
155
|
+
}
|
|
14
156
|
```
|
package/lib/cjs/types.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { Agent, AgentInputItem,
|
|
1
|
+
import { Agent, AgentInputItem, Tool as OpenAITool, RunContext as OpenAIRunContext } from '@openai/agents';
|
|
2
2
|
import { ZodSchema } from 'zod';
|
|
3
3
|
import { OutputMessage, OutputSchema } from './structured-output';
|
|
4
4
|
export interface Context {
|
|
5
5
|
authToken: string;
|
|
6
6
|
}
|
|
7
|
+
export type RunContext = OpenAIRunContext<Context>;
|
|
7
8
|
export interface CustomTool {
|
|
8
9
|
name: string;
|
|
9
10
|
description: string;
|
|
10
11
|
schema: ZodSchema;
|
|
11
|
-
func: (input?: any, runContext?: RunContext
|
|
12
|
+
func: (input?: any, runContext?: RunContext) => Promise<any>;
|
|
12
13
|
}
|
|
13
14
|
export type Tool = OpenAITool<Context>;
|
|
14
15
|
export type AIAgent = Agent<Context, typeof OutputSchema>;
|
package/lib/esm/types.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { Agent, AgentInputItem,
|
|
1
|
+
import { Agent, AgentInputItem, Tool as OpenAITool, RunContext as OpenAIRunContext } from '@openai/agents';
|
|
2
2
|
import { ZodSchema } from 'zod';
|
|
3
3
|
import { OutputMessage, OutputSchema } from './structured-output';
|
|
4
4
|
export interface Context {
|
|
5
5
|
authToken: string;
|
|
6
6
|
}
|
|
7
|
+
export type RunContext = OpenAIRunContext<Context>;
|
|
7
8
|
export interface CustomTool {
|
|
8
9
|
name: string;
|
|
9
10
|
description: string;
|
|
10
11
|
schema: ZodSchema;
|
|
11
|
-
func: (input?: any, runContext?: RunContext
|
|
12
|
+
func: (input?: any, runContext?: RunContext) => Promise<any>;
|
|
12
13
|
}
|
|
13
14
|
export type Tool = OpenAITool<Context>;
|
|
14
15
|
export type AIAgent = Agent<Context, typeof OutputSchema>;
|
package/package.json
CHANGED
package/src/types.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent,
|
|
3
3
|
AgentInputItem,
|
|
4
|
-
RunContext,
|
|
5
4
|
Tool as OpenAITool,
|
|
5
|
+
RunContext as OpenAIRunContext,
|
|
6
6
|
} from '@openai/agents'
|
|
7
7
|
import { ZodSchema } from 'zod'
|
|
8
8
|
|
|
@@ -12,11 +12,13 @@ export interface Context {
|
|
|
12
12
|
authToken: string
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
export type RunContext = OpenAIRunContext<Context>
|
|
16
|
+
|
|
15
17
|
export interface CustomTool {
|
|
16
18
|
name: string
|
|
17
19
|
description: string
|
|
18
20
|
schema: ZodSchema
|
|
19
|
-
func: (input?: any, runContext?: RunContext
|
|
21
|
+
func: (input?: any, runContext?: RunContext) => Promise<any>
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
export type Tool = OpenAITool<Context>
|