Skip to content

Commit

Permalink
Should be good, unable to test yet
Browse files Browse the repository at this point in the history
  • Loading branch information
nonprofittechy committed Nov 30, 2023
1 parent 2248e37 commit d08f90c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
1 change: 0 additions & 1 deletion formfyxer/keys/openai_key.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
your_OPENAI_API_key goes here
1 change: 0 additions & 1 deletion formfyxer/keys/openai_org.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
your_OPENAI_API_org goes here
64 changes: 46 additions & 18 deletions formfyxer/lit_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
)

import openai
from openai import OpenAI

from transformers import GPT2TokenizerFast

tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
Expand Down Expand Up @@ -125,14 +127,27 @@
os.path.join(os.path.dirname(__file__), "keys", "spot_token.txt"), "r"
) as in_file:
default_spot_token = in_file.read().rstrip()
with open(
os.path.join(os.path.dirname(__file__), "keys", "openai_org.txt"), "r"
) as in_file:
openai.organization = in_file.read().rstrip()
with open(
os.path.join(os.path.dirname(__file__), "keys", "openai_key.txt"), "r"
) as in_file:
openai.api_key = in_file.read().rstrip()
try:
with open(
os.path.join(os.path.dirname(__file__), "keys", "openai_key.txt"), "r"
) as in_file:
default_key = OpenAI(api_key=in_file.read().rstrip())
except:
default_key = None
try:
with open(
os.path.join(os.path.dirname(__file__), "keys", "openai_org.txt"), "r"
) as in_file:
default_org = in_file.read().rstrip()
except:
default_org = None
if default_key:
client = OpenAI(api_key=default_key, organization=default_org or None)
elif os.getenv("OPENAI_API_KEY"):
client = OpenAI()
else:
client = None


# TODO(brycew): remove by retraining the model to work with random_state=4.
NEEDS_STABILITY = True if os.getenv("ISUNITTEST") else False
Expand Down Expand Up @@ -801,22 +816,35 @@ class OpenAiCreds(TypedDict):
key: str


def text_complete(prompt, max_tokens=500, creds: Optional[OpenAiCreds] = None) -> str:
if creds:
openai.organization = creds["org"].strip() or ""
openai.api_key = creds["key"].strip() or ""
def text_complete(prompt:str, max_tokens:int=500, creds: Optional[OpenAiCreds] = None, temperature:float=0) -> str:
"""Run a prompt via openAI's API and return the result.
Args:
prompt (str): The prompt to send to the API.
max_tokens (int, optional): The number of tokens to generate. Defaults to 500.
creds (Optional[OpenAiCreds], optional): The credentials to use. Defaults to None.
temperature (float, optional): The temperature to use. Defaults to 0.
"""
if creds:
openai_client = OpenAI(api_key=creds["key"], organization=creds["org"])
else:
openai_client = client
try:
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0,
response = openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": prompt
},
],
temperature=temperature,
max_tokens=max_tokens,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
presence_penalty=0.0
)
return str(response["choices"][0]["text"].strip())
return str(response.choices[0].message.content.strip())
except Exception as ex:
print(f"{ex}")
return "ApiError"
Expand Down

0 comments on commit d08f90c

Please sign in to comment.