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.3.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.3-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lamoom_cicd-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 ccebbb6f01b629d4065a4c1e900904b5e1629c93e0ae97faae1c2b36d58f1710
MD5 40c63ccb0c65f2ea5b2bfac22b4689a0
BLAKE2b-256 076c4c905a648757f296a7aaebe55c9d80fe863f21fbe32a98d695e7b8101bd2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lamoom_cicd-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 79e06f9734556f2a8a659eca597f838faff3b670a8eb9eba53f3e58516a5fd3d
MD5 993633e134dcbf330b90fe3201390ccf
BLAKE2b-256 832623dcab4767fb2a0a9fc1f7f8599ad4ac7f372c5d8d7a6aac47411f05b8f2

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