mdAmin313/Cognitive-ai
0
๐ง 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
- Create a new Space on Hugging Face
- Clone this repo or upload the files
- Set the runtime to "CPU basic" or higher
- The `app.py` will auto-launch the Gradio interface
Running Locally
# 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_agentProject 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 examplesQuick Start
Web Interface (Gradio)
python app.py
# Opens at http://localhost:7860Programmatic Usage
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:
- First interaction: "Tell me about Python"
- Agent responds and stores knowledge about Python
- Second interaction: "What programming languages did we discuss?"
- Agent retrieves from memory and responds with Python
- Third interaction: "Compare Python to other languages"
- Agent uses previous context to make comparisons
Customization
Change memory sizes
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)
import os
agent = CognitiveAgent(
mode='api',
provider='anthropic',
api_key=os.environ.get('ANTHROPIC_API_KEY')
)Disable self-reflection (faster)
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
- Context Retrieval - Find relevant memories using vector similarity
- Reasoning - Break down problem into steps with confidence
- Brain (LLM) - Generate response using language model
- Reflection - Evaluate response quality on 5 dimensions
- Improvement - Auto-improve if quality score < 0.7
- Learning - Store interaction as new memories
- 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
# 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!
