Skip to main content

Check if a competitor is named

!!! note To download this example as a Jupyter notebook, click here.

We first need to install the CompetitorCheck validator from Guardrails Hub:

pip install nltk --quiet
guardrails hub install hub://guardrails/competitor_check --quiet
    Installing hub://guardrails/competitor_check...
✅Successfully installed guardrails/competitor_check!


from guardrails.hub import CompetitorCheck
import guardrails as gd
from rich import print

Using competitor check validator

This validator checks LLM output to flag sentences naming one of your competitors and removes those sentences from the final output. When setting on-fail to 'fix' this validator will remove the flagged sentences from the output. You need to provide an extensive list of your competitors' names including all common variations (e.g. JP Morgan, JP Morgan Chase, etc.) the compilation of this list will have an impact on the ultimate outcome of the validation.

Set up a competitors list

# Generate competitors list
competitors_list = [
"Acorns",
"Citigroup",
"Citi",
"Fidelity Investments",
"Fidelity",
"JP Morgan Chase and company",
"JP Morgan",
"JP Morgan Chase",
"JPMorgan Chase",
"Chase" "M1 Finance",
"Stash Financial Incorporated",
"Stash",
"Tastytrade Incorporated",
"Tastytrade",
"ZacksTrade",
"Zacks Trade",
]

Set up example text to test the validator

# Define some text to test the validator
text = """\
In the dynamic realm of finance, several prominent entities have emerged as key players,\
leaving an indelible mark on the industry. Acorns, a fintech innovator, has revolutionized saving \
and investing with its user-friendly app. Citigroup, a multinational investment bank, stands as a \
pillar of financial expertise, offering a wide array of services to clients worldwide. HSBC, with \
its extensive global network, has become a powerhouse in the banking sector, catering to the needs \
of millions across different countries. JP Morgan, a venerable institution with a rich history, has \
established itself as a comprehensive financial powerhouse, providing a diverse range of services \
from investment banking to asset management. Santander, a Spanish multinational bank, has earned a \
reputation for its responsible banking practices and customer-centric approach, serving as a trusted \
financial partner to individuals and businesses alike. Together, Acorns, Citi, HSBC, JP Morgan, and \
Santander have redefined the financial landscape, shaping the way we save, invest, and manage our \
money on a global scale.\
"""

Test the validator

Here, we use the text we defined above as an example llm output (llm_output).

We also set the on_fail behavior to 'fix' so that the validator will remove the sentences that mention competitors from the output. We can adjust this behavior by changing it to 'reask' or 'throw'.

# Create the Guard with the CompetitorCheck Validator
guard = gd.Guard().use(
CompetitorCheck(competitors=competitors_list, on_fail="fix")
)

# Test with a given text
output = guard.parse(
llm_output=text,
metadata={},
)

print(output)
/Users/dtam/dev/guardrails/guardrails/validator_service/__init__.py:85: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.
warnings.warn(




ValidationOutcome(
call_id='14233082752',
raw_llm_output='In the dynamic realm of finance, several prominent entities have emerged as key players,leaving
an indelible mark on the industry. Acorns, a fintech innovator, has revolutionized saving and investing with its
user-friendly app. Citigroup, a multinational investment bank, stands as a pillar of financial expertise, offering
a wide array of services to clients worldwide. HSBC, with its extensive global network, has become a powerhouse in
the banking sector, catering to the needs of millions across different countries. JP Morgan, a venerable
institution with a rich history, has established itself as a comprehensive financial powerhouse, providing a
diverse range of services from investment banking to asset management. Santander, a Spanish multinational bank, has
earned a reputation for its responsible banking practices and customer-centric approach, serving as a trusted
financial partner to individuals and businesses alike. Together, Acorns, Citi, HSBC, JP Morgan, and Santander have
redefined the financial landscape, shaping the way we save, invest, and manage our money on a global scale.',
validated_output='In the dynamic realm of finance, several prominent entities have emerged as key
players,leaving an indelible mark on the industry.[COMPETITOR], a fintech innovator, has revolutionized saving and
investing with its user-friendly app.[COMPETITOR], a multinational investment bank, stands as a pillar of financial
expertise, offering a wide array of services to clients worldwide.HSBC, with its extensive global network, has
become a powerhouse in the banking sector, catering to the needs of millions across different
countries.[COMPETITOR], a venerable institution with a rich history, has established itself as a comprehensive
financial powerhouse, providing a diverse range of services from investment banking to asset management.Santander,
a Spanish multinational bank, has earned a reputation for its responsible banking practices and customer-centric
approach, serving as a trusted financial partner to individuals and businesses alike.Together, [COMPETITOR],
[COMPETITOR], HSBC, [COMPETITOR], and Santander have redefined the financial landscape, shaping the way we save,
invest, and manage our money on a global scale.',
reask=None,
validation_passed=True,
error=None
)
# See guard history
print(guard.history.last.tree)
Logs
└── ╭────────────────────────────────────────────────── Step 0 ───────────────────────────────────────────────────╮
╭─────────────────────────────────────────────── Messages ────────────────────────────────────────────────╮
│ No messages. │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── Raw LLM Output ─────────────────────────────────────────────╮
│ In the dynamic realm of finance, several prominent entities have emerged as key players,leaving an │
│ indelible mark on the industry. Acorns, a fintech innovator, has revolutionized saving and investing │
│ with its user-friendly app. Citigroup, a multinational investment bank, stands as a pillar of financial │
│ expertise, offering a wide array of services to clients worldwide. HSBC, with its extensive global │
│ network, has become a powerhouse in the banking sector, catering to the needs of millions across │
│ different countries. JP Morgan, a venerable institution with a rich history, has established itself as │
│ a comprehensive financial powerhouse, providing a diverse range of services from investment banking to │
│ asset management. Santander, a Spanish multinational bank, has earned a reputation for its responsible │
│ banking practices and customer-centric approach, serving as a trusted financial partner to individuals │
│ and businesses alike. Together, Acorns, Citi, HSBC, JP Morgan, and Santander have redefined the │
│ financial landscape, shaping the way we save, invest, and manage our money on a global scale. │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────── Validated Output ────────────────────────────────────────────╮
│ 'In the dynamic realm of finance, several prominent entities have emerged as key players,leaving an │
│ indelible mark on the industry.[COMPETITOR], a fintech innovator, has revolutionized saving and │
│ investing with its user-friendly app.[COMPETITOR], a multinational investment bank, stands as a pillar │
│ of financial expertise, offering a wide array of services to clients worldwide.HSBC, with its extensive │
│ global network, has become a powerhouse in the banking sector, catering to the needs of millions across │
│ different countries.[COMPETITOR], a venerable institution with a rich history, has established itself │
│ as a comprehensive financial powerhouse, providing a diverse range of services from investment banking │
│ to asset management.Santander, a Spanish multinational bank, has earned a reputation for its │
│ responsible banking practices and customer-centric approach, serving as a trusted financial partner to │
│ individuals and businesses alike.Together, [COMPETITOR], [COMPETITOR], HSBC, [COMPETITOR], and │
│ Santander have redefined the financial landscape, shaping the way we save, invest, and manage our money │
│ on a global scale.' │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