From 2d7154cf4cbaa08e03c5c2669f7b60dc3165e5be Mon Sep 17 00:00:00 2001 From: tope-olajide <31278728+tope-olajide@users.noreply.github.com> Date: Mon, 2 Dec 2024 18:18:24 +0100 Subject: [PATCH] Fix summarizeText instruction prompt and serverless function - Corrected the instruction prompt for text summarization to ensure clarity and accuracy. - Fixed issues in the summarizeText serverless function to improve functionality and performance. --- assembly/index.ts | 20 +++++++++----- assembly/instructions.ts | 60 +++++++++++----------------------------- 2 files changed, 29 insertions(+), 51 deletions(-) diff --git a/assembly/index.ts b/assembly/index.ts index 651a258..8399591 100644 --- a/assembly/index.ts +++ b/assembly/index.ts @@ -1,21 +1,26 @@ -import { models } from "@hypermode/modus-sdk-as" +import { models } from "@hypermode/modus-sdk-as"; import { OpenAIChatModel, ResponseFormat, SystemMessage, UserMessage, -} from "@hypermode/modus-sdk-as/models/openai/chat" -import { bulletPointSummaryInstruction, generateSummaryInstruction, grammarCheckerInstruction, SummaryCategory, SummaryMode } from "./instructions"; +} from "@hypermode/modus-sdk-as/models/openai/chat"; +import { + bulletPointSummaryInstruction, + generateSummaryInstruction, + grammarCheckerInstruction, +} from "./instructions"; export function sayHello(name: string | null = null): string { + return "hello " + (name != null ? name : "world"); +} - return `hello ${name || "world"}`;} +const grammarCheckerModel: string = "grammar-error-checker"; +const summarizeTextModel: string = "text-summarizer"; -const grammarCheckerModel: string = "grammar-error-checker" -const summarizeTextModel:string = "text-summarizer" export function checkGrammarErrors(text: string): string { const model = models.getModel(grammarCheckerModel) const input = model.createInput([ @@ -27,7 +32,8 @@ export function checkGrammarErrors(text: string): string { return output.choices[0].message.content.trim() } -export function summarizeText(text: string, length:SummaryCategory, summaryMode:SummaryMode ): string { + +export function summarizeText(text: string, length:string, summaryMode:string ): string { const model = models.getModel(summarizeTextModel) const summarizeTextInstruction = generateSummaryInstruction(length) const bulletPointSummaryPrompt = bulletPointSummaryInstruction; diff --git a/assembly/instructions.ts b/assembly/instructions.ts index 65f8ef7..e49140c 100644 --- a/assembly/instructions.ts +++ b/assembly/instructions.ts @@ -1,4 +1,4 @@ -const grammarCheckerInstruction = ` +export const grammarCheckerInstruction = ` You are a grammar expert tasked with analyzing text for grammatical errors. Analyze the provided text for any grammatical errors, including spelling, punctuation, syntax, verb tense, subject-verb agreement, and word choice issues. For each grammatical error found, provide the following details: @@ -27,37 +27,25 @@ Return the results in the following JSON format: -export type SummaryCategory = "Brief" | "Concise" | "Detailed" | "Comprehensive"; -export type SummaryMode = "Bullet" | "Paragraph" -function generateSummaryInstruction(category: SummaryCategory): string { - // Define category descriptions - const categoryDescriptions: { [key in SummaryCategory]: string } = { - Brief: "A very short summary focusing only on the core idea.", - Concise: "A slightly longer summary highlighting key points clearly and succinctly.", - Detailed: "A more descriptive summary with additional context and nuances.", - Comprehensive: "An in-depth and thorough summary covering all critical details." - }; - - // Prompt construction - const prompt = ` - There are four summary length categories: Brief, Concise, Detailed, and Comprehensive. - - Brief: ${categoryDescriptions.Brief} - - Concise: ${categoryDescriptions.Concise} - - Detailed: ${categoryDescriptions.Detailed} - - Comprehensive: ${categoryDescriptions.Comprehensive} - - Based on the specified length category (${category}), generate an appropriate summary of the given text - `; - - return prompt; - } -const bulletPointSummaryInstruction = ` +export function generateSummaryInstruction(category: string): string { + const prompt: string = ` + There are four summary length categories: Brief, Concise, Detailed, and Comprehensive. + - Brief: A very short summary focusing only on the core idea. + - Concise: A slightly longer summary highlighting key points clearly and succinctly. + - Detailed: A more descriptive summary with additional context and nuances. + - Comprehensive: An in-depth and thorough summary covering all critical details. + + Based on the specified length category (${category}), generate an appropriate summary of the given text. + Return only the summary text without any prefixes, introductory phrases, or meta-commentary. `; -"Summarize the following text into key bullet points, each representing a distinct idea or concept. Return the summary in the form of a JSON array, where each bullet point is a separate item in the array. + return prompt; +} +export const bulletPointSummaryInstruction: string = ` +Summarize the following text into key bullet points, each representing a distinct idea or concept. Return the summary in the form of a JSON array, where each bullet point is a separate item in the array. Expected output format: @@ -67,20 +55,4 @@ Expected output format: "Bullet point 3", "Bullet point n" ] - -**Important**: Do not include markdown-style code block indicators (\`json and \`\`) in your response. Return only the array. - -` - - - - - - - - - - - - -export { grammarCheckerInstruction, generateSummaryInstruction, bulletPointSummaryInstruction} \ No newline at end of file +`;