-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add opt for passing abort signal to chatCompletion function (#37)
* add opt for passing abort signal to chatCompletion function
- Loading branch information
Showing
10 changed files
with
152 additions
and
14 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,53 @@ | ||
import 'dotenv/config'; | ||
import { ChatModel, Msg, type Prompt } from '@dexaai/dexter'; | ||
|
||
/** | ||
* npx tsx examples/abort-chat-completion.ts | ||
*/ | ||
async function main() { | ||
const chatModel = new ChatModel({ | ||
debug: true, | ||
params: { | ||
model: 'gpt-3.5-turbo', | ||
temperature: 0.5, | ||
max_tokens: 1000, | ||
}, | ||
}); | ||
|
||
const messages: Prompt.Msg[] = [Msg.user(`Write a short story`)]; | ||
|
||
{ | ||
const abortController = new AbortController(); | ||
abortController.signal.addEventListener('abort', () => { | ||
console.log('\n\nAborted'); | ||
}); | ||
|
||
try { | ||
setTimeout(() => { | ||
abortController.abort(); | ||
}, 2000); | ||
|
||
// Invoke the chat model with the result | ||
await chatModel.run({ | ||
messages, | ||
requestOpts: { | ||
signal: abortController.signal, | ||
}, | ||
handleUpdate: (c) => { | ||
// Note: The abort doesn't always cancel the request, so we need to handle when the request is aborted | ||
// here as well. | ||
if (abortController.signal.aborted) { | ||
return; | ||
} | ||
process.stdout.write(c); | ||
}, | ||
}); | ||
|
||
// console.log(message.content); | ||
} catch (error) { | ||
console.error('Error during chat model run:', error); | ||
} | ||
} | ||
} | ||
|
||
main(); |
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
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
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