Skip to main content

A wrapper library to abstract common llm providers

Project description

ext_llm - a wrapper library for common LLMs (WIP)

Installation

pip install ext_llm

Usage

from concurrent.futures import Future
import ext_llm
from ext_llm.llm.response import Response
from ext_llm.llm.stream import Stream

#read config yaml file
config : str = open("config.yaml").read()

#initialize extllm library
extllm = ext_llm.init(config)

#request a client via user defined presets. In this instance it's "groq-llama"
llm_client = extllm.get_client("groq-llama")

#you can request e concurrent client, based on the same preset. This will allow for non blocking concurrent requests.
llm_concurrent_client = extllm.get_concurrent_client("groq-llama")

#Non blocking call. This will return a future object that will be resolved when the request is completed.
future1: Future [Response | Stream] = llm_concurrent_client.generate_text("You are an helpful assistant", "Recite the first article of the Italian Constitution")

#Blocking call. This will return the result of the request. The result can be either a Response or a Stream object.
#Response or Stream are defined by the user in the config file.
#If the "invocation_method" is "converse" then the result is a Response.
#If the "invocation_method" is "converse_stream" then the result is a Stream.
#A stream has to be handled differently from a response.
result: Response | Stream = llm_client.generate_text("You are an helpful assistant", "Recite the first amendment of the American constitution")


print(result.metadata)
print(result)

print(future1.result().metadata)
print(future1.result())

Config File

Here's an example of a config file:

config:
  logging: true #false
  db_path: logs.db
  presets:
      #User can define multiple presets
      aws-claude-sonnet:
        #available providers: aws, groq
        provider: aws
        #secrets are passed to the library by specifying environment variables names
        aws_access_key_id_variable_name: AWS_ACCESS_KEY_ID
        aws_secret_access_key_variable_name: AWS_SECRET_ACCESS_KEY
        aws_region: eu-west-2
        model_id: anthropic.claude-3-sonnet-20240229-v1:0
        invocation_method: converse_stream
        temperature: 0.5
        max_tokens: 1000

      groq-llama:
        provider: groq
        model_id: deepseek-r1-distill-llama-70b
        groq_api_key_variable_name: GROQ_API_KEY
        invocation_method: converse
        temperature: 0.5
        max_tokens: 500

      groq-llama-streaming:
        provider: groq
        model_id: deepseek-r1-distill-llama-70b
        groq_api_key_variable_name: GROQ_API_KEY
        invocation_method: converse_stream
        temperature: 0.5
        max_tokens: 500

      custom-provider:
        #the library can instantiate custom classes that inherit from the Llm class
        #this can be done by specifying the class name and the module name
        class_name: MyLlm
        module_name: custom_provider_llm
        #these parameters are passed to the custom class, so the user can define custom parameters
        custom_param1: 123
        custom_param2: 456

Defining a custom LLM Client

To define a custom provider that can be used by the ext_llm library, you need to create a class that inherits from the LlmClient class and implements the required methods. Additionally, if your custom provider supports streaming, you should also define a stream class that inherits from the Stream class.

Step-by-Step Guide

  1. Create the Custom LLM Client Class:

    • Inherit from the LlmClient class.
    • Implement the generate_text method.
    • Implement the get_config method.
  2. Create the Custom Stream Class (if needed):

    • Inherit from the Stream class.
    • Implement the __iter__ method to handle streaming responses.
  3. Update the Configuration File:

    • Specify the custom class name and module name in the configuration file.
    • Add any custom parameters required by your custom class.

Example

Custom LLM Client Class

# File: custom_provider_llm.py
from ext_llm import LlmClient

class MyLlmClient(LlmClient):
    def __init__(self, config: dict, preset_name: str):
        super().__init__()
        self.preset_name = preset_name
        self.config = config
        self.custom_param1 = config['custom_param1']
        self.custom_param2 = config['custom_param2']
        

    def generate_text(self, system_prompt: str, prompt: str, max_tokens=None, temperature=None):
        return "Hello, world!"
    
    def get_config(self):
        return self.config

Custom Stream Class (if needed)

# File: custom_provider_llm.py
from ext_llm.llm.stream import Stream
from ext_llm.llm.response import Response

class MyStream(Stream):
    def __init__(self, stream):
        super().__init__()
        self.stream = stream

    def __iter__(self):
        for chunk in self.stream:
            yield Response(chunk, {})

Configuration File

config:
  logging: true
  db_path: logs.db
  presets:
      custom-provider:
        class_name: MyLlmClient
        module_name: custom_provider_llm
        custom_param1: 123
        custom_param2: 456

By following these steps, you can define a custom provider that integrates seamlessly with the ext_llm library.

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

ext_llm-0.1.15.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

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

ext_llm-0.1.15-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file ext_llm-0.1.15.tar.gz.

File metadata

  • Download URL: ext_llm-0.1.15.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for ext_llm-0.1.15.tar.gz
Algorithm Hash digest
SHA256 1046c6961a354e92b66aa77afbf8f741b9e7b6ff4630e40b1d89d9d7e26b6821
MD5 1cd7025f4e161dcae96ea71ad65f6131
BLAKE2b-256 2242ed3130d150e2660ce9fe053dfd936e813cb6fa0d5c0928d3f68574fdcf7b

See more details on using hashes here.

File details

Details for the file ext_llm-0.1.15-py3-none-any.whl.

File metadata

  • Download URL: ext_llm-0.1.15-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for ext_llm-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 68ee38ea298d643ad0af90dc39ffa4ddf3b0cfc6a7d11284b7b459351391e831
MD5 6de924783ce4b9d6f3920506d5848d6e
BLAKE2b-256 fb9168c393f3912781285f74f341d40db2fab4e633a324d82fc3510de1ec8654

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