Skip to content

crazystuffxyz/deepthink-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deepthink

An AI reasoning engine with multi-step thinking, sandboxed code execution, deep web research, autonomous browser control, and self-verifying answer loops. Works with Ollama, OpenAI, Claude, Gemini, Perplexity, Grok, and LM Studio.


Table of Contents


Overview

Deepthink wraps any LLM provider with a stack of reasoning infrastructure:

  • Multi-provider support — Ollama, OpenAI, Claude (Anthropic), Gemini (Google), Perplexity, Grok (xAI), LM Studio, or any OpenAI-compatible endpoint
  • Multi-depth thinking — up to 3 staged internal reasoning passes (analysis → planning → sanity check) before the final response
  • Typed output parsing — returns string, integer, double, or boolean directly from free-form model output
  • Self-verification loops — adversarial and numerical checker agents review responses and drive iterative repair
  • Sandboxed code execution — generates and runs JavaScript (Node.js) and Python code in isolated subprocesses to verify numeric answers
  • MCTS consensus — runs multiple algorithmic approaches in parallel and votes on the most consistent result
  • 9-step deep research pipeline — query planning → crawling → credibility scoring → MMR diversity → fact verification → report writing → critique loops
  • Universal URL-to-HTML extractor — fetches and converts HTML, PDF, DOCX, XLSX, PPTX, EPUB, CSV, RTF, ODT, JSON, XML, Markdown, images, and SVG
  • Chrome TLS fingerprint spoofingimpit-backed axios adapter that bypasses bot-detection
  • Autonomous Electron browser — a free-roaming AI agent that browses the web with human-like mouse events, multi-tab management, and reflective session summaries
  • AI code project generator — cognitive planning → code generation → AST validation → sandbox execution → test oracle → iterative repair

Installation

Install deepthink-js from npm — all dependencies are bundled automatically:

npm install deepthink-js

For the Electron explorer example only:

npm install electron pdf-parse

For Python sandbox support (optional but recommended for numeric verification):

pip install sympy

Node.js ≥ 18 is required. The package uses ES Modules — set "type": "module" in your package.json.


Quick Start

import Deepthink from 'deepthink-js';

// Ollama (default — no API key needed)
const dt = new Deepthink('cogito-2.1:671b-cloud');

// Simple string generation
const answer = await dt.generate('What is the capital of France?');
console.log(answer); // "Paris"

// Typed integer output with 2-stage thinking and 2 verification checks
const count = await dt.generate(
  'How many prime numbers are less than 50?',
  { type: 'integer', depth: 2, checks: 2 }
);
console.log(count); // 15

// Streaming response
await dt.generate(
  'Explain the Riemann Hypothesis in plain language.',
  {
    depth: 1,
    onChunk: (chunk, meta) => process.stdout.write(chunk)
  }
);

Multi-Provider Setup

Pass clientOptions.provider to the constructor to route calls to any supported backend.

OpenAI

import Deepthink from 'deepthink-js';

const dt = new Deepthink('gpt-4o', [process.env.OPENAI_API_KEY], {
  provider: 'openai',
});

const reply = await dt.generate('What is 12 factorial?', { type: 'integer', depth: 2 });
console.log(reply);

Anthropic Claude

import Deepthink from 'deepthink-js';

const dt = new Deepthink('claude-opus-4-6', [process.env.ANTHROPIC_API_KEY], {
  provider: 'claude',
});

const reply = await dt.generate('Summarize the history of calculus.', { depth: 1 });
console.log(reply);

Google Gemini

import Deepthink from 'deepthink-js';

const dt = new Deepthink('gemini-2.5-flash', [process.env.GEMINI_API_KEY], {
  provider: 'gemini',
});

const reply = await dt.generate('Write a haiku about recursion.', { depth: 0 });
console.log(reply);

Perplexity

import Deepthink from 'deepthink-js';

const dt = new Deepthink('sonar-pro', [process.env.PERPLEXITY_API_KEY], {
  provider: 'perplexity',
});

const reply = await dt.generate('What happened in AI research this week?');
console.log(reply);

Grok (xAI)

import Deepthink from 'deepthink-js';

const dt = new Deepthink('grok-3', [process.env.XAI_API_KEY], {
  provider: 'grok',
});

const reply = await dt.generate('Explain quantum entanglement simply.', { depth: 1 });
console.log(reply);

LM Studio (local)

import Deepthink from 'deepthink-js';

