Skip to content

Added helper to estimate cost of query with YOLOPandas #30

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

Merged
merged 4 commits into from
May 5, 2023
Merged
Changes from 2 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
Empty file added utils/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions utils/query_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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):
"""
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