ChessGPT_Stockfish / model.py
nakas's picture
Update model.py
303e229
raw
history blame
1.27 kB
import openai
poem = """Write what is asked of you:
---
{input}
---
This is the result: """
def set_openai_key(key):
"""Sets OpenAI key."""
openai.api_key = key
class GeneralModel:
def __init__(self):
print("Model Intilization--->")
# set_openai_key(API_KEY)
def query(self, prompt, myKwargs={}):
"""
wrapper for the API to save the prompt and the result
"""
# arguments to send the API
kwargs = {
"engine": "text-davinci-003",
"temperature": 0.85,
"max_tokens": 2400,
"best_of": 1,
"top_p": 1,
"frequency_penalty": 0.5,
"presence_penalty": 0.5,
"stop": ["###"],
}
for kwarg in myKwargs:
kwargs[kwarg] = myKwargs[kwarg]
r = openai.Completion.create(prompt=prompt, **kwargs)["choices"][0][
"text"
].strip()
return r
def model_prediction(self, input, api_key):
"""
wrapper for the API to save the prompt and the result
"""
# Setting the OpenAI API key got from the OpenAI dashboard
set_openai_key(api_key)
output = self.query(poem.format(input = input))
return output