openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Stop using connection params as global variable in the package

Open salexln opened this issue 2 years ago • 1 comments

Describe the feature or improvement you're requesting

Currently, in order to create openai connection, we need to do the following:

import openai
openai.organization = "ORG"
openai.api_key = os.getenv("KEY")

openai.Completion.create(.....)

In my use case I have Flask server with 2 endpoint, each on them uses different OpenAI credentials. With the current implementation, when the credentials are global param in the package, I cannot use the 2 endpoints simultaneously

Ideally, I want something like this:

import openai

with openai.create_connection(org, key, ...) as conn:
    conn.Completion.create(.....)

Additional context

No response

salexln avatar Mar 20 '23 19:03 salexln

@salexln It looks like you should be able to pass the api_key and organization parameters (among others) through most model APIs that inherit from OpenAIObject.

ewpatton avatar Mar 22 '23 19:03 ewpatton

@ewpatton thanks! after digging in the code I saw that I can do

import openai
openai.ChatCompletion.create(model=gpt_model_config.model_engine,
                             api_key=SECRET
                             api_base=BASE,
                             api_type=TYPE,
                             api_version=VERSION,
                             ...)

Any chance we can add it to the documentation ?

salexln avatar Mar 27 '23 07:03 salexln

We tend to leave that documentation to high level API changes. If you're interested, I'd happily accept a PR to this repo with this documentation change

hallacy avatar Mar 30 '23 04:03 hallacy

We've moved to a client instance based API in our upcoming v1 beta which addresses this request!

from openai import OpenAI

client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY'],  # this is also the default, it can be omitted
)

RobertCraigie avatar Oct 18 '23 07:10 RobertCraigie