### Install ThinkThread (Getting Started) Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Commands to install the ThinkThread library using either pip or Poetry, as shown in the getting started section. ```bash # Using pip (once published) pip install thinkthread # Using Poetry poetry add thinkthread ``` -------------------------------- ### Getting Help via CLI Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md Shows how to access help information for the ThinkThread CLI, including general help and help for specific commands like `run`, `tot`, and `think`. ```bash thinkthread --help thinkthread run --help thinkthread tot --help thinkthread think --help ``` -------------------------------- ### ThinkThread CLI Usage Examples Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Examples demonstrating various ways to use the ThinkThread command-line interface, including basic usage, specifying providers, streaming output, advanced configuration, and selecting reasoning approaches. ```bash # Basic usage with Chain-of-Recursive-Thoughts 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 # Unified CLI with explicit reasoning approach selection thinkthread think "What are the implications of quantum computing on cryptography?" --approach cort # Using Tree-of-Thoughts reasoning with the unified CLI thinkthread think "Design a system for autonomous vehicles" --approach tot --beam-width 5 ``` -------------------------------- ### Adding a New LLM Provider Client (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Shows how to integrate a new LLM provider by creating a class that implements the `LLMClient` interface. It includes the `__init__` method for setup and placeholder implementations for `generate`, `astream`, `acomplete`, and `aclose` methods, which interact with the new provider's API. Requires `LLMClient`. ```python from typing import AsyncIterator from thinkthread_sdk.llm.base import LLMClient class NewProviderClient(LLMClient): def __init__(self, api_key: str, model_name: str = "default-model"): super().__init__(model_name=model_name) self.api_key = api_key # Initialize any provider-specific clients or configurations def generate(self, prompt: str, **kwargs) -> str: """Generate text using the new provider's API.""" temperature = kwargs.get("temperature", 0.7) # Implement provider-specific API call # Example: # response = new_provider_api.generate( # prompt=prompt, # temperature=temperature, # api_key=self.api_key, # model=self.model_name, # ) # return response.text async def astream(self, prompt: str, **kwargs) -> AsyncIterator[str]: """Asynchronously stream text from the new provider's API.""" temperature = kwargs.get("temperature", 0.7) # Implement provider-specific streaming API call # Example: # async for chunk in new_provider_api.stream( # prompt=prompt, # temperature=temperature, # api_key=self.api_key, # model=self.model_name, # ): # yield chunk.text async def acomplete(self, prompt: str, **kwargs) -> str: """Asynchronously generate text using the new provider's API.""" # Implement native async completion or use the default implementation # which calls generate() in a thread return await super().acomplete(prompt, **kwargs) async def aclose(self) -> None: """Clean up any resources used by the client.""" # Close any open connections, sessions, etc. pass ``` -------------------------------- ### Command Line Interface Examples Source: https://github.com/tomascupr/thinkthread/blob/main/docs/tree_thinker.md Provides examples of using the TreeThinker module from the command line. It shows basic usage, specifying an LLM provider, and configuring advanced parameters like beam width, depth, and iterations. ```bash # Basic usage thinkthread tot "What are the benefits of tree-based search for reasoning?" # With specific provider thinkthread tot "Design a system for autonomous vehicles" --provider anthropic # Advanced configuration thinkthread tot "What are the ethical implications of AI?" --beam-width 5 --max-depth 4 --iterations 3 ``` -------------------------------- ### Example: Basic CoRT Query - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Demonstrates a simple use of the `run` command to explain a scientific concept using Chain-of-Recursive-Thoughts. ```bash thinkthread run "Explain the concept of entropy in thermodynamics" ``` -------------------------------- ### Example: Basic ToT Query - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Demonstrates a simple use of the `tot` command to develop a strategy for a classic puzzle using Tree-of-Thoughts. ```bash thinkthread tot "Develop a strategy for solving the Tower of Hanoi puzzle" ``` -------------------------------- ### Building ThinkThread SDK Documentation with MkDocs (Bash) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/README.md Provides the necessary commands to set up the documentation build environment, build the documentation site, and serve it locally using MkDocs and its Material theme. Requires Python and pip to be installed. ```bash # Install MkDocs and required plugins pip install mkdocs mkdocs-material # Build the documentation mkdocs build # Serve the documentation locally mkdocs serve ``` -------------------------------- ### Getting a Direct LLM Response with ThinkThread SDK in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md This snippet illustrates how to get a direct response from an LLM using the ThinkThread SDK's client without applying advanced reasoning techniques. It initializes an OpenAI client and uses the `generate` method to get a simple answer to a question. ```python from thinkthread_sdk.llm import OpenAIClient client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") question = "What is the best strategy for addressing climate change?" direct_answer = client.generate( f"Question: {question}\n\nAnswer:", temperature=0.7 ) print(f"Direct Answer: {direct_answer}") ``` -------------------------------- ### Exporting New LLM Client in SDK (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Illustrates how to make the newly created `NewProviderClient` available for import within the `thinkthread_sdk.llm` package by adding it to the `__all__` list in the `__init__.py` file. ```python from .new_provider_client import NewProviderClient __all__ = [ # ... existing exports "NewProviderClient", ] ``` -------------------------------- ### ThinkThread Development Commands - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Shows basic command-line commands for setting up and running the ThinkThread project during development. It includes commands for installing dependencies using Poetry and running tests using pytest. ```bash # Install dependencies poetry install # Run tests poetry run pytest ``` -------------------------------- ### Install ThinkThread with Poetry Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Command to add the ThinkThread library as a dependency using the Poetry dependency manager. ```bash poetry add thinkthread ``` -------------------------------- ### Customizing CLI Parameters Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md Provides examples of how to customize the behavior of ThinkThread CLI commands using various options like `--provider`, `--alternatives`, `--rounds`, `--beam-width`, `--max-tree-depth`, `--stream`, and `--verbose`. ```bash # Use a different LLM provider thinkthread think "Explain quantum entanglement" --provider anthropic # CoRT-specific options thinkthread run "What are the ethical implications of AI?" --alternatives 5 --rounds 3 # ToT-specific options thinkthread tot "Design an algorithm for the traveling salesman problem" --beam-width 4 --max-tree-depth 3 # Enable streaming output thinkthread think "Describe the formation of black holes" --stream # Enable verbose logging thinkthread think "Explain blockchain technology" --verbose ``` -------------------------------- ### Running ToT Query via CLI Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md Demonstrates running a simple query using the Tree-of-Thoughts (ToT) approach via the `thinkthread tot` command-line interface. ```bash thinkthread tot "How would you solve the Tower of Hanoi puzzle with 3 disks?" ``` -------------------------------- ### Example: Unified Interface (ToT) - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Shows how to use the unified `think` command with the ToT approach, specifying the beam width for parallel threads. ```bash # Using ToT approach thinkthread think "Solve this logic puzzle: A man has to get a fox, a chicken, and a sack of corn across a river" --approach tot --beam-width 5 ``` -------------------------------- ### Implementing a Custom Reasoning Approach (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Demonstrates how to create a new reasoning approach by inheriting from `BaseReasoner`. It shows the `__init__` method for initialization and the `run` and `run_async` methods where the core reasoning logic should be implemented. Requires `BaseReasoner`, `LLMClient`, `TemplateManager`, and `ThinkThreadConfig`. ```python from thinkthread_sdk.base_reasoner import BaseReasoner from thinkthread_sdk.llm import LLMClient from thinkthread_sdk.prompting import TemplateManager from thinkthread_sdk.config import ThinkThreadConfig class NewReasoner(BaseReasoner): def __init__( self, llm_client: LLMClient, template_manager: Optional[TemplateManager] = None, config: Optional[ThinkThreadConfig] = None, **kwargs ): super().__init__(llm_client, template_manager, config) # Initialize any approach-specific attributes def run(self, question: str) -> str: """Execute the reasoning process on a question.""" # Implement your reasoning approach # 1. Process the question # 2. Generate initial thoughts/answers # 3. Apply your reasoning algorithm # 4. Return the final answer async def run_async(self, question: str) -> str: """Execute the reasoning process asynchronously on a question.""" # Implement the async version of your reasoning approach # You can use the shared utilities in reasoning_utils.py ``` -------------------------------- ### Example: Specify LLM Provider - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Demonstrates how to use the `think` command to specify a different LLM provider (Anthropic) for the reasoning task. ```bash thinkthread think "What are the implications of quantum computing for cryptography?" --provider anthropic ``` -------------------------------- ### Example: Unified Interface (CoRT) - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Shows how to use the unified `think` command with the CoRT approach, specifying the number of refinement rounds. ```bash # Using CoRT approach thinkthread think "Explain quantum computing" --approach cort --rounds 3 ``` -------------------------------- ### Calculating String Similarity (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Provides an example of using the `calculate_similarity` utility function to compute the similarity score between two input strings. ```python # Calculate similarity between two strings similarity = calculate_similarity(str1, str2) ``` -------------------------------- ### Running CoRT Query via CLI Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md Demonstrates running a simple query using the default Chain-of-Recursive-Thoughts (CoRT) approach via the `thinkthread run` command-line interface. ```bash thinkthread run "What is the significance of the Fibonacci sequence in nature?" ``` -------------------------------- ### Install ThinkThread with pip Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Command to install the ThinkThread library using the pip package manager. ```bash pip install thinkthread ``` -------------------------------- ### Generating Alternatives Synchronously (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Shows how to use the `generate_alternatives` utility function to synchronously generate alternative answers for a given question and current answer using an LLM client and template manager. ```python # Generate alternatives for a question and current answer alternatives = generate_alternatives( question, current_answer, llm_client, template_manager, count=3, temperature=0.9 ) ``` -------------------------------- ### Display SDK Version - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Command to display the currently installed version of the ThinkThread SDK. ```bash thinkthread version ``` -------------------------------- ### Using Unified `think` Command via CLI Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md Shows how to use the unified `thinkthread think` command for both CoRT (default) and ToT (using `--approach tot`) reasoning approaches from the command line. ```bash # Using CoRT approach (default) thinkthread think "What is the significance of the Fibonacci sequence in nature?" # Using ToT approach thinkthread think "How would you solve the Tower of Hanoi puzzle with 3 disks?" --approach tot ``` -------------------------------- ### Initializing Tree-of-Thoughts Session (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Demonstrates how to initialize a Tree-of-Thoughts session using the `TreeThinker` class, providing an LLM client and a specific evaluation strategy. ```python tot_session = TreeThinker( llm_client=client, evaluator=NewEvaluationStrategy(), ) ``` -------------------------------- ### Generating Alternatives Asynchronously (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Illustrates the use of the `generate_alternatives_async` utility function for asynchronously generating alternative answers, supporting parallel processing. ```python # Generate alternatives asynchronously alternatives = await generate_alternatives_async( question, current_answer, llm_client, template_manager, count=3, temperature=0.9, parallel=True # Use parallel processing ) ``` -------------------------------- ### ThinkThread Python Performance Configuration Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Example demonstrating how to create a `ThinkThreadConfig` object to enable various performance optimizations like parallel processing, caching, early termination, and batched requests. ```python 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 ) ``` -------------------------------- ### Configuring ThinkThread SDK with a .env File (Plaintext) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/configuration_reference.md Provides an example of a `.env` file structure, showing how to define common, Chain-of-Recursive-Thoughts (CoRT), and Tree-of-Thoughts (ToT) configuration options that the ThinkThread SDK can automatically load. ```plaintext # .env file OPENAI_API_KEY=your-openai-api-key PROVIDER=openai OPENAI_MODEL=gpt-4 # Chain-of-Recursive-Thoughts configuration ALTERNATIVES=5 ROUNDS=3 # Tree-of-Thoughts configuration BEAM_WIDTH=3 MAX_TREE_DEPTH=3 BRANCHING_FACTOR=3 ``` -------------------------------- ### Basic CLI Usage (Python Module and Entry Point) - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Shows the two primary ways to invoke the ThinkThread SDK CLI from the command line: either by running the Python module directly or by using the installed entry point. ```bash # Run using the Python module python -m thinkthread_sdk [COMMAND] [OPTIONS] # Or using the installed entry point thinkthread [COMMAND] [OPTIONS] ``` -------------------------------- ### Customizing Prompt Templates (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Demonstrates how to customize prompt templates by creating a `TemplateManager` instance pointing to a custom template directory and using it with `ThinkThreadSession` (CoRT) or `TreeThinker` (ToT). ```python from thinkthread_sdk.prompting import TemplateManager from thinkthread_sdk.session import ThinkThreadSession from thinkthread_sdk.tree_thinker import TreeThinker template_manager = TemplateManager(template_dir="/path/to/your/templates") # Use with Chain-of-Recursive-Thoughts cort_session = ThinkThreadSession( llm_client=client, template_manager=template_manager ) # Use with Tree-of-Thoughts tot_session = TreeThinker( llm_client=client, template_manager=template_manager ) ``` -------------------------------- ### Using Asynchronous Python API for CoRT Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md Shows how to use the asynchronous Python API for CoRT reasoning, including initializing the client and session, running `run_async`, and demonstrating how to stream the final answer using the LLM client's `astream` method. Requires `asyncio`. ```python import asyncio from thinkthread_sdk.session import ThinkThreadSession from thinkthread_sdk.llm import OpenAIClient async def cort_async_example(): # Initialize client and session client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") session = ThinkThreadSession(llm_client=client, alternatives=3, rounds=2) # Run recursively with async API question = "Explain the impact of climate change on biodiversity" answer = await session.run_async(question) print(f"Question: {question}") print(f"Answer: {answer}") # Streaming the final answer print("Streaming the final answer:") prompt = session.template_manager.render_template( "final_answer.j2", {"question": question, "answer": answer} ) async for token in await session.llm_client.astream(prompt): print(token, end="", flush=True) print() if __name__ == "__main__": asyncio.run(cort_async_example()) ``` -------------------------------- ### Initializing Different LLM Clients with ThinkThread SDK in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md This snippet shows how to initialize client objects for various LLM providers supported by the ThinkThread SDK, including OpenAI, Anthropic, and HuggingFace. Each client requires an API key/token and a model name. ```python # OpenAI from thinkthread_sdk.llm import OpenAIClient openai_client = OpenAIClient( api_key="your-openai-api-key", model_name="gpt-4" ) # Anthropic from thinkthread_sdk.llm import AnthropicClient anthropic_client = AnthropicClient( api_key="your-anthropic-api-key", model_name="claude-2" ) # HuggingFace from thinkthread_sdk.llm import HuggingFaceClient hf_client = HuggingFaceClient( api_token="your-hf-token", model_name="gpt2" ) ``` -------------------------------- ### Using Python API for CoRT Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md Demonstrates how to use the ThinkThread Python API to perform Chain-of-Recursive-Thoughts (CoRT) reasoning by initializing an LLM client, creating a `ThinkThreadSession`, and calling the `run` method. ```python 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 (CoRT approach) session = ThinkThreadSession(llm_client=client, alternatives=3, rounds=2) # Run recursive reasoning on a question question = "What are the implications of quantum computing on cryptography?" answer = session.run(question) print(f"Question: {question}") print(f"Answer: {answer}") ``` -------------------------------- ### Ask Question (CoRT) - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Uses the `ask` command to pose a question and get an answer using the Chain-of-Recursive-Thoughts (CoRT) approach with default options. ```bash thinkthread ask "What is the meaning of life?" ``` -------------------------------- ### Using Python API for ToT Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md Illustrates how to use the ThinkThread Python API for Tree-of-Thoughts (ToT) reasoning by initializing an LLM client, creating a `TreeThinker` instance with specific parameters, and calling the `solve` method. ```python from thinkthread_sdk.tree_thinker import TreeThinker from thinkthread_sdk.llm import OpenAIClient # Initialize an LLM client client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") # Create a TreeThinker session tree_thinker = TreeThinker( llm_client=client, max_tree_depth=3, branching_factor=3 ) # Run tree-based reasoning on a problem problem = "How would you solve the Tower of Hanoi puzzle with 3 disks?" solution = tree_thinker.solve(problem, beam_width=3) print(f"Problem: {problem}") print(f"Solution: {solution}") ``` -------------------------------- ### Using ThinkThread Tree-of-Thoughts CLI - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Provides examples of using the ThinkThread command-line interface for Tree-of-Thoughts reasoning. It shows how to use the dedicated `tot` command and the unified `think` command with the `--approach tot` flag, including options for specifying the provider, beam width, and maximum depth. ```bash # Using the dedicated ToT command thinkthread tot "What are the benefits of tree-based search for reasoning?" # With specific provider thinkthread tot "Design a system for autonomous vehicles" --provider anthropic # Advanced configuration thinkthread tot "What are the ethical implications of AI?" --beam-width 5 --max-depth 4 # Using the unified CLI interface thinkthread think "What are the benefits of tree-based search for reasoning?" --approach tot # Unified CLI with advanced configuration thinkthread think "What are the ethical implications of AI?" --approach tot --beam-width 5 --max-depth 4 ``` -------------------------------- ### Set Tree-of-Thoughts Environment Variables (Bash) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/performance_optimization.md Provides examples of setting environment variables to configure Tree-of-Thoughts (ToT) specific optimizations and standard configuration parameters like beam width, tree depth, branching factor, and iterations. ```bash # ToT-specific optimizations export SIMILARITY_THRESHOLD=0.85 # Standard ToT configuration export BEAM_WIDTH=3 export MAX_TREE_DEPTH=3 export BRANCHING_FACTOR=3 export MAX_ITERATIONS=3 ``` -------------------------------- ### Customizing Advanced Configuration (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Shows how to create a custom `ThinkThreadConfig` object to set various session parameters like API key, model, number of alternatives/rounds, and evaluation strategies, and then use this config with `ThinkThreadSession` (CoRT) or `TreeThinker` (ToT). ```python from thinkthread_sdk.config import ThinkThreadConfig from thinkthread_sdk.session import ThinkThreadSession from thinkthread_sdk.tree_thinker import TreeThinker # Create custom configuration config = ThinkThreadConfig( openai_api_key="your-api-key", provider="openai", openai_model="gpt-4", alternatives=5, rounds=3, use_pairwise_evaluation=True, use_self_evaluation=False, parallel_alternatives=True # Enable parallel processing ) # Use with Chain-of-Recursive-Thoughts cort_session = ThinkThreadSession( llm_client=client, config=config ) # Use with Tree-of-Thoughts tot_session = TreeThinker( llm_client=client, max_tree_depth=3, branching_factor=3, config=config ) ``` -------------------------------- ### Using a Custom Evaluation Strategy (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Demonstrates how to instantiate a `ThinkThreadSession` and configure it to use the custom `NewEvaluationStrategy` when processing questions, allowing the session to use the defined evaluation logic. Requires `ThinkThreadSession`, `TreeThinker`, and the custom `NewEvaluationStrategy`. ```python from thinkthread_sdk.session import ThinkThreadSession from thinkthread_sdk.tree_thinker import TreeThinker from your_module import NewEvaluationStrategy # With Chain-of-Recursive-Thoughts cort_session = ThinkThreadSession( llm_client=client, evaluation_strategy=NewEvaluationStrategy(), ) ``` -------------------------------- ### Running Asynchronous Tree-of-Thoughts (ToT) with ThinkThread SDK in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md This snippet demonstrates how to use the TreeThinker class asynchronously. It initializes an OpenAI client, configures the TreeThinker with depth and branching factor, and solves a problem using the `solve_async` method. It requires the `asyncio` library and the `thinkthread_sdk`. ```python import asyncio from thinkthread_sdk.tree_thinker import TreeThinker from thinkthread_sdk.llm import OpenAIClient async def tot_async_example(): # Initialize client and tree thinker client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") tree_thinker = TreeThinker( llm_client=client, max_tree_depth=3, branching_factor=3 ) # Run tree-based reasoning with async API problem = "Design an algorithm for the traveling salesman problem" solution = await tree_thinker.solve_async(problem, beam_width=3) print(f"Problem: {problem}") print(f"Solution: {solution}") if __name__ == "__main__": asyncio.run(tot_async_example()) ``` -------------------------------- ### Implementing a Custom Evaluation Strategy (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md Provides the structure for creating a new evaluation strategy by inheriting from `EvaluationStrategy`. It shows the `evaluate` method, which takes the question, answers, LLM client, and template manager as input and should return the index of the best answer based on custom logic. Requires `EvaluationStrategy`, `LLMClient`, and `TemplateManager`. ```python from typing import List from thinkthread_sdk.evaluation import EvaluationStrategy from thinkthread_sdk.llm import LLMClient from thinkthread_sdk.prompting import TemplateManager class NewEvaluationStrategy(EvaluationStrategy): def evaluate( self, question: str, answers: List[str], llm_client: LLMClient, template_manager: TemplateManager, ) -> int: """Evaluate answers using a new strategy.""" # Implement your evaluation logic # Example: # 1. Create a prompt for evaluation # 2. Send it to the LLM # 3. Parse the response # 4. Return the index of the best answer # ... return best_index ``` -------------------------------- ### Rendering Templates with TemplateManager in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md This class manages Jinja2 templates used for prompt generation within the SDK. The `render_template` method loads a specified template by name and renders it using the provided context dictionary, returning the resulting string. ```python class TemplateManager: def render_template(self, template_name: str, context: Dict[str, Any]) -> str: """Render a template with the given context.""" template = self.env.get_template(template_name) return template.render(**context) ``` -------------------------------- ### Using Chain-of-Recursive-Thoughts (CoRT) with ThinkThread SDK in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md This snippet demonstrates how to use the Chain-of-Recursive-Thoughts (CoRT) approach via the `ThinkThreadSession` class. It initializes an OpenAI client and a session object, then runs the session with a question to generate a CoRT-based answer. ```python from thinkthread_sdk.session import ThinkThreadSession from thinkthread_sdk.llm import OpenAIClient client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") session = ThinkThreadSession(llm_client=client, alternatives=3, rounds=2) question = "What is the best strategy for addressing climate change?" cort_answer = session.run(question) print(f"CoRT Answer: {cort_answer}") ``` -------------------------------- ### Using Tree-of-Thoughts (ToT) with ThinkThread SDK in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/usage_guide.md This snippet shows how to apply the Tree-of-Thoughts (ToT) reasoning method using the `TreeThinker` class. It initializes an OpenAI client and a TreeThinker object, then uses the `solve` method to find a solution to a question. ```python from thinkthread_sdk.tree_thinker import TreeThinker from thinkthread_sdk.llm import OpenAIClient client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") tree_thinker = TreeThinker(llm_client=client, max_tree_depth=3, branching_factor=3) question = "What is the best strategy for addressing climate change?" tot_answer = tree_thinker.solve(question, beam_width=3) print(f"ToT Answer: {tot_answer}") ``` -------------------------------- ### Defining LLMClient Interface in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md This abstract base class specifies the interface for integrating different Large Language Model (LLM) providers into the SDK. It requires implementations to provide methods for synchronous text generation (`generate`) and asynchronous streaming generation (`astream`). ```python class LLMClient(ABC): @abstractmethod def generate(self, prompt: str, **kwargs) -> str: """Generate text from the language model.""" pass @abstractmethod async def astream(self, prompt: str, **kwargs) -> AsyncIterator[str]: """Asynchronously stream text generation from the language model.""" pass ``` -------------------------------- ### Defining BaseReasoner Interface in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md This abstract base class defines the required interface for all reasoning components within the ThinkThread SDK. It includes an initializer to set up dependencies like the LLM client and template manager, and abstract methods (`run`, `run_async`) that concrete reasoner implementations must provide for executing the reasoning process. ```python class BaseReasoner(ABC): def __init__( self, llm_client: LLMClient, template_manager: Optional[TemplateManager] = None, config: Optional[ThinkThreadConfig] = None, ): """Initialize the reasoner with an LLM client and optional components.""" self.llm_client = llm_client self.config = config or create_config() self.template_manager = template_manager or TemplateManager( self.config.prompt_dir ) @abstractmethod def run(self, question: str) -> str: """Execute the reasoning process on a question.""" pass @abstractmethod async def run_async(self, question: str) -> str: """Execute the reasoning process asynchronously on a question.""" pass ``` -------------------------------- ### Defining Evaluation Interfaces in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/developer_guide.md These abstract base classes define the interfaces for implementing evaluation strategies in the SDK. `EvaluationStrategy` is used for evaluating a list of answers and selecting the best one, while `Evaluator` is for pairwise comparison to determine if a new answer is better than a previous one. Both require an `evaluate` method. ```python class EvaluationStrategy(ABC): @abstractmethod def evaluate( self, question: str, answers: List[str], llm_client: LLMClient, template_manager: TemplateManager, ) -> int: """Evaluate answers and return the index of the best one.""" pass class Evaluator(ABC): @abstractmethod def evaluate( self, question: str, prev_answer: str, new_answer: str, llm_client: LLMClient, template_manager: TemplateManager, ) -> bool: """Evaluate whether the new answer is better than the previous.""" pass ``` -------------------------------- ### Initializing ThinkThread OpenAI Client Source: https://github.com/tomascupr/thinkthread/blob/main/docs/comparison.md This snippet demonstrates how to initialize an OpenAI client using the ThinkThread SDK. It requires the `thinkthread_sdk` library and an OpenAI API key. The client is configured with the API key and a specific model name (gpt-4). This client is typically used to power a `ThinkThreadSession` for executing reasoning tasks. ```Python from thinkthread_sdk.session import ThinkThreadSession from thinkthread_sdk.llm import OpenAIClient # Initialize client client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") ``` -------------------------------- ### Initializing Clients for Different LLM Providers Source: https://github.com/tomascupr/thinkthread/blob/main/docs/tree_thinker.md Shows how to initialize client objects for various supported LLM providers, including OpenAI, Anthropic, HuggingFace, and a dummy client for testing. These clients implement the required `LLMClient` interface. ```python # OpenAI from thinkthread_sdk.llm import OpenAIClient client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") # Anthropic from thinkthread_sdk.llm import AnthropicClient client = AnthropicClient(api_key="your-api-key", model="claude-2") # HuggingFace from thinkthread_sdk.llm import HuggingFaceClient client = HuggingFaceClient(api_token="your-token", model="mistralai/Mistral-7B-Instruct-v0.2") # Custom or mock client for testing from thinkthread_sdk.llm import DummyLLMClient client = DummyLLMClient() ``` -------------------------------- ### Configuring ThinkThread SDK with Environment Variables and Running CLI (Bash) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/configuration_reference.md Demonstrates setting common, Chain-of-Recursive-Thoughts (CoRT), and Tree-of-Thoughts (ToT) configuration options using `export` commands and then running the `thinkthread` command-line interface with specific questions and approaches. ```bash # Common configuration export OPENAI_API_KEY=your-openai-api-key export PROVIDER=openai export OPENAI_MODEL=gpt-4 # Chain-of-Recursive-Thoughts configuration export ALTERNATIVES=5 export ROUNDS=3 # Tree-of-Thoughts configuration export BEAM_WIDTH=3 export MAX_TREE_DEPTH=3 export BRANCHING_FACTOR=3 # Run with Chain-of-Recursive-Thoughts thinkthread run "Your question here" # Run with Tree-of-Thoughts thinkthread tot "Your question here" # Run with unified CLI thinkthread think "Your question here" --approach cort ``` -------------------------------- ### Loading Environment-Specific Configurations in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/configuration_reference.md Illustrates how to load different configuration settings based on the environment (development, testing, production) by calling a `create_config` function with environment-specific `.env` file paths. ```python # Development dev_config = create_config(".env.development") # Testing test_config = create_config(".env.testing") # Production prod_config = create_config(".env.production") ``` -------------------------------- ### Using ThinkThread Tree-of-Thoughts Solver (Sync) - Python Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Demonstrates how to initialize the TreeThinker with an LLM client and configuration, and use the synchronous `solve` method to find a solution to a problem. It shows how to set parameters like tree depth and branching factor during initialization, and beam width and iterations during solving. The snippet also includes logic to find and print the best solution based on node scores. ```python from thinkthread_sdk.tree_thinker import TreeThinker from thinkthread_sdk.llm import OpenAIClient from thinkthread_sdk.config import create_config # Setup client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") tree_thinker = TreeThinker( llm_client=client, max_tree_depth=3, # Maximum depth of the thinking tree branching_factor=3, # Number of branches per node ) # Solve a problem using tree-based search problem = "What are three key benefits of tree-based search for reasoning?" result = tree_thinker.solve( problem=problem, beam_width=2, # Number of parallel thought threads max_iterations=2 # Number of expansion iterations ) # Find the best solution best_node_id = max( tree_thinker.threads.keys(), key=lambda node_id: tree_thinker.threads[node_id].score ) best_node = tree_thinker.threads[best_node_id] print(f"Best solution (score: {best_node.score:.2f}):") print(best_node.state.get("current_answer", "No answer found")) ``` -------------------------------- ### Programmatic Configuration and Session Creation (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/configuration_reference.md Illustrates how to create a `ThinkThreadConfig` object directly in Python, either by loading from environment variables/`.env` or by explicitly setting values. It also shows how to use this configuration when initializing a `ThinkThreadSession` with an LLM client. ```python from thinkthread_sdk.config import ThinkThreadConfig, create_config # Use environment variables and .env file with overrides config = create_config() # Or create a custom configuration custom_config = ThinkThreadConfig( openai_api_key="your-api-key", provider="openai", openai_model="gpt-4", # Chain-of-Recursive-Thoughts options alternatives=5, rounds=3, use_pairwise_evaluation=True, # Tree-of-Thoughts options beam_width=3, max_tree_depth=3, branching_factor=3, ) # Use with Chain-of-Recursive-Thoughts from thinkthread_sdk.session import ThinkThreadSession from thinkthread_sdk.llm import OpenAIClient client = OpenAIClient(api_key=custom_config.openai_api_key, model_name=custom_config.openai_model) cort_session = ThinkThreadSession(llm_client=client, config=custom_config) ``` -------------------------------- ### ThinkThread Python Programmatic Configuration Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Shows how to create a configuration object programmatically using the `create_config` function to customize provider, alternatives, rounds, and evaluation methods. ```python from thinkthread_sdk.config import create_config config = create_config( provider="anthropic", alternatives=4, rounds=2, use_pairwise_evaluation=True ) ``` -------------------------------- ### ThinkThread Python API Basic Usage Source: https://github.com/tomascupr/thinkthread/blob/main/README.md Demonstrates how to use the ThinkThread Python API to set up a session with an LLM client (OpenAI) and run a reasoning task. ```python 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}") ``` -------------------------------- ### Configuring Common ThinkThread Optimizations Source: https://github.com/tomascupr/thinkthread/blob/main/docs/performance_optimization.md Demonstrates how to initialize the ThinkThreadConfig object to enable common performance optimizations such as parallel processing, caching, batching, and semantic caching. ```python from thinkthread_sdk.config import ThinkThreadConfig config = ThinkThreadConfig( # Common performance optimization flags parallel_alternatives=True, # Enable parallel generation of alternatives/branches parallel_evaluation=True, # Enable parallel evaluation processing use_caching=True, # Enable response caching concurrency_limit=5, # Maximum number of concurrent API calls enable_monitoring=True, # Enable performance monitoring # Advanced common optimization flags use_batched_requests=True, # Enable batched API requests use_fast_similarity=True, # Use faster similarity calculation algorithms use_semantic_cache=True, # Enable semantic caching with embeddings ) ``` -------------------------------- ### Running ThinkThread with ToT Strategy (Bash) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Demonstrates how to run the `thinkthread` CLI using the Tree of Thoughts (ToT) strategy, specifying the beam width and maximum tree depth for the search. ```bash thinkthread tot "Design an algorithm to solve the traveling salesman problem" --beam-width 4 --max-tree-depth 4 ``` -------------------------------- ### Set Common Optimization Environment Variables (Bash) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/performance_optimization.md Illustrates how to set environment variables to configure common optimization settings for the ThinkThread SDK, such as enabling parallel processing, caching, concurrency limits, and monitoring. ```bash # Common optimizations export PARALLEL_ALTERNATIVES=true export PARALLEL_EVALUATION=true export USE_CACHING=true export CONCURRENCY_LIMIT=5 export ENABLE_MONITORING=true # Advanced common optimizations export USE_BATCHED_REQUESTS=true export USE_FAST_SIMILARITY=true export USE_SEMANTIC_CACHE=true ``` -------------------------------- ### Configure Tree-of-Thoughts Optimizations (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/performance_optimization.md Demonstrates how to configure Tree-of-Thoughts (ToT) specific and common optimization settings using the `ThinkThreadConfig` class in Python. It also shows how to initialize a `TreeThinker` instance with an OpenAI client and the defined configuration. ```python from thinkthread_sdk.config import ThinkThreadConfig from thinkthread_sdk.tree_thinker import TreeThinker from thinkthread_sdk.llm import OpenAIClient config = ThinkThreadConfig( # Common optimizations parallel_alternatives=True, parallel_evaluation=True, use_caching=True, # ToT-specific optimization flags similarity_threshold=0.85, # Threshold for pruning similar branches # Standard ToT configuration options beam_width=3, # Number of parallel thought threads to maintain max_tree_depth=3, # Maximum depth of the thinking tree branching_factor=3, # Number of branches to generate per node max_iterations=3, # Maximum number of tree expansion iterations ) # Create optimized ToT session client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") tree_thinker = TreeThinker( llm_client=client, max_tree_depth=config.max_tree_depth, branching_factor=config.branching_factor, config=config ) ``` -------------------------------- ### Basic Synchronous Usage with OpenAI Source: https://github.com/tomascupr/thinkthread/blob/main/docs/tree_thinker.md Demonstrates the basic synchronous usage of the TreeThinker module. It shows how to initialize the TreeThinker with an OpenAI client and configuration, solve a problem, and find the best solution from the resulting thought threads. ```python from thinkthread_sdk.tree_thinker import TreeThinker from thinkthread_sdk.llm import OpenAIClient from thinkthread_sdk.config import create_config # Setup client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") config = create_config() # Initialize TreeThinker tree_thinker = TreeThinker( llm_client=client, max_tree_depth=3, # Maximum depth of the thinking tree branching_factor=3, # Number of branches per node config=config, ) # Solve a problem problem = "What are three key benefits of tree-based search for reasoning?" result = tree_thinker.solve( problem=problem, beam_width=2, # Number of parallel thought threads max_iterations=2 # Number of expansion iterations ) # Find the best solution best_node_id = max( tree_thinker.threads.keys(), key=lambda node_id: tree_thinker.threads[node_id].score ) best_node = tree_thinker.threads[best_node_id] print(f"Best solution (score: {best_node.score:.2f}):") print(best_node.state.get("current_answer", "No answer found")) ``` -------------------------------- ### Accessing Performance Monitoring Data with ThinkThread SDK (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/performance_optimization.md This snippet demonstrates how to access performance statistics collected by the GLOBAL_MONITOR after running a reasoning session (CoRT or ToT). It shows how to initialize an LLM client and session, run a query, and then retrieve and print the collected metrics. ```python from thinkthread_sdk.monitoring import GLOBAL_MONITOR # Run your reasoning session (CoRT or ToT) from thinkthread_sdk.session import ThinkThreadSession from thinkthread_sdk.tree_thinker import TreeThinker from thinkthread_sdk.llm import OpenAIClient client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") # With Chain-of-Recursive-Thoughts cort_session = ThinkThreadSession(llm_client=client) cort_result = cort_session.run("What is the meaning of life?") # With Tree-of-Thoughts tot_session = TreeThinker(llm_client=client) tot_result = tot_session.solve("What is the meaning of life?") # Get performance statistics stats = GLOBAL_MONITOR.get_stats() print(stats) # Example output for CoRT: # { # "generate_initial": {"min": 0.5, "max": 1.2, "avg": 0.8, "total": 0.8, "count": 1}, # "generate_alternative": {"min": 0.4, "max": 0.9, "avg": 0.6, "total": 1.8, "count": 3}, # "evaluate": {"min": 0.3, "max": 0.7, "avg": 0.5, "total": 1.0, "count": 2} # } # Example output for ToT: # { # "generate_initial": {"min": 0.5, "max": 1.2, "avg": 0.8, "total": 2.4, "count": 3}, # "generate_continuation": {"min": 0.4, "max": 0.9, "avg": 0.6, "total": 3.6, "count": 6}, # "evaluate_branch": {"min": 0.3, "max": 0.7, "avg": 0.5, "total": 4.5, "count": 9} # } ``` -------------------------------- ### Running ThinkThread with CoRT Strategy (Bash) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Demonstrates how to run the `thinkthread` CLI using the CoRT strategy, specifying the number of rounds and alternatives for the generation process. ```bash thinkthread run "Compare different approaches to artificial general intelligence" --rounds 3 --alternatives 5 ``` -------------------------------- ### Enabling Streaming in ThinkThread (Bash) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Shows how to enable streaming output when running a `thinkthread` command using the `--stream` flag for real-time results. ```bash thinkthread think "Explain blockchain technology" --stream ``` -------------------------------- ### Implementing Adaptive Concurrency for ThinkThread SDK LLMClient (Python) Source: https://github.com/tomascupr/thinkthread/blob/main/docs/performance_optimization.md This snippet illustrates how to implement adaptive concurrency by monitoring the average response time of LLM calls using GLOBAL_MONITOR. It shows how to retrieve statistics and adjust the concurrency limit of an OpenAIClient based on whether responses are fast or slow. ```python from thinkthread_sdk.monitoring import GLOBAL_MONITOR from thinkthread_sdk.llm import OpenAIClient client = OpenAIClient(api_key="your-api-key", model_name="gpt-4") # Run initial requests # ... (your reasoning code here) # Get performance statistics stats = GLOBAL_MONITOR.get_stats() avg_response_time = stats.get("generate_alternative", {}).get("avg", 0.5) # Adjust concurrency limit based on response time if avg_response_time < 0.3: # Fast responses, increase concurrency client.set_concurrency_limit(10) elif avg_response_time > 1.0: # Slow responses, decrease concurrency client.set_concurrency_limit(3) ``` -------------------------------- ### Initializing Tree-of-Thoughts Session in Python Source: https://github.com/tomascupr/thinkthread/blob/main/docs/configuration_reference.md Demonstrates how to initialize a Tree-of-Thoughts session using the `TreeThinker` class from the `thinkthread_sdk`. It requires an LLM client and configuration parameters like max depth, branching factor, and a custom config object. ```python from thinkthread_sdk.tree_thinker import TreeThinker tot_session = TreeThinker( llm_client=client, max_tree_depth=custom_config.max_tree_depth, branching_factor=custom_config.branching_factor, config=custom_config ) ``` -------------------------------- ### Run Tree-of-Thoughts Reasoning - Bash Source: https://github.com/tomascupr/thinkthread/blob/main/docs/cli_reference.md Executes Tree-of-Thoughts (ToT) reasoning on a specified question using the `tot` command, exploring multiple paths in parallel. ```bash thinkthread tot "What is the most effective way to combat climate change?" ```