File size: 2,157 Bytes
8e074b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import ollama from "ollama";

// Function to call the Ollama model through the library
async function callLLM(query) {
  try {
    const response = await ollama.chat({
      model: "qwen-2.5-coder", // Replace with the specific model you want
      messages: [{ role: "user", content: query }],
    });
    return response.response.trim();
  } catch (error) {
    console.error("Error:", error);
    throw error;
  }
}

// Function to create a chain of thought for the input question
async function chainOfThought(inputQuery) {
  let thoughtChain = [];

  const step1 = `Break down the following question into key points: "${inputQuery}"`;
  const understanding = await callLLM(step1);
  thoughtChain.push(understanding);

  const step2 = `Given the key points: "${understanding}", provide any relevant background information.`;
  const context = await callLLM(step2);
  thoughtChain.push(context);

  const step3 = `Analyze the following question based on its background information: "${inputQuery}". What are the different aspects to consider?`;
  const analysis = await callLLM(step3);
  thoughtChain.push(analysis);

  const step4 = `Based on the analysis: "${analysis}", generate possible solutions or insights.`;
  const solutions = await callLLM(step4);
  thoughtChain.push(solutions);

  const step5 = `Given the possible solutions: "${solutions}", evaluate the pros and cons, or refine the best approach.`;
  const evaluation = await callLLM(step5);
  thoughtChain.push(evaluation);

  const step6 = `Based on the evaluation: "${evaluation}", provide a concise and well-reasoned answer to the original question.`;
  const conclusion = await callLLM(step6);
  thoughtChain.push(conclusion);

  return {
    thoughtProcess: thoughtChain,
    finalAnswer: conclusion,
  };
}

// Test the function with an example question
const inputQuestion =
  "How can we improve network security in a large organization?";
chainOfThought(inputQuestion)
  .then((response) => {
    console.log("Thought Process:", response.thoughtProcess);
    console.log("Final Answer:", response.finalAnswer);
  })
  .catch((error) => {
    console.error("Error:", error);
  });