Spaces:
Running
Running
File size: 3,435 Bytes
16b7924 |
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import { historyDatabase } from "./history";
import { addLogEntry } from "./logEntries";
import { getSuppressNextFollowUp } from "./pubSub";
import { generateChatResponse } from "./textGeneration";
import type { ChatMessage } from "./types";
interface FollowUpQuestionParams {
topic: string;
currentContent: string;
previousQuestions?: string[];
}
export async function generateFollowUpQuestion({
topic,
currentContent,
previousQuestions = [],
}: FollowUpQuestionParams): Promise<string> {
try {
if (getSuppressNextFollowUp()) {
return "";
}
addLogEntry("Generating a follow-up question");
await historyDatabase.chatHistory
.add({
role: "user",
content: topic,
timestamp: Date.now(),
conversationId: `follow-up-${Date.now()}`,
})
.catch((error) => {
addLogEntry(`Error saving chat history: ${error}`);
});
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, () => {});
await historyDatabase.chatHistory
.add({
role: "assistant",
content: response,
timestamp: Date.now(),
conversationId: `follow-up-${Date.now()}`,
})
.catch((error) => {
addLogEntry(`Error saving chat history: ${error}`);
});
const lines = response
.trim()
.split("\n")
.map((line) => line.trim())
.reverse()
.find((line) => line.endsWith("?"));
if (!lines) {
addLogEntry("No valid follow-up question generated");
return "";
}
let questionLine = lines.replace(/^[^a-zA-Z]+/, "");
questionLine = questionLine.charAt(0).toUpperCase() + questionLine.slice(1);
addLogEntry("Generated follow-up question successfully");
await historyDatabase.chatHistory
.add({
role: "assistant",
content: questionLine,
timestamp: Date.now(),
conversationId: `follow-up-${Date.now()}`,
})
.catch((error) => {
addLogEntry(`Error saving chat history: ${error}`);
});
return questionLine;
} catch (error) {
addLogEntry(`Error generating follow-up question: ${error}`);
return "";
}
}
|