Skip to main content

A flexible Python framework for building AI chat applications with support for multiple LLM providers including OpenAI, Anthropic, Gemini, DeepSeek, and local Llama models

Project description

Common AI Core

A flexible Python framework for building AI chat applications with support for multiple LLM providers.

Features

  • 🤖 Support for multiple LLM providers:
    • OpenAI (GPT-3.5, GPT-4) - included by default
    • Anthropic (Claude) - optional
    • Llama (local models) - optional
  • 💾 Flexible memory management:
    • Token-based memory limits
    • Prompt-based memory limits
    • System prompt preservation
  • 🔄 Multiple chat modes:
    • Streaming responses
    • Completion responses
  • 📊 Token counting and cost estimation
  • 🎨 Pretty-printed chat history
  • 🔍 Content parsing utilities:
    • JSON structure extraction from LLM outputs
    • Python code parsing

Installation

# Install the complete framework (includes all features, OpenAI provider ready to use)
pip install common-ai-core

# Add support for Anthropic's Claude (requires anthropic package)
pip install "common-ai-core[anthropic]"

# Add support for Google's Gemini (requires google-generativeai package)
pip install "common-ai-core[gemini]"

# Add support for DeepSeek (uses OpenAI client, no extra package needed)
pip install "common-ai-core[deepseek]"

# Install with all cloud providers (OpenAI, Anthropic, Gemini, DeepSeek)
pip install "common-ai-core[all-cloud]"

# Add support for local Llama models (requires llama-cpp-python package)
pip install "common-ai-core[llama]"

# Install with all providers including Llama
pip install "common-ai-core[all]"

# Development installation (includes testing tools)
pip install "common-ai-core[dev]"

Quick Start

from common_ai_core import ProviderBuilder, ProviderType, SystemTokenLimitedMemory, CompletionChat

# Create a provider (using OpenAI by default)
provider = ProviderBuilder(ProviderType.OPENAI).build()

# Create memory with system prompt
memory = SystemTokenLimitedMemory.from_provider(
    provider=provider,
    system_prompt="You are a helpful assistant.",
    max_tokens=1000
)

# Create chat interface
chatbot = CompletionChat(provider, memory)

# Chat!
response = chatbot.chat("Tell me about Python!")
print(response)

Memory Types

  • TokenLimitedMemory: Limits conversation by token count
  • PromptLimitedMemory: Limits conversation by number of exchanges
  • SystemTokenLimitedMemory: Token-limited with preserved system prompt
  • SystemPromptLimitedMemory: Prompt-limited with preserved system prompt

Providers

  • OpenAI (included by default)

    • Supports GPT-4o-mini (default), GPT-4o, and GPT-3.5 models
    • Includes token counting
    • Streaming support
  • Anthropic (optional)

    • Supports Claude models
    • Install with: pip install "common-ai-core[anthropic]"
    provider = ProviderBuilder(ProviderType.ANTHROPIC).build()
    
  • Llama (optional)

    • Supports local models
    • Install with: pip install "common-ai-core[llama]"
    provider = (ProviderBuilder(ProviderType.LLAMA)
               .set_model_path("path/to/model.gguf")
               .build())
    
  • DeepSeek (optional)

    • Supports DeepSeek models including reasoning models
    • Install with: pip install "common-ai-core[deepseek]"
    provider = ProviderBuilder(ProviderType.DEEPSEEK).build()
    
  • Gemini (optional)

    • Supports Google's Gemini models
    • Install with: pip install "common-ai-core[gemini]"
    provider = ProviderBuilder(ProviderType.GEMINI).build()
    

Error Handling

Common AI Core provides clear error messages when optional dependencies are missing:

from common_ai_core import ProviderBuilder, ProviderType

try:
    # This will work if you have openai installed
    provider = ProviderBuilder(ProviderType.OPENAI).build()
    print("OpenAI provider created successfully!")
except Exception as e:
    print(f"Error: {e}")

try:
    # This will fail with a clear message if anthropic is not installed
    provider = ProviderBuilder(ProviderType.ANTHROPIC).build()
except Exception as e:
    print(f"Error: {e}")
    # Output: Error: Anthropic package not installed: No module named 'anthropic'
    # Solution: pip install "common-ai-core[anthropic]"

Parsers

Common AI Core includes utilities for parsing and extracting structured content from LLM outputs:

JSON Parser

Extract valid JSON structures from LLM text outputs:

from common_ai_core.parsers.json_parser import JsonParser

# Extract JSON from LLM output
llm_output = """This is some text with embedded JSON: 
{\"key\": \"value\", \"nested\": {\"data\": 123}} 
and more text after."""

parser = JsonParser(llm_output)
json_objects = parser.extract_json_structures()

# Process extracted JSON objects
for json_obj in json_objects:
    print(json_obj)  # {'key': 'value', 'nested': {'data': 123}}

The JSON parser can extract JSON objects even when they're embedded in markdown code blocks or surrounded by other text.

Development

# Clone the repository
git clone https://github.com/commonai/common-ai-core.git

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

common_ai_core-0.1.4.tar.gz (37.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

common_ai_core-0.1.4-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file common_ai_core-0.1.4.tar.gz.

File metadata

  • Download URL: common_ai_core-0.1.4.tar.gz
  • Upload date:
  • Size: 37.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for common_ai_core-0.1.4.tar.gz
Algorithm Hash digest
SHA256 13c13382902d961d4f7a6fd6e33f1dfc2e79eea55be04e1014aef9feba98f610
MD5 3a5ede0a63ea332371fdbabb0972b1c9
BLAKE2b-256 2b9e88c42e883e56bca3e775990fab20dfca7835ee4eab933d0663f2f93cf837

See more details on using hashes here.

File details

Details for the file common_ai_core-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: common_ai_core-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for common_ai_core-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7e1dd71d0140bf1b037d90c764846adb0468de2375007ebbe42aa80843185791
MD5 605e502348996eceac85c2d852d84184
BLAKE2b-256 8e032ac2669729ca001d9a6d7634b42bc53162e72af1bd342f20da645bb4e67b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page