-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
53 additions
and
15 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
docs/src/content/docs/reference/scripts/chat-participants.mdx
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 @@ | ||
--- | ||
title: Chat Participants | ||
sidebar: | ||
order: 50 | ||
--- | ||
import { Code } from '@astrojs/starlight/components'; | ||
import scriptSource from "../../../../../../packages/sample/genaisrc/multi-turn.genai.js?raw" | ||
|
||
|
||
The `defChatParticipant` allows to register a function that can add new user messages in the chat sequence. | ||
This allows to create multi-turn chat, or to simulate a conversation with multiple participants. | ||
|
||
```js | ||
let turn = 0 | ||
defChatParticipant((_, messages) => { | ||
if (++turn === 1) _.$`Are you sure?` | ||
}) | ||
``` | ||
|
||
In the example above, the `defChatParticipant` function is used to register a function that will be called every time a new message is added to the chat. | ||
|
||
The function receives two arguments: the first argument is the `Chat` object, and the second argument is the list of messages that have been added to the chat since the last call to the function. | ||
|
||
```js | ||
defChatParticipant(async (_, messages) => { | ||
const text = messages.at(-1).content | ||
... | ||
}) | ||
``` | ||
|
||
## Tracking turns | ||
|
||
The participant will be called on every turn so it is important to keep track of the turns to avoid infinite loops. | ||
|
||
```js | ||
let turn = 0 | ||
defChatParticipant((_, messages) => { | ||
if (++turn === 1) _.$`Are you sure?` | ||
}) | ||
``` | ||
|
||
|
||
## Example: QA generator | ||
|
||
This script uses a multi-turn chat to generate questions, answers and validate the quality of the answers. | ||
|
||
<Code code={scriptSource} wrap={true} lang="js" title="qa-gen.genai.js" /> |
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
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