Skip to main content

useLLM - Use Large Language Models in Python App

The usellm Python library enables interaction with a chat-based Large Language Model (LLM) service. This interaction can be used to perform various language-related tasks, like generating chat conversation using the OpenAI API. It's designed as a Python port of the usellm JavaScript library.

Installation

The library can be installed with pip:

pip install usellm

Example Usage

Here is a basic usage example:

from usellm import Message, Options, UseLLM

# Initialize the service
service = UseLLM(service_url="https://usellm.org/api/llm")

# Prepare the conversation
messages = [
  Message(role="system", content="You are a helpful assistant."),
  Message(role="user", content="What can you do for me?"),
]
options = Options(messages=messages)

# Interact with the service
response = service.chat(options)

# Print the assistant's response
print(response.content)

The above code will generate a response using the OpenAI ChatGPT API. The service URL "https://usellm.org/api/llm" should be used only for testing.

Classes and Methods

1. UseLLM class

The UseLLM class provides the interface for interacting with the LLM service.

Methods:

  • __init__(self, service_url: str): Initializes a new instance of the UseLLM class.
  • chat(self, options: Options) -> Message: Interacts with the LLM using the provided Options, and returns a Message instance that represents the LLM's response.

2. Options class

The Options class represents a set of configuration options for a chat interaction with the LLM.

  • messages: A list of Message instances representing the conversation up to the current point.
  • stream: A boolean indicating if the interaction is a streaming interaction. Note: streaming is currently not supported.
  • template: A string representing a message template to guide the conversation.
  • inputs: A dictionary of additional inputs for the conversation.

Methods:

  • __init__(self, messages: Optional[List[Message]] = [], stream: Optional[bool] = None, template: Optional[str] = None, inputs: Optional[dict] = None): Initializes a new instance of the Options class.

3. Message class

The Message class represents a message in a conversation. It consists of two main attributes:

  • role: The role of the message sender. Common values could be system, user, assistant.
  • content: The content of the message.

Methods:

  • __init__(self, role: str, content: str): Initializes a new instance of the Message class.
  • __repr__(self) -> str: Returns a string representation of the Message instance.
  • __str__(self) -> str: Returns a string representation of the Message instance.
  • to_dict(self) -> dict: Returns a dictionary representation of the Message instance.
  • to_json(self) -> str: Returns a JSON string representation of the Message instance.

Exceptions

The library raises an Exception in the following situations:

  • If the stream option is set to True, because streaming is not currently supported.
  • If the HTTP response status code from the LLM service is not 200.
  • If the HTTP response from the LLM service contains an "error" field.
  • If the HTTP response from the LLM service does not contain a "choices" field.

Please create an issue to report bugs or suggest improvements. Learn more about the original JavaScript library here: https://usellm.org

Metadata

Release files for usellm 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for usellm 0.0.7
File Size Uploaded Provenance Actions
usellm-0.0.7.tar.gz 5.1 kB Without provenance Details for usellm-0.0.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for usellm 0.0.7
File Interpreter ABI Platform Actions
usellm-0.0.7-py3-none-any.whl Python 3 none any Details for usellm-0.0.7-py3-none-any.whl

Total release size: 10.6 kB

Release files / usellm-0.0.7.tar.gz

Download URL usellm-0.0.7.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
008e2f02ae03aa37e149e3d3ab92ec8fd72a335a6aedb7c92d678fc9a7a16cf4
BLAKE2b-256 checksum
How to use checksums
fd6bc214151216d75206cf89c42464e249bcf38a86c04289a9ac2c36dc07c47d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / usellm-0.0.7-py3-none-any.whl

Download URL usellm-0.0.7-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c82c15228bace2f6baacc547aecb74a8b4f60ba18d5975cc667126e7511f9b8
BLAKE2b-256 checksum
How to use checksums
5f2d3ff77e73796c80ff199ec37bf4acdb1b9334f6364d9b4597c15abf3c08c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for usellm 0.0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page