Skip to main content

LLM client library OpenAI should have made

Project description

keeptalking

A simple, pythonic interface to any OpenAI-compatible LLM server. You will never type response.choices[0].message.content ever again.

Installation

pip install keeptalking

Usage

The entire library is 3 functions:

from keeptalking import talk, write, vibe

Conversation

talk(model='google/gemini-2.5-flash', 
     roles=['system', 'user'], 
     messages=['Solve a math problem', 'Sum up all possible bases in which 97 is divisible by 17'],
     structure=int,
     tokens=10)

will use grammar constrained decoding and return a single integer with the answer. The return value of talk will always be of type structure, which defaults to str is omitted. If roles are omitted, the first message is considered a system message, the rest are user messages. If model is omitted, the gemini-2.5-flash is used (default model can be overriden by setting the MODEL environment variable). If tokens is omitted, generation is limited to 2048 new tokens (default token limit can be overridden by setting the TOKENS environment variable).

The only parameter that should not be omitted is messages:

talk(['Solve a math problem. Provide your reasoning', 'Sum up all possible bases in which 97 is divisible by 17'])

write is an asynchronous version of talk that lets you beautifully parallelize batch requests:

asyncio.gather(
    write(model='google/gemini-2.5-flash', 
          roles=['system', 'user'], 
          messages=[sys, berry],
          structure=int,
          tokens=10)
    for berry in ['Strawberry', 'Blackberry', 'Raspberry', 'Blueberry', 'Canterbury']
)

write automatically self-throttles as necessary so it's safe to call thousands of write()s in parallel with no external rate limiting.

Vibe functions

Vibe functions are functions defined in natural language.

@vibe(model='google/gemini-2.5-flash', tokens=10)
def count_rs(text) -> int:
    """Count how many Rs are in the text"""
    return text

ELL users will notice that this format is shamelessly stolen inspired by ELL. However, keeptalking is much simpler than ELL. Despite being much simpler, keeptalking supports additional features like async vibe functions

@vibe()
async def homework_assistant(topic, pages=5):
    """Help the student with their homework"""
    return f"Write a {pages}-page essay on {topic}"

fully parallelizable like so

asyncio.gather(
    homework_assistant('Math'),
    homework_assistant('History'),
    homework_assistant('English')
)

and enabling structured outputs with a single type hint:

@vibe()
def count_rs(request) -> int:
    """Count how many Rs are in the request"""
    return request

Backend configuration

The model server to be used is defined via environment variables. It can be defined directly by setting BASE_URL and API_KEY. If those are not set, keeptalking will default to OpenRouter if OPENROUTER_API_KEY is set, then OpenAI if OPENAI_API_KEY is set. Perv advanced users can monkey patch keeptalking.client_sync and keeptalking.client_async instead.

Example

You will find a detailed example in example.py. It takes the openrouter model catalog and reads text description of each model to filter out specialized models like coding or edit models, then runs a small test on each to check if they are working.

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

keeptalking-0.2.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

keeptalking-0.2.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file keeptalking-0.2.0.tar.gz.

File metadata

  • Download URL: keeptalking-0.2.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Darwin/24.5.0

File hashes

Hashes for keeptalking-0.2.0.tar.gz
Algorithm Hash digest
SHA256 22a4c50748625cd650a843c60f10acf4065773ccaff40eb6a2c988029f9f38a0
MD5 69e017515af582535e9411832d0ceb21
BLAKE2b-256 52891bb2be2a19a6dcfd4ef6fdd9e1f55b0f75291021bdce7a6b6e3c4a797bd7

See more details on using hashes here.

File details

Details for the file keeptalking-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: keeptalking-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.11 Darwin/24.5.0

File hashes

Hashes for keeptalking-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4cb1612eee8dae9c5b770f73934079c4495adae1bbc3d506bb2beb051be5d9c8
MD5 eae93e11b56184b172bb82083f2cc746
BLAKE2b-256 379ea7b219e4ed91a0c18b1e17f0645b9232b87cea08588c9a3d46c2787a89b1

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