Chain of Recursive Thoughts (CoRT)

repository·main·Indexed 25 days ago

https://github.com/phialsbasement/chain-of-recursive-thoughts

A framework that enhances AI reasoning through a recursive 'battle royale' of responses. CoRT forces models to generate multiple alternatives, evaluate them, and iteratively refine thoughts over multiple rounds to improve performance on complex tasks. It includes a Python CLI, a Web UI with a Node.js frontend (recthink-frontend v1.0.0), and a React-based state management system via the useRecThink hook and RecThinkProvider.

Tokens
1.4K
Snippets
2
Records
12
Agent score
81%

What's inside CoRT

  1. What is CoRT (Chain of Recursive Thoughts)

    main
    CoRT is a framework designed to improve AI model performance by forcing recursive reasoning. Instead of accepting a single initial response, the system makes the AI generate multiple alternatives, evaluate them, and iteratively refine its thoughts through multiple 'thinking rounds'. This process is particularly effective for complex tasks like programming.
  2. How the CoRT reasoning process works

    main

    The CoRT workflow follows a specific recursive loop to arrive at a high-quality response:

    1. Initial Generation: The AI generates an initial response.
    2. Depth Decision: The AI determines how many additional 'thinking rounds' are required.
    3. Recursive Loop: For each round:
      • The AI generates 3 alternative responses.
      • The AI evaluates all generated responses.
      • The AI selects the best response from the set.
    4. Final Output: The final response is the 'survivor' of these iterative evaluation rounds.
  3. Set up and run the CoRT Web UI on Linux

    main

    The Web UI requires both a Python backend and a Node.js frontend. Follow these steps to install dependencies and run both services in separate shells.

    # Terminal 1: Backend setup and execution
    pip install -r requirements.txt
    cd frontend && npm install
    cd ..
    python ./recthink_web.py
    
    # Terminal 2: Frontend execution
    cd frontend
    npm start
  4. Configure the RecThinkProvider

    main

    To use the RecThink state management in your React application, wrap your component tree with the RecThinkProvider. This provider manages the lifecycle of chat sessions, WebSocket connections, and the recursive thinking state.

    import { RecThinkProvider } from './context/RecThinkContext';
    
    function App() {
      return (
        <RecThinkProvider>
          <YourAppContent />
        </RecThinkProvider>
      );
    }
  5. Use the useRecThink hook to manage RecThink state

    main

    The useRecThink hook provides access to the application's global state and actions for managing recursive thinking sessions. It is used within components wrapped by the RecThinkProvider to interact with the chat session, access messages, and trigger AI actions.

    State available via the hook:

    • sessionId: The current active session ID.
    • messages: An array of conversation messages { role: string, content: string }.
    • isThinking: Boolean indicating if the AI is currently in a recursive thinking cycle.
    • thinkingProcess: Object containing rounds and history of the recursive thoughts.
    • apiKey: The API key being used.
    • model: The AI model identifier.
    • thinkingRounds: Configuration for number of rounds ('auto' or a numeric string).
    • alternativesPerRound: Number of alternative thoughts to generate per round.
    • error: Current error state string.
    • showThinkingProcess: Boolean to toggle visibility of the thinking history.
    • sessions: List of available previous sessions.
    • connectionStatus: WebSocket connection status ('connected', 'disconnected', or 'error').

    Actions available via the hook:

    • initializeChat(): Starts a new session and establishes a WebSocket connection.
    • sendMessage(content): Sends a user message to the AI.
    • saveConversation(filename, fullLog): Saves the current session.
    • loadSessions(): Fetches the list of available sessions.
    • deleteSession(id): Deletes a specific session by ID.
  6. Send a message with sendMessage()

    main

    Use sendMessage to send a user message to an existing session. You can optionally configure the recursive thinking process using the options object.

    Parameters:

    • sessionId: The unique identifier for the current session.
    • message: The text content of the user's message.
    • options (optional): An object containing:
      • thinkingRounds: Number of recursive thinking rounds to perform.
      • alternativesPerRound: Number of alternative perspectives/arguments to generate per round.
  7. Manage conversation logs with saveConversation()

    main

    Use saveConversation to persist a chat session to the server.

    Parameters:

    • sessionId: The ID of the session to save.
    • filename (optional): A specific filename for the saved log. If null, the server uses a default.
    • fullLog (optional): A boolean. If true, the entire reasoning chain and log are saved; otherwise, only the final conversation might be saved.