Skip to main content

LLM provider abstraction layer.

Project description

oneping

Give me a ping, Vasily. One ping only, please.

One ping only, please.

This is a library for querying LLM providers such as OpenAI or Anthropic, as well as local models. Currently the following providers are supported: openai, anthropic, fireworks, and local (local models).

Requesting a local provider will target localhost and use an OpenAI-compatible API as in llama.cpp or llama-cpp-python. Also included is a simple function to start a llama-cpp-python server on the fly (see below).

The various native libraries are soft dependencies and the library can still partially function with or without any or all of them. The native packages for these providers are: openai, anthropic, and fireworks-ai.

There is also a Chat interface that automatically tracks message history. Kind of departing from the "one ping" notion, but oh well. This accepts a provider and system argument. Other parameters are passed by calling it (an alias for chat) or to stream.

Installation

For standard usage, install with:

pip install oneping

To include the native provider dependencies, install with:

pip install oneping[native]

To include the chat and web interface dependencies, install with:

pip install oneping[chat]

Library Usage

Basic usage with Anthropic through the URL interface:

response = oneping.reply(prompt, provider='anthropic')

The reply function accepts a number of arguments including (some of these have per-provider defaults):

  • prompt (required): The prompt to send to the LLM (required)
  • provider = local: The provider to use: openai, anthropic, fireworks, or local
  • system = None: The system prompt to use (not required, but recommended)
  • prefill = None: Start "assistant" response with a string (Anthropic doesn't like newlines in this)
  • model = None: Indicate the desired model for the provider (provider default)
  • max_tokens = 1024: The maximum number of tokens to return
  • history = None: List of prior messages or True to request full history as return value
  • native = False: Use the native provider libraries
  • url = None: Override the default URL for the provider (provider default)
  • port = 8000: Which port to use for local or custom provider
  • api_key = None: The API key to use for non-local providers

For example, to use the OpenAI API with a custom system prompt:

response = oneping.reply(prompt, provider='openai', system=system)

To conduct a full conversation with a local LLM:

history = True
history = oneping.reply(prompt1, provider='local', history=history)
history = oneping.reply(prompt2, provider='local', history=history)

For streaming, use the function stream and for async streaming, use stream_async. Both of these take the same arguments as reply.

Command Line

You can call the oneping module directly and use the following subcommands:

  • reply: get a single response from the LLM
  • stream: stream a response from the LLM
  • embed: get embeddings from the LLM
  • console: start a console (Textual) chat
  • web: start a web (FastHTML) chat

These accept the arguments listed above for reply as command line arguments. For example:

python -m oneping stream "Does Jupiter have a solid core?" --provider anthropic

Or you can pipe in your query from stdin:

echo "Does Jupiter have a solid core?" | python -m oneping stream --provider anthropic

Chat Interface

The Chat interface is a simple wrapper for a conversation history. It can be used to chat with an LLM provider or to simply maintain a conversation history for your bot. If takes the usual reply, stream, and stream_async functions, and calling it directly will map to reply.

chat = oneping.Chat(provider='anthropic', system=system)
response1 = chat(prompt1)
response2 = chat(prompt2)

There is also a textual powered console interface and a fasthtml powered web interface. You can call these with: python -m oneping console or python -m oneping web.

Textual Chat FastHTML Chat

Server

The server module includes a simple function to start a llama-cpp-python server on the fly (oneping.server.start or python -m oneping.server start).

python -m oneping.server start <path-to-gguf>

To run the server in embedding mode, either pass the --embedding flag.

Embeddings

Embeddings queries are supported through the embed function. It accepts the relevant arguments from the reply function. Right now only openai and local providers are supported.

vecs = oneping.embed(text, provider='openai')

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

oneping-0.5.2.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

oneping-0.5.2-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file oneping-0.5.2.tar.gz.

File metadata

  • Download URL: oneping-0.5.2.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for oneping-0.5.2.tar.gz
Algorithm Hash digest
SHA256 7c63622a2c522a064527dce7c538796a8187855a0210a1855c155779a7bccf5b
MD5 16f5cba23f797d473d746906b564b324
BLAKE2b-256 627ad86e5f8b85b1c0a5dfd8a6a6cc255cd5188e3956adbe9bf9bb9143cf2203

See more details on using hashes here.

File details

Details for the file oneping-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: oneping-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for oneping-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 22cfc62e3bb8c79c4d7acafb429d4d483e637a3285f64876d9ce5cf4947d0c1a
MD5 d7ea161b3cf2c94ce4d814834082ec68
BLAKE2b-256 5f32340a8f3fef248351e7517d536d467a84eeece654a367af66511f32d1933c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page