Skip to content

Commit

Permalink
feat: simplify sendMessage options
Browse files Browse the repository at this point in the history
  • Loading branch information
lucgagan committed Jun 25, 2023
1 parent b65bb08 commit dcd3256
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ const chat = createChat({
});

try {
await chat.sendMessage("continue sequence: a b c", ({ cancel }) => {
cancel();
await chat.sendMessage("continue sequence: a b c", {
onUpdate: ({ cancel }) => {
cancel();
},
});
} catch (error) {
if (error instanceof CancelledCompletion) {
Expand Down Expand Up @@ -172,11 +174,9 @@ const chat = createChat({
const response = await chat.sendMessage(
"what is the next token in this sequence: a b c",
{
options: {
maxTokens: 1,
// token 34093 is "boo"
logitBias: { "34093": 100 },
},
maxTokens: 1,
// token 34093 is "boo"
logitBias: { "34093": 100 },
}
);

Expand Down
16 changes: 6 additions & 10 deletions src/createChat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ test("cancel response", async () => {
chat.sendMessage("continue sequence: a b c", {
onUpdate: ({ cancel }) => {
cancel();
}
},
})
);
});
Expand Down Expand Up @@ -144,7 +144,7 @@ test("calls user defined function", async () => {
);

assert.equal(response.role, "assistant");
assert.match(response.content, /the current weather in Albuquerque/i);
assert.match(response.content, /(the current weather in Albuquerque)|(weather in Albuquerque is currently)/i);
});

test("overrides function call", async () => {
Expand Down Expand Up @@ -181,9 +181,7 @@ test("overrides function call", async () => {
});

await chat.sendMessage("What is the weather in Chicago?", {
options: {
functionCall: "none",
},
functionCall: "none",
});

assert.equal(getCurrentWeather.mock.calls.length, 0);
Expand All @@ -198,11 +196,9 @@ test("overrides message options", async () => {
const response = await chat.sendMessage(
"what is the next token in this sequence: a b c",
{
options: {
maxTokens: 1,
// token 34093 is "boo"
logitBias: { "34093": 100 },
},
maxTokens: 1,
// token 34093 is "boo"
logitBias: { "34093": 100 },
}
);

Expand Down
12 changes: 4 additions & 8 deletions src/createChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import { retry } from "./retry";
import { omit } from "./omit";
import { createUserFunction, type UserFunction } from "./createUserFunction";

type MessageOptions = {
functionName?: string;
onUpdate?: CompletionsOptions["onUpdate"];
options?: Partial<
Omit<Omit<Omit<CompletionsOptions, "messages">, "n">, "functions">
>;
};
type MessageOptions = Partial<
Omit<Omit<Omit<CompletionsOptions, "messages">, "n">, "functions">
>;

/**
* @property apiKey - OpenAI API key.
Expand Down Expand Up @@ -81,7 +77,7 @@ export const createChat = (
messages,
onUpdate: messageOptions?.onUpdate,
...options,
...messageOptions?.options,
...messageOptions,
});
});

Expand Down

0 comments on commit dcd3256

Please sign in to comment.