Generate completions

A walkthrough of how to set up the SDK to use humanloop.complete()

The Humanloop Python SDK allows you to easily replace your openai.Completions.create() calls with a humanloop.complete() call that, in addition to calling OpenAI to get a generation, automatically logs the data to your Humanloop project.

Prerequisites

  1. You already have a project created - if not, please pause and first follow our project creation guides.

📘

Using other model providers

This guide assumes you're using an OpenAI model. If you want to use other providers or your own model please also look at our guide to using your own model.

Install and initialize the SDK

📘

The SDK requires Python 3.8 or greater.

First you need to install and initialize the SDK. If you have already done this, skip to the next section. Otherwise, open up your terminal and follow these steps:

  1. Install the Humanloop Python SDK:

    👍

    We recommend pinning your installed Humanloop SDK to a specific version as our API is still currently evolving.

    You will be kept informed of the changes as we make them, and we do support older versions to accommodate a transition period.

    $ pip install humanloop
    
  2. Start a Python interpreter:
    $ python
    
  3. Test your installation by running:
    >>> from humanloop import Humanloop
    

Activate a model

  1. Log in to Humanloop and navigate to the Models tab of your project.
  2. Ensure that there is an active deployment marked in green at the top of the dashboard. If there is no active deployment set, then use the Change button to select one of your existing model configs to use to generate.

Use the SDK to call your model

Now you can use the SDK to generate completions and log the results to your project. The following code demonstrates how:

from humanloop import Humanloop

# You need to initialize the Humanloop SDK with your API Keys
humanloop = Humanloop(api_key="<YOUR Humanloop API KEY>")

# humanloop.complete_deployed(...) will call the active model config on your project.
# The inputs must match the input of the prompt template in your project.
complete_response = humanloop.complete_deployed(
    project="<YOUR UNIQUE PROJECT NAME>", # change the project name to your project
    inputs={"question": "How should I think about competition for my startup?"},
    provider_api_keys={"openai": "<YOUR OpenAI API KEY>"}
) 

# A single call to generate may return multiple outputs.
data_id = complete_response.body["data"][0]["id"]
output = complete_response.body["data"][0]["output"]

# You can also access the raw response from OpenAI.
print(complete_response.body["provider_responses"])

Navigate to your project's Data tab in the browser to see the recorded inputs and outputs of your generation.

🎉 Now that you have generations flowing through your project you can start to log your end user feedback to evaluate and improve your models.