Skip to main content

A Python library providing tools, helpers, and LangChain wrappers for building AI-powered applications with multi-provider LLM support

Project description

Par AI Core

PyPI PyPI - Python Version
Runs on Linux | MacOS | Windows Arch x86-63 | ARM | AppleSilicon PyPI - Downloads

PyPI - License

codecov

Table of Contents

Description

Par AI Core is a Python library that provides a set of tools, helpers, and wrappers built on top of LangChain. It is designed to accelerate the development of AI-powered applications by offering a streamlined and efficient way to interact with various Large Language Models (LLMs) and related services. This library serves as the foundation for my AI projects, encapsulating common functionalities and best practices for LLM integration.

"Buy Me A Coffee"

Technology

  • Python
  • LangChain

Prerequisites

  • Python 3.11 or higher
  • UV package manager
  • API keys for chosen AI provider (except for Ollama and LlamaCpp)
    • See (Environment Variables)[#environment-variables] below for provider-specific variables

Features

  • Simplified LLM Configuration: Easily configure and manage different LLM providers (OpenAI, Anthropic, etc.) and models through a unified interface.
  • Asynchronous and Synchronous Support: Supports both asynchronous and synchronous calls to LLMs.
  • Context Management: Tools for gathering relevant files as context for LLM prompts.
  • Output Formatting: Utilities for displaying LLM outputs in various formats (JSON, CSV, tables).
  • Cost Tracking: Optional integration to display the cost of LLM calls.
  • Tool Call Handling: Support for handling tool calls within LLM interactions.

Documentation

Library Documentation

Installation

uv add par_ai_core

Update

uv add par_ai_core -U

Environment Variables

Create a .env file in the root of your project with the following content adjusted for your needs

# AI API KEYS
OPENAI_API_KEY=
ANTHROPIC_API_KEY=
GROQ_API_KEY=
XAI_API_KEY=
GOOGLE_API_KEY=
MISTRAL_API_KEY=
GITHUB_TOKEN=
OPENROUTER_API_KEY=
DEEPSEEK_API_KEY=
# Used by Bedrock
AWS_PROFILE=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AZURE_OPENAI_API_KEY=

# Search
GOOGLE_CSE_ID=
GOOGLE_CSE_API_KEY=
SERPER_API_KEY=
SERPER_API_KEY_GOOGLE=
TAVILY_API_KEY=
JINA_API_KEY=
BRAVE_API_KEY=
REDDIT_CLIENT_ID=
REDDIT_CLIENT_SECRET=

# Misc API
WEATHERAPI_KEY=
GITHUB_PERSONAL_ACCESS_TOKEN=


### Tracing (optional)
LANGCHAIN_TRACING_V2=false
LANGCHAIN_ENDPOINT=https://api.smith.langchain.com
LANGCHAIN_API_KEY=
LANGCHAIN_PROJECT=par_ai

# PARAI Related (Not all providers / models support all vars)
PARAI_AI_PROVIDER=
PARAI_MODEL=
PARAI_AI_BASE_URL=
PARAI_TEMPERATURE=
PARAI_TIMEOUT=
PARAI_STREAMING=
PARAI_USER_AGENT_APPID=
PARAI_NUM_CTX=
PARAI_NUM_REDICT=
PARAI_REPEAT_LAST_N=
PARAI_REPEAT_PENALTY=
PARAI_MIROSTAT=
PARAI_MIROSTAT_ETA=
PARAI_MIROSTAT_TAU=
PARAI_TFS_Z=
PARAI_TOP_K=
PARAI_TOP_P=
PARAI_SEED=
PARAI_REASONING_EFFORT=
PARAI_REASONING_BUDGET=
PARAI_LOG_LEVEL=

AI API KEYS

Open AI Compatible Providers

If a specify provider is not listed but has an OpenAI compatible endpoint you can use the following combo of vars:

  • PARAI_AI_PROVIDER=OpenAI
  • PARAI_MODEL=Your selected model
  • PARAI_AI_BASE_URL=The providers OpenAI endpoint URL

Search

Misc API

PARAI Related

  • PARAI_AI_PROVIDER is one of Ollama|OpenAI|Groq|XAI|Anthropic|Gemini|Bedrock|Github|LlamaCpp|OpenRouter|LiteLLM
  • PARAI_MODEL is the model to use with the selected provider
  • PARAI_AI_BASE_URL can be used to override the base url used to call a provider
  • PARAI_TEMPERATURE sets model temperature. Range depends on provider usually 0.0 to 1.0
  • PARAI_TIMEOUT length of time to wait in seconds for ai response
  • PARAI_STREAMING enables or disables streaming responses (true/false)
  • PARAI_USER_AGENT_APPID sets an app identifier added to the User-Agent header for API requests
  • PARAI_NUM_CTX sets the context window size. Max size varies by model
  • PARAI_REASONING_EFFORT sets reasoning effort for OpenAI thinking models (low/medium/high)
  • PARAI_REASONING_BUDGET sets the reasoning token budget for Anthropic models
  • PARAI_LOG_LEVEL sets the logging level (DEBUG/INFO/WARNING/ERROR)
  • Other PARAI related params (NUM_PREDICT, REPEAT_LAST_N, REPEAT_PENALTY, MIROSTAT*, TFS_Z, TOP_K, TOP_P, SEED) are to tweak model responses; not all are supported by all providers

Example

"""Basic LLM example using Par AI Core."""

import sys

from dotenv import load_dotenv

from par_ai_core.llm_config import LlmConfig, llm_run_manager
from par_ai_core.llm_providers import (
    LlmProvider,
    is_provider_api_key_set,
    provider_light_models,
)
from par_ai_core.par_logging import console_out
from par_ai_core.pricing_lookup import PricingDisplay
from par_ai_core.provider_cb_info import get_parai_callback


def main() -> None:
    """
    Use OpenAI lightweight model to answer a question from the command line.

    This function performs the following steps:
    1. Checks if OpenAI API key is set
    2. Validates that a prompt is provided as a command-line argument
    3. Configures an OpenAI chat model
    4. Invokes the model with a system and user message
    5. Prints the model's response

    Requires:
    - OPENAI_API_KEY environment variable to be set
    - A prompt provided as the first command-line argument
    """

    load_dotenv()

    # Validate OpenAI API key is available
    if not is_provider_api_key_set(LlmProvider.OPENAI):
        console_out.print("OpenAI API key not set. Please set the OPENAI_API_KEY environment variable.")
        return

    # Ensure a prompt is provided via command-line argument
    if len(sys.argv) < 2:
        console_out.print("Please provide a prompt as 1st command line argument.")
        return

    # Configure the LLM using OpenAI's lightweight model
    llm_config = LlmConfig(provider=LlmProvider.OPENAI, model_name=provider_light_models[LlmProvider.OPENAI])
    chat_model = llm_config.build_chat_model()

    # Use context manager to handle callbacks for pricing and tool calls
    with get_parai_callback(show_pricing=PricingDisplay.DETAILS, show_tool_calls=True, show_end=False):
        # Prepare messages with a system context and user prompt
        messages: list[dict[str, str]] = [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": sys.argv[1]},
        ]

        # Invoke the chat model and get the result
        result = chat_model.invoke(messages, config=llm_run_manager.get_runnable_config(chat_model.name or ""))

        # Print the model's response
        console_out.print(result.content)


if __name__ == "__main__":
    main()

What's New

  • Version 0.5.7:
    • Updated all dependencies to latest versions (langchain, langgraph, openai, litellm, pydantic, etc.)
    • Refreshed pricing-lookup tests to current LiteLLM model database
    • Resolved all pyright type errors in test suite
  • Version 0.5.6:
    • Post-audit release with all 72 audit issues resolved
  • Version 0.5.5:
    • Fix: Made ParAICallbackHandler hashable to prevent LangChain callback merge errors
  • Version 0.5.4:
    • Updated default OpenAI model from gpt-5 to gpt-5.1
    • Updated LiteLLM default model from gpt-5 to gpt-5.1
    • Updated vision model from gpt-5 to gpt-5.1
  • Version 0.4.3:
    • Python 3.14 Support: Added support for Python 3.14 while maintaining compatibility with Python 3.11-3.13
    • Dropped Python 3.10: Minimum required Python version is now 3.11
    • Removed Unused Dependencies: Removed langchain-chroma and langchain-qdrant (not used in codebase)
    • Updated Development Tools: Ruff and Pyright now target Python 3.14
    • CI/CD Updates: GitHub Actions workflows updated to test against Python 3.11-3.14
    • Updated dependencies and ensured Python 3.14 compatibility
  • Version 0.4.2:
    • Updated dependencies
  • Version 0.4.1:
    • Dependency Fix: Fixed compatibility issue between litellm and openai libraries by constraining openai to <1.100.0 to maintain compatibility with litellm 1.75.x
  • Version 0.4.0:
    • Python 3.10-3.13 Support: Full compatibility across Python versions with 3.12 as the development target
    • Optimized Configuration: Standardized Python version targeting across all development tools (ruff, pyright, pre-commit)
    • Enhanced CI/CD Pipeline: Automated workflow chain: Build → TestPyPI → GitHub Release → PyPI
    • Improved .gitignore: Comprehensive, well-organized patterns for modern Python development with AI tools
    • Enhanced Makefile: Fixed lint target to include tests, corrected package commands, improved dependency management
    • Code Quality Improvements: Fixed all linting and type checking errors, updated deprecated patterns
    • Test Reliability: Updated test mocks and model references for better compatibility
  • Version 0.3.2:
    • Improved test coverage to 93%
    • Fixed nest_asyncio safety handling
  • Version 0.3.1:
    • Update dependencies
  • Version 0.3.0:
    • Added support for Azure OpenAI
  • Version 0.2.0:
    • Support for basic auth in ollama base urls
  • Version 0.1.25:
    • Suppress pricing not found warning
  • Version 0.1.24:
    • Changed default fetch wait from idle to sleep
  • Version 0.1.23:
    • Fix issue caused by providing reasoning effort to models that don't support it
  • Version 0.1.22:
    • Fix some asyncio issues with web fetch utils
  • Version 0.1.21:
    • Added config options for OpenAI reasoning effort, and Anthropic reasoning token budget
    • Fix o3 error when temperature is set
  • Version 0.1.20:
    • Added parallel fetch support for fetch_url related utils
  • Version 0.1.19:
    • Added proxy config, http auth support for fetch_url related utils
  • Version 0.1.18:
    • Updated web scraping utils
  • Version 0.1.17:
    • Use LiteLLM for pricing data
    • BREAKING CHANGE: Provider Google is now Gemini
  • Version 0.1.16:
    • Add more default base urls for providers
  • Version 0.1.15:
    • Added support for Deepseek and LiteLLM
    • Added Mistral pricing
    • Better fuzzy model matching for price lookup
  • Version 0.1.14:
    • Added o3-mini pricing
    • Now gets actual model used from OpenRouter
    • Fixed some other pricing issues
    • Fixed open router default model name
  • Version 0.1.13:
    • Added support for supplying extra body params to OpenAI compatible providers like OpenRouter
    • Better handling of model names for pricing lookup
  • Version 0.1.12:
    • Added support for OpenRouter
  • Version 0.1.11:
    • Updated some utility functions
    • Fixed dotenv loading issue
    • Updated pricing for o1 and Deepseek R1
  • Version 0.1.10:
    • Add format param to LlmConfig for Ollama output format
    • Fixed bug with util function has_stdin_content
  • Version 0.1.9:
    • Added Mistral support
    • Fix dotenv loading bug
  • Version 0.1.8:
    • Added time display utils
    • Made LlmConfig.from_json more robust
  • Version 0.1.7:
    • Fix documentation issues
  • Version 0.1.6:
    • Pricing for Deepseek
    • Updated Docs
  • Version 0.1.5:
    • Initial release

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

Author

Paul Robello - probello@gmail.com

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

par_ai_core-0.5.7.tar.gz (129.4 kB view details)

Uploaded Source

Built Distribution

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

par_ai_core-0.5.7-py3-none-any.whl (168.4 kB view details)

Uploaded Python 3

File details

Details for the file par_ai_core-0.5.7.tar.gz.

File metadata

  • Download URL: par_ai_core-0.5.7.tar.gz
  • Upload date:
  • Size: 129.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for par_ai_core-0.5.7.tar.gz
Algorithm Hash digest
SHA256 119bec3634f2fa104fb612d3db0bc2418ce5419c563f4558617b032395d1b30d
MD5 5b499f18a72da9ec77751b838ed230d9
BLAKE2b-256 3d2252b139a94cafe0bdc0e03ded6ebecd71c4302269e5cd7dbbc65333743cda

See more details on using hashes here.

Provenance

The following attestation bundles were made for par_ai_core-0.5.7.tar.gz:

Publisher: publish.yml on paulrobello/par_ai_core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file par_ai_core-0.5.7-py3-none-any.whl.

File metadata

  • Download URL: par_ai_core-0.5.7-py3-none-any.whl
  • Upload date:
  • Size: 168.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for par_ai_core-0.5.7-py3-none-any.whl
Algorithm Hash digest
SHA256 2d420928238d5aa0951bc04251d4fceed61d8e5cf83ca3d00ea5b8f3535dac52
MD5 0405c4d7584960555d13958c9642b9a1
BLAKE2b-256 cc24e84bb1fe01426cdcd51c83351d944ea82f889b5d8849194bbb78b635b474

See more details on using hashes here.

Provenance

The following attestation bundles were made for par_ai_core-0.5.7-py3-none-any.whl:

Publisher: publish.yml on paulrobello/par_ai_core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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