|
|
const OpenAI = require('openai');
|
|
|
|
|
|
const openai = new OpenAI({
|
|
|
apiKey: process.env.OPENAI_API_KEY,
|
|
|
});
|
|
|
|
|
|
|
|
|
const web3Responses = [
|
|
|
"I can help you build that! Let me create a smart contract template for you. Would you like me to start with a basic ERC-20 token or something more complex?",
|
|
|
"Great idea! For Web3 development, I recommend using Hardhat for the development environment. Let me show you how to set up the project structure.",
|
|
|
"That's a solid approach! I can help you implement the security patterns and gas optimizations. Which blockchain are you targeting?",
|
|
|
"Perfect! I'll help you integrate with the trading APIs. For Binance integration, we'll need to handle authentication and websocket connections properly.",
|
|
|
"Excellent question! The best practice for DeFi protocols is to use OpenZeppelin's battle-tested contracts as a foundation. Let me show you the implementation.",
|
|
|
"I can definitely help with that! For NFT projects, we should consider the metadata standards and IPFS integration. What type of NFT collection are you planning?",
|
|
|
"Smart choice! I'll help you set up the analytics dashboard with on-chain data. We can use The Graph protocol for efficient querying.",
|
|
|
"Absolutely! Security audits are crucial. I'll review your contract for common vulnerabilities like reentrancy, overflow, and access control issues."
|
|
|
];
|
|
|
|
|
|
const generateAIResponse = async (userMessage, conversationId = null) => {
|
|
|
const startTime = Date.now();
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (process.env.OPENAI_API_KEY && process.env.OPENAI_API_KEY !== 'your-openai-api-key') {
|
|
|
try {
|
|
|
const completion = await openai.chat.completions.create({
|
|
|
model: "gpt-3.5-turbo",
|
|
|
messages: [
|
|
|
{
|
|
|
role: "system",
|
|
|
content: "You are CRAITE AI, a specialized Web3 development assistant. You help with smart contracts, dApps, DeFi protocols, NFTs, blockchain development, security audits, and gas optimization. You have access to tools for OpenZeppelin, Solidity, Hardhat, Foundry, trading APIs (Binance, Hyperliquid, Axiom), analytics, and on-chain data. Always provide practical, secure, and optimized solutions."
|
|
|
},
|
|
|
{
|
|
|
role: "user",
|
|
|
content: userMessage
|
|
|
}
|
|
|
],
|
|
|
max_tokens: 1000,
|
|
|
temperature: 0.7,
|
|
|
});
|
|
|
|
|
|
const processingTime = Date.now() - startTime;
|
|
|
|
|
|
return {
|
|
|
content: completion.choices[0].message.content,
|
|
|
metadata: {
|
|
|
model: "gpt-3.5-turbo",
|
|
|
tokens: completion.usage.total_tokens,
|
|
|
processingTime
|
|
|
}
|
|
|
};
|
|
|
} catch (openaiError) {
|
|
|
console.error('OpenAI API error:', openaiError.message);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
const response = web3Responses[Math.floor(Math.random() * web3Responses.length)];
|
|
|
const processingTime = Date.now() - startTime;
|
|
|
|
|
|
return {
|
|
|
content: response,
|
|
|
metadata: {
|
|
|
model: "fallback",
|
|
|
tokens: null,
|
|
|
processingTime
|
|
|
}
|
|
|
};
|
|
|
} catch (error) {
|
|
|
console.error('AI service error:', error);
|
|
|
|
|
|
return {
|
|
|
content: "I apologize, but I'm experiencing some technical difficulties right now. Please try again in a moment.",
|
|
|
metadata: {
|
|
|
model: "error",
|
|
|
tokens: null,
|
|
|
processingTime: Date.now() - startTime
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
|
generateAIResponse
|
|
|
}; |