dspy icon indicating copy to clipboard operation
dspy copied to clipboard

Support for setting OpenAI project field in addition to api_key? (I would be happy to send PR; not requesting work from team)

Open willy-b opened this issue 5 months ago • 3 comments

Hello, Thanks for a great project.

In https://platform.openai.com/settings/ , OpenAI allows the configuration (API keys, rate limits, usage, and allowed models, etc) to be grouped by Projects, which have IDs. Screenshot_20240919_132005

The current DSPy OpenAI adapter dsp/modules/gpt3.py (named gpt3.py but supports many OpenAI models like gpt4, and even o-1 as of https://github.com/stanfordnlp/dspy/commit/928068e866a9d13c42cefa851871841c42e6b5fb ) obviously allows passing one's API key when instantiating it: https://github.com/stanfordnlp/dspy/blob/2cc029b03e18fe3bc05b5131ef3e02390af895b3/dsp/modules/gpt3.py#L59

However, DSPy's OpenAI adapter does not currently seem to allow specifying a particular project. One can use project specific API keys to work around this pretty easily but it also just a couple of lines code change to allow picking up a project argument from the kwargs, something like:

if "project" in self.kwargs:
            openai.project = self.kwargs["project"]
            del self.kwargs["project"]

added right AFTER self.kwargs is defined in __init__ at https://github.com/stanfordnlp/dspy/blob/2cc029b03e18fe3bc05b5131ef3e02390af895b3/dsp/modules/gpt3.py#L98 will do the trick (tested and verified by me). add_project_support_manually

Any interest in a quick PR from me (or your team if preferred) to allow users who have a single multi-project API key to specify which project their DSPy activity in that particular code execution should be tracked against?

Thanks again for an awesome project!

(Credit to Stanford XCS224U for introducing me to DSPy)

willy-b avatar Sep 19 '24 17:09 willy-b