Skip to content

Commit d6fdaad

Browse files
committed
Allow assistant-role prompts and multiple prompts
Closes #28. See also #7.
1 parent 6371b94 commit d6fdaad

File tree

1 file changed

+70
-5
lines changed

1 file changed

+70
-5
lines changed

README.md

+70-5
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,68 @@ const result1 = await predictEmoji("Back to the drawing board");
109109
const result2 = await predictEmoji("This code is so good you should get promoted");
110110
```
111111

112+
(Note that merely creating a session does not cause any new responses from the assistant. We need to call `prompt()` or `promptStreaming()` to get a response.)
113+
112114
Some details on error cases:
113115

114116
* Using both `systemPrompt` and a `{ role: "system" }` prompt in `initialPrompts`, or using multiple `{ role: "system" }` prompts, or placing the `{ role: "system" }` prompt anywhere besides at the 0th position in `initialPrompts`, will reject with a `TypeError`.
115117
* If the combined token length of all the initial prompts (including the separate `systemPrompt`, if provided) is too large, then the promise will be rejected with a `"QuotaExceededError"` `DOMException`.
116118

119+
### Customizing the role per prompt
120+
121+
Our examples so far have provided `prompt()` and `promptStreaming()` with a single string. Such cases assume messages will come from the user role. These methods can also take in objects in the `{ role, content }` format, or arrays of such objects, in case you want to provide multiple user or assistant messages:
122+
123+
```js
124+
const multiUserSession = await ai.assistant.create({
125+
systemPrompt: "You are a mediator in a discussion between two departments."
126+
});
127+
128+
const result = await multiUserSession.prompt([
129+
{ "role": "user", content: "Marketing: We need more budget for advertising campaigns." },
130+
{ "role": "user", content: "Finance: We need to cut costs and advertising is on the list." },
131+
{ "role": "assistant", "content": "Let's explore a compromise that satisfies both departments." }
132+
]);
133+
```
134+
135+
Because of their special behavior of being preserved on context window overflow, system prompts cannot be provided this way.
136+
137+
### Emulating tool use or function-calling via assistant-role prompts
138+
139+
A special case of the above is using the assistant role to emulate tool use or function-calling, by marking a response as coming from the assistant side of the conversation:
140+
141+
```js
142+
const session = await ai.assistant.create({
143+
systemPrompt: `
144+
You are a helpful assistant. You have access to the following tools:
145+
- calculator: A calculator. To use it, write "CALCULATOR: <expression>" where <expression> is a valid mathematical expression.
146+
`
147+
});
148+
149+
async function promptWithCalculator(prompt) {
150+
const result = await session.prompt("What is 2 + 2?");
151+
152+
// Check if the assistant wants to use the calculator tool.
153+
const match = /^CALCULATOR: (.*)$/.exec(result);
154+
if (match) {
155+
const expression = match[1];
156+
const mathResult = evaluateMathExpression(expression);
157+
158+
// Add the result to the session so it's in context going forward.
159+
await session.prompt({ role: "assistant", content: mathResult });
160+
161+
// Return it as if that's what the assistant said to the user.
162+
return mathResult;
163+
}
164+
165+
// The assistant didn't want to use the calculator. Just return its response.
166+
return result;
167+
}
168+
169+
console.log(await promptWithCalculator("What is 2 + 2?"));
170+
```
171+
172+
We'll likely explore more specific APIs for tool- and function-calling in the future; follow along in [issue #7](https://github.com/explainers-by-googlers/prompt-api/issues/7).
173+
117174
### Configuration of per-session options
118175

119176
In addition to the `systemPrompt` and `initialPrompts` options shown above, the currently-configurable options are [temperature](https://huggingface.co/blog/how-to-generate#sampling) and [top-K](https://huggingface.co/blog/how-to-generate#top-k-sampling). More information about the values for these parameters can be found using the `capabilities()` API explained [below](#capabilities-detection).
@@ -330,10 +387,10 @@ interface AIAssistantFactory {
330387
331388
[Exposed=(Window,Worker)]
332389
interface AIAssistant : EventTarget {
333-
Promise<DOMString> prompt(DOMString input, optional AIAssistantPromptOptions options = {});
334-
ReadableStream promptStreaming(DOMString input, optional AIAssistantPromptOptions options = {});
390+
Promise<DOMString> prompt(AIAssistantPromptInput input, optional AIAssistantPromptOptions options = {});
391+
ReadableStream promptStreaming(AIAssistantPromptInput input, optional AIAssistantPromptOptions options = {});
335392
336-
Promise<unsigned long long> countPromptTokens(DOMString input, optional AIAssistantPromptOptions options = {});
393+
Promise<unsigned long long> countPromptTokens(AIAssistantPromptInput input, optional AIAssistantPromptOptions options = {});
337394
readonly attribute unsigned long long maxTokens;
338395
readonly attribute unsigned long long tokensSoFar;
339396
readonly attribute unsigned long long tokensLeft;
@@ -364,11 +421,16 @@ dictionary AIAssistantCreateOptions {
364421
AICreateMonitorCallback monitor;
365422
366423
DOMString systemPrompt;
367-
sequence<AIAssistantPrompt> initialPrompts;
424+
sequence<AIAssistantInitialPrompt> initialPrompts;
368425
[EnforceRange] unsigned long topK;
369426
float temperature;
370427
};
371428
429+
dictionary AIAssistantInitialPrompt {
430+
AIAssistantInitialPromptRole role;
431+
DOMString content;
432+
};
433+
372434
dictionary AIAssistantPrompt {
373435
AIAssistantPromptRole role;
374436
DOMString content;
@@ -378,7 +440,10 @@ dictionary AIAssistantPromptOptions {
378440
AbortSignal signal;
379441
};
380442
381-
enum AIAssistantPromptRole { "system", "user", "assistant" };
443+
typedef (DOMString or AIAssistantPrompt or sequence<AIAssistantPrompt>) AIAssistantPromptInput;
444+
445+
enum AIAssistantInitialPromptRole { "system", "user", "assistant" };
446+
enum AIAssistantPromptRole { "user", "assistant" };
382447
```
383448

384449
### Instruction-tuned versus base models

0 commit comments

Comments
 (0)