Skip to main content

Library to test prompt quality

Project description

LLM Prompt Evaluation Tool

This tool allows you to evaluate how well your LLM responses match an ideal answer by comparing generated questions and answers. You can use the tool either by calling the comparison functions manually in your code or by passing a CSV file containing test cases. The tool also supports visualizing your test scores on a chart, with results automatically grouped by different prompt IDs.

Key Parameters

  • ideal_answer (required):
    The reference or "ideal" answer that your LLM response is compared against.
    Example:

    "Blockchain is like a digital ledger that everyone can see but no one can change."
    
  • llm_response (required):
    Your LLM's response.

  • optional_params (optional):
    A JSON-like dictionary that may include extra details for the test. It has the following structure:

    • prompt: A string with the prompt to be used (if you want to override the prompt_id prompt).
    • context: (Optional) Additional context for the prompt.
    • prompt_id: (Optional) A unique identifier for the prompt to be fetched online from Lamoom Service.
      Example:
    {
      "prompt": "Explain blockchain to a beginner.",
      "context": {},
      "prompt_id": "beginner_blockchain"
    }
    

Using the Tool

1. Manual Testing

You can manually call the compare() method by passing the required ideal_answer and llm_response and (optionally) optional_params. Each call will automatically accumulate the test results based on the provided (or default) prompt_id from optional_params.

Example:

from lamoom_cicd import TestLLMResponsePipe
import time

ideal_answer = (
    "Blockchain is like a digital notebook that everyone can see, but no one can secretly change. "
    "Imagine a shared Google Doc where every change is recorded forever, and no one can edit past entries."
)
optional_params = {
    "prompt_id": f"test-{time.now()}"
}

lamoom_pipe = TestLLMResponsePipe(openai_key=os.environ.get("OPENAI_KEY"))
# When llm_response is not passed, it defaults to None.
result = lamoom_pipe.compare(ideal_answer, "Your LLM response here", optional_params=optional_params)

# Print individual question details
for question in result.questions:
    print(question.to_dict())

# Print overall score details
print(result.score.to_dict())

2. Testing with CSV

You can also pass multiple test cases using a CSV file. The CSV file should contain the following columns:

  • ideal_answer: (Required) The ideal answer text.
  • llm_response: (Required) LLM response to compare with.
  • optional_params: (Optional) A JSON string containing the optional parameters.

Multiple rows can be included, and you can use different prompt_id values to test various prompts.

Example CSV Content: IMPORTANT: take notice of double quotes when putting json in a csv file!

ideal_answer,llm_response, optional_params
"blockchain_prompt","Blockchain is a secure, immutable digital ledger.","Blockchain is like a shared Google Doc that records every change.","{""prompt_id"": ""google_doc_blockchain""}", 

Usage Example:

csv_file_path = "test_data.csv"
lamoom_pipe = TestLLMResponsePipe(openai_key=os.environ.get("OPENAI_KEY"))
accumulated_results = lamoom_pipe.compare_from_csv("test_prompt", csv_file_path)

3. Visualizing Test Scores

After running tests (whether manually or using a CSV), the results are automatically accumulated by prompt_id. To see a visual chart of test scores, use the provided visualization function.

Example:

lamoom_pipe.visualize_test_results()

This function will generate a line chart with the x-axis representing the test instance number (as integers) and the y-axis representing the score percentage. Each line on the chart corresponds to a different prompt_id.

Summary

  • ideal_answer, llm_response are required parameters.
  • optional_params are optional, with optional_params offering extra configuration (like a custom prompt and a unique prompt_id for tests).
  • You can compare responses either manually or via CSV (which supports multiple test cases).
  • The tool accumulates results for each prompt_id across multiple calls.
  • Use the visualization function to see your test scores on an easy-to-read chart.

Enjoy using the tool to refine and evaluate your LLM prompts!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lamoom_cicd-0.1.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lamoom_cicd-0.1.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file lamoom_cicd-0.1.2.tar.gz.

File metadata

  • Download URL: lamoom_cicd-0.1.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for lamoom_cicd-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c4b2a3402e0030ec35b1109ffd3a1cba6de9ce2ce0e2e8b11c73b9c3417f4607
MD5 030f92423e96521bfa92e594eb4ad304
BLAKE2b-256 5b3af7255d1bc555d37eb318fe578f8e2a7758c6b4829b389f77b4a090f2de6a

See more details on using hashes here.

File details

Details for the file lamoom_cicd-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: lamoom_cicd-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for lamoom_cicd-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9e6f89305dc450d70271d7d281333871b2e5879860b47daea4c66aa50155b017
MD5 c14cdba4264be8a3f187e22a066578e3
BLAKE2b-256 8f44afd64286967832d074592aec44bb87184cef66b5022557611af6311d3428

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page