Skip to main content

ThinkThread SDK for enhanced LLM responses using Chain-of-Recursive-Thoughts technique

Project description

ThinkThread SDK

License: MIT

The ThinkThread SDK provides a powerful framework for improving LLM responses through the Chain-of-Recursive-Thoughts technique. By generating multiple alternative responses, evaluating them, and selecting the best option over multiple iterations, ThinkThread significantly enhances answer quality and reasoning capabilities.

What is Chain-of-Recursive-Thoughts?

Chain-of-Recursive-Thoughts is a technique that improves the quality of answers from large language models through a recursive self-refinement process:

  1. Generate an initial answer to a question
  2. For each refinement round:
    • Generate alternative answers
    • Evaluate all answers (current and alternatives)
    • Select the best answer for the next round
  3. Return the final selected answer

This process enables the model to critically examine its own responses, consider alternative perspectives, and ultimately produce higher-quality answers.

graph TD
    A[Question] --> B[Initial Answer]
    B --> C[Round 1]
    C --> D[Generate Alternatives]
    D --> E[Evaluate]
    E --> F[Select Best]
    F --> G{Converged?}
    G -->|Yes| K[Final Answer]
    G -->|No| H[Round 2]
    H --> I[Generate & Evaluate]
    I --> J[Select Best]
    J --> K
    
    style A fill:#f9f,stroke:#333,stroke-width:1px
    style K fill:#9f9,stroke:#333,stroke-width:1px
    style C fill:#ddf,stroke:#333,stroke-width:1px
    style H fill:#ddf,stroke:#333,stroke-width:1px

Installation

Using pip (once published)

pip install thinkthread

Using Poetry

poetry add thinkthread

Getting Started

Installation

# Using pip (once published)
pip install thinkthread

# Using Poetry
poetry add thinkthread

Quick Examples

CLI Usage

# Basic usage
thinkthread run "What are the implications of quantum computing on cryptography?"

# With specific provider and streaming
thinkthread run "Explain relativity" --provider anthropic --stream

# Advanced configuration
thinkthread run "Compare democracy and autocracy" --rounds 3 --alternatives 4

Python API

from thinkthread_sdk.session import ThinkThreadSession
from thinkthread_sdk.llm import OpenAIClient

# Setup
client = OpenAIClient(api_key="your-api-key", model_name="gpt-4")
session = ThinkThreadSession(llm_client=client, alternatives=3, rounds=2)

# Run reasoning
question = "What are the challenges in sustainable energy adoption?"
answer = session.run(question)
print(f"Answer: {answer}")

Configuration

Configure the SDK using environment variables, a .env file, or programmatically:

# Environment variables or .env file
OPENAI_API_KEY=your-openai-api-key
ANTHROPIC_API_KEY=your-anthropic-api-key
HF_API_TOKEN=your-huggingface-token

# Default settings
PROVIDER=openai                # Default provider
ALTERNATIVES=3                 # Number of alternatives per round
ROUNDS=2                       # Number of refinement rounds
USE_PAIRWISE_EVALUATION=true   # Evaluation method

Programmatic Configuration

from thinkthread_sdk.config import create_config

config = create_config(
    provider="anthropic",
    alternatives=4,
    rounds=2,
    use_pairwise_evaluation=True
)

Key Features

Feature Description
Multiple LLM Providers Support for OpenAI, Anthropic, and HuggingFace models
Recursive Reasoning Multi-round refinement process for improved answers
Evaluation Strategies Self-evaluation and pairwise comparison of answers
Async & Streaming Non-blocking API and real-time token-by-token output
Customizable Prompts Jinja2 templates for all prompting needs
Performance Optimizations Parallel processing, caching, and early termination
Extensible Architecture Easy to add new providers or evaluation strategies

Performance Optimizations

The SDK includes several performance enhancements that can be enabled through configuration:

Optimization Description Speed Improvement
Parallel Processing Concurrent generation and evaluation 1.4-2.0x
Batched Requests Combine multiple prompts in one API call 2.1-2.2x
Semantic Caching Cache similar prompts using embeddings 1.2-1.8x
Early Termination Stop when answers converge 1.3-4.3x

Configuration Example

from thinkthread_sdk.config import ThinkThreadConfig
from thinkthread_sdk.session import ThinkThreadSession

# Enable optimizations
config = ThinkThreadConfig(
    parallel_alternatives=True,
    use_caching=True,
    early_termination=True,
    use_batched_requests=True
)

# Create optimized session
session = ThinkThreadSession(
    llm_client=client,
    alternatives=3,
    rounds=2,
    config=config
)

For detailed tuning options, see the Performance Guide.

Development

# Install dependencies
poetry install

# Run tests
poetry run pytest

For detailed documentation, see the docs directory.

Contributing

Found a bug or have a feature request? Please open an issue on the GitHub Issues page.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thinkthread-0.6.0.tar.gz (57.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

thinkthread-0.6.0-py3-none-any.whl (40.0 kB view details)

Uploaded Python 3

File details

Details for the file thinkthread-0.6.0.tar.gz.

File metadata

  • Download URL: thinkthread-0.6.0.tar.gz
  • Upload date:
  • Size: 57.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.8 Linux/5.10.223

File hashes

Hashes for thinkthread-0.6.0.tar.gz
Algorithm Hash digest
SHA256 36abec8a513efbdec029f05aa16ee3f1d893eb6f344cdac488a95bee0de15d8f
MD5 7a61e8626ee9b8ad902556607820a68a
BLAKE2b-256 0cd37349fbb98f9b0fd48ae0dd9abdcad96566ac96a7eb00d3eab21096bcc872

See more details on using hashes here.

File details

Details for the file thinkthread-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: thinkthread-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 40.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.8 Linux/5.10.223

File hashes

Hashes for thinkthread-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6079b6194a55c5448a2f223e4a5e6bf1fc6f694e072be2bfe4a3a8ab85f9b151
MD5 e790be87b42d3fa3ea09e18d462d9442
BLAKE2b-256 186603f5563ce23983198646bf15249fbd6894b6909267741f7bf31028bce765

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page