@microsoft/teams.ai 0.2.13 → 2.0.0-preview.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/dist/prompts/chat.d.mts +10 -3
- package/dist/prompts/chat.d.ts +10 -3
- package/dist/prompts/chat.js +69 -8
- package/dist/prompts/chat.js.map +1 -1
- package/dist/prompts/chat.mjs +69 -8
- package/dist/prompts/chat.mjs.map +1 -1
- package/dist/prompts/index.d.mts +1 -1
- package/dist/prompts/index.d.ts +1 -1
- package/dist/prompts/plugin.d.mts +14 -0
- package/dist/prompts/plugin.d.ts +14 -0
- package/package.json +4 -5
package/dist/prompts/chat.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ILogger } from '@microsoft/teams.common';
|
|
1
2
|
import { Function, FunctionHandler } from '../function.mjs';
|
|
2
3
|
import { IMemory } from '../memory.mjs';
|
|
3
4
|
import { Message, ContentPart, ModelMessage } from '../message.mjs';
|
|
@@ -5,7 +6,6 @@ import { IChatModel, TextChunkHandler } from '../models/chat.mjs';
|
|
|
5
6
|
import { Schema } from '../schema.mjs';
|
|
6
7
|
import { ITemplate } from '../template.mjs';
|
|
7
8
|
import { IAiPlugin } from './plugin.mjs';
|
|
8
|
-
import '@microsoft/teams.common';
|
|
9
9
|
import '../citation.mjs';
|
|
10
10
|
|
|
11
11
|
type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
|
|
@@ -36,6 +36,11 @@ type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any
|
|
|
36
36
|
* the conversation history
|
|
37
37
|
*/
|
|
38
38
|
readonly messages?: Message[] | IMemory;
|
|
39
|
+
/**
|
|
40
|
+
* Logger instance to use for logging
|
|
41
|
+
* If not provided, a ConsoleLogger will be used
|
|
42
|
+
*/
|
|
43
|
+
logger?: ILogger;
|
|
39
44
|
};
|
|
40
45
|
type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
|
|
41
46
|
/**
|
|
@@ -129,9 +134,10 @@ declare class ChatPrompt<TOptions extends Record<string, any> = Record<string, a
|
|
|
129
134
|
protected readonly _role: 'system' | 'user';
|
|
130
135
|
protected readonly _template: ITemplate;
|
|
131
136
|
protected readonly _model: IChatModel<TOptions>;
|
|
137
|
+
protected readonly _log: ILogger;
|
|
132
138
|
constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins);
|
|
133
|
-
use(prompt:
|
|
134
|
-
use(name: string, prompt:
|
|
139
|
+
use(prompt: IChatPrompt): this;
|
|
140
|
+
use(name: string, prompt: IChatPrompt): this;
|
|
135
141
|
function(name: string, description: string, handler: FunctionHandler): this;
|
|
136
142
|
function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
|
|
137
143
|
usePlugin<K extends TChatPromptPlugins[number]['name']>(name: K, args: Extract<TChatPromptPlugins[number], {
|
|
@@ -141,6 +147,7 @@ declare class ChatPrompt<TOptions extends Record<string, any> = Record<string, a
|
|
|
141
147
|
[key: string]: any;
|
|
142
148
|
}, R = any>(name: string, args?: A): Promise<R>;
|
|
143
149
|
send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<ModelMessage>;
|
|
150
|
+
protected executeFunction<R = any>(name: string, fn: Function, args?: Record<string, any>): Promise<R>;
|
|
144
151
|
}
|
|
145
152
|
|
|
146
153
|
export { ChatPrompt, type ChatPromptOptions, type ChatPromptPlugin, type ChatPromptSendOptions, type IChatPrompt };
|
package/dist/prompts/chat.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ILogger } from '@microsoft/teams.common';
|
|
1
2
|
import { Function, FunctionHandler } from '../function.js';
|
|
2
3
|
import { IMemory } from '../memory.js';
|
|
3
4
|
import { Message, ContentPart, ModelMessage } from '../message.js';
|
|
@@ -5,7 +6,6 @@ import { IChatModel, TextChunkHandler } from '../models/chat.js';
|
|
|
5
6
|
import { Schema } from '../schema.js';
|
|
6
7
|
import { ITemplate } from '../template.js';
|
|
7
8
|
import { IAiPlugin } from './plugin.js';
|
|
8
|
-
import '@microsoft/teams.common';
|
|
9
9
|
import '../citation.js';
|
|
10
10
|
|
|
11
11
|
type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
|
|
@@ -36,6 +36,11 @@ type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any
|
|
|
36
36
|
* the conversation history
|
|
37
37
|
*/
|
|
38
38
|
readonly messages?: Message[] | IMemory;
|
|
39
|
+
/**
|
|
40
|
+
* Logger instance to use for logging
|
|
41
|
+
* If not provided, a ConsoleLogger will be used
|
|
42
|
+
*/
|
|
43
|
+
logger?: ILogger;
|
|
39
44
|
};
|
|
40
45
|
type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
|
|
41
46
|
/**
|
|
@@ -129,9 +134,10 @@ declare class ChatPrompt<TOptions extends Record<string, any> = Record<string, a
|
|
|
129
134
|
protected readonly _role: 'system' | 'user';
|
|
130
135
|
protected readonly _template: ITemplate;
|
|
131
136
|
protected readonly _model: IChatModel<TOptions>;
|
|
137
|
+
protected readonly _log: ILogger;
|
|
132
138
|
constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins);
|
|
133
|
-
use(prompt:
|
|
134
|
-
use(name: string, prompt:
|
|
139
|
+
use(prompt: IChatPrompt): this;
|
|
140
|
+
use(name: string, prompt: IChatPrompt): this;
|
|
135
141
|
function(name: string, description: string, handler: FunctionHandler): this;
|
|
136
142
|
function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
|
|
137
143
|
usePlugin<K extends TChatPromptPlugins[number]['name']>(name: K, args: Extract<TChatPromptPlugins[number], {
|
|
@@ -141,6 +147,7 @@ declare class ChatPrompt<TOptions extends Record<string, any> = Record<string, a
|
|
|
141
147
|
[key: string]: any;
|
|
142
148
|
}, R = any>(name: string, args?: A): Promise<R>;
|
|
143
149
|
send(input: string | ContentPart[], options?: ChatPromptSendOptions<TOptions>): Promise<ModelMessage>;
|
|
150
|
+
protected executeFunction<R = any>(name: string, fn: Function, args?: Record<string, any>): Promise<R>;
|
|
144
151
|
}
|
|
145
152
|
|
|
146
153
|
export { ChatPrompt, type ChatPromptOptions, type ChatPromptPlugin, type ChatPromptSendOptions, type IChatPrompt };
|
package/dist/prompts/chat.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var teams_common = require('@microsoft/teams.common');
|
|
3
4
|
var localMemory = require('../local-memory');
|
|
4
5
|
var templates = require('../templates');
|
|
5
6
|
|
|
@@ -27,6 +28,7 @@ class ChatPrompt {
|
|
|
27
28
|
_role;
|
|
28
29
|
_template;
|
|
29
30
|
_model;
|
|
31
|
+
_log;
|
|
30
32
|
constructor(options, plugins) {
|
|
31
33
|
this._name = options.name || "chat";
|
|
32
34
|
this._description = options.description || "an agent you can chat with";
|
|
@@ -35,6 +37,7 @@ class ChatPrompt {
|
|
|
35
37
|
this._template = Array.isArray(options.instructions) ? new templates.StringTemplate(options.instructions.join("\n")) : typeof options.instructions !== "object" ? new templates.StringTemplate(options.instructions) : options.instructions;
|
|
36
38
|
this._messages = typeof options.messages === "object" && !Array.isArray(options.messages) ? options.messages : new localMemory.LocalMemory({ messages: options.messages || [] });
|
|
37
39
|
this._plugins = plugins || [];
|
|
40
|
+
this._log = options.logger || new teams_common.ConsoleLogger(`@microsoft/teams.ai/prompts/${this._name}`);
|
|
38
41
|
}
|
|
39
42
|
use(...args) {
|
|
40
43
|
const prompt = args.length === 1 ? args[0] : args[1];
|
|
@@ -74,10 +77,13 @@ class ChatPrompt {
|
|
|
74
77
|
usePlugin(name, args) {
|
|
75
78
|
const plugin = this._plugins.find((p) => p.name === name);
|
|
76
79
|
if (!plugin) {
|
|
80
|
+
this._log.debug(`Plugin "${name}" not found`);
|
|
77
81
|
throw new Error(`Plugin "${name}" not found`);
|
|
78
82
|
}
|
|
79
83
|
if (plugin.onUsePlugin) {
|
|
84
|
+
this._log.debug(`Using plugin "${name}" with args:`, args);
|
|
80
85
|
plugin.onUsePlugin(args);
|
|
86
|
+
this._log.debug(`Successfully initialized plugin "${name}"`);
|
|
81
87
|
}
|
|
82
88
|
return this;
|
|
83
89
|
}
|
|
@@ -86,11 +92,13 @@ class ChatPrompt {
|
|
|
86
92
|
if (!fn) {
|
|
87
93
|
throw new Error(`function "${name}" not found`);
|
|
88
94
|
}
|
|
89
|
-
return
|
|
95
|
+
return this.executeFunction(name, fn, args);
|
|
90
96
|
}
|
|
91
97
|
async send(input, options = {}) {
|
|
98
|
+
this._log.debug(`Processing plugins before send (${this.plugins.length} plugins found)`);
|
|
92
99
|
for (const plugin of this.plugins) {
|
|
93
100
|
if (plugin.onBeforeSend) {
|
|
101
|
+
this._log.debug(`Running onBeforeSend for plugin "${plugin.name}"`);
|
|
94
102
|
input = await plugin.onBeforeSend(input);
|
|
95
103
|
}
|
|
96
104
|
}
|
|
@@ -107,6 +115,7 @@ class ChatPrompt {
|
|
|
107
115
|
role: this._role,
|
|
108
116
|
content: prompt
|
|
109
117
|
};
|
|
118
|
+
this._log.debug("System instructions for LLM:", prompt);
|
|
110
119
|
}
|
|
111
120
|
let functions = Object.values(this._functions);
|
|
112
121
|
const pluginsWithOnBuildFunctions = this._plugins.filter(
|
|
@@ -115,13 +124,38 @@ class ChatPrompt {
|
|
|
115
124
|
for (const plugin of pluginsWithOnBuildFunctions) {
|
|
116
125
|
functions = await plugin.onBuildFunctions(functions);
|
|
117
126
|
}
|
|
118
|
-
const fnMap = functions.reduce(
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
);
|
|
127
|
+
const fnMap = functions.reduce((acc, fn) => {
|
|
128
|
+
acc[fn.name] = {
|
|
129
|
+
...fn,
|
|
130
|
+
handler: (args) => this.executeFunction(fn.name, fn, args)
|
|
131
|
+
};
|
|
132
|
+
return acc;
|
|
133
|
+
}, {});
|
|
134
|
+
if (Object.keys(fnMap).length > 0) {
|
|
135
|
+
this._log.debug(
|
|
136
|
+
"Available functions for LLM:",
|
|
137
|
+
Object.keys(fnMap).map((name) => {
|
|
138
|
+
const fn = fnMap[name];
|
|
139
|
+
const paramDescriptions = "properties" in fn.parameters && fn.parameters.properties ? Object.entries(
|
|
140
|
+
fn.parameters.properties
|
|
141
|
+
).reduce(
|
|
142
|
+
(acc, [key, prop]) => ({
|
|
143
|
+
...acc,
|
|
144
|
+
[key]: prop.description
|
|
145
|
+
}),
|
|
146
|
+
{}
|
|
147
|
+
) : {};
|
|
148
|
+
return {
|
|
149
|
+
name,
|
|
150
|
+
description: fn.description,
|
|
151
|
+
parameters: {
|
|
152
|
+
schema: fn.parameters,
|
|
153
|
+
descriptions: paramDescriptions
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
}
|
|
125
159
|
const res = await this._model.send(
|
|
126
160
|
{
|
|
127
161
|
role: "user",
|
|
@@ -148,13 +182,40 @@ class ChatPrompt {
|
|
|
148
182
|
...res,
|
|
149
183
|
content: res.content || ""
|
|
150
184
|
};
|
|
185
|
+
if (output.function_calls && output.function_calls.length > 0) {
|
|
186
|
+
this._log.debug(
|
|
187
|
+
"LLM requested function calls:",
|
|
188
|
+
output.function_calls.map((call) => ({
|
|
189
|
+
name: call.name,
|
|
190
|
+
id: call.id,
|
|
191
|
+
arguments: call.arguments
|
|
192
|
+
}))
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
this._log.debug(`Processing plugins after send (${this.plugins.length} plugins found)`);
|
|
151
196
|
for (const plugin of this.plugins) {
|
|
152
197
|
if (plugin.onAfterSend) {
|
|
198
|
+
this._log.debug(`Running onAfterSend for plugin "${plugin.name}"`);
|
|
153
199
|
output = await plugin.onAfterSend(output);
|
|
154
200
|
}
|
|
155
201
|
}
|
|
156
202
|
return output;
|
|
157
203
|
}
|
|
204
|
+
async executeFunction(name, fn, args) {
|
|
205
|
+
const processedArgs = args || {};
|
|
206
|
+
for (const plugin of this.plugins) {
|
|
207
|
+
if (plugin.onBeforeFunctionCall) {
|
|
208
|
+
await plugin.onBeforeFunctionCall(name, processedArgs);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
let result = await fn.handler(processedArgs);
|
|
212
|
+
for (const plugin of this.plugins) {
|
|
213
|
+
if (plugin.onAfterFunctionCall) {
|
|
214
|
+
result = await plugin.onAfterFunctionCall(name, processedArgs, result);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return result;
|
|
218
|
+
}
|
|
158
219
|
}
|
|
159
220
|
|
|
160
221
|
exports.ChatPrompt = ChatPrompt;
|
package/dist/prompts/chat.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/prompts/chat.ts"],"names":["StringTemplate","LocalMemory"],"mappings":";;;;;AA2JO,MAAM,UAIb,CAAA;AAAA,EACE,IAAI,IAAO,GAAA;AACT,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd,EACmB,KAAA;AAAA,EAEnB,IAAI,WAAc,GAAA;AAChB,IAAA,OAAO,IAAK,CAAA,YAAA;AAAA;AACd,EACmB,YAAA;AAAA,EAEnB,IAAI,QAAW,GAAA;AACb,IAAA,OAAO,IAAK,CAAA,SAAA;AAAA;AACd,EACmB,SAAA;AAAA,EAEnB,IAAI,SAAY,GAAA;AACd,IAAO,OAAA,MAAA,CAAO,MAAO,CAAA,IAAA,CAAK,UAAU,CAAA;AAAA;AACtC,EACmB,aAAuC,EAAC;AAAA,EAE3D,IAAI,OAAU,GAAA;AACZ,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AACd,EACmB,QAAA;AAAA,EAEA,KAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EAEnB,WAAA,CAAY,SAAsC,OAA8B,EAAA;AAC9E,IAAK,IAAA,CAAA,KAAA,GAAQ,QAAQ,IAAQ,IAAA,MAAA;AAC7B,IAAK,IAAA,CAAA,YAAA,GAAe,Q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Y,SAAA,GAAA,MAAM,MAAO,CAAA,gBAAA,CAAiB,SAAS,CAAA;AAAA;AAGrD,IAAA,MAAM,QAAQ,SAAU,CAAA,MAAA;AAAA,MACtB,CAAC,KAAK,EAAO,KAAA;AACX,QAAI,GAAA,CAAA,EAAA,CAAG,IAAI,CAAI,GAAA,EAAA;AACf,QAAO,OAAA,GAAA;AAAA,OACT;AAAA,MACA;AAAC,KACH;AAEA,IAAM,MAAA,GAAA,GAAM,MAAM,IAAA,CAAK,MAAO,CAAA,IAAA;AAAA,MAC5B;AAAA,QACE,IAAM,EAAA,MAAA;AAAA,QACN,OAAS,EAAA;AAAA,OACX;AAAA,MACA;AAAA,QACE,MAAA;AAAA,QACA,QAAA;AAAA,QACA,SAAS,OAAQ,CAAA,OAAA;AAAA,QACjB,SAAW,EAAA,KAAA;AAAA,QACX,OAAA,EAAS,OAAO,KAAU,KAAA;AACxB,UAAI,IAAA,CAAC,KAAS,IAAA,CAAC,OAAS,EAAA;AACxB,UAAU,MAAA,IAAA,KAAA;AAEV,UAAI,IAAA;AACF,YAAA,MAAM,QAAQ,MAAM,CAAA;AACpB,YAAS,MAAA,GAAA,EAAA;AAAA,mBACF,GAAK,EAAA;AACZ,YAAA;AAAA;AACF;AACF;AACF,KACF;AAEA,IAAA,IAAI,MAAuD,GAAA;AAAA,MACzD,GAAG,GAAA;AAAA,MACH,OAAA,EAAS,IAAI,OAAW,IAAA;AAAA,KAC1B;AACA,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,WAAa,EAAA;AACtB,QAAS,MAAA,GAAA,MAAM,MAAO,CAAA,WAAA,CAAY,MAAM,CAAA;AAAA;AAC1C;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX","file":"chat.js","sourcesContent":["import { Function, FunctionHandler } from '../function';\r\nimport { LocalMemory } from '../local-memory';\r\nimport { IMemory } from '../memory';\r\nimport { ContentPart, Message, ModelMessage, SystemMessage, UserMessage } from '../message';\r\nimport { IChatModel, TextChunkHandler } from '../models';\r\nimport { Schema } from '../schema';\r\nimport { ITemplate } from '../template';\r\nimport { StringTemplate } from '../templates';\r\nimport { WithRequired } from '../utils/types';\r\n\r\nimport { IAiPlugin } from './plugin';\r\n\r\nexport type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {\r\n /**\r\n * the name of the prompt\r\n */\r\n readonly name?: string;\r\n\r\n /**\r\n * the description of the prompt\r\n */\r\n readonly description?: string;\r\n\r\n /**\r\n * the model to send messages to\r\n */\r\n readonly model: IChatModel<TOptions>;\r\n\r\n /**\r\n * the defining characteristics/objective\r\n * of the prompt. This is commonly used to provide a system prompt.\r\n * If you supply the system prompt as part of the messages,\r\n * you do not need to supply this option.\r\n */\r\n readonly instructions?: string | string[] | ITemplate;\r\n\r\n /**\r\n * the `role` of the initial message\r\n */\r\n readonly role?: 'system' | 'user';\r\n\r\n /**\r\n * the conversation history\r\n */\r\n readonly messages?: Message[] | IMemory;\r\n};\r\n\r\nexport type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {\r\n /**\r\n * the conversation history\r\n */\r\n readonly messages?: Message[] | IMemory;\r\n\r\n /**\r\n * the models request options\r\n */\r\n readonly request?: TOptions;\r\n\r\n /**\r\n * the callback to be called for each\r\n * stream chunk\r\n */\r\n readonly onChunk?: TextChunkHandler;\r\n};\r\n\r\n/**\r\n * a prompt that can interface with a\r\n * chat model that provides utility like\r\n * streaming and function calling\r\n */\r\nexport interface IChatPrompt<\r\n TOptions extends Record<string, any> = Record<string, any>,\r\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\r\n> {\r\n /**\r\n * the prompt name\r\n */\r\n readonly name: string;\r\n\r\n /**\r\n * the prompt description\r\n */\r\n readonly description: string;\r\n\r\n /**\r\n * the chat history\r\n */\r\n readonly messages: IMemory;\r\n\r\n /**\r\n * the registered functions\r\n */\r\n readonly functions: Array<Function>;\r\n\r\n /**\r\n * the chat model\r\n */\r\n plugins: TChatPromptPlugins;\r\n\r\n /**\r\n * add another chat prompt as a\r\n */\r\n use(prompt: IChatPrompt): this;\r\n use(name: string, prompt: IChatPrompt): this;\r\n\r\n /**\r\n * add a function that can be called\r\n * by the model\r\n */\r\n function(name: string, description: string, handler: FunctionHandler): this;\r\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\r\n\r\n usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(\r\n name: TPluginName,\r\n args: Extract<TChatPromptPlugins[number], { name: TPluginName }>['onUsePlugin'] extends\r\n | ((args: infer U) => void)\r\n | undefined\r\n ? U\r\n : never\r\n ): this;\r\n\r\n /**\r\n * call a function\r\n */\r\n call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;\r\n\r\n /**\r\n * send a message to the model and get a response\r\n */\r\n send(\r\n input: string | ContentPart[],\r\n options?: ChatPromptSendOptions<TOptions>\r\n ): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;\r\n}\r\n\r\nexport type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<\r\n TPluginName,\r\n TPluginUseArgs,\r\n Parameters<IChatPrompt['send']>[0],\r\n ReturnType<IChatPrompt['send']>\r\n> & {\r\n /**\r\n * Optionally passed in to modify the functions array that\r\n * is passed to the model\r\n * @param functions\r\n * @returns Functions\r\n */\r\n onBuildFunctions?: (functions: Function[]) => Function[] | Promise<Function[]>;\r\n};\r\n\r\n/**\r\n * a prompt that can interface with a\r\n * chat model that provides utility like\r\n * streaming and function calling\r\n */\r\nexport class ChatPrompt<\r\n TOptions extends Record<string, any> = Record<string, any>,\r\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\r\n> implements IChatPrompt<TOptions, TChatPromptPlugins>\r\n{\r\n get name() {\r\n return this._name;\r\n }\r\n protected readonly _name: string;\r\n\r\n get description() {\r\n return this._description;\r\n }\r\n protected readonly _description: string;\r\n\r\n get messages() {\r\n return this._messages;\r\n }\r\n protected readonly _messages: IMemory;\r\n\r\n get functions() {\r\n return Object.values(this._functions);\r\n }\r\n protected readonly _functions: Record<string, Function> = {};\r\n\r\n get plugins() {\r\n return this._plugins;\r\n }\r\n protected readonly _plugins: TChatPromptPlugins;\r\n\r\n protected readonly _role: 'system' | 'user';\r\n protected readonly _template: ITemplate;\r\n protected readonly _model: IChatModel<TOptions>;\r\n\r\n constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins) {\r\n this._name = options.name || 'chat';\r\n this._description = options.description || 'an agent you can chat with';\r\n this._role = options.role || 'system';\r\n this._model = options.model;\r\n this._template = Array.isArray(options.instructions)\r\n ? new StringTemplate(options.instructions.join('\\n'))\r\n : typeof options.instructions !== 'object'\r\n ? new StringTemplate(options.instructions)\r\n : options.instructions;\r\n\r\n this._messages =\r\n typeof options.messages === 'object' && !Array.isArray(options.messages)\r\n ? options.messages\r\n : new LocalMemory({ messages: options.messages || [] });\r\n\r\n this._plugins = plugins || ([] as unknown as TChatPromptPlugins);\r\n }\r\n\r\n use(prompt: ChatPrompt): this;\r\n use(name: string, prompt: ChatPrompt): this;\r\n use(...args: any[]) {\r\n const prompt: ChatPrompt = args.length === 1 ? args[0] : args[1];\r\n const name: string = args.length === 1 ? prompt.name : args[0];\r\n this._functions[name] = {\r\n name,\r\n description: prompt.description,\r\n parameters: {\r\n type: 'object',\r\n properties: {\r\n text: {\r\n type: 'string',\r\n description: 'the text to send to the assistant',\r\n },\r\n },\r\n required: ['text'],\r\n },\r\n handler: ({ text }: { text: string }) => {\r\n return prompt.send(text);\r\n },\r\n };\r\n\r\n return this;\r\n }\r\n\r\n function(name: string, description: string, handler: FunctionHandler): this;\r\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\r\n function(...args: any[]) {\r\n const name: string = args[0];\r\n const description: string = args[1];\r\n const parameters: Schema | null = args.length === 3 ? null : args[2];\r\n const handler: FunctionHandler = args[args.length - 1];\r\n this._functions[name] = {\r\n name,\r\n description,\r\n parameters: parameters || {},\r\n handler,\r\n };\r\n\r\n return this;\r\n }\r\n\r\n usePlugin<K extends TChatPromptPlugins[number]['name']>(\r\n name: K,\r\n args: Extract<TChatPromptPlugins[number], { name: K }>['onUsePlugin'] extends\r\n | ((args: infer U) => void)\r\n | undefined\r\n ? U\r\n : never\r\n ): this {\r\n const plugin = this._plugins.find((p) => p.name === name);\r\n if (!plugin) {\r\n throw new Error(`Plugin \"${name}\" not found`);\r\n }\r\n\r\n if (plugin.onUsePlugin) {\r\n plugin.onUsePlugin(args);\r\n }\r\n\r\n return this;\r\n }\r\n\r\n async call<A extends { [key: string]: any }, R = any>(name: string, args?: A): Promise<R> {\r\n const fn = this._functions[name];\r\n\r\n if (!fn) {\r\n throw new Error(`function \"${name}\" not found`);\r\n }\r\n\r\n return await fn.handler(args || {});\r\n }\r\n\r\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\r\n for (const plugin of this.plugins) {\r\n if (plugin.onBeforeSend) {\r\n input = await plugin.onBeforeSend(input);\r\n }\r\n }\r\n\r\n const { onChunk } = options;\r\n\r\n if (typeof input === 'string') {\r\n input = input.trim();\r\n }\r\n\r\n const messages = !Array.isArray(options.messages)\r\n ? options.messages || this._messages\r\n : new LocalMemory({ messages: options.messages || [] });\r\n\r\n let buffer = '';\r\n let system: SystemMessage | UserMessage | undefined = undefined;\r\n const prompt = await this._template.render();\r\n\r\n if (prompt) {\r\n system = {\r\n role: this._role,\r\n content: prompt,\r\n };\r\n }\r\n\r\n let functions = Object.values(this._functions);\r\n const pluginsWithOnBuildFunctions = this._plugins.filter(\r\n (plugin): plugin is WithRequired<TChatPromptPlugins[number], 'onBuildFunctions'> =>\r\n plugin.onBuildFunctions != null\r\n );\r\n for (const plugin of pluginsWithOnBuildFunctions) {\r\n functions = await plugin.onBuildFunctions(functions);\r\n }\r\n\r\n const fnMap = functions.reduce(\r\n (acc, fn) => {\r\n acc[fn.name] = fn;\r\n return acc;\r\n },\r\n {} as Record<string, Function>\r\n );\r\n\r\n const res = await this._model.send(\r\n {\r\n role: 'user',\r\n content: input,\r\n },\r\n {\r\n system,\r\n messages,\r\n request: options.request,\r\n functions: fnMap,\r\n onChunk: async (chunk) => {\r\n if (!chunk || !onChunk) return;\r\n buffer += chunk;\r\n\r\n try {\r\n await onChunk(buffer);\r\n buffer = '';\r\n } catch (err) {\r\n return;\r\n }\r\n },\r\n }\r\n );\r\n\r\n let output: Awaited<ReturnType<typeof this._model.send>> = {\r\n ...res,\r\n content: res.content || '',\r\n };\r\n for (const plugin of this.plugins) {\r\n if (plugin.onAfterSend) {\r\n output = await plugin.onAfterSend(output);\r\n }\r\n }\r\n\r\n return output;\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/prompts/chat.ts"],"names":["StringTemplate","LocalMemory","ConsoleLogger"],"mappings":";;;;;;AAkKO,MAAM,UAG0C,CAAA;AAAA,EACrD,IAAI,IAAO,GAAA;AACT,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd,EACmB,KAAA;AAAA,EAEnB,IAAI,WAAc,GAAA;AAChB,IAAA,OAAO,IAAK,CAAA,YAAA;AAAA;AACd,EACmB,YAAA;AAAA,EAEnB,IAAI,QAAW,GAAA;AACb,IAAA,OAAO,IAAK,CAAA,SAAA;AAAA;AACd,EACmB,SAAA;AAAA,EAEnB,IAAI,SAAY,GAAA;AACd,IAAO,OAAA,MAAA,CAAO,MAAO,CAAA,IAAA,CAAK,UAAU,CAAA;AAAA;AACtC,EACmB,aAAuC,EAAC;AAAA,EAE3D,IAAI,OAAU,GAAA;AACZ,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AACd,EACmB,QAAA;AAAA,EAEA,KAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EACA,IAAA;AAAA,EAEnB,WAAA,CAAY,SAAsC,OAA8B,EAAA;AAC9E,IAAK,IAAA,CAAA,KAAA,GAAQ,QAAQ,IAAQ,IAAA,MAAA;AAC7B,IA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AAA;AAG3C,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,mBAAqB,EAAA;AAC9B,QAAA,MAAA,GAAS,MAAM,MAAA,CAAO,mBAAoB,CAAA,IAAA,EAAM,eAAe,MAAM,CAAA;AAAA;AACvE;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX","file":"chat.js","sourcesContent":["import { ConsoleLogger, ILogger } from '@microsoft/teams.common';\n\nimport { Function, FunctionHandler } from '../function';\nimport { LocalMemory } from '../local-memory';\nimport { IMemory } from '../memory';\nimport { ContentPart, Message, ModelMessage, SystemMessage, UserMessage } from '../message';\nimport { IChatModel, TextChunkHandler } from '../models';\nimport { Schema } from '../schema';\nimport { ITemplate } from '../template';\nimport { StringTemplate } from '../templates';\nimport { WithRequired } from '../utils/types';\n\nimport { IAiPlugin } from './plugin';\n\nexport type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {\n /**\n * the name of the prompt\n */\n readonly name?: string;\n\n /**\n * the description of the prompt\n */\n readonly description?: string;\n\n /**\n * the model to send messages to\n */\n readonly model: IChatModel<TOptions>;\n\n /**\n * the defining characteristics/objective\n * of the prompt. This is commonly used to provide a system prompt.\n * If you supply the system prompt as part of the messages,\n * you do not need to supply this option.\n */\n readonly instructions?: string | string[] | ITemplate;\n\n /**\n * the `role` of the initial message\n */\n readonly role?: 'system' | 'user';\n\n /**\n * the conversation history\n */\n readonly messages?: Message[] | IMemory;\n\n /**\n * Logger instance to use for logging\n * If not provided, a ConsoleLogger will be used\n */\n logger?: ILogger;\n};\n\nexport type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {\n /**\n * the conversation history\n */\n readonly messages?: Message[] | IMemory;\n\n /**\n * the models request options\n */\n readonly request?: TOptions;\n\n /**\n * the callback to be called for each\n * stream chunk\n */\n readonly onChunk?: TextChunkHandler;\n};\n\n/**\n * a prompt that can interface with a\n * chat model that provides utility like\n * streaming and function calling\n */\nexport interface IChatPrompt<\n TOptions extends Record<string, any> = Record<string, any>,\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []\n> {\n /**\n * the prompt name\n */\n readonly name: string;\n\n /**\n * the prompt description\n */\n readonly description: string;\n\n /**\n * the chat history\n */\n readonly messages: IMemory;\n\n /**\n * the registered functions\n */\n readonly functions: Array<Function>;\n\n /**\n * the chat model\n */\n plugins: TChatPromptPlugins;\n /**\n * add another chat prompt as a\n */\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n\n /**\n * add a function that can be called\n * by the model\n */\n function(name: string, description: string, handler: FunctionHandler): this;\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\n\n usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(\n name: TPluginName,\n args: Extract<TChatPromptPlugins[number], { name: TPluginName }>['onUsePlugin'] extends\n | ((args: infer U) => void)\n | undefined\n ? U\n : never\n ): this;\n\n /**\n * call a function\n */\n call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;\n\n /**\n * send a message to the model and get a response\n */\n send(\n input: string | ContentPart[],\n options?: ChatPromptSendOptions<TOptions>\n ): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;\n}\n\nexport type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<\n TPluginName,\n TPluginUseArgs,\n Parameters<IChatPrompt['send']>[0],\n ReturnType<IChatPrompt['send']>\n> & {\n /**\n * Optionally passed in to modify the functions array that\n * is passed to the model\n * @param functions\n * @returns Functions\n */\n onBuildFunctions?: (functions: Function[]) => Function[] | Promise<Function[]>;\n};\n\n/**\n * a prompt that can interface with a\n * chat model that provides utility like\n * streaming and function calling\n */\nexport class ChatPrompt<\n TOptions extends Record<string, any> = Record<string, any>,\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\n> implements IChatPrompt<TOptions, TChatPromptPlugins> {\n get name() {\n return this._name;\n }\n protected readonly _name: string;\n\n get description() {\n return this._description;\n }\n protected readonly _description: string;\n\n get messages() {\n return this._messages;\n }\n protected readonly _messages: IMemory;\n\n get functions() {\n return Object.values(this._functions);\n }\n protected readonly _functions: Record<string, Function> = {};\n\n get plugins() {\n return this._plugins;\n }\n protected readonly _plugins: TChatPromptPlugins;\n\n protected readonly _role: 'system' | 'user';\n protected readonly _template: ITemplate;\n protected readonly _model: IChatModel<TOptions>;\n protected readonly _log: ILogger;\n\n constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins) {\n this._name = options.name || 'chat';\n this._description = options.description || 'an agent you can chat with';\n this._role = options.role || 'system';\n this._model = options.model;\n this._template = Array.isArray(options.instructions)\n ? new StringTemplate(options.instructions.join('\\n'))\n : typeof options.instructions !== 'object'\n ? new StringTemplate(options.instructions)\n : options.instructions;\n\n this._messages =\n typeof options.messages === 'object' && !Array.isArray(options.messages)\n ? options.messages\n : new LocalMemory({ messages: options.messages || [] });\n\n this._plugins = plugins || ([] as unknown as TChatPromptPlugins);\n this._log = options.logger || new ConsoleLogger(`@microsoft/teams.ai/prompts/${this._name}`);\n }\n\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n use(...args: any[]) {\n const prompt: IChatPrompt = args.length === 1 ? args[0] : args[1];\n const name: string = args.length === 1 ? prompt.name : args[0];\n this._functions[name] = {\n name,\n description: prompt.description,\n parameters: {\n type: 'object',\n properties: {\n text: {\n type: 'string',\n description: 'the text to send to the assistant',\n },\n },\n required: ['text'],\n },\n handler: ({ text }: { text: string }) => {\n return prompt.send(text);\n },\n };\n\n return this;\n }\n\n function(name: string, description: string, handler: FunctionHandler): this;\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\n function(...args: any[]) {\n const name: string = args[0];\n const description: string = args[1];\n const parameters: Schema | null = args.length === 3 ? null : args[2];\n const handler: FunctionHandler = args[args.length - 1];\n this._functions[name] = {\n name,\n description,\n parameters: parameters || {},\n handler,\n };\n\n return this;\n }\n\n usePlugin<K extends TChatPromptPlugins[number]['name']>(\n name: K,\n args: Extract<TChatPromptPlugins[number], { name: K }>['onUsePlugin'] extends\n | ((args: infer U) => void)\n | undefined\n ? U\n : never\n ): this {\n const plugin = this._plugins.find((p) => p.name === name);\n if (!plugin) {\n this._log.debug(`Plugin \"${name}\" not found`);\n throw new Error(`Plugin \"${name}\" not found`);\n }\n\n if (plugin.onUsePlugin) {\n this._log.debug(`Using plugin \"${name}\" with args:`, args);\n plugin.onUsePlugin(args);\n this._log.debug(`Successfully initialized plugin \"${name}\"`);\n }\n\n return this;\n }\n\n async call<A extends { [key: string]: any }, R = any>(name: string, args?: A): Promise<R> {\n const fn = this._functions[name];\n if (!fn) {\n throw new Error(`function \"${name}\" not found`);\n }\n return this.executeFunction(name, fn, args);\n }\n\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\n this._log.debug(`Processing plugins before send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onBeforeSend) {\n this._log.debug(`Running onBeforeSend for plugin \"${plugin.name}\"`);\n input = await plugin.onBeforeSend(input);\n }\n }\n\n const { onChunk } = options;\n\n if (typeof input === 'string') {\n input = input.trim();\n }\n\n const messages = !Array.isArray(options.messages)\n ? options.messages || this._messages\n : new LocalMemory({ messages: options.messages || [] });\n\n let buffer = '';\n let system: SystemMessage | UserMessage | undefined = undefined;\n const prompt = await this._template.render();\n\n if (prompt) {\n system = {\n role: this._role,\n content: prompt,\n };\n this._log.debug('System instructions for LLM:', prompt);\n }\n\n let functions = Object.values(this._functions);\n const pluginsWithOnBuildFunctions = this._plugins.filter(\n (plugin): plugin is WithRequired<TChatPromptPlugins[number], 'onBuildFunctions'> =>\n plugin.onBuildFunctions != null\n );\n for (const plugin of pluginsWithOnBuildFunctions) {\n functions = await plugin.onBuildFunctions(functions);\n }\n\n const fnMap = functions.reduce((acc, fn) => {\n acc[fn.name] = {\n ...fn,\n handler: (args: any) => this.executeFunction(fn.name, fn, args),\n };\n return acc;\n }, {} as Record<string, Function>);\n\n if (Object.keys(fnMap).length > 0) {\n this._log.debug(\n 'Available functions for LLM:',\n Object.keys(fnMap).map((name) => {\n const fn = fnMap[name];\n const paramDescriptions =\n 'properties' in fn.parameters && fn.parameters.properties\n ? Object.entries(\n fn.parameters.properties as Record<string, { description?: string }>\n ).reduce(\n (acc, [key, prop]) => ({\n ...acc,\n [key]: prop.description,\n }),\n {} as Record<string, string | undefined>\n )\n : {};\n\n return {\n name,\n description: fn.description,\n parameters: {\n schema: fn.parameters,\n descriptions: paramDescriptions,\n },\n };\n })\n );\n }\n\n const res = await this._model.send(\n {\n role: 'user',\n content: input,\n },\n {\n system,\n messages,\n request: options.request,\n functions: fnMap,\n onChunk: async (chunk) => {\n if (!chunk || !onChunk) return;\n buffer += chunk;\n\n try {\n await onChunk(buffer);\n buffer = '';\n } catch (err) {\n return;\n }\n },\n }\n );\n\n let output: Awaited<ReturnType<typeof this._model.send>> = {\n ...res,\n content: res.content || '',\n };\n\n // Log function calls if present\n if (output.function_calls && output.function_calls.length > 0) {\n this._log.debug(\n 'LLM requested function calls:',\n output.function_calls.map((call) => ({\n name: call.name,\n id: call.id,\n arguments: call.arguments,\n }))\n );\n }\n\n this._log.debug(`Processing plugins after send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onAfterSend) {\n this._log.debug(`Running onAfterSend for plugin \"${plugin.name}\"`);\n output = await plugin.onAfterSend(output);\n }\n }\n\n return output;\n }\n\n protected async executeFunction<R = any>(\n name: string,\n fn: Function,\n args?: Record<string, any>\n ): Promise<R> {\n const processedArgs = args || {};\n\n // Execute beforeFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onBeforeFunctionCall) {\n await plugin.onBeforeFunctionCall(name, processedArgs);\n }\n }\n\n // Call the function\n let result = await fn.handler(processedArgs);\n\n // Execute afterFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onAfterFunctionCall) {\n result = await plugin.onAfterFunctionCall(name, processedArgs, result);\n }\n }\n\n return result;\n }\n}\n"]}
|
package/dist/prompts/chat.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ConsoleLogger } from '@microsoft/teams.common';
|
|
1
2
|
import { LocalMemory } from '../local-memory';
|
|
2
3
|
import { StringTemplate } from '../templates';
|
|
3
4
|
|
|
@@ -25,6 +26,7 @@ class ChatPrompt {
|
|
|
25
26
|
_role;
|
|
26
27
|
_template;
|
|
27
28
|
_model;
|
|
29
|
+
_log;
|
|
28
30
|
constructor(options, plugins) {
|
|
29
31
|
this._name = options.name || "chat";
|
|
30
32
|
this._description = options.description || "an agent you can chat with";
|
|
@@ -33,6 +35,7 @@ class ChatPrompt {
|
|
|
33
35
|
this._template = Array.isArray(options.instructions) ? new StringTemplate(options.instructions.join("\n")) : typeof options.instructions !== "object" ? new StringTemplate(options.instructions) : options.instructions;
|
|
34
36
|
this._messages = typeof options.messages === "object" && !Array.isArray(options.messages) ? options.messages : new LocalMemory({ messages: options.messages || [] });
|
|
35
37
|
this._plugins = plugins || [];
|
|
38
|
+
this._log = options.logger || new ConsoleLogger(`@microsoft/teams.ai/prompts/${this._name}`);
|
|
36
39
|
}
|
|
37
40
|
use(...args) {
|
|
38
41
|
const prompt = args.length === 1 ? args[0] : args[1];
|
|
@@ -72,10 +75,13 @@ class ChatPrompt {
|
|
|
72
75
|
usePlugin(name, args) {
|
|
73
76
|
const plugin = this._plugins.find((p) => p.name === name);
|
|
74
77
|
if (!plugin) {
|
|
78
|
+
this._log.debug(`Plugin "${name}" not found`);
|
|
75
79
|
throw new Error(`Plugin "${name}" not found`);
|
|
76
80
|
}
|
|
77
81
|
if (plugin.onUsePlugin) {
|
|
82
|
+
this._log.debug(`Using plugin "${name}" with args:`, args);
|
|
78
83
|
plugin.onUsePlugin(args);
|
|
84
|
+
this._log.debug(`Successfully initialized plugin "${name}"`);
|
|
79
85
|
}
|
|
80
86
|
return this;
|
|
81
87
|
}
|
|
@@ -84,11 +90,13 @@ class ChatPrompt {
|
|
|
84
90
|
if (!fn) {
|
|
85
91
|
throw new Error(`function "${name}" not found`);
|
|
86
92
|
}
|
|
87
|
-
return
|
|
93
|
+
return this.executeFunction(name, fn, args);
|
|
88
94
|
}
|
|
89
95
|
async send(input, options = {}) {
|
|
96
|
+
this._log.debug(`Processing plugins before send (${this.plugins.length} plugins found)`);
|
|
90
97
|
for (const plugin of this.plugins) {
|
|
91
98
|
if (plugin.onBeforeSend) {
|
|
99
|
+
this._log.debug(`Running onBeforeSend for plugin "${plugin.name}"`);
|
|
92
100
|
input = await plugin.onBeforeSend(input);
|
|
93
101
|
}
|
|
94
102
|
}
|
|
@@ -105,6 +113,7 @@ class ChatPrompt {
|
|
|
105
113
|
role: this._role,
|
|
106
114
|
content: prompt
|
|
107
115
|
};
|
|
116
|
+
this._log.debug("System instructions for LLM:", prompt);
|
|
108
117
|
}
|
|
109
118
|
let functions = Object.values(this._functions);
|
|
110
119
|
const pluginsWithOnBuildFunctions = this._plugins.filter(
|
|
@@ -113,13 +122,38 @@ class ChatPrompt {
|
|
|
113
122
|
for (const plugin of pluginsWithOnBuildFunctions) {
|
|
114
123
|
functions = await plugin.onBuildFunctions(functions);
|
|
115
124
|
}
|
|
116
|
-
const fnMap = functions.reduce(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
);
|
|
125
|
+
const fnMap = functions.reduce((acc, fn) => {
|
|
126
|
+
acc[fn.name] = {
|
|
127
|
+
...fn,
|
|
128
|
+
handler: (args) => this.executeFunction(fn.name, fn, args)
|
|
129
|
+
};
|
|
130
|
+
return acc;
|
|
131
|
+
}, {});
|
|
132
|
+
if (Object.keys(fnMap).length > 0) {
|
|
133
|
+
this._log.debug(
|
|
134
|
+
"Available functions for LLM:",
|
|
135
|
+
Object.keys(fnMap).map((name) => {
|
|
136
|
+
const fn = fnMap[name];
|
|
137
|
+
const paramDescriptions = "properties" in fn.parameters && fn.parameters.properties ? Object.entries(
|
|
138
|
+
fn.parameters.properties
|
|
139
|
+
).reduce(
|
|
140
|
+
(acc, [key, prop]) => ({
|
|
141
|
+
...acc,
|
|
142
|
+
[key]: prop.description
|
|
143
|
+
}),
|
|
144
|
+
{}
|
|
145
|
+
) : {};
|
|
146
|
+
return {
|
|
147
|
+
name,
|
|
148
|
+
description: fn.description,
|
|
149
|
+
parameters: {
|
|
150
|
+
schema: fn.parameters,
|
|
151
|
+
descriptions: paramDescriptions
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
})
|
|
155
|
+
);
|
|
156
|
+
}
|
|
123
157
|
const res = await this._model.send(
|
|
124
158
|
{
|
|
125
159
|
role: "user",
|
|
@@ -146,13 +180,40 @@ class ChatPrompt {
|
|
|
146
180
|
...res,
|
|
147
181
|
content: res.content || ""
|
|
148
182
|
};
|
|
183
|
+
if (output.function_calls && output.function_calls.length > 0) {
|
|
184
|
+
this._log.debug(
|
|
185
|
+
"LLM requested function calls:",
|
|
186
|
+
output.function_calls.map((call) => ({
|
|
187
|
+
name: call.name,
|
|
188
|
+
id: call.id,
|
|
189
|
+
arguments: call.arguments
|
|
190
|
+
}))
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
this._log.debug(`Processing plugins after send (${this.plugins.length} plugins found)`);
|
|
149
194
|
for (const plugin of this.plugins) {
|
|
150
195
|
if (plugin.onAfterSend) {
|
|
196
|
+
this._log.debug(`Running onAfterSend for plugin "${plugin.name}"`);
|
|
151
197
|
output = await plugin.onAfterSend(output);
|
|
152
198
|
}
|
|
153
199
|
}
|
|
154
200
|
return output;
|
|
155
201
|
}
|
|
202
|
+
async executeFunction(name, fn, args) {
|
|
203
|
+
const processedArgs = args || {};
|
|
204
|
+
for (const plugin of this.plugins) {
|
|
205
|
+
if (plugin.onBeforeFunctionCall) {
|
|
206
|
+
await plugin.onBeforeFunctionCall(name, processedArgs);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
let result = await fn.handler(processedArgs);
|
|
210
|
+
for (const plugin of this.plugins) {
|
|
211
|
+
if (plugin.onAfterFunctionCall) {
|
|
212
|
+
result = await plugin.onAfterFunctionCall(name, processedArgs, result);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
156
217
|
}
|
|
157
218
|
|
|
158
219
|
export { ChatPrompt };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/prompts/chat.ts"],"names":[],"mappings":";;;AA2JO,MAAM,UAIb,CAAA;AAAA,EACE,IAAI,IAAO,GAAA;AACT,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd,EACmB,KAAA;AAAA,EAEnB,IAAI,WAAc,GAAA;AAChB,IAAA,OAAO,IAAK,CAAA,YAAA;AAAA;AACd,EACmB,YAAA;AAAA,EAEnB,IAAI,QAAW,GAAA;AACb,IAAA,OAAO,IAAK,CAAA,SAAA;AAAA;AACd,EACmB,SAAA;AAAA,EAEnB,IAAI,SAAY,GAAA;AACd,IAAO,OAAA,MAAA,CAAO,MAAO,CAAA,IAAA,CAAK,UAAU,CAAA;AAAA;AACtC,EACmB,aAAuC,EAAC;AAAA,EAE3D,IAAI,OAAU,GAAA;AACZ,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AACd,EACmB,QAAA;AAAA,EAEA,KAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EAEnB,WAAA,CAAY,SAAsC,OAA8B,EAAA;AAC9E,IAAK,IAAA,CAAA,KAAA,GAAQ,QAAQ,IAAQ,IAAA,MAAA;AAC7B,IAAK,IAAA,CAAA,YAAA,GAAe,QAAQ,WAAe,IAAA,4BAAA;AAC3C,IAAK,IAAA,CAAA,KAAA,GAAQ,QAAQ,IAAQ,IAAA,QAAA;AAC7B,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,KAAA;AACtB,IAAK,IAAA,CAAA,SAAA,GAAY,MAAM,OAAQ,CAAA,OAAA,CAAQ,YAAY,CAC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AAA,OACT;AAAA,MACA;AAAC,KACH;AAEA,IAAM,MAAA,GAAA,GAAM,MAAM,IAAA,CAAK,MAAO,CAAA,IAAA;AAAA,MAC5B;AAAA,QACE,IAAM,EAAA,MAAA;AAAA,QACN,OAAS,EAAA;AAAA,OACX;AAAA,MACA;AAAA,QACE,MAAA;AAAA,QACA,QAAA;AAAA,QACA,SAAS,OAAQ,CAAA,OAAA;AAAA,QACjB,SAAW,EAAA,KAAA;AAAA,QACX,OAAA,EAAS,OAAO,KAAU,KAAA;AACxB,UAAI,IAAA,CAAC,KAAS,IAAA,CAAC,OAAS,EAAA;AACxB,UAAU,MAAA,IAAA,KAAA;AAEV,UAAI,IAAA;AACF,YAAA,MAAM,QAAQ,MAAM,CAAA;AACpB,YAAS,MAAA,GAAA,EAAA;AAAA,mBACF,GAAK,EAAA;AACZ,YAAA;AAAA;AACF;AACF;AACF,KACF;AAEA,IAAA,IAAI,MAAuD,GAAA;AAAA,MACzD,GAAG,GAAA;AAAA,MACH,OAAA,EAAS,IAAI,OAAW,IAAA;AAAA,KAC1B;AACA,IAAW,KAAA,MAAA,MAAA,IAAU,KAAK,OAAS,EAAA;AACjC,MAAA,IAAI,OAAO,WAAa,EAAA;AACtB,QAAS,MAAA,GAAA,MAAM,MAAO,CAAA,WAAA,CAAY,MAAM,CAAA;AAAA;AAC1C;AAGF,IAAO,OAAA,MAAA;AAAA;AAEX","file":"chat.mjs","sourcesContent":["import { Function, FunctionHandler } from '../function';\r\nimport { LocalMemory } from '../local-memory';\r\nimport { IMemory } from '../memory';\r\nimport { ContentPart, Message, ModelMessage, SystemMessage, UserMessage } from '../message';\r\nimport { IChatModel, TextChunkHandler } from '../models';\r\nimport { Schema } from '../schema';\r\nimport { ITemplate } from '../template';\r\nimport { StringTemplate } from '../templates';\r\nimport { WithRequired } from '../utils/types';\r\n\r\nimport { IAiPlugin } from './plugin';\r\n\r\nexport type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {\r\n /**\r\n * the name of the prompt\r\n */\r\n readonly name?: string;\r\n\r\n /**\r\n * the description of the prompt\r\n */\r\n readonly description?: string;\r\n\r\n /**\r\n * the model to send messages to\r\n */\r\n readonly model: IChatModel<TOptions>;\r\n\r\n /**\r\n * the defining characteristics/objective\r\n * of the prompt. This is commonly used to provide a system prompt.\r\n * If you supply the system prompt as part of the messages,\r\n * you do not need to supply this option.\r\n */\r\n readonly instructions?: string | string[] | ITemplate;\r\n\r\n /**\r\n * the `role` of the initial message\r\n */\r\n readonly role?: 'system' | 'user';\r\n\r\n /**\r\n * the conversation history\r\n */\r\n readonly messages?: Message[] | IMemory;\r\n};\r\n\r\nexport type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {\r\n /**\r\n * the conversation history\r\n */\r\n readonly messages?: Message[] | IMemory;\r\n\r\n /**\r\n * the models request options\r\n */\r\n readonly request?: TOptions;\r\n\r\n /**\r\n * the callback to be called for each\r\n * stream chunk\r\n */\r\n readonly onChunk?: TextChunkHandler;\r\n};\r\n\r\n/**\r\n * a prompt that can interface with a\r\n * chat model that provides utility like\r\n * streaming and function calling\r\n */\r\nexport interface IChatPrompt<\r\n TOptions extends Record<string, any> = Record<string, any>,\r\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\r\n> {\r\n /**\r\n * the prompt name\r\n */\r\n readonly name: string;\r\n\r\n /**\r\n * the prompt description\r\n */\r\n readonly description: string;\r\n\r\n /**\r\n * the chat history\r\n */\r\n readonly messages: IMemory;\r\n\r\n /**\r\n * the registered functions\r\n */\r\n readonly functions: Array<Function>;\r\n\r\n /**\r\n * the chat model\r\n */\r\n plugins: TChatPromptPlugins;\r\n\r\n /**\r\n * add another chat prompt as a\r\n */\r\n use(prompt: IChatPrompt): this;\r\n use(name: string, prompt: IChatPrompt): this;\r\n\r\n /**\r\n * add a function that can be called\r\n * by the model\r\n */\r\n function(name: string, description: string, handler: FunctionHandler): this;\r\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\r\n\r\n usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(\r\n name: TPluginName,\r\n args: Extract<TChatPromptPlugins[number], { name: TPluginName }>['onUsePlugin'] extends\r\n | ((args: infer U) => void)\r\n | undefined\r\n ? U\r\n : never\r\n ): this;\r\n\r\n /**\r\n * call a function\r\n */\r\n call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;\r\n\r\n /**\r\n * send a message to the model and get a response\r\n */\r\n send(\r\n input: string | ContentPart[],\r\n options?: ChatPromptSendOptions<TOptions>\r\n ): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;\r\n}\r\n\r\nexport type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<\r\n TPluginName,\r\n TPluginUseArgs,\r\n Parameters<IChatPrompt['send']>[0],\r\n ReturnType<IChatPrompt['send']>\r\n> & {\r\n /**\r\n * Optionally passed in to modify the functions array that\r\n * is passed to the model\r\n * @param functions\r\n * @returns Functions\r\n */\r\n onBuildFunctions?: (functions: Function[]) => Function[] | Promise<Function[]>;\r\n};\r\n\r\n/**\r\n * a prompt that can interface with a\r\n * chat model that provides utility like\r\n * streaming and function calling\r\n */\r\nexport class ChatPrompt<\r\n TOptions extends Record<string, any> = Record<string, any>,\r\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\r\n> implements IChatPrompt<TOptions, TChatPromptPlugins>\r\n{\r\n get name() {\r\n return this._name;\r\n }\r\n protected readonly _name: string;\r\n\r\n get description() {\r\n return this._description;\r\n }\r\n protected readonly _description: string;\r\n\r\n get messages() {\r\n return this._messages;\r\n }\r\n protected readonly _messages: IMemory;\r\n\r\n get functions() {\r\n return Object.values(this._functions);\r\n }\r\n protected readonly _functions: Record<string, Function> = {};\r\n\r\n get plugins() {\r\n return this._plugins;\r\n }\r\n protected readonly _plugins: TChatPromptPlugins;\r\n\r\n protected readonly _role: 'system' | 'user';\r\n protected readonly _template: ITemplate;\r\n protected readonly _model: IChatModel<TOptions>;\r\n\r\n constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins) {\r\n this._name = options.name || 'chat';\r\n this._description = options.description || 'an agent you can chat with';\r\n this._role = options.role || 'system';\r\n this._model = options.model;\r\n this._template = Array.isArray(options.instructions)\r\n ? new StringTemplate(options.instructions.join('\\n'))\r\n : typeof options.instructions !== 'object'\r\n ? new StringTemplate(options.instructions)\r\n : options.instructions;\r\n\r\n this._messages =\r\n typeof options.messages === 'object' && !Array.isArray(options.messages)\r\n ? options.messages\r\n : new LocalMemory({ messages: options.messages || [] });\r\n\r\n this._plugins = plugins || ([] as unknown as TChatPromptPlugins);\r\n }\r\n\r\n use(prompt: ChatPrompt): this;\r\n use(name: string, prompt: ChatPrompt): this;\r\n use(...args: any[]) {\r\n const prompt: ChatPrompt = args.length === 1 ? args[0] : args[1];\r\n const name: string = args.length === 1 ? prompt.name : args[0];\r\n this._functions[name] = {\r\n name,\r\n description: prompt.description,\r\n parameters: {\r\n type: 'object',\r\n properties: {\r\n text: {\r\n type: 'string',\r\n description: 'the text to send to the assistant',\r\n },\r\n },\r\n required: ['text'],\r\n },\r\n handler: ({ text }: { text: string }) => {\r\n return prompt.send(text);\r\n },\r\n };\r\n\r\n return this;\r\n }\r\n\r\n function(name: string, description: string, handler: FunctionHandler): this;\r\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\r\n function(...args: any[]) {\r\n const name: string = args[0];\r\n const description: string = args[1];\r\n const parameters: Schema | null = args.length === 3 ? null : args[2];\r\n const handler: FunctionHandler = args[args.length - 1];\r\n this._functions[name] = {\r\n name,\r\n description,\r\n parameters: parameters || {},\r\n handler,\r\n };\r\n\r\n return this;\r\n }\r\n\r\n usePlugin<K extends TChatPromptPlugins[number]['name']>(\r\n name: K,\r\n args: Extract<TChatPromptPlugins[number], { name: K }>['onUsePlugin'] extends\r\n | ((args: infer U) => void)\r\n | undefined\r\n ? U\r\n : never\r\n ): this {\r\n const plugin = this._plugins.find((p) => p.name === name);\r\n if (!plugin) {\r\n throw new Error(`Plugin \"${name}\" not found`);\r\n }\r\n\r\n if (plugin.onUsePlugin) {\r\n plugin.onUsePlugin(args);\r\n }\r\n\r\n return this;\r\n }\r\n\r\n async call<A extends { [key: string]: any }, R = any>(name: string, args?: A): Promise<R> {\r\n const fn = this._functions[name];\r\n\r\n if (!fn) {\r\n throw new Error(`function \"${name}\" not found`);\r\n }\r\n\r\n return await fn.handler(args || {});\r\n }\r\n\r\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\r\n for (const plugin of this.plugins) {\r\n if (plugin.onBeforeSend) {\r\n input = await plugin.onBeforeSend(input);\r\n }\r\n }\r\n\r\n const { onChunk } = options;\r\n\r\n if (typeof input === 'string') {\r\n input = input.trim();\r\n }\r\n\r\n const messages = !Array.isArray(options.messages)\r\n ? options.messages || this._messages\r\n : new LocalMemory({ messages: options.messages || [] });\r\n\r\n let buffer = '';\r\n let system: SystemMessage | UserMessage | undefined = undefined;\r\n const prompt = await this._template.render();\r\n\r\n if (prompt) {\r\n system = {\r\n role: this._role,\r\n content: prompt,\r\n };\r\n }\r\n\r\n let functions = Object.values(this._functions);\r\n const pluginsWithOnBuildFunctions = this._plugins.filter(\r\n (plugin): plugin is WithRequired<TChatPromptPlugins[number], 'onBuildFunctions'> =>\r\n plugin.onBuildFunctions != null\r\n );\r\n for (const plugin of pluginsWithOnBuildFunctions) {\r\n functions = await plugin.onBuildFunctions(functions);\r\n }\r\n\r\n const fnMap = functions.reduce(\r\n (acc, fn) => {\r\n acc[fn.name] = fn;\r\n return acc;\r\n },\r\n {} as Record<string, Function>\r\n );\r\n\r\n const res = await this._model.send(\r\n {\r\n role: 'user',\r\n content: input,\r\n },\r\n {\r\n system,\r\n messages,\r\n request: options.request,\r\n functions: fnMap,\r\n onChunk: async (chunk) => {\r\n if (!chunk || !onChunk) return;\r\n buffer += chunk;\r\n\r\n try {\r\n await onChunk(buffer);\r\n buffer = '';\r\n } catch (err) {\r\n return;\r\n }\r\n },\r\n }\r\n );\r\n\r\n let output: Awaited<ReturnType<typeof this._model.send>> = {\r\n ...res,\r\n content: res.content || '',\r\n };\r\n for (const plugin of this.plugins) {\r\n if (plugin.onAfterSend) {\r\n output = await plugin.onAfterSend(output);\r\n }\r\n }\r\n\r\n return output;\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/prompts/chat.ts"],"names":[],"mappings":";;;;AAkKO,MAAM,UAG0C,CAAA;AAAA,EACrD,IAAI,IAAO,GAAA;AACT,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd,EACmB,KAAA;AAAA,EAEnB,IAAI,WAAc,GAAA;AAChB,IAAA,OAAO,IAAK,CAAA,YAAA;AAAA;AACd,EACmB,YAAA;AAAA,EAEnB,IAAI,QAAW,GAAA;AACb,IAAA,OAAO,IAAK,CAAA,SAAA;AAAA;AACd,EACmB,SAAA;AAAA,EAEnB,IAAI,SAAY,GAAA;AACd,IAAO,OAAA,MAAA,CAAO,MAAO,CAAA,IAAA,CAAK,UAAU,CAAA;AAAA;AACtC,EACmB,aAAuC,EAAC;AAAA,EAE3D,IAAI,OAAU,GAAA;AACZ,IAAA,OAAO,IAAK,CAAA,QAAA;AAAA;AACd,EACmB,QAAA;AAAA,EAEA,KAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EACA,IAAA;AAAA,EAEnB,WAAA,CAAY,SAAsC,OAA8B,EAAA;AAC9E,IAAK,IAAA,CAAA,KAAA,GAAQ,QAAQ,IAAQ,IAAA,MAAA;AAC7B,IAAK,IAAA,CAAA,YAAA,GAAe,QAAQ,WAAe,IAAA,4BAAA;AAC3C,IAAK,IAAA,CAAA,KAAA,GAAQ,QAAQ,IAAQ,IAAA,QAAA;AAC7B,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,KAAA;AACtB,IAAK,IAAA,CAAA,SAAA,GAAY,MAAM,OAAQ,CAAA,OAAA,CAAQ,YAAY,CAC/C,GAAA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ger, ILogger } from '@microsoft/teams.common';\n\nimport { Function, FunctionHandler } from '../function';\nimport { LocalMemory } from '../local-memory';\nimport { IMemory } from '../memory';\nimport { ContentPart, Message, ModelMessage, SystemMessage, UserMessage } from '../message';\nimport { IChatModel, TextChunkHandler } from '../models';\nimport { Schema } from '../schema';\nimport { ITemplate } from '../template';\nimport { StringTemplate } from '../templates';\nimport { WithRequired } from '../utils/types';\n\nimport { IAiPlugin } from './plugin';\n\nexport type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {\n /**\n * the name of the prompt\n */\n readonly name?: string;\n\n /**\n * the description of the prompt\n */\n readonly description?: string;\n\n /**\n * the model to send messages to\n */\n readonly model: IChatModel<TOptions>;\n\n /**\n * the defining characteristics/objective\n * of the prompt. This is commonly used to provide a system prompt.\n * If you supply the system prompt as part of the messages,\n * you do not need to supply this option.\n */\n readonly instructions?: string | string[] | ITemplate;\n\n /**\n * the `role` of the initial message\n */\n readonly role?: 'system' | 'user';\n\n /**\n * the conversation history\n */\n readonly messages?: Message[] | IMemory;\n\n /**\n * Logger instance to use for logging\n * If not provided, a ConsoleLogger will be used\n */\n logger?: ILogger;\n};\n\nexport type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {\n /**\n * the conversation history\n */\n readonly messages?: Message[] | IMemory;\n\n /**\n * the models request options\n */\n readonly request?: TOptions;\n\n /**\n * the callback to be called for each\n * stream chunk\n */\n readonly onChunk?: TextChunkHandler;\n};\n\n/**\n * a prompt that can interface with a\n * chat model that provides utility like\n * streaming and function calling\n */\nexport interface IChatPrompt<\n TOptions extends Record<string, any> = Record<string, any>,\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []\n> {\n /**\n * the prompt name\n */\n readonly name: string;\n\n /**\n * the prompt description\n */\n readonly description: string;\n\n /**\n * the chat history\n */\n readonly messages: IMemory;\n\n /**\n * the registered functions\n */\n readonly functions: Array<Function>;\n\n /**\n * the chat model\n */\n plugins: TChatPromptPlugins;\n /**\n * add another chat prompt as a\n */\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n\n /**\n * add a function that can be called\n * by the model\n */\n function(name: string, description: string, handler: FunctionHandler): this;\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\n\n usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(\n name: TPluginName,\n args: Extract<TChatPromptPlugins[number], { name: TPluginName }>['onUsePlugin'] extends\n | ((args: infer U) => void)\n | undefined\n ? U\n : never\n ): this;\n\n /**\n * call a function\n */\n call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;\n\n /**\n * send a message to the model and get a response\n */\n send(\n input: string | ContentPart[],\n options?: ChatPromptSendOptions<TOptions>\n ): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;\n}\n\nexport type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<\n TPluginName,\n TPluginUseArgs,\n Parameters<IChatPrompt['send']>[0],\n ReturnType<IChatPrompt['send']>\n> & {\n /**\n * Optionally passed in to modify the functions array that\n * is passed to the model\n * @param functions\n * @returns Functions\n */\n onBuildFunctions?: (functions: Function[]) => Function[] | Promise<Function[]>;\n};\n\n/**\n * a prompt that can interface with a\n * chat model that provides utility like\n * streaming and function calling\n */\nexport class ChatPrompt<\n TOptions extends Record<string, any> = Record<string, any>,\n TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = [],\n> implements IChatPrompt<TOptions, TChatPromptPlugins> {\n get name() {\n return this._name;\n }\n protected readonly _name: string;\n\n get description() {\n return this._description;\n }\n protected readonly _description: string;\n\n get messages() {\n return this._messages;\n }\n protected readonly _messages: IMemory;\n\n get functions() {\n return Object.values(this._functions);\n }\n protected readonly _functions: Record<string, Function> = {};\n\n get plugins() {\n return this._plugins;\n }\n protected readonly _plugins: TChatPromptPlugins;\n\n protected readonly _role: 'system' | 'user';\n protected readonly _template: ITemplate;\n protected readonly _model: IChatModel<TOptions>;\n protected readonly _log: ILogger;\n\n constructor(options: ChatPromptOptions<TOptions>, plugins?: TChatPromptPlugins) {\n this._name = options.name || 'chat';\n this._description = options.description || 'an agent you can chat with';\n this._role = options.role || 'system';\n this._model = options.model;\n this._template = Array.isArray(options.instructions)\n ? new StringTemplate(options.instructions.join('\\n'))\n : typeof options.instructions !== 'object'\n ? new StringTemplate(options.instructions)\n : options.instructions;\n\n this._messages =\n typeof options.messages === 'object' && !Array.isArray(options.messages)\n ? options.messages\n : new LocalMemory({ messages: options.messages || [] });\n\n this._plugins = plugins || ([] as unknown as TChatPromptPlugins);\n this._log = options.logger || new ConsoleLogger(`@microsoft/teams.ai/prompts/${this._name}`);\n }\n\n use(prompt: IChatPrompt): this;\n use(name: string, prompt: IChatPrompt): this;\n use(...args: any[]) {\n const prompt: IChatPrompt = args.length === 1 ? args[0] : args[1];\n const name: string = args.length === 1 ? prompt.name : args[0];\n this._functions[name] = {\n name,\n description: prompt.description,\n parameters: {\n type: 'object',\n properties: {\n text: {\n type: 'string',\n description: 'the text to send to the assistant',\n },\n },\n required: ['text'],\n },\n handler: ({ text }: { text: string }) => {\n return prompt.send(text);\n },\n };\n\n return this;\n }\n\n function(name: string, description: string, handler: FunctionHandler): this;\n function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;\n function(...args: any[]) {\n const name: string = args[0];\n const description: string = args[1];\n const parameters: Schema | null = args.length === 3 ? null : args[2];\n const handler: FunctionHandler = args[args.length - 1];\n this._functions[name] = {\n name,\n description,\n parameters: parameters || {},\n handler,\n };\n\n return this;\n }\n\n usePlugin<K extends TChatPromptPlugins[number]['name']>(\n name: K,\n args: Extract<TChatPromptPlugins[number], { name: K }>['onUsePlugin'] extends\n | ((args: infer U) => void)\n | undefined\n ? U\n : never\n ): this {\n const plugin = this._plugins.find((p) => p.name === name);\n if (!plugin) {\n this._log.debug(`Plugin \"${name}\" not found`);\n throw new Error(`Plugin \"${name}\" not found`);\n }\n\n if (plugin.onUsePlugin) {\n this._log.debug(`Using plugin \"${name}\" with args:`, args);\n plugin.onUsePlugin(args);\n this._log.debug(`Successfully initialized plugin \"${name}\"`);\n }\n\n return this;\n }\n\n async call<A extends { [key: string]: any }, R = any>(name: string, args?: A): Promise<R> {\n const fn = this._functions[name];\n if (!fn) {\n throw new Error(`function \"${name}\" not found`);\n }\n return this.executeFunction(name, fn, args);\n }\n\n async send(input: string | ContentPart[], options: ChatPromptSendOptions<TOptions> = {}) {\n this._log.debug(`Processing plugins before send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onBeforeSend) {\n this._log.debug(`Running onBeforeSend for plugin \"${plugin.name}\"`);\n input = await plugin.onBeforeSend(input);\n }\n }\n\n const { onChunk } = options;\n\n if (typeof input === 'string') {\n input = input.trim();\n }\n\n const messages = !Array.isArray(options.messages)\n ? options.messages || this._messages\n : new LocalMemory({ messages: options.messages || [] });\n\n let buffer = '';\n let system: SystemMessage | UserMessage | undefined = undefined;\n const prompt = await this._template.render();\n\n if (prompt) {\n system = {\n role: this._role,\n content: prompt,\n };\n this._log.debug('System instructions for LLM:', prompt);\n }\n\n let functions = Object.values(this._functions);\n const pluginsWithOnBuildFunctions = this._plugins.filter(\n (plugin): plugin is WithRequired<TChatPromptPlugins[number], 'onBuildFunctions'> =>\n plugin.onBuildFunctions != null\n );\n for (const plugin of pluginsWithOnBuildFunctions) {\n functions = await plugin.onBuildFunctions(functions);\n }\n\n const fnMap = functions.reduce((acc, fn) => {\n acc[fn.name] = {\n ...fn,\n handler: (args: any) => this.executeFunction(fn.name, fn, args),\n };\n return acc;\n }, {} as Record<string, Function>);\n\n if (Object.keys(fnMap).length > 0) {\n this._log.debug(\n 'Available functions for LLM:',\n Object.keys(fnMap).map((name) => {\n const fn = fnMap[name];\n const paramDescriptions =\n 'properties' in fn.parameters && fn.parameters.properties\n ? Object.entries(\n fn.parameters.properties as Record<string, { description?: string }>\n ).reduce(\n (acc, [key, prop]) => ({\n ...acc,\n [key]: prop.description,\n }),\n {} as Record<string, string | undefined>\n )\n : {};\n\n return {\n name,\n description: fn.description,\n parameters: {\n schema: fn.parameters,\n descriptions: paramDescriptions,\n },\n };\n })\n );\n }\n\n const res = await this._model.send(\n {\n role: 'user',\n content: input,\n },\n {\n system,\n messages,\n request: options.request,\n functions: fnMap,\n onChunk: async (chunk) => {\n if (!chunk || !onChunk) return;\n buffer += chunk;\n\n try {\n await onChunk(buffer);\n buffer = '';\n } catch (err) {\n return;\n }\n },\n }\n );\n\n let output: Awaited<ReturnType<typeof this._model.send>> = {\n ...res,\n content: res.content || '',\n };\n\n // Log function calls if present\n if (output.function_calls && output.function_calls.length > 0) {\n this._log.debug(\n 'LLM requested function calls:',\n output.function_calls.map((call) => ({\n name: call.name,\n id: call.id,\n arguments: call.arguments,\n }))\n );\n }\n\n this._log.debug(`Processing plugins after send (${this.plugins.length} plugins found)`);\n for (const plugin of this.plugins) {\n if (plugin.onAfterSend) {\n this._log.debug(`Running onAfterSend for plugin \"${plugin.name}\"`);\n output = await plugin.onAfterSend(output);\n }\n }\n\n return output;\n }\n\n protected async executeFunction<R = any>(\n name: string,\n fn: Function,\n args?: Record<string, any>\n ): Promise<R> {\n const processedArgs = args || {};\n\n // Execute beforeFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onBeforeFunctionCall) {\n await plugin.onBeforeFunctionCall(name, processedArgs);\n }\n }\n\n // Call the function\n let result = await fn.handler(processedArgs);\n\n // Execute afterFunctionCall hooks\n for (const plugin of this.plugins) {\n if (plugin.onAfterFunctionCall) {\n result = await plugin.onAfterFunctionCall(name, processedArgs, result);\n }\n }\n\n return result;\n }\n}\n"]}
|
package/dist/prompts/index.d.mts
CHANGED
|
@@ -3,10 +3,10 @@ export { AudioPrompt, AudioPromptOptions } from './audio.mjs';
|
|
|
3
3
|
import { IChatPrompt } from './chat.mjs';
|
|
4
4
|
export { ChatPrompt, ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions } from './chat.mjs';
|
|
5
5
|
import '../models/audio.mjs';
|
|
6
|
+
import '@microsoft/teams.common';
|
|
6
7
|
import '../function.mjs';
|
|
7
8
|
import '../schema.mjs';
|
|
8
9
|
import '../memory.mjs';
|
|
9
|
-
import '@microsoft/teams.common';
|
|
10
10
|
import '../message.mjs';
|
|
11
11
|
import '../citation.mjs';
|
|
12
12
|
import '../models/chat.mjs';
|
package/dist/prompts/index.d.ts
CHANGED
|
@@ -3,10 +3,10 @@ export { AudioPrompt, AudioPromptOptions } from './audio.js';
|
|
|
3
3
|
import { IChatPrompt } from './chat.js';
|
|
4
4
|
export { ChatPrompt, ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions } from './chat.js';
|
|
5
5
|
import '../models/audio.js';
|
|
6
|
+
import '@microsoft/teams.common';
|
|
6
7
|
import '../function.js';
|
|
7
8
|
import '../schema.js';
|
|
8
9
|
import '../memory.js';
|
|
9
|
-
import '@microsoft/teams.common';
|
|
10
10
|
import '../message.js';
|
|
11
11
|
import '../citation.js';
|
|
12
12
|
import '../models/chat.js';
|
|
@@ -21,6 +21,20 @@ interface IAiPlugin<TPluginName extends string = string, TPluginUseArgs extends
|
|
|
21
21
|
* @returns the modified output result of the send function
|
|
22
22
|
*/
|
|
23
23
|
onAfterSend?: (response: Awaited<TAfterSendResponse>) => Awaited<TAfterSendResponse> | Promise<Awaited<TAfterSendResponse>>;
|
|
24
|
+
/**
|
|
25
|
+
* Called before a function is executed, allowing modification of the arguments
|
|
26
|
+
* @param functionName the name of the function being called
|
|
27
|
+
* @param args the arguments being passed to the function
|
|
28
|
+
*/
|
|
29
|
+
onBeforeFunctionCall?: (functionName: string, args: Record<string, any>) => void | Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Called after a function is executed, allowing modification of the result
|
|
32
|
+
* @param functionName the name of the function that was called
|
|
33
|
+
* @param args the arguments that were passed to the function
|
|
34
|
+
* @param result the result returned by the function
|
|
35
|
+
* @returns the modified result
|
|
36
|
+
*/
|
|
37
|
+
onAfterFunctionCall?: (functionName: string, args: Record<string, any>, result: any) => any | Promise<any>;
|
|
24
38
|
}
|
|
25
39
|
|
|
26
40
|
export type { IAiPlugin };
|
package/dist/prompts/plugin.d.ts
CHANGED
|
@@ -21,6 +21,20 @@ interface IAiPlugin<TPluginName extends string = string, TPluginUseArgs extends
|
|
|
21
21
|
* @returns the modified output result of the send function
|
|
22
22
|
*/
|
|
23
23
|
onAfterSend?: (response: Awaited<TAfterSendResponse>) => Awaited<TAfterSendResponse> | Promise<Awaited<TAfterSendResponse>>;
|
|
24
|
+
/**
|
|
25
|
+
* Called before a function is executed, allowing modification of the arguments
|
|
26
|
+
* @param functionName the name of the function being called
|
|
27
|
+
* @param args the arguments being passed to the function
|
|
28
|
+
*/
|
|
29
|
+
onBeforeFunctionCall?: (functionName: string, args: Record<string, any>) => void | Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Called after a function is executed, allowing modification of the result
|
|
32
|
+
* @param functionName the name of the function that was called
|
|
33
|
+
* @param args the arguments that were passed to the function
|
|
34
|
+
* @param result the result returned by the function
|
|
35
|
+
* @returns the modified result
|
|
36
|
+
*/
|
|
37
|
+
onAfterFunctionCall?: (functionName: string, args: Record<string, any>, result: any) => any | Promise<any>;
|
|
24
38
|
}
|
|
25
39
|
|
|
26
40
|
export type { IAiPlugin };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microsoft/teams.ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "2.0.0-preview.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -31,14 +31,13 @@
|
|
|
31
31
|
"lint": "npx eslint",
|
|
32
32
|
"lint:fix": "npx eslint --fix",
|
|
33
33
|
"build": "npx tsup",
|
|
34
|
-
"test": "npx jest"
|
|
35
|
-
"fmt": "npx prettier --write \"**/*.{js,ts,tsx,md,json}\""
|
|
34
|
+
"test": "npx jest"
|
|
36
35
|
},
|
|
37
36
|
"peerDependencies": {
|
|
38
|
-
"@microsoft/teams.common": "
|
|
37
|
+
"@microsoft/teams.common": "2.0.0-preview.0"
|
|
39
38
|
},
|
|
40
39
|
"devDependencies": {
|
|
41
|
-
"@microsoft/teams.config": "
|
|
40
|
+
"@microsoft/teams.config": "2.0.0-preview.0",
|
|
42
41
|
"@types/jest": "^29.5.12",
|
|
43
42
|
"@types/node": "^22.0.2",
|
|
44
43
|
"jest": "^29.7.0",
|