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

Added helper to estimate cost of query with YOLOPandas #30

Merged
merged 4 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions tests/integration_tests/test_llm_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from tests import TEST_DIRECTORY
from yolopandas import pd
from yolopandas.utils.query_helpers import run_query_with_cost


class TestLLMAccessor(unittest.TestCase):
Expand All @@ -20,6 +21,11 @@ def test_basic_use(self):
expected_result = 15
self.assertEqual(expected_result, result)

result = run_query_with_cost(
self.product_df, "What is the price of the highest-priced book?", yolo=True
)
self.assertEqual(expected_result, result)

result = self.product_df.llm.query(
"What is the average price of products grouped by type?",
yolo=True,
Expand Down
Empty file added yolopandas/utils/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions yolopandas/utils/query_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Any

from langchain.callbacks import get_openai_callback

from yolopandas import pd


def run_query_with_cost(df: pd.DataFrame, query: str, yolo: bool = False) -> Any:
"""
A function to run a YOLOPandas query with cost estimation returned for your query in terms of tokens used.
This includes total tokens, prompt tokens, completion tokens, and the total cost in USD.

Parameters
----------
df : pd.DataFrame
The Pandas DataFrame with your data
query : str
The query you want to run against your data
yolo : bool
Boolean value used to return a prompt to a user or not to accept the code result before
running the code (False means to return the prompt)

Returns
-------
result : Any
The results of the query run against your data. A prompt may be returned as intermediary
output to proceed with generating the result or not.
"""
with get_openai_callback() as cb:
result = df.llm.query(query, yolo=yolo)
print(f"Total Tokens: {cb.total_tokens}")
print(f"Prompt Tokens: {cb.prompt_tokens}")
print(f"Completion Tokens: {cb.completion_tokens}")
print(f"Total Cost (USD): ${cb.total_cost}")
return result