**Cogito** is a powerful Go library for building intelligent, co-operative agentic software and LLM-powered workflows, focusing on improving results for small, open source language models that scales to any LLM.
๐งช **Tested on Small Models** ! Our test suite runs on 0.6b Qwen (not fine-tuned), proving effectiveness even with minimal resources.
> ๐ **Working on Official Paper**
> I am currently working on the official academic/white paper for Cogito. The paper will provide detailed theoretical foundations, experimental results, and comprehensive analysis of the framework's capabilities.
## ๐๏ธ Architecture
Cogito is the result of building [LocalAI](https://github.com/mudler/LocalAI), [LocalAGI](https://github.com/mudler/LocalAGI) and [LocalOperator](https://github.com/mudler/LocalOperator) (yet to be released).
Cogito uses an internal pipeline to first make the LLM reason about a specific task, forcing the model to reason and later extracts with BNF grammars exact data structures from the LLM. This is applied to every primitive exposed by the framework.
It provides a comprehensive framework for creating conversational AI systems with advanced reasoning, tool execution, goal-oriented planning, and iterative content refinement capabilities.
> ๐ง **Composable Primitives**
> Cogito primitives can be combined to form more complex pipelines, enabling sophisticated AI workflows.
## ๐ Quick Start
### Installation
```bash
go get github.com/mudler/cogito
```
### Basic Usage
```go
package main
import (
"context"
"fmt"
"github.com/mudler/cogito"
)
func main() {
// Create an LLM client
llm := cogito.NewOpenAILLM("your-model", "api-key", "https://api.openai.com")
// Create a conversation fragment
fragment := cogito.NewEmptyFragment().
AddMessage("user", "Tell me about artificial intelligence")
// Get a response
newFragment, err := llm.Ask(context.Background(), fragment)
if err != nil {
panic(err)
}
fmt.Println(newFragment.LastMessage().Content)
}
```
### Using Tools
```go
// Create a fragment with user input
fragment := cogito.NewFragment(openai.ChatCompletionMessage{
Role: "user",
Content: "What's the weather in San Francisco?",
})
// Execute with tools
result, err := cogito.ExecuteTools(llm, fragment,
cogito.WithTools(&WeatherTool{}))
if err != nil {
panic(err)
}
// result.Status.ToolsCalled will contain all the tools being called
```
### Goal-Oriented Planning
```go
// Extract a goal from conversation
goal, err := cogito.ExtractGoal(llm, fragment)
if err != nil {
panic(err)
}
// Create a plan to achieve the goal
plan, err := cogito.ExtractPlan(llm, fragment, goal,
cogito.WithTools(&SearchTool{}))
if err != nil {
panic(err)
}
// Execute the plan
result, err := cogito.ExecutePlan(llm, fragment, plan, goal,
cogito.WithTools(&SearchTool{}))
if err != nil {
panic(err)
}
```
### Content Refinement
```go
// Refine content through iterative improvement
refined, err := cogito.ContentReview(llm, fragment,
cogito.WithIterations(3),
cogito.WithTools(&SearchTool{}))
if err != nil {
panic(err)
}
```
### Iterative Content Improvement
An example on how to iteratively improve content by using two separate models:
```go
// Create initial content
initial := cogito.NewEmptyFragment().
AddMessage("user", "Write about climate change")
llm := cogito.NewOpenAILLM("your-model", "api-key", "https://api.openai.com")
response, err := llm.Ask(ctx, initial)
reviewerLLM := cogito.NewOpenAILLM("your-reviewer-model", "api-key", "https://api.openai.com")
// Iteratively improve with tool support
improved, err := cogito.ContentReview(reviewerLLM, response,
cogito.WithIterations(3),
cogito.WithTools(&SearchTool{}, &FactCheckTool{}),
cogito.EnableToolReasoner)
```
### Custom Prompts
```go
customPrompt := cogito.NewPrompt(`Your custom prompt template with {{.Context}}`)
result, err := cogito.ExecuteTools(llm, fragment,
cogito.WithPrompt(cogito.ToolSelectorType, customPrompt))
```
## ๐ฎ Examples
### Interactive Chat Bot
```bash
# Run the example chat application
make example-chat
```
This starts an interactive chat session with tool support including web search capabilities.
### Custom Tool Implementation
See `examples/internal/search/search.go` for a complete example of implementing a DuckDuckGo search tool.
## ๐งช Testing
The library includes comprehensive test coverage using Ginkgo and Gomega. Tests use containerized LocalAI for integration testing.
### Running Tests
```bash
# Run all tests
make test
# Run with specific log level
LOG_LEVEL=debug make test
# Run with custom arguments
GINKGO_ARGS="--focus=Fragment" make test
```
## ๐ License
Ettore Di Giacinto 2025-now. Cogito is released under the Apache 2.0 License.
## ๐ Citation
If you use Cogito in your research or academic work, please cite our paper:
```bibtex
@article{cogito2025,
title={Cogito: A Framework for Building Intelligent Agentic Software with LLM-Powered Workflows},
author={Ettore Di Giacinto },
journal={https://github.com/mudler/cogito},
year={2025},
note={}
}
```