From 3467c1b07682f34441dc72baa84d3261a94de41a Mon Sep 17 00:00:00 2001 From: Michele Riva Date: Wed, 24 Apr 2024 18:48:05 -0700 Subject: [PATCH] improves interface --- src/answerSession.ts | 34 +++++++++++++++------------------- src/client.ts | 22 +--------------------- src/eventEmitter.ts | 29 ----------------------------- 3 files changed, 16 insertions(+), 69 deletions(-) delete mode 100644 src/eventEmitter.ts diff --git a/src/answerSession.ts b/src/answerSession.ts index 9fbd6bf..2c0b1c8 100644 --- a/src/answerSession.ts +++ b/src/answerSession.ts @@ -1,6 +1,5 @@ -import type { Results, AnyDocument } from '@orama/orama' +import type { Results, AnyDocument, SearchParams, AnyOrama } from '@orama/orama' import { OramaClient } from './client.js' -import { EventEmitter } from './eventEmitter.js' export type Context = Results['hits'] @@ -9,12 +8,9 @@ export type Message = { content: string } -export type Mode = 'fulltext' | 'vector' | 'hybrid' - export type InferenceType = 'documentation' type AnswerParams = { - mode: Mode initialMessages: Message[] inferenceType: InferenceType oramaClient: OramaClient @@ -25,9 +21,8 @@ type AnswerParams = { } } -export class AnswerSession extends EventEmitter { +export class AnswerSession { private messages: Message[] - private mode: Mode = 'fulltext' private inferenceType: InferenceType private oramaClient: OramaClient private endpoint: string @@ -35,9 +30,7 @@ export class AnswerSession extends EventEmitter { private events: AnswerParams['events'] constructor(params: AnswerParams) { - super() this.messages = params.initialMessages || [] - this.mode = params.mode this.inferenceType = params.inferenceType this.oramaClient = params.oramaClient // @ts-expect-error - sorry TypeScript @@ -45,13 +38,14 @@ export class AnswerSession extends EventEmitter { this.events = params.events } - public askStream(question: string, context: Context): AsyncGenerator { - this.messages.push({ role: 'user', content: question }) - return this.fetchAnswer(question, context) + public async askStream(params: SearchParams): Promise> { + this.messages.push({ role: 'user', content: params.term ?? '' }) + const inferenceResult = await this.runInference(params) + return this.fetchAnswer(params.term ?? '', inferenceResult?.hits ?? []) } - public async ask(question: string, context: Context): Promise { - const generator = this.askStream(question, context) + public async ask(params: SearchParams): Promise { + const generator = await this.askStream(params) let result = '' for await (const message of generator) { result = message @@ -68,10 +62,6 @@ export class AnswerSession extends EventEmitter { return this.messages } - public setMode(mode: Mode): void { - this.mode = mode - } - public clearSession(): void { this.messages = [] } @@ -88,6 +78,10 @@ export class AnswerSession extends EventEmitter { } } + private runInference(params: SearchParams) { + return this.oramaClient.search(params) + } + private async *fetchAnswer(query: string, context: Context): AsyncGenerator { this.abortController = new AbortController() const { signal } = this.abortController @@ -140,7 +134,9 @@ export class AnswerSession extends EventEmitter { } } catch (err) { if ((err as any).name === 'AbortError') { - this.emit('answer-aborted', true) + if (this.events?.onAnswerAborted) { + this.events.onAnswerAborted(true) + } } else { throw err } diff --git a/src/client.ts b/src/client.ts index 0c06e2f..4f6c789 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,6 +1,6 @@ import type { Endpoint, IOramaClient, Method, OramaInitResponse, HeartBeatConfig, OramaError } from './types.js' import type { SearchParams, Results, AnyDocument, AnyOrama, Nullable } from '@orama/orama' -import type { Message, Mode, InferenceType } from './answerSession.js' +import type { Message, InferenceType } from './answerSession.js' import { formatElapsedTime } from '@orama/orama/components' import { createId } from '@paralleldrive/cuid2' @@ -36,7 +36,6 @@ export type ClientSearchParams = SearchParams & AdditionalSearchParams export type AnswerSessionParams = { inferenceType?: InferenceType initialMessages?: Message[] - mode?: Mode events?: { onMessageChange?: (messages: Message[]) => void onMessageLoading?: (receivingMessage: boolean) => void @@ -210,7 +209,6 @@ export class OramaClient { return new AnswerSession({ inferenceType: params?.inferenceType || 'documentation', initialMessages: params?.initialMessages || [], - mode: params?.mode || 'fulltext', oramaClient: this, events: params?.events }) @@ -268,7 +266,6 @@ export class OramaClient { } if (method === 'POST' && body !== undefined) { - // biome-ignore lint/suspicious/noExplicitAny: keep any for now const b = body as any b.version = version b.id = this.id @@ -289,20 +286,3 @@ export class OramaClient { return await res.json() } } - -const client = new OramaClient({ - endpoint: 'https://cloud.orama.foo/v1/indexes/test-answer-dalfkj', - api_key: '5thXEia7alVyZaomQwbtFdAZuztPMHIt' -}) - -const session = client.createAnswerSession({ - events: { - onAnswerAborted: (aborted: true) => console.log({ aborted }), - onMessageChange: (messages: Message[]) => console.log({ messages }), - onMessageLoading: (receivingMessage: boolean) => console.log({ receivingMessage }) - } -}) - -const results = await client.search({ term: 'Pinscher' }) - -await session.ask('What is the best guarding dog?', results.hits) diff --git a/src/eventEmitter.ts b/src/eventEmitter.ts deleted file mode 100644 index 76b065f..0000000 --- a/src/eventEmitter.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { Message } from './answerSession.js' - -type Events = { - 'message-change': (messages: Message[]) => void - 'message-loading': (receivingMessage: boolean) => void - 'answer-aborted': (aborted: true) => void -} - -export class EventEmitter { - private events: { [K in keyof T]?: T[K][] } = {} - - on(event: K, listener: T[K]) { - if (!this.events[event]) { - this.events[event] = [] - } - ;(this.events[event] as T[K][]).push(listener) - } - - // @ts-expect-error - sorry TypeScript - emit(event: K, ...args: Parameters) { - const listeners = (this.events[event] as T[K][]) || [] - if (listeners) { - for (const listener of listeners) { - // @ts-expect-error - no way I will type this - listener(...args) - } - } - } -}