to bound it between 0 and 1 as below. Is this correct approach to do that. I see the token probability is 0.4 which is very less for the correct tokens
import os
import openai
import math
from dotenv import load_dotenv
load_dotenv()
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
# Initialize the OpenAI API client with your API key
openai.api_key = OPENAI_API_KEY
def sigmoid(x):
"""Sigmoid function to map any value to a range between 0 and 1."""
return 1 / (1 + math.exp(-x))
def get_logprobs(prompt, model="text-davinci-002"):
"""
Get log probabilities of tokens generated by OpenAI for a given prompt.
Args:
- prompt (str): The input text to generate a response.
- model (str): The model to use for generation. Default is "text-davinci-002".
Returns:
- list: A list of token log probabilities mapped between 0 and 1.
"""
response = openai.Completion.create(
engine=model,
prompt=prompt,
max_tokens=25, # You can adjust this value as needed
n=1,
logprobs=2 # Number of log probabilities to return for each token
)
# Extract log probabilities from the response
raw_logprobs = response.choices[0].logprobs.top_logprobs
# Apply sigmoid function to map logprobs between 0 and 1
# transformed_logprobs = [sigmoid(value[1]) for value in raw_logprobs[0]] # Assuming the logprob is the second item in each tuple
return raw_logprobs
# Example usage
prompt_text = "What is the capital of paris"
logprobs = get_logprobs(prompt_text)
for lp in logprobs:
for key,value in lp.items():
print(key,sigmoid(value))
print("---")