stanfordnlp / dspy

DSPy: The framework for programming—not prompting—foundation models
https://dspy-docs.vercel.app/
MIT License
17.36k stars 1.33k forks source link

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

Open Bobsimonoff opened 3 months ago

Bobsimonoff commented 3 months ago

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.

nikhilk7153 commented 2 months ago

Hi, I'm having the same error too. I was wondering if you had any resolution to this?