Setup Prompt Evaluation API with OpenAI output
Use LLUMO AI’s proprietary technology to evaluate output from OpenAI GPT models.
##Setting up the environment ###Importing libraries:
- os: This module provides a way to use operating system dependent functionality, like reading environment variables.
- OpenAI: This is the official OpenAI Python client, used to interact with OpenAI’s API.
- requests: A popular library for making HTTP requests in Python.
- json: Used for parsing JSON data, which is common in API responses.
- logging: Provides a flexible framework for generating log messages in Python.
- getpass: Allows secure password prompts where the input is not displayed on the screen.
###Setting up logging:
- We use
logging.basicConfig()
to configure the logging system. Thelevel=logging.INFO
argument sets the threshold for logging messages to INFO level and above. - We create a logger object named logger that we’ll use throughout our script to log important information and errors.
This setup ensures we have all necessary tools to interact with APIs, handle data, and track any issues that might occur during execution.
##Secure API key handling ###Secure API key input:
- We use
getpass()
to prompt the user for their API keys. This function hides the input, making it more secure than using regular input(). - This approach is safer than hardcoding API keys in your script, which could accidentally be shared or exposed.
###Setting environment variables:
- We use
os.environ
to set environment variables for both API keys. - Environment variables are a secure way to store sensitive information, as they’re not part of your code and are only accessible within the current process.
###Initializing the OpenAI client:
- We create an instance of the OpenAI client using the API key we just set.
- Using
os.getenv("OPENAI_API_KEY")
retrieves the API key from the environment variables.
This method ensures that your API keys are handled securely and are readily available for use in your script.
Llumo Evaluation Function Documentation
Define Llumo Evaluation Function
Function Definition:
- We define a function
evaluate_with_llumo
that takes aprompt
andoutput
as inputs.
API Setup:
- We retrieve the Llumo API key from environment variables.
- We set the API endpoint and prepare headers for the HTTP request.
Payload Preparation:
- We create a payload dictionary with the
prompt
,output
, and predefined analytics type (“Clarity”).
API Request:
- We use
requests.post()
to send a POST request to the Llumo API. response.raise_for_status()
will raise an exception for HTTP errors.
Response Parsing:
- We parse the JSON response and extract the evaluation data.
- We print the API response for debugging purposes.
Error Handling:
-
We use a try-except block to catch potential errors:
- JSON decoding errors
- Request exceptions
-
If an error occurs, we log it and return an empty dictionary with a failure indicator.
Return Values:
- The function returns a tuple containing:
- Evaluation data (or empty dictionary if evaluation failed)
- Success boolean
This function encapsulates the entire process of interacting with the Llumo API for text evaluation, including error handling and result processing.
Getting Respone from OpenAI
Define Example Prompt:
- We define an example prompt to be used for testing without compression.
Example Prompt:
This segment of code demonstrates how to define an example prompt and test the response generation without applying any compression, using the OpenAI API. It includes the prompt definition, API request, and response extraction steps.
Evaluating Responses with Llumo
Evaluate the OpenAI Response:
- We evaluate the response generated by the OpenAI API using the Llumo evaluation function.
- We call the evaluate_with_llumo function with the example prompt and the openai_output.
- We check if the evaluation was successful:
- If successful, we print the Llumo evaluation results.
- If the evaluation fails, we print an error message and indicate that the original prompt can be used if evaluation fails.
Was this page helpful?