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.1.tar.gz (6.3 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.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lamoom_cicd-0.1.1.tar.gz
  • Upload date:
  • Size: 6.3 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.1.tar.gz
Algorithm Hash digest
SHA256 bfb8f4a0b28fb4772161b529eae26d4ebab910bb5227459cdaec63bce2744a3f
MD5 272b6f75878c6c4ec6b77ef7ad88d776
BLAKE2b-256 213f040e4fa3e1a3906635fedb7226bffed332c6ff4d76f8a312154147363444

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lamoom_cicd-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d059b4b98b70432125f895a74c5e72d8ea2ee141fe0bd4bb5f84cea31941e742
MD5 cb7ad137aecd9addb350b8309302609f
BLAKE2b-256 16764486fa71a0489bb6baa2d95d87e9c14fc61fbc8460b947402360bd73baee

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