aura-ai
Version:
AI-powered marketing strategist CLI tool for developers
73 lines (60 loc) • 2.1 kB
JavaScript
import { openai } from '@ai-sdk/openai'
import { streamText } from 'ai'
import dotenv from 'dotenv'
import fs from 'fs/promises'
// 優先載入 .env.local,如果不存在則載入 .env
dotenv.config({ path: '.env.local' })
dotenv.config()
class ChatService {
constructor() {
this.apiKey = process.env.OPENAI_API_KEY
this.model = process.env.AI_MODEL || 'gpt-4o-mini'
this.temperature = parseFloat(process.env.AI_TEMPERATURE || '0.7')
this.maxTokens = parseInt(process.env.AI_MAX_TOKENS || '200')
}
async checkAuraConfig() {
try {
await fs.access('./aura.md')
return true
} catch {
return false
}
}
async getSystemPrompt() {
const hasConfig = await this.checkAuraConfig()
if (hasConfig) {
try {
const auraContent = await fs.readFile('./aura.md', 'utf-8')
return `You are Aura, an AI marketing strategist. You have analyzed the user's product and created a personalized marketing strategy.
Product Analysis:
${auraContent}
Provide specific, actionable marketing advice based on this product analysis. Be concise and practical.`
} catch (error) {
console.error('Error reading aura.md:', error)
}
}
return `You are Aura, an AI marketing strategist. The user hasn't run product analysis yet.
Provide general marketing advice and encourage them to run /init for personalized insights.
Keep responses concise and actionable.`
}
async sendMessage(messages) {
if (!this.apiKey || this.apiKey === 'your_openai_api_key_here') {
throw new Error('OpenAI API key not configured. Please set OPENAI_API_KEY in .env file')
}
try {
const systemPrompt = await this.getSystemPrompt()
const result = await streamText({
model: openai(this.model),
system: systemPrompt,
messages,
temperature: this.temperature,
maxTokens: this.maxTokens,
})
return result
} catch (error) {
console.error('Error calling OpenAI API:', error)
throw error
}
}
}
export default new ChatService()