The Librarian API is an interface for managing sessions with Neuradex’s built-in AI assistant (Librarian ). Librarian provides RAG functionality that answers questions by referencing your knowledge base .
Overview
const client = new NdxClient ({
apiKey: process . env . NEURADEX_API_KEY ,
projectId: 'your-project-id' ,
});
// Access Librarian API
const sessions = await client . librarian . listSessions ();
const session = await client . librarian . getSession ( 'session-id' );
Method List
Method Description listSessions()Get list of sessions getSession(id, options?)Get session details with message history getSessionAsYaml(id)Get session in YAML format getSessionAsJson(id)Get session in JSON format deleteSession(id)Delete a session
listSessions()
Get list of Librarian sessions in the project.
Return Value
interface LibrarianSession {
id : string ;
title : string ;
messageCount : number ;
createdAt : string ;
updatedAt : string ;
}
Usage
const sessions = await client . librarian . listSessions ();
for ( const session of sessions ) {
console . log ( ` ${ session . title } ( ${ session . messageCount } messages)` );
console . log ( ` Created: ${ session . createdAt } ` );
}
getSession()
Get session details with message history.
Parameters
Output format: 'json' | 'yaml'
Return Value
interface LibrarianSessionWithMessages {
id : string ;
title : string ;
messages : LibrarianMessage [];
metadata : object ;
createdAt : string ;
updatedAt : string ;
}
interface LibrarianMessage {
role : 'user' | 'assistant' ;
content : string ;
sources ?: KnowledgeSource [];
timestamp : string ;
}
Usage
const session = await client . librarian . getSession ( 'session-id' );
console . log ( `Session: ${ session . title } ` );
for ( const message of session . messages ) {
const prefix = message . role === 'user' ? 'User' : 'Librarian' ;
console . log ( ` ${ prefix } : ${ message . content } ` );
// Display referenced knowledge
if ( message . sources ) {
for ( const source of message . sources ) {
console . log ( ` Source: ${ source . title } ` );
}
}
}
getSessionAsYaml() / getSessionAsJson()
Get session in a specific format. Useful for LLM input or exports.
Usage
// Get in YAML format (human-readable)
const yamlData = await client . librarian . getSessionAsYaml ( 'session-id' );
console . log ( yamlData );
// Get in JSON format (easy to process programmatically)
const jsonData = await client . librarian . getSessionAsJson ( 'session-id' );
const parsed = JSON . parse ( jsonData );
deleteSession()
Delete a session.
Parameters
Usage
await client . librarian . deleteSession ( 'session-id' );
console . log ( 'Session deleted' );
Use Cases
Analyzing Session History
async function analyzeLibrarianUsage () {
const sessions = await client . librarian . listSessions ();
let totalMessages = 0 ;
const topicsMap = new Map < string , number >();
for ( const session of sessions ) {
totalMessages += session . messageCount ;
// Get session details to analyze topics
const detail = await client . librarian . getSession ( session . id );
for ( const message of detail . messages ) {
if ( message . role === 'user' ) {
// Extract keywords from questions (simplified example)
const keywords = extractKeywords ( message . content );
for ( const keyword of keywords ) {
topicsMap . set ( keyword , ( topicsMap . get ( keyword ) || 0 ) + 1 );
}
}
}
}
console . log ( `Total messages: ${ totalMessages } ` );
console . log ( 'Frequently asked topics:' );
for ( const [ topic , count ] of topicsMap ) {
console . log ( ` ${ topic } : ${ count } times` );
}
}
Exporting Sessions
import fs from 'fs' ;
async function exportSessions () {
const sessions = await client . librarian . listSessions ();
const exportData = [];
for ( const session of sessions ) {
const detail = await client . librarian . getSession ( session . id );
exportData . push ( detail );
}
fs . writeFileSync (
'librarian-sessions-backup.json' ,
JSON . stringify ( exportData , null , 2 )
);
console . log ( `Exported ${ sessions . length } sessions` );
}
Next Steps
Knowledge API Knowledge management
Memory API Context assembly