Skip to main content

A library to transform messages for different LLMs like OpenAI, Mistral, LangChain, Gemini, DBRX, Cohere, Claude, and AWS Bedrock to Pangea messages format

Project description

pangea-llm-translators

Description

llm-message-translator is a Python library designed to convert, transform, and censor messages for various LLM (Large Language Model) providers. It supports multiple AI models and platforms, including:

  • OpenAI
  • Mistral
  • LangChain
  • Gemini (Google AI)
  • DBRX (Databricks)
  • Cohere
  • Claude (Anthropic)
  • AWS Bedrock
  • Pangea

Features

  • Seamlessly convert various LLM message formats to the Pangea Messages format. Preserve message structure while transforming the original input using responses from Pangea AI Guard APIs.

Installation

pip install llm-message-translator

Usage Example

from ai import get_translator

# Define an OpenAI-style LLM input
openapi_message = {
   "model": "gpt-4o",
   "messages": [
      {"role": "developer", "content": "you are a joker"},
      {"role": "user", "content": [{"type": "text", "text": "knock knock"}]},
      {"role": "assistant", "content": [{"type": "text", "text": "Who's there?"}]},
   ],
}

# Initialize the translator
translator = get_translator(openapi_message, llm_hint="openai")

# Convert to Pangea format
pangea_messages = translator.get_pangea_messages()

# Print transformed messages
print(pangea_messages.get_messages_list())
# [{'role': 'system', 'content': 'you are a joker'}, {'role': 'user', 'content': 'knock knock'}, {'role': 'assistant', 'content': "Who's there?"}]

# Mimic some api behavior like modify content (Example: Censor "joker")
for message in pangea_messages.messages:
   message.content = message.content.replace("joker", "*****")

# Convert back to OpenAPI input format
original_output = translator.transformed_original_input(messages=pangea_messages.get_messages_list())

print(original_output)

Prerequisites

Ensure you have the following installed:

Installation

  1. Clone the repository:
    git clone https://github.com/pangeacyber/pangea-llm-translators.git
    cd pangea-llm-translators
    
  2. Install dependencies using Poetry:
    poetry install
    

Running Tests

To run the test suite, use:

poetry run pytest

Running the Application

To run the application, use:

poetry run python examples/openai_translator.py

Virtual Environment

To activate the virtual environment:

poetry shell

License

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

Contributing

Contributions are welcome! Please create an issue or submit a pull request.

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

pangea_llm_translator-1.0.1.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

pangea_llm_translator-1.0.1-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file pangea_llm_translator-1.0.1.tar.gz.

File metadata

  • Download URL: pangea_llm_translator-1.0.1.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.9.21 Linux/6.8.0-1021-azure

File hashes

Hashes for pangea_llm_translator-1.0.1.tar.gz
Algorithm Hash digest
SHA256 591d5ea0347a820d02aacc2a1911a3248a5f565ae15ee34f5f9c1530b25b1cdb
MD5 440ff8a990cf29b65a1d6562978f530e
BLAKE2b-256 2b8d1255d2f72f3eceba851c1cd4fcfc815462eb7eaafb9c1a3922bbf8385e3b

See more details on using hashes here.

File details

Details for the file pangea_llm_translator-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pangea_llm_translator-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b09e8a5867601e4e932525d49c6b2c11c1f1373aea150c2d26185deeef650605
MD5 2790319cdbe00d11b273cf3c00f3f8fa
BLAKE2b-256 282d107405e1bbb18cffd889c6a49621c471c97e4eef72da8386e9a67017e815

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