Skip to content

Instantly share code, notes, and snippets.

@bhavsarpratik
Last active November 8, 2022 11:12
Embed
What would you like to do?
OpenaAI GPT3 inference
# 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