CoolFace
Apppublic

mdAmin313/Cognitive-ai

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
App README

๐Ÿง  Cognitive AI Agent

A human-like AI system with persistent memory, multi-step reasoning, and self-reflection.

Features

  • โ€”4-Layer Memory System: Short-term, episodic, semantic, and procedural memory
  • โ€”Chain-of-Thought Reasoning: Explains its thinking step-by-step
  • โ€”Self-Reflection: Evaluates and improves its own outputs
  • โ€”Goal Management: Tracks and pursues objectives
  • โ€”No API Keys Required: Runs completely locally (uses stub mode)
  • โ€”Confidence Scores: Returns confidence levels with responses

How It Works

User Input
    โ†“
Memory Retrieval (Find relevant context)
    โ†“
Reasoning (Plan step-by-step approach)
    โ†“
Response Generation (LLM or stub)
    โ†“
Self-Reflection (Evaluate quality)
    โ†“
Learning (Store new knowledge)
    โ†“
Output (Response + Confidence)

Running on Hugging Face Spaces

  1. 1.Create a new Space on Hugging Face
  2. 2.Clone this repo or upload the files
  3. 3.Set the runtime to "CPU basic" or higher
  4. 4.The `app.py` will auto-launch the Gradio interface

Running Locally

bash
# Install requirements
pip install -r requirements.txt

# Option 1: Web interface (Gradio)
python app.py

# Option 2: Interactive CLI
python -m examples.examples_interactive

# Option 3: Run demos
python -m examples.examples_simple_agent

Project Structure

cognitive-agent/
โ”œโ”€โ”€ app.py                      # Hugging Face Spaces app
โ”œโ”€โ”€ requirements.txt            # Dependencies
โ”œโ”€โ”€ cognitive_agent/            # Main package
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ core_agent.py          # Main orchestrator
โ”‚   โ”œโ”€โ”€ core_brain.py          # LLM integration
โ”‚   โ”œโ”€โ”€ core_memory.py         # Memory systems
โ”‚   โ”œโ”€โ”€ core_reasoning.py      # Reasoning engine
โ”‚   โ”œโ”€โ”€ core_reflection.py     # Self-reflection
โ”‚   โ”œโ”€โ”€ core_retrieval.py      # Context retrieval
โ”‚   โ”œโ”€โ”€ utils_config.py        # Configuration
โ”‚   โ””โ”€โ”€ utils_embeddings.py    # Vector embeddings
โ””โ”€โ”€ examples/                   # Examples
    โ”œโ”€โ”€ examples_interactive.py # CLI demo
    โ””โ”€โ”€ examples_simple_agent.py # 6 code examples

Quick Start

Web Interface (Gradio)

bash
python app.py
# Opens at http://localhost:7860

Programmatic Usage

python
from cognitive_agent import CognitiveAgent, Config

# Create agent
agent = CognitiveAgent(mode='stub', provider='local')

# Ask a question
response, metadata = agent.think("What is artificial intelligence?")

print(response)
print(f"Confidence: {metadata['confidence']:.1%}")
print(f"Memory items: {metadata['memory_stats']['total_items']}")

Memory Across Conversations

The agent learns and retains information across conversations:

  1. 1.First interaction: "Tell me about Python"
  2. 2.Agent responds and stores knowledge about Python
  1. 1.Second interaction: "What programming languages did we discuss?"
  2. 2.Agent retrieves from memory and responds with Python
  1. 1.Third interaction: "Compare Python to other languages"
  2. 2.Agent uses previous context to make comparisons

Customization

Change memory sizes

python
from cognitive_agent import Config

config = Config()
config.memory.short_term_max_items = 100
config.memory.episodic_memory_max = 2000

agent = CognitiveAgent(mode='stub', config=config)

Use real LLM (Anthropic Claude)

python
import os

agent = CognitiveAgent(
    mode='api',
    provider='anthropic',
    api_key=os.environ.get('ANTHROPIC_API_KEY')
)

Disable self-reflection (faster)

python
config = Config()
config.reflection.enable_self_reflection = False
agent = CognitiveAgent(config=config)

Performance

  • โ€”Response time: ~500ms (stub mode)
  • โ€”Memory usage: ~5MB after 100 interactions
  • โ€”Confidence range: 0.75-0.85 typical
  • โ€”Reasoning depth: 3-5 steps per query

Architecture Details

7-Step Cognitive Loop

  1. 1.Context Retrieval - Find relevant memories using vector similarity
  2. 2.Reasoning - Break down problem into steps with confidence
  3. 3.Brain (LLM) - Generate response using language model
  4. 4.Reflection - Evaluate response quality on 5 dimensions
  5. 5.Improvement - Auto-improve if quality score < 0.7
  6. 6.Learning - Store interaction as new memories
  7. 7.Output - Return response with confidence score

Memory Types

  • โ€”Short-term (50 items, 1 hour): Conversation context
  • โ€”Episodic (1000 items): Specific events and experiences
  • โ€”Semantic (5000 items): General knowledge and facts
  • โ€”Procedural (expandable): Learned patterns and strategies

Reflection Checks

The system evaluates responses on:

  • โ€”โœ“ Coherence - Does it flow logically?
  • โ€”โœ“ Completeness - Does it address the query?
  • โ€”โœ“ Accuracy - Are claims supported?
  • โ€”โœ“ Clarity - Is it understandable?
  • โ€”โœ“ Consistency - Does it match memory?

If overall score < 0.7, the system regenerates an improved version.

Examples

See examples/ directory for:

  • โ€”6 programmatic demonstrations (examples_simple_agent.py)
  • โ€”Interactive CLI interface (examples_interactive.py)

API Reference

CognitiveAgent

python
# Initialize
agent = CognitiveAgent(
    mode='stub' | 'api',
    provider='local' | 'anthropic' | 'openai',
    api_key=str,
    config=Config
)

# Main method
response, metadata = agent.think(
    user_input: str,
    enable_reflection: bool = True
)

# Introspection
agent.get_memory_summary() -> Dict
agent.get_cognitive_trace(limit: int = 5) -> List[Dict]

# Persistence
agent.save_state(filepath: str)
agent.load_state(filepath: str)

Limitations

  • โ€”Stub mode uses deterministic responses (good for testing)
  • โ€”No fine-tuning on user data (future enhancement)
  • โ€”Memory limited to in-process storage (add persistent DB for production)
  • โ€”No multi-agent reasoning (future enhancement)

Future Enhancements

  • โ€”[ ] Persistent database (SQLite)
  • โ€”[ ] Tool integration (search, calculator, etc.)
  • โ€”[ ] Multi-agent communication
  • โ€”[ ] Fine-tuning on domain data
  • โ€”[ ] Streaming responses
  • โ€”[ ] Vision integration

License

MIT License - Feel free to use and modify

Citation

If you use this system, cite as:

Cognitive AI Agent - A human-like AI prototype with memory, reasoning, and self-reflection.
(2024)

Questions & Support

  • โ€”Check the documentation in cognitive_agent/ directory
  • โ€”Look at examples in examples/ directory
  • โ€”Read code comments for implementation details

Ready to start? Run python app.py for the web interface or choose a script in the examples directory!