Last active
February 16, 2024 13:12
-
-
Save bhavsarpratik/6e233885e847114e120619c0c24b77e2 to your computer and use it in GitHub Desktop.
OpenaAI GPT3 inference
This file contains hidden or 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