Skip to content

Commit

Permalink
✨ add generate chains (#73)
Browse files Browse the repository at this point in the history
* ⚠️ Fix eslint warnings

* ⚠️ Pass target & cmd eslint

* ✨ Add generate chains

* 🔖 Change to 0.1.73
  • Loading branch information
lowczarc authored Sep 26, 2023
1 parent 420a424 commit 3ca6d66
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
30 changes: 5 additions & 25 deletions examples/simple_answer.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import { Chat } from "../lib/index";
import { generate } from "../lib/index";

async function main() {
const chat = new Chat({});
const answer = await generate("Who was Neil Armstrong ?")
.generate("Who was the US president at the time ?")
.generate("Who was his wife ?");

console.log(await chat.chatId);

console.log(await chat.getMessages());

let prompt = "Who is the first person to have walked on the moon?";
console.log(prompt);

let result = await chat.sendMessage(prompt);
console.log(result);

prompt = "Who is the second ?";
console.log(prompt);

result = await chat.sendMessage(prompt);
console.log(result);

prompt = "Who was the president of the United States at the time?";
console.log(prompt);

result = await chat.sendMessage(prompt);
console.log(result);

console.log(await chat.getMessages());
console.log(answer);
}

main();
1 change: 1 addition & 0 deletions lib/chats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class Chat {
stopped = true;
resultStream.push(null);
},
clientOptions: this.clientOptions,
});
let aiMessage = "";

Expand Down
18 changes: 16 additions & 2 deletions lib/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@ export class Generation extends Readable implements Promise<string> {

stop: () => void;

constructor({ stop }: { stop?: () => void } = {}) {
clientOptions: InputClientOptions;

constructor({ stop, clientOptions }: { stop?: () => void; clientOptions: InputClientOptions }) {
super({ read() {}, objectMode: true });
this.stop = stop || (() => {});
this.clientOptions = clientOptions;
}

pipeInto(stream: Generation): Generation {
Expand Down Expand Up @@ -214,6 +217,16 @@ export class Generation extends Readable implements Promise<string> {
});
});
}

generate(task: string, options: GenerationOptions = {}): Generation {
const resultStream = new Generation({ clientOptions: this.clientOptions });
(async () => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
generate(`${await this} ${task}`, options).pipeInto(resultStream);
})();

return resultStream;
}
}

function stream(
Expand All @@ -228,6 +241,7 @@ function stream(
stopped = true;
resultStream.push(null);
},
clientOptions,
});
(async () => {
const genOptions = options as GenerationCompleteOptions;
Expand Down Expand Up @@ -279,7 +293,7 @@ const GenerateDataType = t.type({

export function generate(
task: string,
options: GenerationOptions,
options?: GenerationOptions,
clientOptions?: InputClientOptions,
): Generation {
return stream(task, options, clientOptions, (data: unknown, resultStream: Generation) => {
Expand Down

0 comments on commit 3ca6d66

Please sign in to comment.