Skip to main content

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

Project description

ThinkThread SDK

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

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[Initial Question] --> B[Generate Initial Answer]
    B --> C[Round 1]
    C --> D[Generate Alternative Answers]
    D --> E[Evaluate All Answers]
    E --> F[Select Best Answer]
    F --> G[Round 2]
    G --> H[Generate Alternative Answers]
    H --> I[Evaluate All Answers]
    I --> J[Select Best Answer]
    J --> K[Final Answer]
    
    style A fill:#f9f,stroke:#333,stroke-width:2px
    style K fill:#9f9,stroke:#333,stroke-width:2px
    style C fill:#ddf,stroke:#333,stroke-width:2px
    style G fill:#ddf,stroke:#333,stroke-width:2px

Installation

Using pip (once published)

pip install thinkthread

Using Poetry

poetry add thinkthread

Quickstart

Using the CLI

# Run a query with default settings
thinkthread run "What are the implications of quantum computing on cryptography?"

# Run a query with a specific provider
thinkthread run "Explain the theory of relativity" --provider anthropic

# Run a query with more refinement rounds
thinkthread run "Compare and contrast democracy and autocracy" --rounds 3 --alternatives 5

# Stream the response as it's generated
thinkthread run "Describe the water cycle" --stream

Using the Python API

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

# Initialize an LLM client
client = OpenAIClient(api_key="your-api-key", model_name="gpt-4")

# Create a ThinkThread session
session = ThinkThreadSession(llm_client=client, alternatives=3, rounds=2)

# Run recursive reasoning on a question
question = "What are the major challenges in sustainable energy adoption?"
answer = session.run(question)

print(f"Question: {question}")
print(f"Answer: {answer}")

Configuration

The ThinkThread SDK can be configured using environment variables or a .env file:

# LLM Provider API Keys
OPENAI_API_KEY=your-openai-api-key
ANTHROPIC_API_KEY=your-anthropic-api-key
HF_API_TOKEN=your-huggingface-token

# Default Provider and Model
PROVIDER=openai
OPENAI_MODEL=gpt-4
ANTHROPIC_MODEL=claude-2

# ThinkThread Algorithm Parameters
ALTERNATIVES=3
ROUNDS=2
MAX_ROUNDS=3

# Evaluation Options
USE_PAIRWISE_EVALUATION=true
USE_SELF_EVALUATION=false

# Other Settings
PROMPT_DIR=/path/to/custom/prompts

Features

The ThinkThread SDK provides these key capabilities:

  • Multiple LLM Providers: Support for OpenAI, Anthropic, and HuggingFace models
  • Prompt Templating: Customisable Jinja2 templates for all prompting needs
  • Recursive Reasoning: Multi-round refinement process for improved answers
  • Self-Evaluation: Ability to evaluate answer quality without external criteria
  • Pairwise Evaluation: Compare answers head-to-head for better selection
  • Asynchronous Support: Non-blocking API for integration with async applications
  • Streaming Responses: Real-time token-by-token output for better user experience
  • Extensible Architecture: Easily add new providers or evaluation strategies

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.5.1.tar.gz (40.3 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.5.1-py3-none-any.whl (33.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: thinkthread-0.5.1.tar.gz
  • Upload date:
  • Size: 40.3 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.5.1.tar.gz
Algorithm Hash digest
SHA256 0e7c71faeb5c6d825cebd8b0ce1ca37228505288f132627c224cc3447994e690
MD5 0ebfe848a22241c92efce7cbc0f309e0
BLAKE2b-256 fe9c0391ead9d3431784324d669038938d6caa3b3fdd76232e00303d209315d2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: thinkthread-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 33.3 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.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 202479e7c773ab863092bd8879478788663c18b8206143654b4a06ac7909e026
MD5 c312cab44b455f2ef368e8d9f4762446
BLAKE2b-256 83d11f44cfff37a28f1265db46467c0ba640efd5bdc2afb14718be0ccd40a116

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