Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /promptdriven_pdd /context /generation_completion_example.py
| """Example usage for deterministic generation completion helpers.""" | |
| # pylint: disable=wrong-import-position | |
| import sys | |
| from pathlib import Path | |
| # Prefer this checkout when the example is run directly from another directory. | |
| REPO_ROOT = Path(__file__).resolve().parents[1] | |
| if str(REPO_ROOT) not in sys.path: | |
| sys.path.insert(0, str(REPO_ROOT)) | |
| from pdd.generation_completion import ( | |
| completion_check_tail, | |
| output_is_structurally_complete, | |
| provider_finished_structurally, | |
| provider_reports_completion, | |
| ) | |
| def main(): | |
| """ | |
| This example demonstrates how to verify if an LLM's response is both | |
| technically finished by the provider and structurally valid (e.g., valid Python/JSON). | |
| """ | |
| # 1. Example: Validating a Python Code Generation | |
| # The LLM claims it finished ('stop') and provided a fenced Python block. | |
| python_output = """```python | |
| def greet(name): | |
| return f"Hello, {name}!" | |
| ```""" | |
| python_finish_reason = "stop" | |
| is_complete_py = provider_finished_structurally( | |
| output=python_output, | |
| finish_reason=python_finish_reason, | |
| language="python", | |
| ) | |
| print(f"Python completion valid: {is_complete_py}") | |
| # 2. Example: Validating JSON Structural Integrity | |
| # If the provider says 'length' or None, it means it was truncated. | |
| json_output = '{"name": "Assistant", "status": "runn' # Incomplete JSON | |
| json_finish_reason = "length" | |
| is_complete_json = provider_finished_structurally( | |
| output=json_output, | |
| finish_reason=json_finish_reason, | |
| language="json", | |
| ) | |
| print( | |
| f"JSON completion valid: {is_complete_json} " | |
| "(Expected False due to 'length' reason)" | |
| ) | |
| # 3. Example: Checking Provider Reason Independently | |
| # Normalizes strings like 'end-turn' or 'COMPLETED' | |
| reason = "end-turn" | |
| reports_ok = provider_reports_completion(reason) | |
| print(f"Provider reports success for '{reason}': {reports_ok}") | |
| # 4. Example: Direct Structural Check (Markdown Fences) | |
| # For generic languages, it checks if the code fence was closed. | |
| markdown_text = "```sql\nSELECT * FROM users;\n```" | |
| struct_ok = output_is_structurally_complete(markdown_text, "sql") | |
| print(f"SQL Markdown fence closed: {struct_ok}") | |
| # 5. Example: Tail Extraction for Logging | |
| # Useful for showing the end of a long generation in logs without flooding the console. | |
| long_text = "Line 1\nLine 2\n" + ("Data...\n" * 50) + "Final Line of Result" | |
| tail = completion_check_tail(long_text, max_chars=50) | |
| print("--- Tail of generation ---") | |
| print(tail) | |
| print("--------------------------") | |
| # Detailed Parameter Descriptions: | |
| # provider_finished_structurally(output, finish_reason, language): | |
| # - output (Any): Usually the raw string generated by the LLM. | |
| # - finish_reason (Any): The reason returned by the LLM API (string, list, or None). | |
| # - language (str): The target language ('python', 'json', etc.) for structural parsing. | |
| # - Returns: bool (True if both provider says finished AND structure is valid). | |
| # | |
| # completion_check_tail(text, max_chars): | |
| # - text (str): The full generation text. | |
| # - max_chars (int): The maximum number of characters to return (default 600). | |
| # - Returns: str (The end of the text, aligned to the nearest newline if possible). | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 3.42 kB
- Xet hash:
- c3439294786be0ef290449706f452237c1b13fce6515d02137b30915208b152e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.