S SmartDocs
系列: Golden-Hour-backend-layer-2 typescript 35 行 · 更新于 2026-02-22

geminiService.ts

Golden-Hour-backend-layer-2/services/geminiService.ts


import { GoogleGenAI } from "@google/genai";

export class GeminiService {
  // Use the correct initialization pattern inside methods as per latest guidelines
  static async generateContentSummary(title: string, category: string): Promise<string> {
    const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
    try {
      const response = await ai.models.generateContent({
        model: 'gemini-3-flash-preview',
        contents: `Generate a short 2-sentence SEO-friendly description for a ${category} item titled "${title}".`,
      });
      // Directly access .text property as per guidelines
      return response.text || "No summary generated.";
    } catch (error) {
      console.error("Gemini Error:", error);
      return "Failed to generate AI summary.";
    }
  }

  static async analyzeBusinessData(data: any): Promise<string> {
    const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
    try {
      const response = await ai.models.generateContent({
        model: 'gemini-3-flash-preview',
        contents: `As a business analyst, provide 3 bullet points of insights based on this management dashboard data: ${JSON.stringify(data)}`,
      });
      // Directly access .text property as per guidelines
      return response.text || "No insights available.";
    } catch (error) {
      console.error("Gemini Error:", error);
      return "Analysis unavailable.";
    }
  }
}

相关文章