-
Notifications
You must be signed in to change notification settings - Fork 84
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
feat: DIA-1384: add cost estimate endpoint #225
Merged
+256
−10
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5868a19
feat: add cost estimate endpoint
pakelley 19338fb
Merge branch 'master' into fb-dia-1384
pakelley 1ba4083
updates from CR
pakelley 576610b
move cost estimation to runtime definition
pakelley aba6b9c
add tests
pakelley 6d79ff4
add use_openai tag to tests
pakelley 4142925
use client fixture in test
pakelley 07367fc
updates from CR
pakelley 55ed503
improve error handling
pakelley b922d4d
typo
pakelley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python3 | ||
from adala.runtimes._litellm import AsyncLiteLLMChatRuntime | ||
from adala.runtimes.base import CostEstimate | ||
from adala.agents import Agent | ||
from adala.skills import ClassificationSkill | ||
import numpy as np | ||
import os | ||
from fastapi.testclient import TestClient | ||
from server.app import app, CostEstimateRequest | ||
|
||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | ||
|
||
|
||
def test_simple_estimate_cost(): | ||
runtime = AsyncLiteLLMChatRuntime(model="gpt-4o-mini", api_key=OPENAI_API_KEY) | ||
|
||
cost_estimate = runtime.get_cost_estimate( | ||
prompt="testing, {text}", | ||
substitutions=[{"text": "knock knock, who's there"}], | ||
output_fields=["text"], | ||
) | ||
|
||
assert isinstance(cost_estimate, CostEstimate) | ||
assert isinstance(cost_estimate.prompt_cost_usd, float) | ||
assert isinstance(cost_estimate.completion_cost_usd, float) | ||
assert isinstance(cost_estimate.total_cost_usd, float) | ||
assert np.isclose( | ||
cost_estimate.total_cost_usd, | ||
cost_estimate.prompt_cost_usd + cost_estimate.completion_cost_usd, | ||
) | ||
|
||
|
||
def test_estimate_cost_endpoint(): | ||
test_client = TestClient(app) | ||
pakelley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
req = { | ||
"agent": { | ||
"skills": [ | ||
{ | ||
"type": "ClassificationSkill", | ||
"name": "text_classifier", | ||
"instructions": "Always return the answer 'Feature Lack'.", | ||
"input_template": "{text}", | ||
"output_template": "{output}", | ||
"labels": [ | ||
"Feature Lack", | ||
"Price", | ||
"Integration Issues", | ||
"Usability Concerns", | ||
"Competitor Advantage", | ||
], | ||
} | ||
], | ||
"runtimes": { | ||
"default": { | ||
"type": "AsyncLiteLLMChatRuntime", | ||
"model": "gpt-4o-mini", | ||
"api_key": OPENAI_API_KEY, | ||
} | ||
}, | ||
}, | ||
"prompt": "test {text}", | ||
"substitutions": [{"text": "test"}], | ||
} | ||
resp = test_client.post( | ||
"/estimate-cost", | ||
json=req, | ||
) | ||
resp_data = resp.json()["data"] | ||
cost_estimate = CostEstimate(**resp_data) | ||
|
||
assert isinstance(cost_estimate, CostEstimate) | ||
assert isinstance(cost_estimate.prompt_cost_usd, float) | ||
assert isinstance(cost_estimate.completion_cost_usd, float) | ||
assert isinstance(cost_estimate.total_cost_usd, float) | ||
assert np.isclose( | ||
cost_estimate.total_cost_usd, | ||
cost_estimate.prompt_cost_usd + cost_estimate.completion_cost_usd, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need this function? Skills must be always Dict mapping from skill name to Skill (there is a
skills_validator
function to prevalidate that), so we can always iterate likeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type sig for
skills
isskills: SerializeAsAny[Union[Skill, SkillSet]]
, so we can't count on this always being aSkillSet
(and therefore havingget_skill_names
defined), yeah?Plus, it seems simpler to be able to get the list of skills than the list of names, I'd think? Since it doesn't seem that
get_skill_names
is being used, I think it's simpler to change that to get the skills themselves (if we can, in fact, assume this is aSkillSet
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can because of https://github.com/HumanSignal/Adala/blob/master/adala/agents/base.py#L109
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change the type sig of
skills
inAgent
then?And still, like I said in my first comment, it seems simpler to have a
SkillSet.get_skills
thanSkillSet.get_skill_names
, esp sinceSkillSet.get_skill_names
doesn't seem to be used anywhere. I'll go ahead and change that and the type sig ofAgent.skills