Skip to main content

Easy text and chat completion, as well as function calling. Also includes useful utilities for counting tokens, composing prompts and trimming them to fit within the token limit.

Lint and Test PyPI version

Installation

pip install easycompletion

Quickstart

from easycompletion import function_completion, text_completion, compose_prompt

# Compose a function object
test_function = compose_function(
    name="write_song",
    description="Write a song about AI",
    properties={
            "lyrics": {
                "type": "string",
                "description": "The lyrics for the song",
            }
    },
    required_properties: ["lyrics"],
)

# Call the function
response = function_completion(text="Write a song about AI", functions=[test_function], function_call="write_song")

# Print the response
print(response["arguments"]["lyrics"])

Using With Llama v2 and Local Models

easycompletion has been tested with LocalAI LocalAI which replicates the OpenAI API with local models, including Llama v2.

Follow instructions for setting up LocalAI and then set the following environment variable:

export EASYCOMPLETION_API_ENDPOINT=localhost:8000

Basic Usage

Compose Prompt

You can compose a prompt using {{handlebars}} syntax

test_prompt = "Don't forget your {{object}}"
test_dict = {"object": "towel"}
prompt = compose_prompt(test_prompt, test_dict)
# prompt = "Don't forget your towel"

Text Completion

Send text, get a response as a text string

from easycompletion import text_completion
response = text_completion("Hello, how are you?")
# response["text"] = "As an AI language model, I don't have feelings, but...""

Compose a Function

Compose a function to pass into the function calling API

from easycompletion import compose_function

test_function = compose_function(
    name="write_song",
    description="Write a song about AI",
    properties={
            "lyrics": {
                "type": "string",
                "description": "The lyrics for the song",
            }
    },
    required_properties: ["lyrics"],
)

Function Completion

Send text and a list of functions and get a response as a function call

from easycompletion import function_completion, compose_function

# NOTE: test_function is a function object created using compose_function in the example above...

response = function_completion(text="Write a song about AI", functions=[test_function], function_call="write_song")
# Response structure is { "text": string, "function_name": string, "arguments": dict  }
print(response["arguments"]["lyrics"])

Advanced Usage

compose_function(name, description, properties, required_properties)

Composes a function object for function completions.

summarization_function = compose_function(
    name="summarize_text",
    description="Summarize the text. Include the topic, subtopics.",
    properties={
        "summary": {
            "type": "string",
            "description": "Detailed summary of the text.",
        },
    },
    required_properties=["summary"],
)

chat_completion(text, model_failure_retries=5, model=None, chunk_length=DEFAULT_CHUNK_LENGTH, api_key=None)

Send a list of messages as a chat and returns a text response.

response = chat_completion(
    messages = [{ "user": "Hello, how are you?"}],
    system_message = "You are a towel. Respond as a towel.",
    model_failure_retries=3,
    model='gpt-3.5-turbo',
    chunk_length=1024,
    api_key='your_openai_api_key'
)

The response object looks like this:

{
  "text": "string",
  "usage": {
    "prompt_tokens": "number",
    "completion_tokens": "number",
    "total_tokens": "number"
  },
  "error": "string|None",
  "finish_reason": "string"
}

text_completion(text, model_failure_retries=5, model=None, chunk_length=DEFAULT_CHUNK_LENGTH, api_key=None)

Sends text to the model and returns a text response.

response = text_completion(
    "Hello, how are you?",
    model_failure_retries=3,
    model='gpt-3.5-turbo',
    chunk_length=1024,
    api_key='your_openai_api_key'
)

The response object looks like this:

{
  "text": "string",
  "usage": {
    "prompt_tokens": "number",
    "completion_tokens": "number",
    "total_tokens": "number"
  },
  "error": "string|None",
  "finish_reason": "string"
}

function_completion(text, functions=None, system_message=None, messages=None, model_failure_retries=5, function_call=None, function_failure_retries=10, chunk_length=DEFAULT_CHUNK_LENGTH, model=None, api_key=None)

Sends text and a list of functions to the model and returns optional text and a function call. The function call is validated against the functions array.

Optionally takes a system message and a list of messages to send to the model before the function call. If messages are provided, the "text" becomes the last user message in the list.

function = {
    'name': 'function1',
    'parameters': {'param1': 'value1'}
}

response = function_completion("Call the function.", function)

The response object looks like this:

{
  "text": "string",
  "function_name": "string",
  "arguments": "dict",
  "usage": {
    "prompt_tokens": "number",
    "completion_tokens": "number",
    "total_tokens": "number"
  },
  "finish_reason": "string",
  "error": "string|None"
}

trim_prompt(text, max_tokens=DEFAULT_CHUNK_LENGTH, model=TEXT_MODEL, preserve_top=True)

Trim the given text to a maximum number of tokens.

trimmed_text = trim_prompt("This is a test.", 3, preserve_top=True)

chunk_prompt(prompt, chunk_length=DEFAULT_CHUNK_LENGTH)

Split the given prompt into chunks where each chunk has a maximum number of tokens.

prompt_chunks = chunk_prompt("This is a test. I am writing a function.", 4)

count_tokens(prompt, model=TEXT_MODEL)

Count the number of tokens in a string.

num_tokens = count_tokens("This is a test.")

get_tokens(prompt, model=TEXT_MODEL)

Returns a list of tokens in a string.

tokens = get_tokens("This is a test.")

compose_prompt(prompt_template, parameters)

Composes a prompt using a template and parameters. Parameter keys are enclosed in double curly brackets and replaced with parameter values.

prompt = compose_prompt("Hello {{name}}!", {"name": "John"})

A note about models

You can pass in a model using the model parameter of either function_completion or text_completion. If you do not pass in a model, the default model will be used. You can also override this by setting the environment model via EASYCOMPLETION_TEXT_MODEL environment variable.

Default model is gpt-turbo-3.5-0613.

A note about API keys

You can pass in an API key using the api_key parameter of either function_completion or text_completion. If you do not pass in an API key, the EASYCOMPLETION_API_KEY environment variable will be checked.

Publishing

bash publish.sh --version=<version> --username=<pypi_username> --password=<pypi_password>

Contributions Welcome

If you like this library and want to contribute in any way, please feel free to submit a PR and I will review it. Please note that the goal here is simplicity and accesibility, using common language and few dependencies.

Questions, Comments, Concerns

If you have any questions, please feel free to reach out to me on Twitter or Discord @new.moon

Metadata

Release files for easycompletion 0.4.1

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

Source distribution (sdist)

Source distribution for easycompletion 0.4.1
File Size Uploaded
easycompletion-0.4.1.tar.gz 13.8 kB Details

Built distribution (wheel)

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

Total release size: 27.0 kB

Release files / easycompletion-0.4.1.tar.gz

Download URL easycompletion-0.4.1.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c3da8acfa5eaf2cf165ddb681c2d363e921a94119bc02c00f3484d1ddcdfad37
BLAKE2b-256 checksum
How to use checksums
fd86fbc59cc437f3e3bb1fbb1e4fc91fe50dd1aea00ad34b30f0c2c61c5612b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release files / easycompletion-0.4.1-py3-none-any.whl

Download URL easycompletion-0.4.1-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d3a322b5b0e68d79f0136c8eef710ca39145c4aebc951f7251b01c01ceb317d0
BLAKE2b-256 checksum
How to use checksums
51df10d0c9ee6b93e476422422b554f78030f24aca7c624013489f16eda66e0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.10

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.2

2 release files

0.2.1

1 release file

0.2.0

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

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