From 51e61a5c799976579c9edee477430e9604c82bda Mon Sep 17 00:00:00 2001 From: dh-rico-freitas <110987484+dh-rico-freitas@users.noreply.github.com> Date: Wed, 14 Jun 2023 09:39:39 +0200 Subject: [PATCH 1/2] Pass model_name as a default parameter. This let explore other (perhaps cheaper) text completition models. The changes didn't affect prior usage. --- betterprompt/__init__.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/betterprompt/__init__.py b/betterprompt/__init__.py index 50d2b2e..b2d3fcd 100644 --- a/betterprompt/__init__.py +++ b/betterprompt/__init__.py @@ -4,8 +4,6 @@ import os -model_name: str = "text-davinci-003" - def get_from_dict_or_env(env_key: str) -> str: """Get a value from a dictionary or an environment variable.""" if env_key in os.environ and os.environ[env_key]: @@ -18,12 +16,12 @@ def get_from_dict_or_env(env_key: str) -> str: ) -def call_openai(prompt: str): +def call_openai(prompt: str, model_name: str = "text-davinci-003"): openai_api_key = get_from_dict_or_env("OPENAI_API_KEY") openai.api_key = openai_api_key #GET THE LOGPROB OF THE PROMPT response = openai.Completion.create( - model="text-davinci-003", + model=model_name, prompt=prompt, max_tokens=0, echo=True, @@ -31,14 +29,14 @@ def call_openai(prompt: str): ) return response["choices"][0]["logprobs"]["token_logprobs"] -def calculate_perplexity(prompt: str): +def calculate_perplexity(prompt: str, model_name: str = "text-davinci-003"): nlls = [] - token_logprobs = call_openai(prompt) + token_logprobs = call_openai(prompt, model_name) for neg_log_likelihood in token_logprobs: if neg_log_likelihood == None: #default to -100, handles the initial token case neg_log_likelihood = -100 nlls.append(neg_log_likelihood) perplexity = math.exp(sum(nlls)/len(token_logprobs)) - return perplexity \ No newline at end of file + return perplexity From 9a7715b718d43dd01b616480383d9aafceeb70ea Mon Sep 17 00:00:00 2001 From: dh-rico-freitas <110987484+dh-rico-freitas@users.noreply.github.com> Date: Wed, 14 Jun 2023 09:42:13 +0200 Subject: [PATCH 2/2] Update __init__.py --- betterprompt/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/betterprompt/__init__.py b/betterprompt/__init__.py index b2d3fcd..79eca7c 100644 --- a/betterprompt/__init__.py +++ b/betterprompt/__init__.py @@ -40,3 +40,4 @@ def calculate_perplexity(prompt: str, model_name: str = "text-davinci-003"): perplexity = math.exp(sum(nlls)/len(token_logprobs)) return perplexity +