From 3ca6d6655547958a82fc2f2539eec49f30fd763b Mon Sep 17 00:00:00 2001 From: Lancelot Owczarczak Date: Tue, 26 Sep 2023 10:28:32 +0200 Subject: [PATCH] :sparkles: add generate chains (#73) * :warning: Fix eslint warnings * :warning: Pass target & cmd eslint * :sparkles: Add generate chains * :bookmark: Change to 0.1.73 --- examples/simple_answer.ts | 30 +++++------------------------- lib/chats/index.ts | 1 + lib/generate.ts | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/examples/simple_answer.ts b/examples/simple_answer.ts index a03d1b5..4b53022 100644 --- a/examples/simple_answer.ts +++ b/examples/simple_answer.ts @@ -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(); diff --git a/lib/chats/index.ts b/lib/chats/index.ts index 5fcd7df..d9fee6e 100644 --- a/lib/chats/index.ts +++ b/lib/chats/index.ts @@ -103,6 +103,7 @@ export class Chat { stopped = true; resultStream.push(null); }, + clientOptions: this.clientOptions, }); let aiMessage = ""; diff --git a/lib/generate.ts b/lib/generate.ts index 66204fb..e4b61e2 100644 --- a/lib/generate.ts +++ b/lib/generate.ts @@ -144,9 +144,12 @@ export class Generation extends Readable implements Promise { 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 { @@ -214,6 +217,16 @@ export class Generation extends Readable implements Promise { }); }); } + + 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( @@ -228,6 +241,7 @@ function stream( stopped = true; resultStream.push(null); }, + clientOptions, }); (async () => { const genOptions = options as GenerationCompleteOptions; @@ -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) => {