Skip to main content

Overview

The AnswerSession class provides conversational search capabilities by combining Orama search with LLM chat models. It maintains conversation state, manages message history, and streams AI-generated responses based on search results.

Constructor

new AnswerSession<SourceT = AnyDocument>(db: AnyOrama, config: IAnswerSessionConfig<SourceT>)

Parameters

db
AnyOrama
required
The Orama database instance to search.
config
IAnswerSessionConfig<SourceT>
required
Configuration object for the answer session.

Properties

state

public state: Interaction<SourceT>[]
Array of all interactions in the current session. Each interaction contains:
interactionId
string
Unique identifier for this interaction.
query
string
The user’s search query.
response
string
The AI-generated response.
aborted
boolean
Whether this interaction was aborted.
loading
boolean
Whether this interaction is currently loading.
sources
Nullable<Results<SourceT>>
Search results used to generate the response.
translatedQuery
Nullable<SearchParams<AnyOrama>>
The translated search parameters.
error
boolean
Whether an error occurred during this interaction.
errorMessage
Nullable<string>
Error message if an error occurred.

Methods

ask()

Asks a question and returns the complete response after streaming finishes.
async ask(query: AskParams): Promise<string>
query
AskParams
required
Search parameters. Same as SearchParams<AnyDocument>.
returns
Promise<string>
The complete AI-generated response.

Example

const session = new AnswerSession(db, {
  systemPrompt: 'You are a helpful assistant for a documentation site.'
});

const answer = await session.ask({
  term: 'How do I install Orama?'
});

console.log(answer);

askStream()

Asks a question and returns an async generator that streams the response.
async askStream(query: AskParams): Promise<AsyncGenerator<string>>
query
AskParams
required
Search parameters. Same as SearchParams<AnyDocument>.
returns
Promise<AsyncGenerator<string>>
An async generator that yields response chunks.

Example

const session = new AnswerSession(db, {
  systemPrompt: 'You are a helpful assistant.'
});

for await (const chunk of await session.askStream({ term: 'What is Orama?' })) {
  process.stdout.write(chunk);
}

abortAnswer()

Aborts the current streaming response.
abortAnswer(): void

Example

const session = new AnswerSession(db, {});

const streamPromise = session.askStream({ term: 'Tell me everything about Orama' });

// Abort after 1 second
setTimeout(() => {
  session.abortAnswer();
}, 1000);

getMessages()

Returns the current conversation messages.
getMessages(): Message[]
returns
Message[]
Array of all messages in the conversation.

clearSession()

Clears all messages and interactions from the current session.
clearSession(): void

Example

const session = new AnswerSession(db, {});

await session.ask({ term: 'First question' });
await session.ask({ term: 'Follow-up question' });

session.clearSession(); // Clears all history

await session.ask({ term: 'Fresh start' }); // New conversation

regenerateLast()

Regenerates the last AI response with the same query parameters.
regenrateLast(params: RegenerateLastParams): Promise<string> | Promise<AsyncGenerator<string>>
params
RegenerateLastParams
required
returns
Promise<string> | Promise<AsyncGenerator<string>>
Either the complete response (if stream: false) or an async generator (if stream: true).

Example

const session = new AnswerSession(db, {});

await session.ask({ term: 'Explain search indexing' });

// Regenerate with streaming
for await (const chunk of await session.regenerateLast({ stream: true })) {
  console.log(chunk);
}

Type Definitions

GenericContext

type GenericContext = string | object

MessageRole

type MessageRole = 'system' | 'user' | 'assistant'

AskParams

type AskParams = SearchParams<AnyDocument>
Same as the standard Orama SearchParams type. See SearchParams for details.

Requirements

The AnswerSession class requires:
  1. The Orama database must have the Secure Proxy plugin installed
  2. A chat model must be configured in the plugin parameters
import { create } from '@orama/orama';
import { pluginSecureProxy } from '@orama/plugin-secure-proxy';

const db = await create({
  schema: {
    title: 'string',
    description: 'string',
  },
  plugins: [
    await pluginSecureProxy({
      apiKey: 'your-api-key',
      chat: {
        model: 'gpt-4'
      }
    })
  ]
});

const session = new AnswerSession(db, {
  systemPrompt: 'You are a helpful assistant.'
});

Build docs developers (and LLMs) love