PromptLayer OpenAI#

This example showcases how to connect to PromptLayer to start recording your OpenAI requests.

Install PromptLayer#

The promptlayer package is required to use PromptLayer with OpenAI. Install promptlayer using pip.

pip install promptlayer

Imports#

import os
from langchain.llms import PromptLayerOpenAI
import promptlayer

Set the Environment API Key#

You can create a PromptLayer API Key at www.promptlayer.com by clicking the settings cog in the navbar.

Set it as an environment variable called PROMPTLAYER_API_KEY.

os.environ["PROMPTLAYER_API_KEY"] = "********"

Use the PromptLayerOpenAI LLM like normal#

You can optionally pass in pl_tags to track your requests with PromptLayer’s tagging feature.

llm = PromptLayerOpenAI(pl_tags=["langchain"])
llm("I am a cat and I want")
' to go outside\n\nUnfortunately, cats cannot go outside without being supervised by a human. Going outside can be dangerous for cats, as they may come into contact with cars, other animals, or other dangers. If you want to go outside, ask your human to take you on a supervised walk or to a safe, enclosed outdoor space.'

The above request should now appear on your PromptLayer dashboard.

Using PromptLayer Track#

If you would like to use any of the PromptLayer tracking features, you need to pass the argument return_pl_id when instantializing the PromptLayer LLM to get the request id.

llm = PromptLayerOpenAI(return_pl_id=True)
llm_results = llm.generate(["Tell me a joke"])

for res in llm_results.generations:
    pl_request_id = res[0].generation_info["pl_request_id"]
    promptlayer.track.score(request_id=pl_request_id, score=100)

Using this allows you to track the performance of your model in the PromptLayer dashboard. If you are using a prompt template, you can attach a template to a request as well. Overall, this gives you the opportunity to track the performance of different templates and models in the PromptLayer dashboard.