Files
sapiens-mobile/server/openai-utils.ts
jungwoo choi 919afe56f2 feat: SAPIENS Mobile App - Initial commit
React Native mobile application for SAPIENS news platform.
Consolidated all previous history into single commit.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-23 14:30:25 +09:00

75 lines
3.3 KiB
TypeScript

import OpenAI from "openai";
// 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({ apiKey: process.env.OPENAI_API_KEY });
export interface MediaOutletProfile {
name: string;
category: string;
focusSubject: string;
bio: string;
fullBio: string[];
}
export async function generateDetailedProfile(outlet: MediaOutletProfile): Promise<string> {
try {
const prompt = `Create a comprehensive, Wikipedia-style profile for the following media outlet/subject. The profile should be detailed, extensive, and well-structured with multiple sections. Write in a neutral, encyclopedic tone similar to Wikipedia articles.
Media Outlet Information:
- Name: ${outlet.name}
- Category: ${outlet.category}
- Focus Subject: ${outlet.focusSubject}
- Current Bio: ${outlet.bio}
- Additional Info: ${outlet.fullBio.join(' ')}
Please create a detailed profile that includes:
1. **Overview/Introduction** - Comprehensive introduction paragraph
2. **Background & History** - Detailed history and formation
3. **Key Achievements** - Major accomplishments and milestones
4. **Technology & Innovation** - Technical aspects, innovations, or methodologies (if applicable)
5. **Market Position & Influence** - Position in industry/market and influence
6. **Notable Developments** - Significant events, partnerships, or developments
7. **Future Outlook** - Current projects and future direction
8. **Industry Impact** - Broader impact on the industry or field
For people: Include personal background, career history, education, major contributions, and influence.
For companies: Include founding story, business model, products/services, market position, and key partnerships.
For topics/technologies: Include technical background, development history, applications, and significance.
Make it comprehensive and informative, similar to a detailed Wikipedia article. Use HTML formatting with proper headings (h2, h3), paragraphs, and lists where appropriate. Aim for 2000-3000 words.
Respond with only the HTML content, no markdown or additional formatting.`;
const response = await openai.chat.completions.create({
model: "gpt-5",
messages: [{ role: "user", content: prompt }],
max_completion_tokens: 4000
});
return response.choices[0].message.content || "";
} catch (error) {
console.error("Error generating detailed profile:", error);
throw new Error(`Failed to generate profile: ${error instanceof Error ? error.message : "Unknown error"}`);
}
}
export async function generateBatchProfiles(outlets: MediaOutletProfile[]): Promise<Record<string, string>> {
const profiles: Record<string, string> = {};
for (const outlet of outlets) {
try {
console.log(`Generating profile for ${outlet.name}...`);
const profile = await generateDetailedProfile(outlet);
profiles[outlet.name] = profile;
// Add a small delay to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 1000));
} catch (error) {
console.error(`Failed to generate profile for ${outlet.name}:`, error);
profiles[outlet.name] = `<h2>Profile Generation Error</h2><p>Unable to generate detailed profile for ${outlet.name}. Please try again later.</p>`;
}
}
return profiles;
}