Advanced Chain of Thought (CoT) Reasoning API with Reinforcement Learning (RL)
Project description
Fragaria - From 'r's in Strawberry to Complex Problem-Solving AI
Advanced Chain of Thought (CoT) Reasoning API with Reinforcement Learning (RL)
Fragaria is a powerful and flexible Chain of Thought (CoT) reasoning library that leverages various Language Model (LLM) providers and incorporates Reinforcement Learning (RL) techniques to solve complex problems and answer intricate questions. Named after the botanical genus of strawberries, Fragaria pays homage to the famous "How many 'r's in strawberry?" problem, symbolizing its ability to tackle both simple and complex queries with equal finesse.
Table of Contents
Features
- Multi-Provider Support: Seamlessly switch between OpenAI, Groq, and Together.ai as LLM providers.
- Chain of Thought Reasoning: Employ sophisticated CoT techniques to break down and solve complex problems.
- Reinforcement Learning: Utilize RL algorithms to continuously improve problem-solving strategies and adapt to new challenges.
- Adaptive Learning: Enhance performance over time through a SQLite-based scoring system integrated with RL techniques.
- Configurable: Easy-to-update YAML configuration file for flexible setup of both LLM and RL parameters.
- OpenAPI Documentation: Comprehensive API documentation with Swagger UI and ReDoc.
- CORS Support: Built-in Cross-Origin Resource Sharing for easy integration with web applications.
- CLI Tools: Command-line interface for easy testing and integration.
- Python Library: Usable as a Python library in your own projects.
Installation
Install Fragaria using pip:
pip install fragaria
Or if you want to install from source:
git clone https://github.com/terraprompt/fragaria.git
cd fragaria
poetry install
Configuration
-
Create a configuration file by copying the default:
# If installed via pip cp /path/to/site-packages/fragaria/config.yaml ./config.yaml # If installed from source cp fragaria/fragaria/config.yaml ./config.yaml
-
Open
config.yamland update the following settings:- Set your preferred
llm_provider(openai, groq, or together) - Add your API keys for the respective providers
- Adjust the model names if necessary
- Modify the database path and server settings if needed
- Set your preferred
Important: You must update the configuration file with your actual API keys for the LLM provider you want to use. The default values are placeholders and will not work.
Usage
As a Library
Fragaria can be used as a Python library in your own projects:
import asyncio
from fragaria import analyze_problem
async def main():
result = await analyze_problem("How many 'r's in strawberry?")
print(result["result"])
asyncio.run(main())
For more examples, see the example.py file in the repository.
You can also use the FragariaCore class for more advanced usage:
import asyncio
from fragaria import FragariaCore
async def main():
# Initialize with a custom config file path (optional)
core = FragariaCore("path/to/your/config.yaml")
result = await core.parallel_cot_reasoning("How many 'r's in strawberry?")
print(result["result"])
asyncio.run(main())
Note: Before running the examples, you must configure your API keys in the config.yaml file. See the Configuration section for details.
Command Line Interface
After installation, you can use the fragaria command to analyze problems:
# Analyze a simple problem
fragaria "How many 'r's in strawberry?"
# Use with a system prompt
fragaria "What is the capital of France?" --system-prompt "You are a helpful geography assistant."
# Read from stdin
echo "A princess is as old as the prince will be when the princess is twice as old as the prince was when the princess's age was half the sum of their present age. What is the age of prince and princess?" | fragaria
# Get JSON output
fragaria "How many 'r's in strawberry?" --output-format json
Web Service
Start the Fragaria API server:
# Using the CLI command
fragaria-server
# Or directly with Python
python -m fragaria.main
The API will be available at http://localhost:8000 (or the host/port specified in your config).
You can now send POST requests to http://localhost:8000/v1/chat/completions to use the Chain of Thought reasoning capabilities.
API Documentation
Fragaria provides comprehensive API documentation:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc - OpenAPI JSON schema:
http://localhost:8000/openapi.json
Examples
Here are some sample problems you can solve using Fragaria:
-
The classic strawberry problem:
{ "model": "faragia-dev", "messages": [ {"role": "user", "content": "How many 'r's in strawberry?"} ] }
-
A more complex age-related puzzle:
{ "model": "faragia-dev", "messages": [ {"role": "user", "content": "A princess is as old as the prince will be when the princess is twice as old as the prince was when the princess's age was half the sum of their present age. What is the age of prince and princess? Provide all solutions to that question."} ] }
To solve these problems, send a POST request to /v1/chat/completions with the above JSON payloads.
How It Works
Fragaria employs a sophisticated Chain of Thought (CoT) reasoning process enhanced by Reinforcement Learning:
- Problem Classification: Categorizes the input problem into known or new problem types.
- CoT Path Generation: Creates multiple reasoning approaches for the problem type, influenced by past performance.
- Parallel Execution: Applies each CoT path to the problem concurrently.
- Result Combination: Synthesizes the results from different paths.
- Evaluation: Scores the effectiveness of each approach.
- Reinforcement Learning Update: Uses the evaluation scores as rewards to update the RL policy, influencing future path selections and generations.
- Adaptive Learning: Updates the scoring database and RL model to improve future performance.
This RL-enhanced process allows Fragaria to not only tackle a wide range of problems but also to learn and adapt its strategies over time, becoming increasingly efficient at solving both familiar and novel problem types.
Core Library
Fragaria's core library provides a powerful Python API for integrating Chain of Thought reasoning into your applications. The main components are:
FragariaCore Class
The FragariaCore class is the primary interface for interacting with Fragaria's reasoning engine:
from fragaria.core import FragariaCore
# Initialize the core with default or custom configuration
core = FragariaCore()
# Perform reasoning on a problem
result = await core.parallel_cot_reasoning("How many 'r's in strawberry?")
Key methods of the FragariaCore class include:
parallel_cot_reasoning(text, system_prompt): Main entry point that performs the complete CoT reasoning processclassify_or_create_problem_type(text): Classifies a problem or creates a new typegenerate_cot_paths(text, problem_type): Generates multiple reasoning approachesrun_cot_path(session, text, path, problem_type, system_prompt): Executes a single reasoning pathcombine_results(results, problem_type, system_prompt): Synthesizes results from multiple pathsevaluate_result(text, result, problem_type, system_prompt): Evaluates the quality of resultsupdate_cot_scores(problem_type, paths, scores): Updates path scores in the databaseselect_cot_paths(problem_type, n): Selects reasoning paths using UCB algorithmadapt_cot_path(path, problem_type, text, system_prompt): Adapts existing paths for new problems
Convenience Functions
For simpler use cases, Fragaria provides convenience functions:
from fragaria.core import analyze_problem
# Simple async function for analyzing problems
result = await analyze_problem("How many 'r's in strawberry?")
Configuration
The core library is configured through a YAML file that specifies:
- LLM provider settings (OpenAI, Groq, Together.ai)
- Model configurations for different reasoning stages
- Database path for storing CoT path scores
- Server settings for the web API
Database Integration
Fragaria uses SQLite to store and update scores for different reasoning paths, enabling the Reinforcement Learning component to improve over time. The database tracks:
- Problem types
- Reasoning methods
- Performance scores
- Usage statistics
Contributing
We welcome contributions to Fragaria! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them with clear, descriptive messages.
- Push your changes to your fork.
- Submit a pull request to the main Fragaria repository.
Please ensure your code adheres to the project's coding standards and include tests for new features.
License
Fragaria is released under the MIT License. See the LICENSE file for details.
Citation
If you use Fragaria in your research or wish to refer to it in your publications, please use the following BibTeX entry:
@software{fragaria2024,
author = {{Dipankar Sarkar}},
title = {Fragaria: Advanced Chain of Thought Reasoning API with Reinforcement Learning},
year = 2024,
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/terraprompt/fragaria}},
}
For academic papers, you can cite Fragaria as:
Dipankar Sarkar. (2024). Fragaria: Advanced Chain of Thought Reasoning API with Reinforcement Learning [Computer software]. https://github.com/terraprompt/fragaria
Fragaria is maintained by the TerraPrompt team. For any questions or support, please open an issue on the GitHub repository.
Project details
Release history Release notifications | RSS feed
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 fragaria-0.1.2.tar.gz.
File metadata
- Download URL: fragaria-0.1.2.tar.gz
- Upload date:
- Size: 36.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
659a5cd84860c18b42753a4d9d127d1af43335824d68cd8916e338d5ceffdd7e
|
|
| MD5 |
7e337533d9d9a198176952333a80f7c1
|
|
| BLAKE2b-256 |
5547a7db16e3792d4c70f66a03347172c52ad6011efd15bd78657bffa33b85d0
|
Provenance
The following attestation bundles were made for fragaria-0.1.2.tar.gz:
Publisher:
publish.yml on terraprompt/fragaria
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fragaria-0.1.2.tar.gz -
Subject digest:
659a5cd84860c18b42753a4d9d127d1af43335824d68cd8916e338d5ceffdd7e - Sigstore transparency entry: 445584725
- Sigstore integration time:
-
Permalink:
terraprompt/fragaria@e2098460c109ca59e29465dae0ee5071ac568b82 -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/terraprompt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e2098460c109ca59e29465dae0ee5071ac568b82 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fragaria-0.1.2-py3-none-any.whl.
File metadata
- Download URL: fragaria-0.1.2-py3-none-any.whl
- Upload date:
- Size: 38.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e37b651f5f7296b00dcde770a530f60e3b1985629a961dfbd921b2b3ce06691
|
|
| MD5 |
73de3b09c044540747e88a21366bff34
|
|
| BLAKE2b-256 |
c73582eb78aa5872816c35cfe602688cc81a61cb3c09559fbdc76735f46636e2
|
Provenance
The following attestation bundles were made for fragaria-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on terraprompt/fragaria
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fragaria-0.1.2-py3-none-any.whl -
Subject digest:
3e37b651f5f7296b00dcde770a530f60e3b1985629a961dfbd921b2b3ce06691 - Sigstore transparency entry: 445584797
- Sigstore integration time:
-
Permalink:
terraprompt/fragaria@e2098460c109ca59e29465dae0ee5071ac568b82 -
Branch / Tag:
refs/tags/0.1.2 - Owner: https://github.com/terraprompt
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e2098460c109ca59e29465dae0ee5071ac568b82 -
Trigger Event:
release
-
Statement type: