Integrate an AI chatbot feature allowing users to interact with media outlets, fetch chat history, and generate AI responses using OpenAI. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9a264234-c5d7-4dcc-adf3-a954b149b30d Replit-Commit-Checkpoint-Type: intermediate_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/3df548ff-50ae-432f-9be4-25d34eccc983/9a264234-c5d7-4dcc-adf3-a954b149b30d/d35d7YU
55 lines
2.7 KiB
TypeScript
55 lines
2.7 KiB
TypeScript
import OpenAI from "openai";
|
|
|
|
// This is using Replit's AI Integrations service, which provides OpenAI-compatible API access without requiring your own OpenAI API key.
|
|
// the newest OpenAI model is "gpt-5" which was released August 7, 2025. do not change this unless explicitly requested by the user
|
|
const openai = new OpenAI({
|
|
baseURL: process.env.AI_INTEGRATIONS_OPENAI_BASE_URL,
|
|
apiKey: process.env.AI_INTEGRATIONS_OPENAI_API_KEY
|
|
});
|
|
|
|
interface ChatbotPersonality {
|
|
systemPrompt: string;
|
|
name: string;
|
|
}
|
|
|
|
export function getChatbotPersonality(outletName: string, outletCategory: string, outletDescription?: string): ChatbotPersonality {
|
|
const categoryContext = {
|
|
people: `You are an AI assistant representing ${outletName}. ${outletDescription ? outletDescription : 'You speak from their perspective, embodying their personality, values, and communication style.'} Engage with users as if you are ${outletName} themselves, sharing insights, opinions, and knowledge from their unique perspective.`,
|
|
topics: `You are an AI expert on ${outletName}. ${outletDescription ? outletDescription : 'You provide in-depth knowledge and analysis about this topic.'} Answer questions with expertise and provide thoughtful commentary from various angles on this subject.`,
|
|
companies: `You are an AI representative of ${outletName}. ${outletDescription ? outletDescription : 'You speak on behalf of the company, sharing information about its mission, products, and values.'} Engage professionally while representing the company's interests and perspective.`
|
|
};
|
|
|
|
const systemPrompt = categoryContext[outletCategory as keyof typeof categoryContext] ||
|
|
`You are an AI assistant for ${outletName}. Provide helpful and informative responses.`;
|
|
|
|
return {
|
|
systemPrompt: systemPrompt + '\n\nKeep responses conversational, engaging, and concise (2-4 paragraphs maximum). Use Korean language for communication.',
|
|
name: outletName
|
|
};
|
|
}
|
|
|
|
export async function generateChatbotResponse(
|
|
messages: Array<{ role: 'user' | 'assistant' | 'system'; content: string }>,
|
|
outletName: string,
|
|
outletCategory: string,
|
|
outletDescription?: string
|
|
): Promise<string> {
|
|
const personality = getChatbotPersonality(outletName, outletCategory, outletDescription);
|
|
|
|
const chatMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
|
|
{ role: 'system', content: personality.systemPrompt },
|
|
...messages.map(msg => ({
|
|
role: msg.role as 'user' | 'assistant',
|
|
content: msg.content
|
|
}))
|
|
];
|
|
|
|
const completion = await openai.chat.completions.create({
|
|
model: 'gpt-5',
|
|
messages: chatMessages,
|
|
max_completion_tokens: 8192,
|
|
});
|
|
|
|
return completion.choices[0]?.message?.content || '죄송합니다. 응답을 생성할 수 없습니다.';
|
|
}
|