Skip to content

Commit ae4f046

Browse files
authored
Merge pull request #142 from AgentOps-AI/feat/add-openai-prefixed-models
2 parents 505870e + 1df603d commit ae4f046

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

update_prices.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010

1111

1212
def diff_dicts(dict1, dict2):
13-
diff_keys = dict1.keys() ^ dict2.keys()
14-
differences = {k: (dict1.get(k), dict2.get(k)) for k in diff_keys}
13+
# Filter out keys from dict1 that start with 'openai/'
14+
dict1_filtered = {k: v for k, v in dict1.items() if not k.startswith('openai/')}
15+
16+
diff_keys_initial = dict1_filtered.keys() ^ dict2.keys()
17+
diff_keys = {k for k in diff_keys_initial if not k.startswith('openai/')}
18+
differences = {k: (dict1_filtered.get(k), dict2.get(k)) for k in diff_keys}
1519
differences.update(
16-
{k: (dict1[k], dict2[k]) for k in dict1 if k in dict2 and dict1[k] != dict2[k]}
20+
{k: (dict1_filtered[k], dict2[k]) for k in dict1_filtered if k in dict2 and dict1_filtered[k] != dict2[k]}
1721
)
1822

1923
if differences:
@@ -30,17 +34,33 @@ def diff_dicts(dict1, dict2):
3034
with open("tokencost/model_prices.json", "r") as f:
3135
model_prices = json.load(f)
3236

33-
# Compare the refreshed TOKEN_COSTS with the file
37+
# Compare the refreshed TOKEN_COSTS with the file, ignoring "openai/" keys
3438
if diff_dicts(model_prices, tokencost.TOKEN_COSTS):
3539
print("Updating model_prices.json")
3640
with open("tokencost/model_prices.json", "w") as f:
3741
json.dump(tokencost.TOKEN_COSTS, f, indent=4)
3842
print("File updated successfully")
43+
# Reload the prices after updating
44+
with open("tokencost/model_prices.json", "r") as f:
45+
model_prices = json.load(f)
3946
else:
4047
print("File is already up to date")
4148

49+
# Add/overwrite the "openai/" keys from the unprefixed keys in the final price list
50+
openai_models = {key: value for key, value in model_prices.items() if value.get("litellm_provider") == "openai"}
51+
52+
for key, value in openai_models.items():
53+
if not key.startswith('openai/'):
54+
new_key = f"openai/{key}"
55+
model_prices[new_key] = value
56+
57+
# Write the final, consistent data back to the file
58+
print("Adding 'openai/' pre-fixed models to model_prices.json")
59+
with open("tokencost/model_prices.json", "w") as f:
60+
json.dump(model_prices, f, indent=4)
61+
4262
# Load the data
43-
df = pd.DataFrame(tokencost.TOKEN_COSTS).T
63+
df = pd.DataFrame(model_prices).T
4464
df.loc[df.index[1:], "max_input_tokens"] = (
4565
df["max_input_tokens"].iloc[1:].apply(lambda x: "{:,.0f}".format(x))
4666
)

0 commit comments

Comments
 (0)