// Start LM Studio, load a model, and enable the server on port 1234
const dt = new Deepthink('my-local-model', [], {
  provider: 'lmstudio',
  // Optional: override default http://localhost:1234/v1
  // baseUrl: 'http://192.168.1.5:1234/v1',
});

const reply = await dt.generate('Write a sorting algorithm in Python.', { depth: 1 });
console.log(reply);

Any OpenAI-Compatible Endpoint

import Deepthink from 'deepthink-js';

const dt = new Deepthink('my-model', ['my-api-key'], {
  provider: 'openai-compat',
  baseUrl: 'https://my-custom-server.example.com/v1',
});

const reply = await dt.generate('Hello!');
console.log(reply);

Core API — Deepthink Class

Constructor

new Deepthink(model, apiKeys, clientOptions, concurrency, auditModel)
Parameter Type Default Description
model string process.env.OLLAMA_MODEL || 'llama3.1' Primary model identifier
apiKeys string[] [] API keys for the selected provider, rotated automatically on failure and quarantined for 60 s after 2 consecutive errors
clientOptions object {} Provider configuration — see clientOptions table below
concurrency number Infinity Maximum simultaneous in-flight requests
auditModel string same as model Model used for verification checker agents

clientOptions fields:

Field Description
provider One of: ollama (default), openai, claude, gemini, perplexity, grok, lmstudio, openai-compat
baseUrl Override the base URL for the selected provider
host Alias for baseUrl
apiKey Inline API key (alternative to passing via constructor apiKeys array)
headers Extra HTTP headers merged into every request
anthropicVersion Anthropic API version header (default: 2023-06-01)

Environment variables (fallback when no key is passed):

Variable Used by provider
OLLAMA_HOST Ollama
OLLAMA_API_KEY Ollama
OLLAMA_MODEL Ollama
OPENAI_API_KEY OpenAI
ANTHROPIC_API_KEY Claude
GEMINI_API_KEY Gemini
PERPLEXITY_API_KEY Perplexity
XAI_API_KEY Grok

callChat()

Low-level chat method with automatic retry, streaming fallback, and API key rotation.

const result = await dt.callChat(messages, stream, onChunk, opts);
// result: { content: string, thinking: string }
Parameter Type Description
messages Message[] OpenAI-style [{ role, content }] array
stream boolean Enable streaming
onChunk function (chunk, { kind: 'content' | 'thinking' }) => void
opts object See options reference

Retries up to 3 times with exponential backoff (500 ms → 1 s → 2 s).


generate()

High-level generation with named parameters, multi-stage thinking, code sandboxing, and self-verification.

const result = await dt.generate(input, opts);
Parameter Type Description
input string | Message[] | object The prompt — plain string, messages array, or any object
opts object Named options — see full table below

All options are passed as a single named object. This makes call sites self-documenting:

// Simple call — all defaults
await dt.generate('What is pi?');

// Fully configured call
await dt.generate('Count the prime numbers below 1000', {
  type:    'integer',
  depth:   2,
  checks:  2,
  model:   'gpt-4o',          // override for this call only
  onChunk: chunk => process.stdout.write(chunk),
});

// Streaming creative writing
await dt.generate('Write a short story about a robot.', {
  depth:   1,
  onChunk: (chunk, meta) => process.stdout.write(chunk),
  options: { temperature: 0.9 },
});

// Research-style deep thinking with analytical decomposition
const answer = await dt.generate('What are all distinct ways to tile a 2×8 board with 1×2 dominoes?', {
  type:       'integer',
  depth:      3,
  checks:     3,
  analytical: true,
});

generate() options object:

Option Type Default Description
type string 'string' Return type: 'string', 'integer', 'double', 'boolean'
depth 0–3 1 Internal thinking stages before final answer
checks number 0 Verification checker passes (max 3)
onChunk function null Streaming callback (chunk, meta) => void
model string constructor model Override model for this call
systemPrompt string auto Custom system prompt
autoSystemPrompt boolean true Inject a default system prompt
think boolean false Enable model's native <think> token
enableCode boolean true Auto-detect and run sandboxed code
mcts boolean true Enable MCTS multi-approach consensus
mctsNumApproaches number 4 Algorithmic approaches for MCTS
mctsConsensusThreshold number 3 Minimum agreement count for HIGH confidence
analytical boolean false Multi-agent analytical decomposition mode
humanBrain boolean false Attach a BrainMemory (working + semantic)
maxCheckIterations number 10 Max self-verification repair iterations
monitorWindowSize number 5 MetacognitiveMonitor response history window
images string[] [] Base64 image strings for multimodal input
options object {} Raw provider sampling params (temperature, top_p, etc.)
_globalBudget object none { maxLLMCalls: number } — hard cap on total LLM calls
ollamaOutput boolean false Return raw <think> blocks in output (Ollama only)

