Skip to content

Commit

Permalink
API key check
Browse files Browse the repository at this point in the history
  • Loading branch information
robertvacareanu committed Apr 13, 2024
1 parent a139e98 commit 6b1ed3b
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
import warnings
from pathlib import Path

with open('api_personal.key') as fin:
os.environ['OPENAI_API_KEY'] = fin.readlines()[0].strip()
if 'OPENAI_API_KEY' not in os.environ:
print("No OpenAI API key found in environment variables. Will attempt to read from `api.key`.")
if os.path.exists('api.key'):
with open('api.key') as fin:
os.environ['OPENAI_API_KEY'] = fin.readlines()[0].strip()
else:
print("No `api.key` file found. Please create one with your OpenAI API key or set the `OPENAI_API_KEY` variable.")
exit()


llm = ChatOpenAI(model_name="gpt-4-0125-preview", temperature=0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
import warnings
from pathlib import Path

with open('api_openrouter_personal.key') as fin:
os.environ['OPENROUTER_API_KEY'] = fin.readlines()[0].strip()
if 'OPENROUTER_API_KEY' not in os.environ:
print("No OpenRouter API key found in environment variables. Will attempt to read from `api_openrouter_personal.key`.")
if os.path.exists('api_openrouter_personal.key'):
with open('api_openrouter_personal.key') as fin:
os.environ['OPENROUTER_API_KEY'] = fin.readlines()[0].strip()
else:
print("No `api_openrouter_personal.key` file found. Please create one with your OpenAI API key or set the `OPENROUTER_API_KEY` variable.")
exit()


for (llm, model_name) in [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import warnings
from pathlib import Path

with open('api_deepinfra_personal.key') as fin:
os.environ['DEEPINFRA_API_TOKEN'] = fin.readlines()[0].strip()
if 'DEEPINFRA_API_TOKEN' not in os.environ:
print("No Fireworks API key found in environment variables. Will attempt to read from `api_deepinfra_personal.key`.")
if os.path.exists('api_deepinfra_personal.key'):
with open('api_deepinfra_personal.key') as fin:
os.environ['DEEPINFRA_API_TOKEN'] = fin.readlines()[0].strip()
else:
print("No `api_deepinfra_personal.key` file found. Please create one with your Fireworks API key or set the `DEEPINFRA_API_TOKEN` variable.")
exit()


for (llm, model_name) in [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
from src.regressors.sklearn_regressors import (linear_regression,
ridge,
lasso,
mlp_universal_approximation_theorem1,
mlp_universal_approximation_theorem2,
mlp_universal_approximation_theorem3,
mlp_deep1,
mlp_deep2,
mlp_deep3,
random_forest,
bagging,
gradient_boosting,
adaboost,
voting,
baseline
)
from src.regressors.fireworks_llm_regressor import *
from src.dataset_utils import get_dataset
from src.score_utils import scores
Expand All @@ -23,21 +7,14 @@
import warnings
from pathlib import Path

with open('api_fireworks_personal.key') as fin:
os.environ['FIREWORKS_API_KEY'] = fin.readlines()[0].strip()

# llm = DeepInfra(model_id='mistralai/Mixtral-8x7B-Instruct-v0.1')
# model_name = 'mixtral8x7B'
# llm = DeepInfra(model_id='meta-llama/Llama-2-70b-chat-hf')
# model_name = 'llama270bchathf'
# llm = DeepInfra(model_id='01-ai/Yi-34B-Chat')
# model_name = 'yi34chat'
# llm = DeepInfra(model_id='mistralai/Mistral-7B-Instruct-v0.1')
# model_name = 'mixtral7B'
# llm = DeepInfra(model_id='EleutherAI/pythia-12b')
# model_name = 'pythia12B'
# llm = DeepInfra(model_id='codellama/CodeLlama-70b-Instruct-hf')
# model_name = 'codellama70b'
if 'FIREWORKS_API_KEY' not in os.environ:
print("No Fireworks API key found in environment variables. Will attempt to read from `api_fireworks_personal.api`.")
if os.path.exists('api_fireworks_personal.api'):
with open('api_fireworks_personal.api') as fin:
os.environ['FIREWORKS_API_KEY'] = fin.readlines()[0].strip()
else:
print("No `api_fireworks_personal.api` file found. Please create one with your Fireworks API key or set the `FIREWORKS_API_KEY` variable.")
exit()


for (llm, model_name) in [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
import warnings
from pathlib import Path

with open('api_personal.key') as fin:
os.environ['OPENAI_API_KEY'] = fin.readlines()[0].strip()
if 'OPENAI_API_KEY' not in os.environ:
print("No OpenAI API key found in environment variables. Will attempt to read from `api.key`.")
if os.path.exists('api.key'):
with open('api.key') as fin:
os.environ['OPENAI_API_KEY'] = fin.readlines()[0].strip()
else:
print("No `api.key` file found. Please create one with your OpenAI API key or set the `OPENAI_API_KEY` variable.")
exit()

seed = 1

# llm = ChatOpenAI(model_name="gpt-4-0125-preview", temperature=0)
# model_name = 'gpt4_0125_preview'
Expand All @@ -36,7 +41,7 @@
'friedman3',

]:
for seed in [2, 3]:
for seed in [1, 2, 3]:
outputs = []
((x_train, _, y_train, _), y_fn) = get_dataset(dataset)(max_train=101, noise=0, random_state=seed, round=True, round_value=2)
def run():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,14 @@
import warnings
from pathlib import Path

with open('api_openrouter_personal.key') as fin:
os.environ['OPENROUTER_API_KEY'] = fin.readlines()[0].strip()

# llm = DeepInfra(model_id='mistralai/Mixtral-8x7B-Instruct-v0.1')
# model_name = 'mixtral8x7B'
# llm = DeepInfra(model_id='meta-llama/Llama-2-70b-chat-hf')
# model_name = 'llama270bchathf'
# llm = DeepInfra(model_id='01-ai/Yi-34B-Chat')
# model_name = 'yi34chat'
# llm = DeepInfra(model_id='mistralai/Mistral-7B-Instruct-v0.1')
# model_name = 'mixtral7B'
# llm = DeepInfra(model_id='EleutherAI/pythia-12b')
# model_name = 'pythia12B'
# llm = DeepInfra(model_id='codellama/CodeLlama-70b-Instruct-hf')
# model_name = 'codellama70b'
if 'OPENROUTER_API_KEY' not in os.environ:
print("No OpenRouter API key found in environment variables. Will attempt to read from `api_openrouter_personal.key`.")
if os.path.exists('api_openrouter_personal.key'):
with open('api_openrouter_personal.key') as fin:
os.environ['OPENROUTER_API_KEY'] = fin.readlines()[0].strip()
else:
print("No `api_openrouter_personal.key` file found. Please create one with your OpenAI API key or set the `OPENROUTER_API_KEY` variable.")
exit()


for (llm, model_name) in [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
python -m src.experiments.regression_performance.regression_performance_deepinfra
"""
from src.regressors.remote_llm_regressor import *
from src.dataset_utils import get_dataset
from src.score_utils import scores
Expand All @@ -8,8 +12,16 @@
import warnings
from pathlib import Path

with open('api_deepinfra_personal.key') as fin:
os.environ['DEEPINFRA_API_TOKEN'] = fin.readlines()[0].strip()
if 'DEEPINFRA_API_TOKEN' not in os.environ:
print("No Fireworks API key found in environment variables. Will attempt to read from `api_deepinfra_personal.key`.")
if os.path.exists('api_deepinfra_personal.key'):
with open('api_deepinfra_personal.key') as fin:
os.environ['DEEPINFRA_API_TOKEN'] = fin.readlines()[0].strip()
else:
print("No `api_deepinfra_personal.key` file found. Please create one with your Fireworks API key or set the `DEEPINFRA_API_TOKEN` variable.")
exit()




for (llm, model_name) in [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
python -m src.experiments.regression_performance.regression_performance_fireworks
"""
from src.regressors.fireworks_llm_regressor import *
from src.dataset_utils import get_dataset
from src.score_utils import scores
Expand All @@ -6,8 +10,14 @@
import os
from pathlib import Path

with open('api_fireworks_personal.key') as fin:
os.environ['FIREWORKS_API_KEY'] = fin.readlines()[0].strip()
if 'FIREWORKS_API_KEY' not in os.environ:
print("No Fireworks API key found in environment variables. Will attempt to read from `api_fireworks_personal.api`.")
if os.path.exists('api_fireworks_personal.api'):
with open('api_fireworks_personal.api') as fin:
os.environ['FIREWORKS_API_KEY'] = fin.readlines()[0].strip()
else:
print("No `api_fireworks_personal.api` file found. Please create one with your Fireworks API key or set the `FIREWORKS_API_KEY` variable.")
exit()


for (llm, model_name) in [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
import os
from pathlib import Path

with open('api.key') as fin:
os.environ['OPENAI_API_KEY'] = fin.readlines()[0].strip()

if 'OPENAI_API_KEY' not in os.environ:
print("No OpenAI API key found in environment variables. Will attempt to read from `api.key`.")
if os.path.exists('api.key'):
with open('api.key') as fin:
os.environ['OPENAI_API_KEY'] = fin.readlines()[0].strip()
else:
print("No `api.key` file found. Please create one with your OpenAI API key or set the `OPENAI_API_KEY` variable.")
exit()

# llm = ChatOpenAI(model_name="gpt-4-turbo-2024-04-09", temperature=0)
# model_name = 'gpt4-turbo-20240409'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@
import warnings
from pathlib import Path

with open('api_openrouter_personal.key') as fin:
os.environ['OPENROUTER_API_KEY'] = fin.readlines()[0].strip()

# llm = DeepInfra(model_id='mistralai/Mixtral-8x7B-Instruct-v0.1')
# model_name = 'mixtral8x7B'
# llm = DeepInfra(model_id='meta-llama/Llama-2-70b-chat-hf')
# model_name = 'llama270bchathf'
# llm = DeepInfra(model_id='01-ai/Yi-34B-Chat')
# model_name = 'yi34chat'
# llm = DeepInfra(model_id='mistralai/Mistral-7B-Instruct-v0.1')
# model_name = 'mixtral7B'
# llm = DeepInfra(model_id='EleutherAI/pythia-12b')
# model_name = 'pythia12B'
# llm = DeepInfra(model_id='codellama/CodeLlama-70b-Instruct-hf')
# model_name = 'codellama70b'
if 'OPENROUTER_API_KEY' not in os.environ:
print("No OpenRouter API key found in environment variables. Will attempt to read from `api_openrouter_personal.key`.")
if os.path.exists('api_openrouter_personal.key'):
with open('api_openrouter_personal.key') as fin:
os.environ['OPENROUTER_API_KEY'] = fin.readlines()[0].strip()
else:
print("No `api_openrouter_personal.key` file found. Please create one with your OpenAI API key or set the `OPENROUTER_API_KEY` variable.")
exit()



for (llm, model_name) in [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
import warnings
from pathlib import Path

with open('api_personal.key') as fin:
os.environ['OPENAI_API_KEY'] = fin.readlines()[0].strip()
if 'OPENAI_API_KEY' not in os.environ:
print("No OpenAI API key found in environment variables. Will attempt to read from `api.key`.")
if os.path.exists('api.key'):
with open('api.key') as fin:
os.environ['OPENAI_API_KEY'] = fin.readlines()[0].strip()
else:
print("No `api.key` file found. Please create one with your OpenAI API key or set the `OPENAI_API_KEY` variable.")
exit()


llm = ChatOpenAI(model_name="gpt-4-0125-preview", temperature=0)
Expand Down
1 change: 1 addition & 0 deletions src/regressors/fireworks_llm_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
import time
from src.regressors.prompts import construct_few_shot_prompt
from langchain_fireworks import Fireworks

def llm_regression(llm, x_train, x_test, y_train, y_test, encoding_type, model_name, add_instr_prefix=False, instr_prefix='The task is to provide your best estimate for "Output". Please provide that and only that, without any additional text.\n\n\n\n\n'):
examples_test = []
Expand Down
1 change: 1 addition & 0 deletions src/regressors/local_llm_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
from src.regressors.prompts import construct_few_shot_prompt
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch



Expand Down

0 comments on commit 6b1ed3b

Please sign in to comment.