Skip to main content

A Python toolkit for chain-of-thought prompting

Project description

Cogitator Logo

Cogitator

Tests Code Coverage Code Quality Python Version PyPI Version Downloads
License Documentation DOI

A Python toolkit for chain-of-thought prompting


Cogitator is a Python toolkit for experimenting and working with chain-of-thought (CoT) prompting methods in large language models (LLMs). CoT prompting improves LLM performance on complex tasks (like question-answering, reasoning, and problem-solving) by guiding the models to generate intermediate reasoning steps before arriving at the final answer. Additionally, it can be used to improve the interpretability of LLMs by providing insight into the model's reasoning process. The toolkit aims to make it easier to use popular CoT strategies and frameworks for research or integrating them into AI applications.

Features


Getting Started

pip install cogitator

Or, if you want to install from the latest version with examples and benchmarks included

git clone --depth=1 https://github.com/habedi/cogitator && cd cogitator

# Set up Python environment
pip install poetry
poetry install --with dev

# Run the tests to make sure everything is working (optional)
poetry run pytest

Examples

See the examples directory for examples.

import logging
from cogitator import SelfConsistency, OllamaLLM

# Step 1: Configure logging (optional, but helpful)
logging.basicConfig(level=logging.INFO)
logging.getLogger("httpx").setLevel(logging.WARNING)  # Suppress HTTPX logs

# Step 2: Initialize the LLM (using Ollama)
# Needs Ollama running locally with the model pulled (e.g., `ollama pull gemma3:4b`)
try:
    llm = OllamaLLM(model="gemma3:4b")
except Exception as e:
    print(f"Error initializing Ollama LLM: {e}")
    print("Please make sure Ollama is running and the model is pulled.")
    exit(1)

# Step 3: Choose a CoT strategies (Self-Consistency in this case)
# Self-Consistency generates multiple reasoning paths and finds the most common answer
sc_strategy = SelfConsistency(
    llm,
    n_samples=5,  # Number of reasoning paths to generate
    temperature=0.7  # Higher temperature can lead to more diverse answers
)

# Step 4: Define the prompt (with a basic CoT trigger)
question = "A bat and a ball cost $1.10 in total. The bat costs $1.00 more than the ball. How much does the ball cost?"
prompt = f"Q: {question}\nA: Let's think step by step."

# Step 5: Run the CoT prompting sc_strategy
print(f"\nQuestion: {question}")
print("Running Self-Consistency CoT...")
final_answer = sc_strategy.run(prompt)  # Returns the most consistent (repeated) answer

# Expected output: $0.05 or 0.05 (may vary slightly based on model and temperature)
print(f"\nCogitator's Answer (Self-Consistency): {final_answer}")

Documentation

See the docs directory for the documentation for the Cogitator toolkit.


Benchmarking Framework

This project includes a customizable and extensible benchmarking framework to evaluate the performance of different CoT strategies on various datasets like GSM8K and StrategyQA.

Check out the benches directory for more details about the framework and how it could be used.


Contributing

See CONTRIBUTING.md for details on how to make a contribution.

Logo

The logo is named "Cognition" and was originally created by vectordoodle.

License

Cogitator is licensed under the MIT License.

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

cogitator-0.1.0a3.tar.gz (28.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cogitator-0.1.0a3-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

Details for the file cogitator-0.1.0a3.tar.gz.

File metadata

  • Download URL: cogitator-0.1.0a3.tar.gz
  • Upload date:
  • Size: 28.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.17 Linux/6.11.0-1012-azure

File hashes

Hashes for cogitator-0.1.0a3.tar.gz
Algorithm Hash digest
SHA256 e3a0fffba1c3612912327f6cbf6d727e413a82f697b1443f9974cd8422e28c35
MD5 b3d8031db6b2485d3996e01337fe9324
BLAKE2b-256 c2949de85b7293b83511977f2407e52a1f2822b68a743f73a632e5f60952c3d0

See more details on using hashes here.

File details

Details for the file cogitator-0.1.0a3-py3-none-any.whl.

File metadata

  • Download URL: cogitator-0.1.0a3-py3-none-any.whl
  • Upload date:
  • Size: 34.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.10.17 Linux/6.11.0-1012-azure

File hashes

Hashes for cogitator-0.1.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 33745bad04bd1ca3f0cb45343b34109d25ce4944e1f74c215a55ebd69024f464
MD5 226843aae0a7d4ecbffbbf65d908423a
BLAKE2b-256 e69c046c27554d1084520725d75a7919245b5dc6f0e81c0cad679b0b17d66d18

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