A Python library for formal reasoning methods with LLM integration
Project description
Reasoning Library
A Python library demonstrating various reasoning methods formalized through functional programming principles.
This library aims to showcase how complex reasoning can be built from simple, pure functions and composition,
while also providing a structured "chain-of-thought" mechanism similar to the chain-of-thought-tool package.
Features
- Structured Reasoning Steps: Each reasoning operation produces a
ReasoningStepobject, encapsulating the result along with metadata like confidence, stage, evidence, and assumptions. - Reasoning Chain Management: The
ReasoningChainclass allows for collecting and managing a sequence ofReasoningStepobjects, providing a summary of the entire reasoning process. - Tool Specification Generation: Functions intended for use by Large Language Models (LLMs) are decorated to automatically generate JSON Schema tool specifications, enabling seamless integration with LLM function calling APIs.
- Deductive Reasoning: Implementation of basic logical operations (AND, OR, NOT, IMPLIES) and the Modus Ponens rule.
- Functions are curried for flexible composition.
- Inductive Reasoning: Simple pattern recognition for numerical sequences (arithmetic and geometric progressions).
- Predicts the next number in a sequence.
- Describes the identified pattern.
Installation
This is a self-contained example. To use it, ensure you have the required dependencies. You can use uv to install them:
cd reasoning_library
uv pip install -r requirements.txt
cd ..
Usage
To see the library in action, run the example.py script:
python reasoning_library/example.py
Core Concepts
ReasoningStep
Represents a single step in a reasoning process. It includes:
step_number: Sequential identifier for the step.stage: A string describing the type of reasoning (e.g., "Deductive Reasoning: Modus Ponens").description: A human-readable explanation of what the step did.result: The outcome of the reasoning step.confidence: (Optional) A float indicating the confidence in the result.evidence: (Optional) A string detailing the evidence used.assumptions: (Optional) A list of strings outlining assumptions made.metadata: (Optional) A dictionary for any additional relevant information.
ReasoningChain
Manages a collection of ReasoningStep objects. Key methods:
add_step(...): Adds a newReasoningStepto the chain.get_summary(): Returns a formatted string summarizing all steps in the chain.clear(): Resets the chain, removing all steps.last_result: Property to get the result of the last step.
LLM Tool Integration
Functions decorated with @tool_spec automatically generate a JSON Schema representation, making them callable by LLMs that support function calling. This allows an LLM to use the library's reasoning capabilities as external tools.
Deductive Reasoning Example
from reasoning_library.deductive import apply_modus_ponens
from reasoning_library.core import ReasoningChain
chain = ReasoningChain()
# If P is true, and (P -> Q) is true, then Q is true.
result = apply_modus_ponens(True, True, reasoning_chain=chain) # P=True, Q=True
print(f"Modus Ponens (P=True, Q=True): {result}") # Output: True
result = apply_modus_ponens(True, False, reasoning_chain=chain) # P=True, Q=False (P->Q is false)
print(f"Modus Ponens (P=True, Q=False): {result}") # Output: None
print(chain.get_summary())
Inductive Reasoning Example
from reasoning_library.inductive import predict_next_in_sequence, find_pattern_description
from reasoning_library.core import ReasoningChain
chain = ReasoningChain()
seq = [1.0, 2.0, 3.0, 4.0]
chain.add_step(stage="Inductive Reasoning", description=f"Analyzing sequence {seq}", result=seq)
pattern = find_pattern_description(seq, reasoning_chain=chain)
predicted = predict_next_in_sequence(seq, reasoning_chain=chain)
print(f"Sequence: {seq}")
print(f"Pattern: {pattern}")
print(f"Predicted next: {predicted}")
print(chain.get_summary())
LLM Tool Specification Example
import json
from reasoning_library import TOOL_SPECS
# TOOL_SPECS is a list containing the JSON Schema for all registered tools.
# You can "just drop it in" to your LLM's tool configuration.
print(json.dumps(TOOL_SPECS, indent=2))
# This specification list can be provided to any LLM API that supports function calling.
Design Principles
- Pure Functions: Reasoning functions are designed to be pure, producing the same output for the same input and having no side effects (apart from optionally adding steps to a
ReasoningChainobject passed as an argument). - Functional Composition: Reasoning steps are built by composing smaller, independent functions, promoting modularity and reusability.
- Type Hinting: Used throughout the codebase for clarity and maintainability.
Extending the Library
This library is designed to be extensible. You can add new reasoning modules (e.g., for Abductive, Analogical, Causal reasoning) by creating new Python files and implementing their logic using the ReasoningStep and ReasoningChain structures, and decorating functions with @tool_spec to expose them to LLMs.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file reasoning_library-0.2.1.tar.gz.
File metadata
- Download URL: reasoning_library-0.2.1.tar.gz
- Upload date:
- Size: 46.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0df76363c4e631da762a2eded0e564389415fbbbfad05dae349c1a5c04b95176
|
|
| MD5 |
df4ff4e8de007e49aecc12580d1bb9df
|
|
| BLAKE2b-256 |
a45a0470dcb7d33fd14682b2310f2eb656c3831b93545da96f75f1eb19c3ff1c
|
File details
Details for the file reasoning_library-0.2.1-py3-none-any.whl.
File metadata
- Download URL: reasoning_library-0.2.1-py3-none-any.whl
- Upload date:
- Size: 29.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14a899d9708b6da9d6c2acb37ca9fa6215f351b354c80bfb9ce45f345d32f3a8
|
|
| MD5 |
024c3edf39f322231cce3273472c6b3d
|
|
| BLAKE2b-256 |
e3fd09c59d98bedf696378a8d05949a28f47c466667a45d258a58b1ded84ee25
|