Getting log probability of the sentence

Hello,
I want to use GPT models to assign log probabilities to each sentence. Is there any way to do that? I wrote the following code but I receive an error:

define your sentence

sentence = “The quick brown fox jumps over the lazy dog.”

get log probability scores from OpenAI GPT-2 model

response = openai.Completion.create(
engine=“davinci”,
prompt=sentence,
max_tokens=0,
n=1,
stop=None,
temperature=0.0,
)

extract log probability score for the entire sentence

sentence_logprobs = response.choices[0].logprobs.token_logprobs[-1]

#print out log probability score for the entire sentence
print(f"Sentence log probability score: {sentence_logprobs}")

InvalidRequestError: You requested a length 0 completion, but did not specify a ‘logprobs’ parameter to see the probability of each token. Either request a completion with length greater than 1, or set ‘logprobs’ to 0 (to see the probabilities of each token you submitted) or more (to also see the probabilities over alternative tokens).

I don’t wanna to generate another token, so I have to set max_tokens=0.

2 Likes

As the error says, just set logprobs=0 in the openai.Completion.create function call