Depth levels:

Depth Stages
0 Direct answer — no pre-thinking
1 Analysis pass
2 Analysis → Planning
3 Analysis → Planning → Sanity Check

generateAndRunProject() (via codeGenerator)

Generates a complete, runnable multi-file project from a task description.

import Deepthink from 'deepthink-js';
import { generateAndRunProject } from 'deepthink-js/thinking/codeGenerator.js';

const dt = new Deepthink('cogito-2.1:671b-cloud');
const callChat = dt.callChat.bind(dt);

const result = await generateAndRunProject(callChat, 'Build a CLI tool that converts CSV to JSON', {
  thinkingDepth:   2,
  maxProjectLoops: 6,
  maxOracleLoops:  3,
});

if (result.success) {
  console.log(result.files);        // { 'index.js': '...', ... }
  console.log(result.buildCommands);
  console.log(result.runCommands);
}

Research Agent

import Deepthink from 'deepthink-js';
import runDeepResearch from 'deepthink-js/thinking/researchAgent.js';

const dt = new Deepthink('cogito-2.1:671b-cloud', []);
const callChat = dt.callChat.bind(dt);

const result = await runDeepResearch(
  callChat,
  'What are the causes of the 2008 financial crisis?',
  {
    maxQueries:           12,
    maxConcurrency:       10,
    credibilityThreshold: 45,
    maxSummaries:         20,
    useOllamaSearch:      true,
    academicFilter:       false,
  }
);

console.log(result.report);      // Full markdown research report
console.log(result.references);  // Array of APA-formatted citations
console.log(result.claimCount);  // Number of verified facts
console.log(result.success);     // boolean

Pipeline steps:

Step Name Description
0 Answer Format Detection Classifies what a correct answer looks like
1 Query Planning Generates layered search queries at up to 3 recursion depths
2 Parallel Web Crawling Fetches all URLs concurrently via extractArticleText
3 Credibility Scoring Scores each source (0–100)
4 MMR Diversity Filter Maximal Marginal Relevance selects a diverse, high-credibility subset
5 Fact Verification Loop Each extracted claim is verified against its source
6 Report Writing Synthesises verified claims into a structured markdown report
7–9 Critique & Repair Loop Domain expert, adversarial, source fidelity, and math/logic critic agents

Internet Utilities

Universal Content Extractor

import { extractArticleText } from 'deepthink-js/internet/extractFromUrl.js';

const html = await extractArticleText('https://example.com/paper.pdf');

Supports HTML, PDF, DOCX, XLSX, PPTX, EPUB, CSV, TSV, JSON, XML, Markdown, plain text, RTF, ODT, SVG, and images.


Chrome-Fingerprinted Axios Adapter

import axios from 'deepthink-js/internet/axios.js';

const response = await axios.get('https://example.com', {
  responseType: 'arraybuffer',
  timeout: 15000,
});

Uses impit to mimic Chrome's TLS fingerprint, bypassing most bot-detection middleware.


Ollama Web Search

import { getOllamaSearchResults } from 'deepthink-js/internet/ollamaSearch.js';

const results = await getOllamaSearchResults('Riemann hypothesis latest research', 5);
// results: [{ title, link, snippet, cite }, ...]

Electron Free Explorer

An autonomous AI browsing agent that explores the web with human-like behaviour.

npm install electron pdf-parse && npx electron examples/electron_explorer.js

Press Ctrl+C to stop — a reflective journal-style session summary is generated and saved to disk.

Features:

  • Up to 3 concurrent browser tabs; least-productive tabs pruned automatically
  • Human-like mouse movement, clicking, triple-click-and-type, scroll, and key press simulation
  • PDF detection — automatically fetches and injects text as readable HTML
  • Goal compression every 20 loops — the AI builds and evolves a first-person "current focus" narrative
  • Topic drift detection — nudges the AI toward new territory after 8+ loops on the same hostname
  • Full action log written to log.txt; session summaries saved as summary_<timestamp>.txt

Advanced Options Reference

generate() Options

See the full options table in the generate() section above.


researchAgent Options

