Skip to content
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

Triggering the Azure OpenAi (GPT 4) Triggers content warning and DSPy exception #1183

Open
Bobsimonoff opened this issue Jun 20, 2024 · 0 comments

Comments

@Bobsimonoff
Copy link

Bobsimonoff commented Jun 20, 2024

The code toward the end throws this exception:

Traceback (most recent call last):
  File "/Users/bobsimonoff/dspy/pepscico.py", line 49, in <module>
    final_pred = fixBrandMultiChain(all_preds, manufacturer="PepsiCo, Inc.", brand=brand)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bobsimonoff/dspy/.venv/lib/python3.12/site-packages/dspy/primitives/program.py", line 26, in __call__
    return self.forward(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bobsimonoff/dspy/.venv/lib/python3.12/site-packages/dspy/predict/multi_chain_comparison.py", line 54, in forward
    return self.predict(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bobsimonoff/dspy/.venv/lib/python3.12/site-packages/dspy/predict/predict.py", line 61, in __call__
    return self.forward(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bobsimonoff/dspy/.venv/lib/python3.12/site-packages/dspy/predict/predict.py", line 103, in forward
    x, C = dsp.generate(template, **config)(x, stage=self.stage)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bobsimonoff/dspy/.venv/lib/python3.12/site-packages/dsp/primitives/predict.py", line 120, in do_generate
    return generate(template, **new_kwargs)(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bobsimonoff/dspy/.venv/lib/python3.12/site-packages/dsp/primitives/predict.py", line 78, in do_generate
    completions: list[Example] = [template.extract(example, p) for p in completions]
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bobsimonoff/dspy/.venv/lib/python3.12/site-packages/dsp/templates/template_v2.py", line 152, in extract
    raw_pred = raw_pred.strip()
               ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'strip'

Code:


import dspy
import os
import logging

# Configuration for logging
logging.basicConfig(
    filename='dspy-app.log',
    filemode='a',
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s'
)


api_key = os.getenv("AZURE_OPENAI_KEY")

turbo = dspy.AzureOpenAI(api_base="https://jdpaipoc.openai.azure.com/",
                         api_version="2024-02-15-preview",
                         model="gpt4",
                         api_key=api_key,
                         model_type="chat",
                         max_tokens=4000)

dspy.settings.configure(lm=turbo)

class FixBrand(dspy.Signature):
    """
    Output the correct brand name followed by ~ followed by the correct top-level corporate entity that owns the brand.
    If the correct brand can not be determined, output INDETERMINATE for the brand
    """

    brand=dspy.InputField()
    manufacturer=dspy.InputField()
    answer=dspy.OutputField(desc="[Correct brand name|INDETERMINATE]~<Correct manufacturer name>")

brandChain = dspy.ChainOfThought(FixBrand, n=5)

# for brand in ["naked pro", "naked juice"]:
for brand in ["naked pro"]:
    preds = brandChain(manufacturer="PepsiCo, Inc.", brand=brand)

    all_preds = []
    for pred in preds.completions:
#        print(f"#    Rationale: {pred.rationale}")
        print(f"#    Answer: {pred.answer}")
        all_preds.append(dspy.primitives.prediction.Prediction(rationale=pred.rationale, answer=pred.answer))
        
    fixBrandMultiChain = dspy.MultiChainComparison(FixBrand, M=len(all_preds))
    if fixBrandMultiChain is not None:
        final_pred = fixBrandMultiChain(all_preds, manufacturer="PepsiCo, Inc.", brand=brand)
        print(final_pred)
    else:
        print("We did not get a fixBrandMultiChain")

When I paste the generated prompt into the Azure OpenAI console it triggers the sexual content warning. While the content warning isn't DSPy's issue, the exception should probably be handled better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant