import AlchemystAI from '@alchemystai/sdk';
import * as readline from 'readline';
import 'dotenv/config';
import { GoogleGenerativeAI } from "@google/generative-ai";
const client = new AlchemystAI({
apiKey: process.env.ALCHEMYST_AI_API_KEY || '',
});
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || '');
const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const askQuestion = (prompt: string): Promise<string> => {
return new Promise((resolve) => {
rl.question(prompt, (answer) => resolve(answer));
});
};
async function runCLI() {
console.log('Alchemyst AI CLI Tool');
console.log('Type your questions and get AI-powered answers with context!');
console.log('Type "exit", "quit", or "bye" to stop.\n');
if (!process.env.ALCHEMYST_AI_API_KEY || !process.env.GEMINI_API_KEY) {
console.error('Missing required environment variables.');
process.exit(1);
}
while (true) {
const userQuestion = await askQuestion('Ask me anything: ');
if (['exit', 'quit', 'bye', 'stop'].includes(userQuestion.toLowerCase().trim())) {
console.log('Goodbye!');
break;
}
if (!userQuestion.trim()) {
console.log(' Please enter a question.\n');
continue;
}
console.log('Searching for relevant context...');
const { contexts } = await client.v1.context.search({
query: userQuestion,
similarity_threshold: 0.8,
minimum_similarity_threshold: 0.5,
scope: 'internal',
});
let promptText = userQuestion;
if (contexts && contexts.length > 0) {
const formattedContexts = contexts
.map((c, i) => `Context ${i + 1}: ${c.content}`)
.join('\n\n');
promptText = `
Based on the following context, answer the question.
If the context is insufficient, say so clearly.
Contexts:
${formattedContexts}
Question: ${userQuestion}
`;
}
console.log('Generating response...');
const result = await model.generateContent(promptText);
console.log(result.response.text());
console.log('\n' + '─'.repeat(50) + '\n');
}
rl.close();
}
runCLI();