Option Type Default Description
maxQueries number 12 Total search queries to plan
maxConcurrency number 10 Parallel URL fetch workers
credibilityThreshold number 45 Minimum score (0–100) to include a source
maxSummaries number 20 Max sources after MMR diversity filter
diversityLambda number 0.6 MMR trade-off: 1.0 = pure relevance, 0.0 = pure diversity
chunkSize number 20 Claims per report-writing chunk
useOllamaSearch boolean false Use Ollama web search. Falls back to SearXNG when false
academicFilter boolean false Restrict sources to trusted academic/news domains
academicWhitelist string[] built-in Additional trusted domains
academicBlacklist string[] built-in Additional domains to block
academicWhitelistMode 'extend' | 'replace' 'extend' Whitelist merge mode
academicBlacklistMode 'extend' | 'replace' 'extend' Blacklist merge mode
credNegativePatterns RegExp[] built-in URL patterns that reduce credibility
enableCritique boolean true Run critique-and-repair loop (steps 7–9)
recursionDepth number 2 Query tree depth

Internal Architecture

Deepthink.generate()
│
├── runThink()                    ← Multi-stage pre-thinking (think.js)
│   ├── depth ≥ 1 → Analysis pass
│   ├── depth ≥ 2 → Planning pass
│   └── depth ≥ 3 → Sanity-check pass
│
├── detectComputeNeeds()          ← Decide: none / single / parallel
│
├── generateAndRunCode()          ← codeGenerator.js
│   ├── runMCTSApproaches()       ← 4 parallel Python sandboxes, consensus vote
│   ├── mathematicianAgent()      ← Formal spec (no code)
│   ├── engineerAgent() × 2      ← JS + Python implementations
│   ├── runJSSandbox()
│   ├── runPythonSandbox()
│   └── reconcileResults()
│
├── callChat()                    ← Final answer with ground truth injected
│   └── buildProviderClient()     ← Routes to correct provider adapter
│
└── runChecks() × N              ← Self-verification loop
    ├── Standard checker
    ├── Adversarial checker
    └── Numerical checker
        └── MetacognitiveMonitor
runDeepResearch()
│
├── Step 0  detectAnswerFormat()
├── Step 1  plannerAgent()
├── Step 2  crawlerAgent()
├── Step 3  verificationAgent()
├── Step 4  extractWithFallback() + applyMMR()
├── Step 5  factVerificationLoop()
├── Step 6  reportWriterAgent()
└── Steps 7–9  critiqueAndRepairLoop()

Project Structure

thinking/
├── deepthink.js          Main Deepthink class (generate, callChat, verification)
├── researchAgent.js      9-step deep research pipeline
├── codeGenerator.js      Project generation, MCTS, sandboxes, test oracle
├── analytical.js         Multi-agent analytical decomposition mode
├── think.js              Multi-stage pre-thinking passes
└── dataTypes.js          Type parsing, message normalisation utilities

providers/
└── index.js              Multi-provider adapter (OpenAI, Claude, Gemini, Perplexity, Grok, LM Studio, Ollama)

internet/
├── extractFromUrl.js     Universal URL → HTML extractor (20+ formats)
├── axios.js              Chrome TLS-spoofing axios adapter (impit)
├── ollamaSearch.js       Ollama web search (REST + JS client fallback)
├── interactWithInternet.js Search + fetch orchestration layer
└── extractCitation.js    APA citation generator

examples/
├── electron_explorer.js  Autonomous AI browser agent (Electron)
└── research.js           Standalone deep research usage example

Requirements & Dependencies

Install via npm install deepthink-js — everything below is included automatically.

Dependency Purpose
ollama Ollama JS client
axios HTTP client (wrapped by the impit adapter)
impit Chrome TLS fingerprint for bot bypass
cheerio HTML parsing and cleaning
jsdom DOM for Readability
@mozilla/readability Article extraction from HTML
mammoth DOCX → HTML conversion
xlsx Spreadsheet (XLSX/XLS/ODS) parsing
jszip PPTX, ODT, EPUB unpacking
fast-xml-parser XML/PPTX slide text extraction
@iarna/rtf-to-html RTF → HTML
papaparse CSV/TSV parsing
marked Markdown → HTML
pdf-parse PDF text extraction
electron (examples/electron_explorer.js only — install separately)
python3 + sympy Python sandbox and symbolic math (optional — install separately)

Node.js ≥ 18 is required for native fetch, ReadableStream, and top-level await.


License

MIT

About

SOTA NPM module for agentic processes using local or cloud LLMs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors