OpenaAI GPT3 inference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install openai retry | |
import openai | |
from retry import retry | |
model="curie:ft-xxx-2021-01-09-19-21-20 | |
@retry(Exception, tries=3, delay=10) | |
def get_gpt_reason(prompt, model, prob=False, max_tokens=10): | |
if prob: | |
max_tokens=1 | |
prediction = openai.Completion.create( | |
model=model, | |
prompt=prompt, | |
max_tokens=max_tokens, | |
stop="END", | |
logprobs=0, | |
temperature=0.0, | |
top_p=1 | |
) | |
predicted_reason = prediction["choices"][0]["text"].strip() | |
if prob: | |
token_logprobs = prediction["choices"][0]["logprobs"]["token_logprobs"] | |
prob = round(np.exp(token_logprobs).sum(), 2) | |
return prediction, predicted_reason, prob | |
return predicted_reason |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment