Skip to main content

A simple library for making requests to LLMs

Project description

LLMLib

LLMLib is a simple Python library for making requests to Language Learning Models (LLMs) such as OpenAI's GPT and Anthropic's Claude.

Installation

You can install LLMLib using pip:

pip install llmlib-amq

Usage

Here's a quick example of how to use LLMLib:

from PIL import Image
from pathlib import Path
from llmlib import LLMClient, Provider, AnthropicModel, OpenAIModel, Role, \
    LLMResponse, TextMessage, ImageMessage, print_stream, encode_image_webp
import os


client = LLMClient(
    provider=Provider.ANTHROPIC,
    model=AnthropicModel.CLAUDE_3_5_SONNET,
    # provider=Provider.OPENAI,
    # model=OpenAIModel.GPT_4O,
    openai_key=os.environ["OPENAI_API_KEY"],
    anthropic_key=os.environ["ANTHROPIC_API_KEY"],
)

#############################################
### Non-streaming example                 ###
#############################################
output: LLMResponse = client.chat(
    messages=[
        TextMessage(content="You are very concise, answering a question in one sentence.", role=Role.SYSTEM),
        TextMessage(content="What is the capital of South Africa?", role=Role.USER)
    ]
)
print(f"[{client.model_id}]")
print(output.content)

#############################################
### Non-streaming image example           ###
#############################################
example_image = Path(__file__).parent.parent / "tests/bird.webp"
img_base64 = encode_image_webp(Image.open(example_image))
output: LLMResponse = client.chat(
    messages=[
        TextMessage(content="You are very concise image analyst", role=Role.SYSTEM),
        TextMessage(content="Please describe this image", role=Role.USER),
        ImageMessage(content=img_base64, role=Role.USER)
    ]
)
print(f"[{client.model_id}]")
print(output.content)
print(output.usage)

#############################################
### Streaming example                     ###
#############################################
for chunk in client.chat_stream(
        messages=[TextMessage(content="How can I solve tic-tac-toe in Python?", role=Role.USER)]
):
    print(chunk, end="", flush=True)

#############################################
### Streaming and collecting output       ###
#############################################
stream = client.chat_stream(
    messages=[
        TextMessage(
            content="What is the bash command to rollback a git commit? "
            "Please be extremely succinct, output only the bash "
            "command. Do not include any commentary. The output "
            "will be executed directly",
            role=Role.USER,
        )
    ]
)
result = print_stream(stream)
print()
print(result)

License

This project is licensed under the MIT License.

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

llmlib_amq-0.0.8.tar.gz (210.1 kB view details)

Uploaded Source

Built Distribution

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

llmlib_amq-0.0.8-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file llmlib_amq-0.0.8.tar.gz.

File metadata

  • Download URL: llmlib_amq-0.0.8.tar.gz
  • Upload date:
  • Size: 210.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for llmlib_amq-0.0.8.tar.gz
Algorithm Hash digest
SHA256 17b94aeb62b6af3f0c70d28234bc0fb179a52ffa12e8476f21b22cb01a64c272
MD5 7e8b5fe884efd32f884aa6c2823335ef
BLAKE2b-256 033277c2ec19eb2ec0467c11b5c36339cba362cd8f7f9ec5cb60dfe7e485ebb6

See more details on using hashes here.

File details

Details for the file llmlib_amq-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: llmlib_amq-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for llmlib_amq-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 ecfa3c9a09f3e33a6a1da84aea5a56f9c8bee93392cb970b1b89d3dba4e8004a
MD5 4e3e5824f4508b60f2605522b9de2e12
BLAKE2b-256 d4ec3b90b4839ebc63db391963f03e1a8fcf338077c4f5db7a222635d61ed230

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