-
Notifications
You must be signed in to change notification settings - Fork 65
fix: text analysis config and docs #472
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
Open
manuelporrasojeda
wants to merge
7
commits into
master
Choose a base branch
from
fix/assert_text_criteria_doc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
643031e
text analysis config and docs
manuelporrasojeda d53a2bb
import order
manuelporrasojeda dfc00a1
config to openai method
manuelporrasojeda 63020d4
use set default to allow overwrite config
manuelporrasojeda c0de81f
fixes to openai defaults
manuelporrasojeda 2b19eb7
fix model to kwargs
manuelporrasojeda f4d3e51
Update docs/ai_utils.rst
manuelporrasojeda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,8 +76,8 @@ For SpaCy, you also need to download the language model, i.e. for small English | |
|
|
||
| python -m spacy download en_core_web_sm | ||
|
|
||
| For OpenAI LLM, you need to set up your configuration in environment variables, that it may depend on the type of access | ||
| you have (direct OpenAI access or Azure OpenAI): | ||
| For OpenAI LLM, you need to set up your configuration using environment variables or include it in the Toolium configuration. | ||
| The required parameters will vary depending on your access type (direct OpenAI or Azure OpenAI): | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
|
|
@@ -91,6 +91,110 @@ you have (direct OpenAI access or Azure OpenAI): | |
| AZURE_OPENAI_ENDPOINT=<your_endpoint> | ||
| OPENAI_API_VERSION=<your_api_version> | ||
|
|
||
| *[AI]* section:: | ||
|
|
||
| [AI] | ||
| text_similarity_method: azure_openai | ||
| azure_endpoint: https://your-endpoint.azure.com | ||
| api_version: 2025-01-01-preview | ||
| azure_deployment: gpt-4o-mini | ||
|
|
||
|
|
||
| Text Criteria Analysis | ||
| ---------------------- | ||
|
|
||
| Text criteria analysis evaluates how well an input text matches a set of target characteristics | ||
| (e.g., tone, style, clarity, domain vocabulary, specific described content, etc) using an LLM. | ||
| Toolium provides utilities to both retrieve a structured analysis and assert quality thresholds. | ||
|
|
||
| Usage | ||
| ~~~~~ | ||
|
|
||
| You can use functions from the `toolium.utils.ai_utils.text_analysis` module: | ||
|
|
||
| * **get_text_criteria_analysis()**: returns a JSON string with an overall score and low-scored criteria. | ||
| * **assert_text_criteria()**: validates that the overall score is above a threshold and raises `AssertionError` otherwise. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from toolium.utils.ai_utils.text_analysis import get_text_criteria_analysis, assert_text_criteria | ||
| import json | ||
|
|
||
| input_text = "Hey team! Quick heads-up: deployment is done, all checks green." | ||
| text_criteria = [ | ||
| "professional tone", | ||
| "clear and concise message", | ||
| "it includes the status of the deployment" | ||
| ] | ||
|
|
||
| # Get analysis (JSON string) | ||
| raw_analysis = get_text_criteria_analysis( | ||
| text_input=input_text, | ||
| text_criteria=text_criteria, | ||
| model_name="gpt-4o-mini", # optional | ||
| azure=False # True for Azure OpenAI | ||
| ) | ||
|
|
||
| analysis = json.loads(raw_analysis) | ||
| print(analysis["overall_match"]) | ||
| print(analysis["features"]) # Only low-scored features (<= 0.2) | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from toolium.utils.ai_utils.text_analysis import assert_text_criteria | ||
|
|
||
| # Assert text quality against a minimum score | ||
| assert_text_criteria( | ||
| text_input="This message should be short, formal and informative.", | ||
| text_criteria=["formal tone", "brevity", "informative content"], | ||
| threshold=0.75, | ||
| model_name="gpt-4o-mini", # optional | ||
| azure=False # set True for Azure OpenAI | ||
| ) | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. después de Usage, añade una sección de Configuration, igual que en 'Text Similarity', para poner las variables de configuración |
||
|
|
||
| Configuration | ||
| ~~~~~~~~~~~~~ | ||
|
|
||
| Default text analysis method can be set in the properties.cfg file with the property *text_analysis_method* in | ||
| *[AI]* section:: | ||
|
|
||
| [AI] | ||
| text_analysis_method: openai # Options: 'openai', 'azure_openai' (default: azure_openai) | ||
|
|
||
|
|
||
| Response Format | ||
| ~~~~~~~~~~~~~~~ | ||
|
|
||
| `get_text_criteria_analysis()` expects the LLM to return a JSON object with: | ||
|
|
||
| .. code-block:: json | ||
|
|
||
| { | ||
| "overall_match": 0.82, | ||
| "features": [ | ||
| { | ||
| "name": "formal tone", | ||
| "score": 0.15 | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| Where: | ||
|
|
||
| - `overall_match`: float in `[0.0, 1.0]` | ||
| - `features`: list containing only low-scored criteria (score `<= 0.2`) | ||
|
|
||
| Assertion Behavior | ||
| ~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| `assert_text_criteria()`: | ||
|
|
||
| - parses the JSON response returned by `get_text_criteria_analysis()` | ||
| - compares `overall_match` with the provided `threshold` | ||
| - raises `AssertionError` if `overall_match < threshold` | ||
| - logs low-scored features to help diagnose why validation failed | ||
|
|
||
|
|
||
| Text Readability | ||
| ---------------- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no está bien formateado: https://github.com/Telefonica/toolium/blob/dfc00a183f924da7dc73ee15df158264a9d07ffd/docs/ai_utils.rst
creo que le falta una línea en blanco