Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): Fix credentials cost filter not able to filter the block cost #8837

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions autogpt_platform/backend/backend/data/block_cost_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,8 @@


LLM_COST = (
# Anthropic Models
[
BlockCost(
cost_type=BlockCostType.RUN,
cost_filter={
"model": model,
"api_key": None, # Running LLM with user own API key is free.
},
cost_amount=cost,
)
for model, cost in MODEL_COST.items()
]
+ [
BlockCost(
cost_type=BlockCostType.RUN,
cost_filter={
Expand All @@ -99,6 +89,7 @@
for model, cost in MODEL_COST.items()
if MODEL_METADATA[model].provider == "anthropic"
]
# OpenAI Models
+ [
BlockCost(
cost_type=BlockCostType.RUN,
Expand All @@ -115,6 +106,7 @@
for model, cost in MODEL_COST.items()
if MODEL_METADATA[model].provider == "openai"
]
# Groq Models
+ [
BlockCost(
cost_type=BlockCostType.RUN,
Expand All @@ -127,13 +119,6 @@
for model, cost in MODEL_COST.items()
if MODEL_METADATA[model].provider == "groq"
]
+ [
BlockCost(
# Default cost is running LlmModel.GPT4O.
cost_amount=MODEL_COST[LlmModel.GPT4O],
cost_filter={"api_key": None},
),
]
# Open Router Models
+ [
BlockCost(
Expand All @@ -151,6 +136,13 @@
for model, cost in MODEL_COST.items()
if MODEL_METADATA[model].provider == "open_router"
]
+ [
BlockCost(
# Default cost is running LlmModel.GPT4O.
cost_amount=MODEL_COST[LlmModel.GPT4O],
cost_filter={"api_key": None},
),
]
)

# =============== This is the exhaustive list of cost for each Block =============== #
Expand Down
54 changes: 34 additions & 20 deletions autogpt_platform/backend/backend/data/credit.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,24 @@ async def get_or_refill_credit(self, user_id: str) -> int:
def time_now():
return datetime.now(timezone.utc)

@staticmethod
def _is_filter_match(self, cost_filter: BlockInput, input_data: BlockInput) -> bool:
"""
Filter rules:
- If costFilter is an object, then check if costFilter is the subset of inputValues
- Otherwise, check if costFilter is equal to inputValues.
- Undefined, null, and empty string are considered as equal.
"""
if not isinstance(cost_filter, dict) or not isinstance(input_data, dict):
return cost_filter == input_data

return all(
(not input_data.get(k) and not v)
or (input_data.get(k) and self._is_filter_match(v, input_data[k]))
for k, v in cost_filter.items()
)
majdyz marked this conversation as resolved.
Show resolved Hide resolved

def _block_usage_cost(
self,
block: Block,
input_data: BlockInput,
data_size: float,
Expand All @@ -119,25 +135,23 @@ def _block_usage_cost(
return 0, {}

for block_cost in block_costs:
if all(
# None, [], {}, "", are considered the same value.
input_data.get(k) == b or (not input_data.get(k) and not b)
for k, b in block_cost.cost_filter.items()
):
if block_cost.cost_type == BlockCostType.RUN:
return block_cost.cost_amount, block_cost.cost_filter

if block_cost.cost_type == BlockCostType.SECOND:
return (
int(run_time * block_cost.cost_amount),
block_cost.cost_filter,
)

if block_cost.cost_type == BlockCostType.BYTE:
return (
int(data_size * block_cost.cost_amount),
block_cost.cost_filter,
)
if not self._is_filter_match(block_cost.cost_filter, input_data):
continue

if block_cost.cost_type == BlockCostType.RUN:
return block_cost.cost_amount, block_cost.cost_filter

if block_cost.cost_type == BlockCostType.SECOND:
return (
int(run_time * block_cost.cost_amount),
block_cost.cost_filter,
)

if block_cost.cost_type == BlockCostType.BYTE:
return (
int(data_size * block_cost.cost_amount),
block_cost.cost_filter,
)

return 0, {}

Expand Down
22 changes: 17 additions & 5 deletions autogpt_platform/frontend/src/components/CustomNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,26 @@ export function CustomNode({
});

const inputValues = data.hardcodedValues;

const isFilterMatch = (costFilter: any, inputValues: any): boolean => {
majdyz marked this conversation as resolved.
Show resolved Hide resolved
/*
Filter rules:
- If costFilter is an object, then check if costFilter is the subset of inputValues
- Otherwise, check if costFilter is equal to inputValues.
- Undefined, null, and empty string are considered as equal.
*/
return typeof costFilter === "object" && typeof inputValues === "object"
? Object.entries(costFilter).every(
([k, v]) =>
(!v && !inputValues[k]) || isFilterMatch(v, inputValues[k]),
)
: costFilter === inputValues;
};

const blockCost =
data.blockCosts &&
data.blockCosts.find((cost) =>
Object.entries(cost.cost_filter).every(
// Undefined, null, or empty values are considered equal
([key, value]) =>
value === inputValues[key] || (!value && !inputValues[key]),
),
isFilterMatch(cost.cost_filter, inputValues),
);

const LineSeparator = () => (
Expand Down
Loading