Skip to content

Commit 6cc07f3

Browse files
authored
Disable automatic function calling, and show structured output (#241)
* Disable automatic function calling, and show structured output Lint fix * Fix template * Remove raw * Update * Fix assertion * Fix export * Fixes * Use open ai and fix build * Rename
1 parent 05085e8 commit 6cc07f3

File tree

12 files changed

+327
-212
lines changed

12 files changed

+327
-212
lines changed

.github/workflows/template-sync.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: Template Sync Verification
22
on:
33
workflow_dispatch:
44
pull_request:
5+
types: [opened, synchronize, reopened, edited]
56
paths:
67
- "packages/cli/templates/typescript/**"
78
- "tests/*/**"

packages/ai/src/models/chat.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export type ChatSendOptions<TOptions = Record<string, any>> = {
3030
* stream chunk
3131
*/
3232
readonly onChunk?: TextChunkHandler;
33+
34+
/**
35+
* enable/disable automatic function calling
36+
* @default true
37+
*/
38+
readonly autoFunctionCalling?: boolean;
3339
};
3440

3541
/**

packages/ai/src/prompts/chat-types.ts

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import { ILogger } from '@microsoft/teams.common';
2+
3+
import { Function, FunctionHandler } from '../function';
4+
import { IMemory } from '../memory';
5+
import { ContentPart, Message, ModelMessage } from '../message';
6+
import { IChatModel, TextChunkHandler } from '../models';
7+
import { Schema } from '../schema';
8+
import { ITemplate } from '../template';
9+
import { PromiseOrValue } from '../utils/types';
10+
11+
import { IAiPlugin } from './plugin';
12+
13+
export type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
14+
/**
15+
* the name of the prompt
16+
*/
17+
readonly name?: string;
18+
19+
/**
20+
* the description of the prompt
21+
*/
22+
readonly description?: string;
23+
24+
/**
25+
* the model to send messages to
26+
*/
27+
readonly model: IChatModel<TOptions>;
28+
29+
/**
30+
* the defining characteristics/objective
31+
* of the prompt. This is commonly used to provide a system prompt.
32+
* If you supply the system prompt as part of the messages,
33+
* you do not need to supply this option.
34+
*/
35+
readonly instructions?: string | string[] | ITemplate;
36+
37+
/**
38+
* the `role` of the initial message
39+
*/
40+
readonly role?: 'system' | 'user';
41+
42+
/**
43+
* the conversation history
44+
*/
45+
readonly messages?: Message[] | IMemory;
46+
47+
/**
48+
* Logger instance to use for logging
49+
* If not provided, a ConsoleLogger will be used
50+
*/
51+
logger?: ILogger;
52+
};
53+
54+
export type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
55+
/**
56+
* the conversation history
57+
*/
58+
readonly messages?: Message[] | IMemory;
59+
60+
/**
61+
* the models request options
62+
*/
63+
readonly request?: TOptions;
64+
65+
/**
66+
* the callback to be called for each
67+
* stream chunk
68+
*/
69+
readonly onChunk?: TextChunkHandler;
70+
71+
/**
72+
* enable/disable automatic function calling
73+
* @default true
74+
*/
75+
readonly autoFunctionCalling?: boolean;
76+
};
77+
78+
/**
79+
* a prompt that can interface with a
80+
* chat model that provides utility like
81+
* streaming and function calling
82+
*/
83+
export interface IChatPrompt<
84+
TOptions extends Record<string, any> = Record<string, any>,
85+
TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []
86+
> {
87+
/**
88+
* the prompt name
89+
*/
90+
readonly name: string;
91+
92+
/**
93+
* the prompt description
94+
*/
95+
readonly description: string;
96+
97+
/**
98+
* the chat history
99+
*/
100+
readonly messages: IMemory;
101+
102+
/**
103+
* the registered functions
104+
*/
105+
readonly functions: Array<Function>;
106+
107+
/**
108+
* the chat model
109+
*/
110+
plugins: TChatPromptPlugins;
111+
/**
112+
* add another chat prompt as a
113+
*/
114+
use(prompt: IChatPrompt): this;
115+
use(name: string, prompt: IChatPrompt): this;
116+
117+
/**
118+
* add a function that can be called
119+
* by the model
120+
*/
121+
function(name: string, description: string, handler: FunctionHandler): this;
122+
function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
123+
124+
usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(
125+
name: TPluginName,
126+
args: Extract<TChatPromptPlugins[number], { name: TPluginName }>['onUsePlugin'] extends
127+
| ((args: infer U) => void)
128+
| undefined
129+
? U
130+
: never
131+
): this;
132+
133+
/**
134+
* call a function
135+
*/
136+
call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;
137+
138+
/**
139+
* send a message to the model and get a response
140+
*/
141+
send(
142+
input: string | ContentPart[],
143+
options?: ChatPromptSendOptions<TOptions>
144+
): Promise<ModelMessage>;
145+
}
146+
147+
export type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<
148+
TPluginName,
149+
TPluginUseArgs,
150+
Parameters<IChatPrompt['send']>[0],
151+
ReturnType<IChatPrompt['send']>
152+
> & {
153+
/**
154+
* Optionally passed in to modify the functions array that
155+
* is passed to the model
156+
* @param functions
157+
* @returns Functions
158+
*/
159+
onBuildFunctions?: (functions: Function[]) => PromiseOrValue<Function[]>;
160+
/**
161+
* Optionally passed in to modify the system prompt before it is sent to the model.
162+
* @param systemPrompt The system prompt string (or undefined)
163+
* @returns The modified system prompt string (or undefined)
164+
*/
165+
onBuildPrompt?: (systemPrompt: string | undefined) => PromiseOrValue<string | undefined>;
166+
};

