Show HN: Cogitator - 用于 Chain-of-Thought Prompting 的 Python 工具包

habedi / **cogitator ** 公开 从 habedi/template-python-library 生成

A Python toolkit for chain-of-thought prompting 🐍

License

MIT license 30 stars 2 forks

habedi/cogitator

main BranchesTags

Code

文件夹和文件

Name| Name| Last commit message| Last commit date
---|---|---|---

最新提交

历史

6 Commits .github/workflows| .github/workflows
benches| benches
cogitator| cogitator
docs| docs
examples| examples
tests| tests
.editorconfig| .editorconfig
.gitattributes| .gitattributes
.gitignore| .gitignore
.pre-commit-config.yaml| .pre-commit-config.yaml
CODE_OF_CONDUCT.md| CODE_OF_CONDUCT.md
CONTRIBUTING.md| CONTRIBUTING.md
LICENSE| LICENSE
Makefile| Makefile
README.md| README.md
benches.yml| benches.yml
codecov.yml| codecov.yml
logo.svg| logo.svg
mkdocs.yml| mkdocs.yml
poetry.toml| poetry.toml
pyproject.toml| pyproject.toml

Cogitator Logo

Cogitator

Tests Code Coverage Code Quality Python Version PyPI Version Downloads License Docs DOI Cogitator 是一个用于 Chain-of-Thought Prompting 的 Python 工具包

Cogitator 是一个 Python 工具包,用于在大型语言模型 (LLM) 中实验和使用 chain-of-thought (CoT) prompting 方法。 CoT prompting 通过引导模型在得出最终答案之前生成中间推理步骤,从而提高 LLM 在复杂任务(如问答、推理和解决问题)方面的性能。 此外,它还可通过深入了解模型的推理过程来提高 LLM 的可解释性。 该工具包旨在使研究人员更容易使用流行的 CoT 策略和框架,或将它们集成到 AI 应用程序中。

特性

快速上手

您可以使用以下命令安装 Cogitator:

pip install cogitator

或者,如果您想从包含示例和基准测试的最新版本安装:

git clone https://github.com/habedi/cogitator && cd cogitator
# 设置 Python 环境
pip install poetry
poetry install --with dev
# 运行测试以确保一切正常(可选)
poetry run pytest

示例

下面是一个使用 Ollama 的 Self-Consistency CoT 的简单示例。

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}")

查看 examples 目录以获取更多示例。

文档

Cogitator 文档可在此处获得:链接

基准测试框架

该项目包含一个可自定义且可扩展的基准测试框架,用于评估不同 CoT 策略在各种数据集(如 GSM8KStrategyQA)上的性能。

查看 benches 目录以获取有关该框架及其使用方式的更多详细信息。

贡献

有关如何做出贡献的详细信息,请参阅 CONTRIBUTING.md

引用

如果您发现此项目有用,请给它一个 star! 如果您有任何问题或反馈,请使用存储库的讨论部分或打开一个 issue。 如果您在研究中使用此项目,请考虑使用以下信息进行引用:

@software{abedi_cogitator_2025,
  author = {Abedi Firouzjaei, Hassan},
  title = {{Cogitator: A Python Toolkit for Chain-of-Thought Prompting}},
  year = {2025--},
  publisher = {Zenodo},
  doi = {10.5281/zenodo.15331821},
  url = {https://github.com/habedi/cogitator}
}

Logo

该 logo 名为“Cognition”,最初由 vectordoodle 创建。

许可证

Cogitator 在 MIT License 下获得许可。

关于

A Python toolkit for chain-of-thought prompting 🐍

Topics

python machine-learning python-library explainable-ai ai-research ai-toolkit large-language-models prompt-engineering llms chain-of-thought cot-prompting

Resources

Readme

License

MIT license

Code of conduct

Code of conduct Activity

Languages