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
The Orama database instance to search.
config
IAnswerSessionConfig<SourceT>
required
Configuration object for the answer session. Show IAnswerSessionConfig properties
Optional conversation identifier. Auto-generated if not provided.
Optional system prompt to guide the AI assistant’s behavior.
Additional context to provide to the AI. Can be a string or object.
Initial conversation messages to seed the session.
events
AnswerSessionEvents<SourceT>
Event handlers for state changes. Show AnswerSessionEvents properties
onStateChange
(state: Interaction<SourceT>[]) => void
Called whenever the interaction state changes.
Properties
state
public state : Interaction < SourceT > []
Array of all interactions in the current session. Each interaction contains:
Unique identifier for this interaction.
The AI-generated response.
Whether this interaction was aborted.
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.
Whether an error occurred during this interaction.
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 >
Search parameters. Same as SearchParams<AnyDocument>.
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 >>
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.
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.
Array of all messages in the conversation. Message role: 'system', 'user', or 'assistant'.
clearSession()
Clears all messages and interactions from the current session.
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
Show RegenerateLastParams properties
Whether to stream the regenerated response.
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:
The Orama database must have the Secure Proxy plugin installed
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.'
});