A Python-based conversational AI chat application that seamlessly integrates with OpenAI models or compatible local LLM services. This project allows you to have interactive conversations while maintaining full conversation history.
- Features
- Prerequisites
- Installation
- Configuration
- Usage
- Project Structure
- Advanced Configuration
- Troubleshooting
- Contributing
- License
β¨ Core Features:
- π¬ Interactive conversational CLI interface with persistent message history
- π Flexible LLM backend support (OpenAI, local models, or compatible endpoints)
- βοΈ Highly configurable model parameters (temperature, max tokens, etc.)
- π‘οΈ Secure API key management with environment variable support
- π Conversation context preservation for multi-turn interactions
- π Simple and intuitive command-line interface
Before you begin, ensure you have the following installed on your system:
- Python: Version 3.8 or higher
- pip: Python package manager (comes with Python)
- Git: For cloning the repository (optional)
Depending on your LLM backend choice:
- Local LLM Service: If using local models, ensure a compatible LLM service is running (e.g., LM Studio, Ollama, or similar)
- OpenAI API Key: If using OpenAI's API directly instead of a local service
git clone https://github.com/sudipta-chaudhari/ai-chat.git
cd ai-chatOn Windows:
python -m venv venv
venv\Scripts\activateOn macOS/Linux:
python -m venv venv
source venv/bin/activatepip install -e .Or install dependencies manually:
pip install "openai>=1.3.0"The project uses a Settings class to manage all LLM configuration parameters. Configuration is defined in src/settings.py.
The Settings class requires all 5 parameters to be provided during initialization (no defaults):
| Parameter | Description | Type | Constraints |
|---|---|---|---|
base_url |
API endpoint URL | str |
Must start with http or https |
api_key |
Authentication key | str |
Any string (local models may use "not needed") |
model |
Model identifier | str |
Model name/ID |
temperature |
Response creativity | float |
0.0 - 1.0 |
max_tokens |
Max response tokens | int |
Positive integer |
Configure settings in main.py when creating the Settings instance:
settings = Settings(
base_url="http://127.0.0.1:1234/v1",
api_key="not needed",
model="liquid/lfm2.5-1.2b",
temperature=0.7,
max_tokens=512
)All configuration values can be modified after creation using property setters:
settings.temperature = 0.9 # Adjust creativity
settings.max_tokens = 1024 # Increase response lengthFor better security, especially for API keys, use environment variables in main.py:
import os
settings = Settings(
base_url=os.getenv("LLM_BASE_URL", "http://127.0.0.1:1234/v1"),
api_key=os.getenv("LLM_API_KEY", "not needed"),
model=os.getenv("LLM_MODEL", "liquid/lfm2.5-1.2b"),
temperature=float(os.getenv("LLM_TEMPERATURE", "0.7")),
max_tokens=int(os.getenv("LLM_MAX_TOKENS", "512"))
)Then set environment variables:
On Windows (PowerShell):
$env:LLM_API_KEY = "your-api-key-here"
$env:LLM_BASE_URL = "https://api.openai.com/v1"
$env:LLM_MODEL = "gpt-3.5-turbo"On macOS/Linux (Bash):
export LLM_API_KEY="your-api-key-here"
export LLM_BASE_URL="https://api.openai.com/v1"
export LLM_MODEL="gpt-3.5-turbo"python main.pyπ€ Chat initialized with model: liquid/lfm2.5-1.2b
Type 'exit' to quit, 'clear' to clear conversation history
--------------------------------------------------
You: What is Machine Learning? Explain in one sentence.
Assistant: Machine learning is a subset of artificial intelligence that enables computers to learn patterns from data without being explicitly programmed.
You: Write one more sentence.
Assistant: Machine learning empowers systems to improve their performance on tasks through experience and data analysis.
You: exit
Goodbye!
| Command | Action |
|---|---|
| Type any question | Send message to the chat |
clear |
Clear conversation history |
exit |
Quit the application |
ai-chat/
βββ main.py # Application entry point
βββ pyproject.toml # Project metadata and dependencies
βββ README.md # This file is primary documentation for a project
βββ src/
β βββ __init__.py # Package initialization
β βββ settings.py # Configuration settings (Settings class)
β βββ chat/
β βββ __init__.py # Package initialization
β βββ chat_client.py # OpenAI client wrapper
β βββ chat_session.py # Conversation history management
βββ openai_chat.egg-info/ # Package metadata (generated)
- main.py: Implements the CLI interface and main application loop
- src/settings.py:
Settingsclass with LLM configuration parameters - src/chat/chat_client.py:
ChatClientclass that handles API communication - src/chat/chat_session.py:
ChatSessionclass for managing conversation history - pyproject.toml: Project metadata, version, and dependencies
To use OpenAI's official API instead of a local service:
- Create an OpenAI account at https://openai.com
- Generate an API key from your account dashboard
- Configure settings in
main.pywhen creating theSettingsinstance:
settings = Settings(
base_url="https://api.openai.com/v1",
api_key="your-api-key-here", # IMPORTANT: For production, use environment variables as shown above!
model="gpt-3.5-turbo", # or "gpt-4"
temperature=0.7,
max_tokens=512
)- Download from https://lmstudio.ai
- Download a model in the LM Studio interface
- Start the local server (usually runs on
http://127.0.0.1:1234) - Keep default configuration in
src/settings.py(or override inmain.py).
- Install from https://ollama.ai
- Pull a model:
ollama pull llama2 - Models run on
http://127.0.0.1:11434by default - Update
src/settings.py(or override inmain.py):
# Example of overriding in main.py, before initializing ChatClient:
settings.base_url = "http://127.0.0.1:11434/v1"
settings.model = "llama2"You can optimize the model's behavior by adjusting temperature and max_tokens settings. These are defined as default values in src/settings.py and can be overridden in main.py before initializing the ChatClient.
For Factual/Precise Responses:
# Default in src/settings.py or override in main.py before initializing ChatClient:
settings.temperature = 0.1 # Lower temperature = more deterministic/focused
settings.max_tokens = 256 # Shorter responses for concisenessFor Creative Responses:
# Default in src/settings.py or override in main.py before initializing ChatClient:
settings.temperature = 0.9 # Higher temperature = more creative/varied
settings.max_tokens = 1024 # Longer responses for richer contentFor Balanced Performance (Default):
# Default settings in src/settings.py:
settings.temperature = 0.7 # Moderate creativity and consistency
settings.max_tokens = 512 # Balanced response lengthCause: LLM service is not running or URL is incorrect
Solution:
- Verify the LLM service is running on the configured URL
- Check
base_urlinsrc/settings.py - Test the endpoint with:
curl http://127.0.0.1:1234/v1/models
Cause: Invalid or missing API key
Solution:
- For local models: Set
api_key = "not needed"insrc/settings.py - For OpenAI: Verify your API key is correct and active
- Check for typos or extra whitespace in the key
Cause: Model is processing-intensive or network issues
Solution:
- Reduce
max_tokensinsrc/settings.py - Lower
temperaturefor simpler processing - Check network connectivity
- Try a smaller or faster model
Cause: Dependencies not installed
Solution:
pip install -e .
# or
pip install "openai>=1.3.0"Cause: Specified model is not available
Solution:
- Verify the model name in
model - Ensure the model is downloaded/installed on your system
- Check available models:
ollama listor your service's model manager
Contributions are welcome! Please follow these guidelines:
- Fork the repository on GitHub
- Create a feature branch:
git checkout -b feature/your-feature-name - Make your changes with clear, descriptive commits
- Test thoroughly before submitting
- Push to your fork:
git push origin feature/your-feature-name - Submit a Pull Request with a description of your changes
- Follow PEP 8 guidelines
- Use meaningful variable and function names
- Add docstrings to functions
- Keep functions focused and single-purpose
This project is open-source and available under the MIT License. See LICENSE file for details.
- π§ Email: Not provided
- π Issues: Report bugs on the GitHub Issues page
- π¬ Discussions: Join conversations in GitHub Discussions
- Initial release
- Basic CLI interface
- OpenAI API integration
- Local LLM support
- Conversation history management
- Built with OpenAI Python SDK
- Compatible with local LLM services like LM Studio and Ollama
- Inspired by modern AI chat interfaces
Last Updated: March 2026