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

token usage parser for llama_index #1248

Open
tarunn2799 opened this issue Sep 5, 2024 · 5 comments
Open

token usage parser for llama_index #1248

tarunn2799 opened this issue Sep 5, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@tarunn2799
Copy link

[ *] I checked the documentation and related resources and couldn't find an answer to my question.

Your Question
what is unclear to you? What would you like to know?

How do I use the token usage parser when I'm using llama_index evaluate?

Code Examples
This community speaks code. Share your code snippets to help us understand your question better.

Additional context
Anything else you want to share with us?

@tarunn2799 tarunn2799 added the question Further information is requested label Sep 5, 2024
Copy link

dosubot bot commented Sep 5, 2024

Hey @tarunn2799! 👋 I'm here to help you with any bugs, questions, or contributions while you wait for a human maintainer. Let's tackle this together!

To use the token usage parser for ragas.integrations.llama_index.evaluate with get_token_usage_for_openai, follow these steps:

  1. Import the necessary modules and functions:

    from datasets import load_dataset
    from ragas.metrics import (
        answer_relevancy,
        faithfulness,
        context_recall,
        context_precision,
    )
    from ragas import evaluate
    from ragas.cost import get_token_usage_for_openai
    from langchain_openai.chat_models import ChatOpenAI
    from langchain_core.prompt_values import StringPromptValue
  2. Load the dataset:

    amnesty_qa = load_dataset("explodinggradients/amnesty_qa", "english_v2")
  3. Initialize the LLM model:

    gpt4o = ChatOpenAI(model="gpt-4o")
  4. Perform the evaluation with the token usage parser:

    result = evaluate(
        amnesty_qa["eval"],
        metrics=[
            context_precision,
            faithfulness,
            answer_relevancy,
            context_recall,
        ],
        llm=gpt4o,
        token_usage_parser=get_token_usage_for_openai,
    )
  5. Get the total tokens used:

    total_tokens = result.total_tokens()
  6. Compute the total cost:

    total_cost = result.total_cost(cost_per_input_token=5 / 1e6, cost_per_output_token=15 / 1e6)

Here is the complete code snippet:

from datasets import load_dataset
from ragas.metrics import (
    answer_relevancy,
    faithfulness,
    context_recall,
    context_precision,
)
from ragas import evaluate
from ragas.cost import get_token_usage_for_openai
from langchain_openai.chat_models import ChatOpenAI
from langchain_core.prompt_values import StringPromptValue

# Load the dataset
amnesty_qa = load_dataset("explodinggradients/amnesty_qa", "english_v2")

# Initialize the LLM model
gpt4o = ChatOpenAI(model="gpt-4o")

# Perform the evaluation with the token usage parser
result = evaluate(
    amnesty_qa["eval"],
    metrics=[
        context_precision,
        faithfulness,
        answer_relevancy,
        context_recall,
    ],
    llm=gpt4o,
    token_usage_parser=get_token_usage_for_openai,
)

# Get the total tokens used
total_tokens = result.total_tokens()

# Compute the total cost
total_cost = result.total_cost(cost_per_input_token=5 / 1e6, cost_per_output_token=15 / 1e6)

This code will help you evaluate the dataset using the specified metrics and compute the token usage and cost using the get_token_usage_for_openai parser [1].

To continue talking to Dosu, mention @dosu.

@tarunn2799
Copy link
Author

That's just the general documentation - ragas.integrations.llama_index.evaluate does not support token_usage_parser as an argument. That's my problem.

@jjmachan
Copy link
Member

jjmachan commented Sep 6, 2024

@tarunn2799 currently llama_index doesnot support token_parser sadly. I will take a look at it but right now we are working on #1009 so this might take some time sadly 🙁

if you want to take a stab at it I can help you out, would that be something you would be interested?

@jjmachan jjmachan changed the title token usage parser for ragas.integrations.llama_index.evaluate to use get_token_usage_openai token usage parser for llama_index Sep 6, 2024
@jjmachan jjmachan added enhancement New feature or request and removed question Further information is requested labels Sep 6, 2024
@tarunn2799
Copy link
Author

Yes it would be! Let me know how I can contribute. Thanks :)

@jjmachan
Copy link
Member

jjmachan commented Sep 7, 2024

awesome! this is my cal, can you find a time there?
https://cal.com/jjmachan/huddle

we can go over how you might do this 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants