Skip to content

Commit

Permalink
Add cancellation example
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Apr 4, 2023
1 parent 4f7b8f6 commit 1381dd2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
52 changes: 52 additions & 0 deletions examples/cancellation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import "dotenv/config";
import { AI_PROMPT, Client, HUMAN_PROMPT } from "../src";

const apiKey = process.env.ANTHROPIC_API_KEY;
if (!apiKey) {
throw new Error("The ANTHROPIC_API_KEY environment variable must be set");
}

const client = new Client(apiKey);
const abortController = new AbortController();

client
.complete(
{
prompt: `${HUMAN_PROMPT} How many toes do dogs have?${AI_PROMPT}`,
stop_sequences: [HUMAN_PROMPT],
max_tokens_to_sample: 200,
model: "claude-v1",
},
{ signal: abortController.signal }
)
.catch((error) => {
if (error.name === "AbortError") {
console.log("Cancelled complete()");
}
});

client
.completeStream(
{
prompt: `${HUMAN_PROMPT} How many toes do dogs have?${AI_PROMPT}`,
stop_sequences: [HUMAN_PROMPT],
max_tokens_to_sample: 200,
model: "claude-v1",
},
{
onOpen: (response) => {
console.log("Opened stream, HTTP status code", response.status);
},
onUpdate: (completion) => {
console.log(completion.completion);
},
signal: abortController.signal,
}
)
.catch((error) => {
if (error.name === "AbortError") {
console.log("Cancelled completeStream()");
}
});

abortController.abort();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"lint": "eslint src && prettier --check .",
"format": "prettier --write .",
"example:basic_stream": "tsc --build && node ./build/examples/basic_stream.js",
"example:basic_sync": "tsc --build && node ./build/examples/basic_sync.js"
"example:basic_sync": "tsc --build && node ./build/examples/basic_sync.js",
"example:cancellation": "tsc --build && node ./build/examples/cancellation.js"
},
"dependencies": {
"@fortaine/fetch-event-source": "^3.0.6",
Expand Down

0 comments on commit 1381dd2

Please sign in to comment.