Skip to main content

LLMFactory is a modular framework built on LangChain, offering a factory-based approach for seamless integration and management of Large Language Models (LLMs) from multiple providers, ensuring flexibility, scalability, and maintainability.

Project description

LLMFactory

LLMFactory is a modular and extensible framework built on top of the LangChain library, designed for streamlined integration and management of Large Language Models (LLMs). By leveraging LangChain's powerful abstractions and tools, LLMFactory provides a factory-based approach to initialize and manage LLMs from multiple providers, offering flexibility, maintainability, and scalability.

Key Features

1. LangChain Foundation

  • LLMFactory utilizes LangChain as its core, ensuring compatibility with its powerful tools, chains, and agents.
  • It is well-suited for applications requiring complex workflows and advanced LLM capabilities.

2. Abstract Factory Pattern

  • The framework employs the abstract factory design pattern through the AbstractLLMFactory base class.
  • This ensures a standardized interface for initializing and managing diverse LLMs.

3. Support for Multiple LLM Providers

  • Pre-configured factories for leading LLM providers, including:
    • OpenAI (ChatOpenAI): Supporting models like GPT-3.5 and GPT-4.
    • DeepSeek: Extended functionality for OpenAI APIs.
    • Fireworks (ChatFireworks): Tailored models for specific workflows with additional parameters like top_p and retries.
    • Ollama (OllamaLLM): Lightweight models with customizable context size.
    • Anthropic (ChatAnthropic): Human-aligned conversational models (e.g., Claude).

5. Model Configuration

  • The ModelConfig dataclass encapsulates essential initialization parameters, such as:
    • Model name
    • Temperature for response diversity
    • API keys and base URLs
    • Advanced parameters like top_p, max_tokens, timeouts, and retries

Installation

You can install just the base llmfactory package, or install a provider's package along with llmfactory.

This installs just the base package without installing any provider's SDK.

pip install ecq-llmfactory

Set up

To get started, you will need API Keys for the providers you intend to use. You'll need to install the provider-specific library either separately or when installing aisuite.

The API Keys can be set as environment variables, or can be passed as config to the aisuite Client constructor. You can use tools like python-dotenv or direnv to set the environment variables manually.

Set the API keys.

export DEEPSEEK_API_KEY="your-deepseek-api-key"
export ANTHROPIC_API_KEY="your-anthropic-api-key"

Use the python client: https://python.langchain.com/v0.2/docs/tutorials/

import os
import dotenv

from llm_factory import LLMFactoryProvider, ModelConfig
from langchain.prompts import ChatPromptTemplate

dotenv.load_dotenv()

ANTHROPIC_KEY = os.environ['ANTHROPIC_API_KEY']
model_config = ModelConfig(
    provider_name='Anthropic',
    model_name="claude-3-opus-20240229",
    api_key=ANTHROPIC_KEY,
    temperature=0.0,
)

system_template = "Translate the following into {language}:"
prompt_template = ChatPromptTemplate.from_messages(
    [("system", system_template), ("user", "{text}")]
)

factory = LLMFactoryProvider.get_factory(model_config)
model = factory.get_model()

chain = prompt_template | model
result = chain.invoke({"language": "italian", "text": "hi"})
# result = 'ciao'

List supported providers

# list supported providers
print(LLMFactoryProvider.get_supported_providers())

For a list of provider values, you can look at the directory - /providers/. The list of supported providers are of the format - <provider>_provider.py in that directory. We welcome providers adding support to this library by adding an implementation file in this directory. Please see section below for how to contribute.

Adding support for a provider

We have made easy for a provider or volunteer to add support for a new platform.

Naming Convention for Provider Modules

We follow a convention-based approach for loading providers, which relies on strict naming conventions for both the module name and the class name.

  • The provider's module file must be named in the format <provider>_provider.py.
  • The class inside this module must follow the format: the provider name with the first letter capitalized, followed by the suffix Provider.

Examples

  • Hugging Face: The provider class should be defined as:

    class HuggingfaceProvider(AbstractLLMFactory)
    

    in providers/huggingface_provider.py.

  • OpenAI: The provider class should be defined as:

    class OpenaiProvider(AbstractLLMFactory)
    

    in providers/openai_provider.py

This convention simplifies the addition of new providers and ensures consistency across provider implementations.

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

ecq_llmfactory-0.1.8.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

ecq_llmfactory-0.1.8-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file ecq_llmfactory-0.1.8.tar.gz.

File metadata

  • Download URL: ecq_llmfactory-0.1.8.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.19

File hashes

Hashes for ecq_llmfactory-0.1.8.tar.gz
Algorithm Hash digest
SHA256 9d2850795571b73558bc76dd07677d695d3567ca41a42cb6ff3461bdfd6eddd3
MD5 f123a1472035aecd27c8411852a8e5e6
BLAKE2b-256 ceb9524cc4797f7dc81d9be7cd99f5654700915745fecc711b681ceb8d6685c3

See more details on using hashes here.

File details

Details for the file ecq_llmfactory-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: ecq_llmfactory-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.19

File hashes

Hashes for ecq_llmfactory-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6b8692a7c5fb345e8e9e88237a94edd99b11f45e1b6fabb5a1bb31fb3ceb18f9
MD5 62a9aebe0c67f3e12ad9cd5a1de0f637
BLAKE2b-256 e4efd50c6280d6e2e27dbc152b7057e8e5b220e72a3dad14ca250886dd201565

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