Skip to main content

A unified interface for interacting with language models from multiple providers

Project description

LingualLens

License: MIT Python 3.8+ PRs Welcome Multi-Provider

A unified interface for interacting with language models from multiple providers.

Features

  • Multi-Provider Support: Interact with models from OpenAI, Anthropic, Google, and HuggingFace through a single consistent API
  • Simple Interface: Generate text, classify sentiment, and extract entities with just a few lines of code
  • Provider-Agnostic: Switch between different providers and models without changing your code
  • Extensible: Easy to add support for additional providers
  • Configurable: Configure models and providers through code or configuration files

Installation

# Basic installation
pip install linguallens

# With support for specific providers
pip install linguallens[openai]      # OpenAI support
pip install linguallens[anthropic]   # Anthropic support
pip install linguallens[google]      # Google support
pip install linguallens[huggingface] # HuggingFace support

# Full installation with all providers
pip install linguallens[all]

Quick Start

from linguallens import LingualLens

# Initialize with API keys for different providers
lens = LingualLens(
    openai_api_key="your-openai-key",
    anthropic_api_key="your-anthropic-key",
    google_api_key="your-google-key",
    default_provider="openai",
    default_model="gpt-3.5-turbo"
)

# Generate text with the default provider and model
response = lens.generate("Explain quantum computing in simple terms")
print(response)

# Use a different provider and model
response = lens.generate(
    "What are the ethical implications of AI?",
    provider="anthropic",
    model="claude-3-opus-20240229"
)
print(response)

# Analyze sentiment
sentiment = lens.classify_sentiment("I love this product! It works great.")
print(sentiment)  # {'score': 1.0, 'label': 'positive', ...}

# Extract entities
entities = lens.extract_entities("Contact us at support@example.com or visit https://example.com")
print(entities)  # [{'type': 'EMAIL', 'value': 'support@example.com', ...}, ...]

Advanced Usage

Configuration Files

LingualLens supports configuration through JSON or YAML files:

lens = LingualLens(config_path="config.yaml")

Example config.yaml:

providers:
  openai:
    api_key: "your-openai-key"
    models:
      gpt-4:
        max_tokens: 8192
        default_params:
          temperature: 0.7
          top_p: 1.0
  anthropic:
    api_key: "your-anthropic-key"
    models:
      claude-3-opus-20240229:
        max_tokens: 4096
        default_params:
          temperature: 0.5

Working with Local Models

For HuggingFace models, you can choose between using the Inference API or local models:

# Use a local model
response = lens.generate(
    "What is machine learning?",
    provider="huggingface",
    model="mistralai/Mistral-7B-Instruct-v0.2",
    use_api=False  # Use local model instead of API
)

Adding a New Provider

LingualLens is designed to be easily extensible. To add a new provider:

  1. Create a new class that inherits from BaseProvider
  2. Implement the required methods: generate, load_model, and available_models
  3. Register the provider with LingualLens
from linguallens import BaseProvider, LingualLens

class MyCustomProvider(BaseProvider):
    def __init__(self, api_key=None):
        super().__init__(api_key)
        # Initialize your provider here
    
    def generate(self, prompt, model, max_tokens=100, temperature=0.7, top_p=1.0, 
                 stop_sequences=None, streaming=False):
        # Implement text generation
        return "Generated text"
    
    def load_model(self, model_name):
        # Load or get a reference to a model
        return model_proxy
    
    def available_models(self):
        # Return a list of available models
        return ["model1", "model2"]

# Register the provider
LingualLens.register_provider("my_provider", MyCustomProvider)

# Use the provider
lens = LingualLens(my_provider_api_key="your-key")
response = lens.generate("Hello", provider="my_provider", model="model1")

License

MIT

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

linguallens-0.1.0.tar.gz (56.8 kB view details)

Uploaded Source

Built Distribution

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

linguallens-0.1.0-py3-none-any.whl (66.4 kB view details)

Uploaded Python 3

File details

Details for the file linguallens-0.1.0.tar.gz.

File metadata

  • Download URL: linguallens-0.1.0.tar.gz
  • Upload date:
  • Size: 56.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for linguallens-0.1.0.tar.gz
Algorithm Hash digest
SHA256 916251d1b2c0d08e4f97e44f0778a79f54977bbb1df4953e3bd7334121e8bb25
MD5 4d6690176771f4a2deee3097f84f41c3
BLAKE2b-256 d44f3e5df075313822a79e8345455b359e63b938d3373124bede5a2811f4ba2d

See more details on using hashes here.

File details

Details for the file linguallens-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: linguallens-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 66.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for linguallens-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0361eae664b27c2e6dfbe8da264bce58ece8cb4294d9d8b6c9ec166123237a86
MD5 66e6356ec0f17f4da4229aa022e3a47b
BLAKE2b-256 322105caeb935d408b76f17bb1fd5ca964b7ff6e94e75bc7ee5a3b5ba5774b43

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