Spaces:
Sleeping

File size: 2,696 Bytes
90b3952
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//todo: can fetch and generate methods be unified?
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { HfInference } from "@huggingface/inference";

const MODEL = "mistralai/Mistral-7B-Instruct-v0.3";

/**
 * Fetches the system prompt from the markdown file.
 * @returns {string} The prompt content.
 */

export const fetchPrompt = () => {
  try {
    const __dirname = path.dirname(fileURLToPath(import.meta.url));
    const promptFilePath = path.resolve(__dirname, "../prompts/feedback.md");

    const content = fs.readFileSync(promptFilePath, "utf-8").trim();
    console.log("Fetched Prompt:", content); // Debugging

    return content;
  } catch (error) {
    console.error("Error reading prompt:", error.message);
    throw new Error("Failed to load default prompt.");
  }
};

/**
 * Fetches feedback analysis using Hugging Face's chatCompletion API.
 * @param {string} text - The text to analyze.
 * @param {string} sentiment - The detected sentiment of the text.
 * @param {string[]} emotions - The detected emotions in the text.
 * @returns {string} Generated feedback from Mistral.
 */
const fetchFeedbackAnalysis = async (
  text,
  sentiment,
  emotions,
  customPrompt = null,
  apiKey
) => {
  try {
    const hf = new HfInference(apiKey);
    const systemPrompt = customPrompt || fetchPrompt();

    console.log("Used prompt: ", systemPrompt);

    const context = `Users input detected sentiment: ${sentiment}. Detected emotions: ${emotions}.`;

    const messages = [
      { role: "system", content: `${systemPrompt}\n\n${context}` },
      { role: "user", content: text },
    ];

    const chatCompletion = await hf.chatCompletion({
      model: MODEL,
      messages,
      max_tokens: 500,
    });

    const feedback = chatCompletion.choices[0].message.content.trim();
    console.log("Generated feedback:", feedback);
    return feedback;
  } catch (error) {
    console.error("Error fetching feedback analysis:", error.message);
    throw error;
  }
};

/**
 * Generates feedback based on text input, sentiment, and emotions.
 * @param {string} text - The text to analyze.
 * @param {string} sentiment - The detected sentiment of the text.
 * @param {string[]} emotions - The detected emotions in the text.
 * @returns {string} Processed feedback.
 */
export const generateFeedback = async (
  text,
  sentiment,
  emotions,
  customPrompt,
  apiKey
) => {
  try {
    const feedback = await fetchFeedbackAnalysis(
      text,
      sentiment,
      emotions,
      customPrompt,
      apiKey
    );
    return feedback;
  } catch (error) {
    console.error("Error in generateFeedback:", error.message);
    throw error;
  }
};