packages/ai/src/prompts/chat.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { ContentPart, Message } from '../message';
22
import { IChatModel } from '../models';
33
import { Schema } from '../schema';
44

5-
import { ChatPrompt, ChatPromptPlugin } from './chat';
5+
import { ChatPrompt } from './chat';
6+
import { ChatPromptPlugin } from './chat-types';
67

78
// Mock implementations
89
const mockChatModel: IChatModel<any> = {

packages/ai/src/prompts/chat.ts

Lines changed: 5 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -3,163 +3,14 @@ import { ConsoleLogger, ILogger } from '@microsoft/teams.common';
33
import { Function, FunctionHandler } from '../function';
44
import { LocalMemory } from '../local-memory';
55
import { IMemory } from '../memory';
6-
import { ContentPart, Message, ModelMessage, SystemMessage, UserMessage } from '../message';
7-
import { IChatModel, TextChunkHandler } from '../models';
6+
import { ContentPart, SystemMessage, UserMessage } from '../message';
7+
import { IChatModel } from '../models';
88
import { Schema } from '../schema';
99
import { ITemplate } from '../template';
1010
import { StringTemplate } from '../templates';
11-
import { PromiseOrValue, WithRequired } from '../utils/types';
12-
13-
import { IAiPlugin } from './plugin';
14-
15-
export type ChatPromptOptions<TOptions extends Record<string, any> = Record<string, any>> = {
16-
/**
17-
* the name of the prompt
18-
*/
19-
readonly name?: string;
20-
21-
/**
22-
* the description of the prompt
23-
*/
24-
readonly description?: string;
25-
26-
/**
27-
* the model to send messages to
28-
*/
29-
readonly model: IChatModel<TOptions>;
30-
31-
/**
32-
* the defining characteristics/objective
33-
* of the prompt. This is commonly used to provide a system prompt.
34-
* If you supply the system prompt as part of the messages,
35-
* you do not need to supply this option.
36-
*/
37-
readonly instructions?: string | string[] | ITemplate;
38-
39-
/**
40-
* the `role` of the initial message
41-
*/
42-
readonly role?: 'system' | 'user';
43-
44-
/**
45-
* the conversation history
46-
*/
47-
readonly messages?: Message[] | IMemory;
48-
49-
/**
50-
* Logger instance to use for logging
51-
* If not provided, a ConsoleLogger will be used
52-
*/
53-
logger?: ILogger;
54-
};
55-
56-
export type ChatPromptSendOptions<TOptions extends Record<string, any> = Record<string, any>> = {
57-
/**
58-
* the conversation history
59-
*/
60-
readonly messages?: Message[] | IMemory;
61-
62-
/**
63-
* the models request options
64-
*/
65-
readonly request?: TOptions;
66-
67-
/**
68-
* the callback to be called for each
69-
* stream chunk
70-
*/
71-
readonly onChunk?: TextChunkHandler;
72-
};
11+
import { WithRequired } from '../utils/types';
7312

74-
/**
75-
* a prompt that can interface with a
76-
* chat model that provides utility like
77-
* streaming and function calling
78-
*/
79-
export interface IChatPrompt<
80-
TOptions extends Record<string, any> = Record<string, any>,
81-
TChatPromptPlugins extends readonly ChatPromptPlugin<string, any>[] = []
82-
> {
83-
/**
84-
* the prompt name
85-
*/
86-
readonly name: string;
87-
88-
/**
89-
* the prompt description
90-
*/
91-
readonly description: string;
92-
93-
/**
94-
* the chat history
95-
*/
96-
readonly messages: IMemory;
97-
98-
/**
99-
* the registered functions
100-
*/
101-
readonly functions: Array<Function>;
102-
103-
/**
104-
* the chat model
105-
*/
106-
plugins: TChatPromptPlugins;
107-
/**
108-
* add another chat prompt as a
109-
*/
110-
use(prompt: IChatPrompt): this;
111-
use(name: string, prompt: IChatPrompt): this;
112-
113-
/**
114-
* add a function that can be called
115-
* by the model
116-
*/
117-
function(name: string, description: string, handler: FunctionHandler): this;
118-
function(name: string, description: string, parameters: Schema, handler: FunctionHandler): this;
119-
120-
usePlugin<TPluginName extends TChatPromptPlugins[number]['name']>(
121-
name: TPluginName,
122-
args: Extract<TChatPromptPlugins[number], { name: TPluginName }>['onUsePlugin'] extends
123-
| ((args: infer U) => void)
124-
| undefined
125-
? U
126-
: never
127-
): this;
128-
129-
/**
130-
* call a function
131-
*/
132-
call<A extends Record<string, any>, R = any>(name: string, args?: A): Promise<R>;
133-
134-
/**
135-
* send a message to the model and get a response
136-
*/
137-
send(
138-
input: string | ContentPart[],
139-
options?: ChatPromptSendOptions<TOptions>
140-
): Promise<Pick<ModelMessage, 'content'> & Omit<ModelMessage, 'content'>>;
141-
}
142-
143-
export type ChatPromptPlugin<TPluginName extends string, TPluginUseArgs extends {}> = IAiPlugin<
144-
TPluginName,
145-
TPluginUseArgs,
146-
Parameters<IChatPrompt['send']>[0],
147-
ReturnType<IChatPrompt['send']>
148-
> & {
149-
/**
150-
* Optionally passed in to modify the functions array that
151-
* is passed to the model
152-
* @param functions
153-
* @returns Functions
154-
*/
155-
onBuildFunctions?: (functions: Function[]) => PromiseOrValue<Function[]>;
156-
/**
157-
* Optionally passed in to modify the system prompt before it is sent to the model.
158-
* @param systemPrompt The system prompt string (or undefined)
159-
* @returns The modified system prompt string (or undefined)
160-
*/
161-
onBuildPrompt?: (systemPrompt: string | undefined) => PromiseOrValue<string | undefined>;
162-
};
13+
import { ChatPromptOptions, ChatPromptPlugin, ChatPromptSendOptions, IChatPrompt } from './chat-types';
16314

16415
/**
16516
* a prompt that can interface with a
@@ -403,6 +254,7 @@ export class ChatPrompt<
403254
return;
404255
}
405256
},
257+
autoFunctionCalling: options.autoFunctionCalling,
406258
}
407259
);
408260

packages/ai/src/prompts/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { IAudioPrompt } from './audio';
2-
import { IChatPrompt } from './chat';
2+
import { IChatPrompt } from './chat-types';
33

44
export type Prompt = IChatPrompt | IAudioPrompt;
55

6-
export * from './chat';
76
export * from './audio';
7+
export * from './chat';
8+
export * from './chat-types';
9+

0 commit comments

Comments
 (0)