File size: 2,678 Bytes
06b47dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import type { ChatMessage } from "gpt-tokenizer/GptEncoding";
import { addLogEntry } from "./logEntries";
import { generateChatResponse } from "./textGeneration";

interface FollowUpQuestionParams {
  topic: string;
  currentContent: string;
  previousQuestions?: string[];
}

export async function generateFollowUpQuestion({
  topic,
  currentContent,
  previousQuestions = [],
}: FollowUpQuestionParams): Promise<string> {
  try {
    addLogEntry("Generating a follow-up question");

    const promptMessages: ChatMessage[] = [
      {
        role: "user",
        content: `You are a question generator.

Your task is to generate a follow-up question to the topic based on the response.

The topic was:
~~~
${topic}
~~~

The response was:
~~~
${currentContent}
~~~

Follow these instructions:

1. Language detection and matching:
   - Carefully analyze the language of the original question and response.
   - The follow-up question must be written in the exact same language as both the original question and response.
   - Pay special attention to distinguishing between similar languages.
   - If in doubt, prioritize matching the language of the most recent message.

2. Question generation rules:
   - Generate one short follow-up question exploring an important unexplored aspect of the topic.
   - The question must be different from all previously asked questions listed below.
   - Keep it to 1-2 sentences maximum.
   - End with a question mark.

Previously asked follow-up questions (do not repeat):
${
  previousQuestions.length > 0
    ? previousQuestions.map((q, i) => `${i + 1}. ${q}`).join("\n")
    : "(No previous follow-up questions yet)"
}

Respond with just the question, no additional text or explanations.`,
      },
    ];

    const response = await generateChatResponse(promptMessages, () => {});

    const lines = response
      .trim()
      .split("\n")
      .map((line) => line.trim())
      .filter((line) => line.length > 0);

    let questionLine = lines
      .reverse()
      .find(
        (line) =>
          line.endsWith("?") || line.endsWith('?"') || line.endsWith("?'"),
      );

    if (!questionLine) {
      addLogEntry("No valid follow-up question generated");
      return "";
    }

    if (questionLine.startsWith('"') || questionLine.startsWith("'")) {
      questionLine = questionLine.slice(1);
    }

    if (questionLine.endsWith('"') || questionLine.endsWith("'")) {
      questionLine = questionLine.slice(0, -1);
    }

    addLogEntry("Generated follow-up question successfully");

    return questionLine;
  } catch (error) {
    addLogEntry(`Error generating follow-up question: ${error}`);
    return "";
  }
}