Skip to content

Commit

Permalink
improves interface
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Apr 25, 2024
1 parent d0659fd commit 3467c1b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 69 deletions.
34 changes: 15 additions & 19 deletions src/answerSession.ts
Original file line number Diff line number Diff line change
@@ -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<AnyDocument>['hits']

Expand All @@ -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
Expand All @@ -25,33 +21,31 @@ 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
private abortController?: AbortController
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
this.endpoint = `${this.oramaClient.endpoint}/answer?api-key=${this.oramaClient.api_key}`
this.events = params.events
}

public askStream(question: string, context: Context): AsyncGenerator<string> {
this.messages.push({ role: 'user', content: question })
return this.fetchAnswer(question, context)
public async askStream(params: SearchParams<AnyOrama>): Promise<AsyncGenerator<string>> {
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<string> {
const generator = this.askStream(question, context)
public async ask(params: SearchParams<AnyOrama>): Promise<string> {
const generator = await this.askStream(params)
let result = ''
for await (const message of generator) {
result = message
Expand All @@ -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 = []
}
Expand All @@ -88,6 +78,10 @@ export class AnswerSession extends EventEmitter {
}
}

private runInference(params: SearchParams<AnyOrama>) {
return this.oramaClient.search(params)
}

private async *fetchAnswer(query: string, context: Context): AsyncGenerator<string> {
this.abortController = new AbortController()
const { signal } = this.abortController
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 1 addition & 21 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -36,7 +36,6 @@ export type ClientSearchParams = SearchParams<AnyOrama> & AdditionalSearchParams
export type AnswerSessionParams = {
inferenceType?: InferenceType
initialMessages?: Message[]
mode?: Mode
events?: {
onMessageChange?: (messages: Message[]) => void
onMessageLoading?: (receivingMessage: boolean) => void
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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
Expand All @@ -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)
29 changes: 0 additions & 29 deletions src/eventEmitter.ts

This file was deleted.

0 comments on commit 3467c1b

Please sign in to comment.