DEV Community

Sergey Inozemtsev
Sergey Inozemtsev

Posted on

LLM API Adapter SDK for Python

Here is my LLM API Adapter SDK for Python that allows you to easily switch between different LLM APIs.

At the moment, it supports: OpenAI, Anthropic, and Google. And only the chat function (for now).

It simplifies integration and debugging as it has standardized error classes across all supported LLMs.

It also manages request parameters like temperature, max tokens, and other settings for better control.

To use the adapter, you need to download the library and obtain API keys for the LLMs you want. In the code, I demonstrated how easy it is to use it.

from llm_api_adapter.messages.chat_message import AIMessage, Prompt, UserMessage
from llm_api_adapter.universal_adapter import UniversalLLMAPIAdapter

messages = [
    Prompt(
        "You are a friendly assistant who explains complex concepts "
        "in simple terms."
    ),
    UserMessage("Hi! Can you explain how artificial intelligence works?"),
    AIMessage(
        "Sure! Artificial intelligence (AI) is a system that can perform "
        "tasks requiring human-like intelligence, such as recognizing images "
        "or understanding language. It learns by analyzing large amounts of "
        "data, finding patterns, and making predictions."
    ),
    UserMessage("How does AI learn?"),
]

gpt = UniversalLLMAPIAdapter(
    organization="openai",
    model="gpt-3.5-turbo",
    api_key=openai_api_key
)
gpt_response = gpt.generate_chat_answer(messages=messages)
print(gpt_response.content)

claude = UniversalLLMAPIAdapter(
    organization="anthropic",
    model="claude-3-haiku-20240307",
    api_key=anthropic_api_key
)
claude_response = claude.generate_chat_answer(messages=messages)
print(claude_response.content)

google = UniversalLLMAPIAdapter(
    organization="google",
    model="gemini-1.5-flash",
    api_key=google_api_key
)
google_response = google.generate_chat_answer(messages=messages)
print(google_response.content)
Enter fullscreen mode Exit fullscreen mode

I have explained everything in more detail in the documentation: https://github.com/Inozem/llm_api_adapter

This is the first stage, and it is just the beginning. I'd love to hear your thoughts, feedback, or ideas on where it could go next.

GenAi #Python #LLM #OpenAI #GPT #Anthropic #Claude #Google #Gemini

Top comments (0)