#llm #ai Created at 170623 # [Anonymous feedback](https://www.admonymous.co/louis030195) # [[Epistemic status]] #shower-thought Last modified date: 170623 Commit: 0 # Related # Tree of thoughts agent ``` Procedure Tree_of_Thoughts(x, pθ, G, k, V, T, b): Input: x: the input problem pθ: pre-trained language model G: thought generator k: number of candidate thoughts to be generated V: state evaluator T: maximum number of search steps b: breadth limit for BFS algorithm 1. Initialize tree of thoughts (ToT) with input problem x at the root 2. Decompose the problem into thought steps, considering problem properties 3. Set t = 0 4. While t < T: a. Generate k candidate thoughts for each state using G(pθ, s, k) b. Evaluate the candidates using V(pθ, S) c. Select the b most promising candidates based on the evaluation values d. Update the tree of thoughts with the selected candidates e. Increment t by 1 5. Return the output generated by G(pθ, arg maxs∈ST VT (s), 1) Procedure BFS(x, pθ, G, k, V, T, b): Input: Same as Tree_of_Thoughts S0 ← {x} for t = 1 to T do a. Generate candidate thoughts using G(pθ, s, k) b. Evaluate candidate thoughts using V(pθ, S0t) c. Select the b most promising candidates d. Update the current state with the selected candidates return G(pθ, arg maxs∈ST VT (s), 1) Procedure DFS(s, t, pθ, G, k, V, T, vth): Input: Same as Tree_of_Thoughts, plus current state s, step t, and threshold vth if t > T: Record output G(pθ, s, 1) return for s0 in G(pθ, s, k): if V(pθ, {s0})(s) > vth: DFS(s0, t + 1, pθ, G, k, V, T, vth) ``` ```js // WARNING: this is extremely experimental code and is likely not following rigorously enough the Tree Of Thoughts agent paper. // import embedbase const { createClient } = require('embedbase-js'); // initialize client const embedbase = createClient( // this uses the hosted version of embedbase 'https://api.embedbase.xyz', // Go to https://app.embedbase.xyz/signup to get your own api key '...'); const epistemologicalComments = require("./karl-popper-agent.json").comments; const getContexts = async (problem) => { const searchResults = await embedbase.internetSearch(problem); return searchResults.map((result) => result.snippet); }; const treeOfThoughts = async (comment) => { const problem = `Is the statement "${comment.text}" true?`; const hypothesis = `The statement "${comment.text}" is true.`; const counterHypothesis = `The statement "${comment.text}" is false.`; const MAX_DEPTH = 3; const NUM_CANDIDATES = 5; const THRESHOLD = 0.6; const contexts = await getContexts(problem); async function getTruthValue(node, depth) { if (depth >= MAX_DEPTH) return node.value; const prompt = `Given the following context: ${contexts.join(" ")} Which of the following statements has a higher probability of being true: "${hypothesis}" or "${counterHypothesis}"?`; const candidates = []; for (let i = 0; i < NUM_CANDIDATES; i++) { candidates.push((await embedbase.generate(prompt).get()).join("")); console.log(`Generated candidate: ${candidates[i]}`); } const supportingEvidence = candidates.filter((c) => c.includes(hypothesis)).length; const supportingRatio = supportingEvidence / NUM_CANDIDATES; if (supportingRatio > THRESHOLD) { node.value = "true"; } else if (supportingRatio < 1 - THRESHOLD) { node.value = "false"; } else { node.value = "uncertain"; node.children.push({ text: hypothesis, value: null, children: [] }); node.children.push({ text: counterHypothesis, value: null, children: [] }); for (const child of node.children) { await getTruthValue(child, depth + 1); } } return node.value; } const rootNode = { text: problem, value: null, children: [] }; console.log(`Generating thoughts for problem: "${problem}"`); return await getTruthValue(rootNode, 0); }; const evaluateComments = async (comments) => { for (const comment of comments) { const truthValue = await treeOfThoughts(comment); comment.truth = truthValue; console.log(`Truth value for comment "${comment.text}" is ${comment.truth}`); } return comments; }; evaluateComments(epistemologicalComments).then((updatedComments) => { console.log(updatedComments.map((comment) => ({ id: comment.id, truth: comment.truth }))); }); ```