Chain of Recursive Thoughts (CoRT)
repository·main·Indexed 25 days ago
https://github.com/phialsbasement/chain-of-recursive-thoughtsA 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.
What's inside CoRT
- 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.
How the CoRT reasoning process works
mainThe CoRT workflow follows a specific recursive loop to arrive at a high-quality response:
- Initial Generation: The AI generates an initial response.
- Depth Decision: The AI determines how many additional 'thinking rounds' are required.
- 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.
- Final Output: The final response is the 'survivor' of these iterative evaluation rounds.
Run CoRT via the Python CLI
mainTo run the recursive thinking logic directly from the command line, install the dependencies, set your
OPENROUTER_API_KEYenvironment variable, and execute the main script.pip install -r requirements.txt export OPENROUTER_API_KEY="your-key-here" python recursive-thinking-ai.pySet up and run the CoRT Web UI on Linux
mainThe 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 startSet up the CoRT Web UI on Windows
mainOn Windows, you can use the provided batch file to automate the installation and startup process.Configure the RecThinkProvider
mainTo 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> ); }Use the useRecThink hook to manage RecThink state
mainThe
useRecThinkhook provides access to the application's global state and actions for managing recursive thinking sessions. It is used within components wrapped by theRecThinkProviderto 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 containingroundsandhistoryof 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.
Initialize a chat session with initializeChat()
mainUseinitializeChatto start a new chat session by providing an API key and a model name. This function sends a POST request to the/initializeendpoint. It returns a promise that resolves to the session initialization data.Send a message with sendMessage()
mainUse
sendMessageto send a user message to an existing session. You can optionally configure the recursive thinking process using theoptionsobject.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.
Create a WebSocket connection for real-time updates
mainTo receive real-time streaming updates or intermediate thoughts from the CoRT process, usecreateWebSocketConnection(sessionId). This establishes a connection tows://localhost:8000/ws/{sessionId}.Manage conversation logs with saveConversation()
mainUse
saveConversationto 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. Ifnull, the server uses a default.fullLog(optional): A boolean. Iftrue, the entire reasoning chain and log are saved; otherwise, only the final conversation might be saved.
List or delete chat sessions
mainThe API provides methods to manage existing sessions:
listSessions(): Fetches a list of all available chat sessions via a GET request to/sessions.deleteSession(sessionId): Removes a specific session via a DELETE request to/sessions/{sessionId}.