I believe that many students have used the GPT API more or less in Python, and installed openai
the library through Python to call the GPT model.
An example is given in the official OpenAI documentation, as shown in the following figure:
If you only have one API account, then you may not see any problem writing this way. But what if you want to use two accounts at the same time?
Some students may know that Microsoft Azure also provides the GPT interface, which also needs to be called through the library in Python openai
. Its calling example is:
After you set it up globally openai.api_type = 'azure'
, how do you use OpenAI's GPT interface at the same time?
The example writing methods given in these two documents are all global writing methods. Once set, the parameters set here will be used in all places where the GPT interface is called during the entire runtime:
import openai
openai.xx = yy
Some students don't know how to use multiple accounts in the Python SDK at the same time, so they only use GPT's Rest HTTP interface to encapsulate a function to initiate a request to switch between different accounts. Giving up all the conveniences provided by the Python SDK.
But in reality, it's not that much trouble at all. In openai
the module, it is natural to switch between multiple accounts. Although it is not written in the document, we can find this method through the function signature.
As shown in the figure below, in PyCharm, just write a piece of openai
code that calls the module, then Windows presses the keyboard’s Ctrl, MacOS presses the keyboard’s Command, and clicks create
the function with the left mouse button:
In the jump function, there is another create
function, continue to jump in according to the above method, as shown in the figure below:
Next, you will see that create
the parameters that this function can accept include several familiar names:
In other words, when you want to call multiple accounts at the same time, you don't need to set the corresponding parameters for openai at the beginning, you only need to .create
pass in the corresponding API parameters when calling the function. The sample code is as follows:
import openai
# 使用OpenAI账号1
response1 = openai.ChatCompletion.create(
engine="chatgpt",
messages=messages,
temperature=0.9,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
api_key='xxxxxxxx', # 在这里传入API Key
stop=["<|im_end|>"])
# 使用OpenAI账号2
response2 = openai.ChatCompletion.create(
engine="chatgpt16k",
messages=messages,
temperature=0.9,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
api_key='yyyyyyyyy', # 在这里传入API Key
stop=["<|im_end|>"])
# 使用Azure OpenAI 账号
response3 = openai.ChatCompletion.create(
engine="gpt4",
messages=messages,
temperature=0.9,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
api_key='zzzzzzz', # 在这里传入API Key
api_base='https://xxx.openai.azure.com/',
api_type="azure",
api_version='2023-05-15',
stop=["<|im_end|>"])
Using this method, we can use multiple GPT accounts in one program at the same time.