File

src/blogs/blog-summary.service.ts

Index

Methods

Constructor

constructor(openAiService: OpenAIWrappedService)
Parameters :
Name Type Optional
openAiService OpenAIWrappedService No

Methods

Async generateBlogSummary
generateBlogSummary(options: CreateBlogSummaryDto)
Parameters :
Name Type Optional
options CreateBlogSummaryDto No
Returns : unknown
Private generatePrompt
generatePrompt(language: string, maxLength: number, tone: string)
Parameters :
Name Type Optional
language string No
maxLength number No
tone string No
Returns : any
import { Injectable } from "@nestjs/common";

import { OpenAIWrappedService } from "../openai-wrapped/openai-wrapped.service";
import { CreateBlogSummaryDto } from "./dtos/create-blog-summary.dto";

@Injectable()
export class BlogSummaryService {
  constructor(private openAiService: OpenAIWrappedService) {}

  private generatePrompt(language: string, maxLength: number, tone: string) {
    return [
      `I will generate a blog summary for a blog with the specifications mentioned below`,
      `The summary should be written in the first person with a ${tone} tone`,
      `Summary language: ${language}`,
      `Summary must be a maximum of ${maxLength} characters.`,
      "Exclude anything unnecessary such as translation. Your entire response will be used to summarize and analyze the blog.",
    ].join("\n");
  }

  async generateBlogSummary(options: CreateBlogSummaryDto) {
    const content = `Blog Title: ${options.blogTitle}\n\nBlog Content: ${options.blogMarkdown}`;

    try {
      const completion = this.openAiService.generateCompletion(
        this.generatePrompt(options.language, options.summaryLength, options.tone),
        content,
        options.temperature
      );

      return completion;
    } catch (error: unknown) {
      if (error instanceof Error) {
        console.error("OpenAI error: ", error.message);
      }
    }
  }
}

results matching ""

    No results matching ""