stanfordnlp / dspy

DSPy: The framework for programming—not prompting—language models
https://dspy.ai
MIT License
18.84k stars 1.44k forks source link

How to handle Exceptions when calling Azure OpenAI model API #1326

Open sartyagi91 opened 3 months ago

sartyagi91 commented 3 months ago

I am currently working on a project that labels the sentiment of a response, For a response the API results in a BadRequestError due to Azure's content policy management. I am trying to catch this exception by handling it via a try-and-catch block but It isn't getting caught and I get the error repeatedly as it retries sending that response to API indefinitely. I am using DSPY to abstract away the prompting techniques but I am curious about how I can access the payload to handle exceptions from the API calls to Azure OpenAI models

nslaughter commented 2 months ago

Hi, @okhat. Can you share the relevant code? There's a good chance I can resolve that, but without some code to refer to it would be hard to say. I'm interested in the indefinite retries as well, but again it would be hard to help without some reference to what your code is doing now.

sartyagi91 commented 2 months ago

Hey Unfortunately I cannot share the code as it is with a company I am working with it

okhat commented 1 month ago

Thanks for opening this! We released DSPy 2.5 yesterday. I think the new dspy.LM and the underlying dspy.ChatAdapter will probably resolve this problem.

Here's the (very short) migration guide, it should typically take you 2-3 minutes to change the LM definition and you should be good to go: https://github.com/stanfordnlp/dspy/blob/main/examples/migration.ipynb

Please let us know if this resolves your issue. I will close for now but please feel free to re-open if the problem persists.