import * as lai from 'lucidicai';
import dotenv from 'dotenv';
dotenv.config();
interface UserQuery {
question: string;
context?: string;
}
class CustomerSupportAssistant {
private openai: any;
async initialize() {
// Initialize Lucidic with session details
const OpenAI = (await import('openai')).default;
await lai.init({
sessionName: 'Customer Support Session',
task: 'Answer customer questions about our product',
instrumentModules: { OpenAI }
});
this.openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
}
async processQuery(query: UserQuery): Promise<string> {
try {
// Step 1: Analyze the question
await lai.createStep();
const analysis = await this.analyzeIntent(query.question);
await lai.endStep({ evalScore: 90, evalDescription: 'Successfully identified intent' });
// Step 2: Search for information
await lai.createStep();
const searchResults = await this.searchKnowledge(analysis);
await lai.endStep({ evalScore: 85, evalDescription: 'Found relevant articles' });
// Step 3: Generate response
await lai.createStep();
const response = await this.generateResponse(
query.question,
searchResults
);
await lai.endStep({ evalScore: 95, evalDescription: 'Generated comprehensive response' });
return response;
} catch (error) {
await lai.endStep({ evalScore: 0, evalDescription: `Error: ${error.message}` });
throw error;
}
}
private async analyzeIntent(question: string): Promise<string> {
const response = await this.openai.chat.completions.create({
model: 'gpt-4',
messages: [
{
role: 'system',
content: 'Extract the intent and key topics from the user question.'
},
{
role: 'user',
content: question
}
],
temperature: 0.3
});
return response.choices[0].message.content;
}
private async searchKnowledge(intent: string): Promise<string> {
// Simulate knowledge base search
const response = await this.openai.chat.completions.create({
model: 'gpt-4',
messages: [
{
role: 'system',
content: 'Based on the intent, provide relevant product information.'
},
{
role: 'user',
content: `Intent: ${intent}\n\nProvide relevant information about our product.`
}
]
});
return response.choices[0].message.content;
}
private async generateResponse(
question: string,
information: string
): Promise<string> {
const response = await this.openai.chat.completions.create({
model: 'gpt-4',
messages: [
{
role: 'system',
content: 'You are a helpful customer support assistant. Use the provided information to answer the user\'s question clearly and concisely.'
},
{
role: 'user',
content: `Question: ${question}\n\nRelevant Information: ${information}\n\nProvide a helpful response.`
}
],
temperature: 0.7
});
return response.choices[0].message.content;
}
async close(success: boolean = true) {
await lai.endSession({ isSuccessful: success, isSuccessfulReason: success ? 'Support session completed' : 'Session ended with errors' });
}
}
// Example usage
async function main() {
const assistant = new CustomerSupportAssistant();
try {
await assistant.initialize();
const response = await assistant.processQuery({
question: 'How do I reset my password?',
context: 'User has been locked out of their account'
});
console.log('\nResponse:', response);
await assistant.close(true);
console.log('\nSession completed and tracked!');
} catch (error) {
console.error('Error:', error);
await assistant.close(false);
}
}
main();