-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add (experimental) ai/prompts construction helpers for StarChat and O…
…penAssistant (#343)
- Loading branch information
Showing
9 changed files
with
173 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
Add (experimental) prompt construction helpers for StarChat and OpenAssistant |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
title: ai/prompts | ||
--- | ||
|
||
import { Callout } from 'nextra-theme-docs' | ||
|
||
# `ai/prompts` | ||
|
||
The `ai/prompts` module contains functions to assist with converting `Message`'s into prompts that can be used with the [`useChat`](./use-chat) and [`useCompletion`](./use-completion) hooks. | ||
|
||
<Callout> | ||
The `experimental_` prefix on the functions in this module indicates that the | ||
API is not yet stable and may change in the future without a major version | ||
bump. | ||
</Callout> | ||
|
||
## `experimental_buildOpenAssistantPrompt` | ||
|
||
Uses `<|prompter|>`, `<|endoftext|>`, and `<|assistant>` tokens. If a `Message` with an unsupported `role` is passed, an error will be thrown. | ||
|
||
```ts filename="route.ts" {6} | ||
import { experimental_buildOpenAssistantPrompt } from 'ai/prompts' | ||
|
||
const { messages } = await req.json() | ||
const response = Hf.textGenerationStream({ | ||
model: 'OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5', | ||
inputs: experimental_buildOpenAssistantPrompt(messages) | ||
}) | ||
``` | ||
|
||
## `experimental_buildStarChatBetaPrompt` | ||
|
||
Uses `<|user|>`, `<|end|>`, `<|system|>`, and `<|assistant>` tokens. If a `Message` with an unsupported `role` is passed, an error will be thrown. | ||
|
||
```ts filename="route.ts" {6} | ||
import { buildStarChatBetaPrompt } from 'ai/prompts' | ||
|
||
const { messages } = await req.json() | ||
const response = Hf.textGenerationStream({ | ||
model: 'HuggingFaceH4/starchat-beta', | ||
inputs: experimental_buildStarChatBetaPrompt(messages) | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
buildOpenAssistantPrompt, | ||
buildStarChatBetaPrompt | ||
} from './huggingface' | ||
import type { Message } from '../shared/types' | ||
|
||
describe('buildStarChatBetaPrompt', () => { | ||
it('should return a string with user, assistant, and system messages', () => { | ||
const messages: Pick<Message, 'content' | 'role'>[] = [ | ||
{ content: 'You are a chat bot.', role: 'system' }, | ||
{ content: 'Hello!', role: 'user' }, | ||
{ content: 'Hi there!', role: 'assistant' } | ||
] | ||
|
||
const expectedPrompt = `<|system|>\nYou are a chat bot.<|end|>\n<|user|>\nHello!<|end|>\n<|assistant|>\nHi there!<|end|>\n<|assistant|>` | ||
const prompt = buildStarChatBetaPrompt(messages) | ||
expect(prompt).toEqual(expectedPrompt) | ||
}) | ||
|
||
it('should throw an error if a function message is included', () => { | ||
const messages: Pick<Message, 'content' | 'role'>[] = [ | ||
{ content: 'someFunction()', role: 'function' } | ||
] | ||
expect(() => buildStarChatBetaPrompt(messages)).toThrow() | ||
}) | ||
}) | ||
|
||
describe('buildOpenAssistantPrompt', () => { | ||
it('should return a string with user and assistant messages', () => { | ||
const messages: Pick<Message, 'content' | 'role'>[] = [ | ||
{ content: 'Hello!', role: 'user' }, | ||
{ content: 'Hi there!', role: 'assistant' } | ||
] | ||
|
||
const expectedPrompt = | ||
'<|prompter|>Hello!<|endoftext|><|assistant|>Hi there!<|endoftext|><|assistant|>' | ||
const prompt = buildOpenAssistantPrompt(messages) | ||
expect(prompt).toEqual(expectedPrompt) | ||
}) | ||
|
||
it('should throw an error if a function message is included', () => { | ||
const messages: Pick<Message, 'content' | 'role'>[] = [ | ||
{ content: 'someFunction()', role: 'function' } | ||
] | ||
expect(() => buildOpenAssistantPrompt(messages)).toThrow() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Message } from '../shared/types' | ||
|
||
/** | ||
* A prompt constructor for the HuggingFace StarChat Beta model. | ||
* Does not support `function` messages. | ||
* @see https://huggingface.co/HuggingFaceH4/starchat-beta | ||
*/ | ||
export function buildStarChatBetaPrompt( | ||
messages: Pick<Message, 'content' | 'role'>[] | ||
) { | ||
return ( | ||
messages | ||
.map(({ content, role }) => { | ||
if (role === 'user') { | ||
return `<|user|>\n${content}<|end|>\n` | ||
} else if (role === 'assistant') { | ||
return `<|assistant|>\n${content}<|end|>\n` | ||
} else if (role === 'system') { | ||
return `<|system|>\n${content}<|end|>\n` | ||
} else if (role === 'function') { | ||
throw new Error('StarChat Beta does not support function calls.') | ||
} | ||
}) | ||
.join('') + '<|assistant|>' | ||
) | ||
} | ||
|
||
/** | ||
* A prompt constructor for HuggingFace OpenAssistant models. | ||
* Does not support `function` or `system` messages. | ||
* @see https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5 | ||
*/ | ||
export function buildOpenAssistantPrompt( | ||
messages: Pick<Message, 'content' | 'role'>[] | ||
) { | ||
return ( | ||
messages | ||
.map(({ content, role }) => { | ||
if (role === 'user') { | ||
return `<|prompter|>${content}<|endoftext|>` | ||
} else if (role === 'function') { | ||
throw new Error('OpenAssistant does not support function calls.') | ||
} else if (role === 'system') { | ||
throw new Error('OpenAssistant does not support system messages.') | ||
} else { | ||
return `<|assistant|>${content}<|endoftext|>` | ||
} | ||
}) | ||
.join('') + '<|assistant|>' | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './huggingface' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters