olrea / openai-cpp

OpenAI C++ is a community-maintained library for the Open AI API
https://openai.com/api/
MIT License
195 stars 66 forks source link

About multiple secret keys rate limit #16

Open lzonel opened 1 year ago

lzonel commented 1 year ago

If I want to use multiple sks and dynamically change sks, how should I write them because a single sk affects the rate?

coin-au-carre commented 1 year ago

@lzonel I suppose you are referring to secret keys as sks ?

Approach 1: If you want to have only one instance and manage dynamically keys

If you are conformtable with C++, you can use the undocumented method setToken on the session see https://github.com/olrea/openai-cpp/blob/61ad823ba3653d9751e1e5f60846af208858f69e/include/openai/openai.hpp#L97

I am willing to add more helpers and documentation if this something you might really need and you don't know how to proceed.

Approach 2: If you want to have several instances with different secret keys

Check out https://github.com/olrea/openai-cpp#pass-by-reference

Pass by reference

An other approach is to pass the OpenAI instance by reference, store it, and call the appropriate methods when needed.

void bar(openai::OpenAI& openai) {
    openai.completion.create({
        {"model", "text-davinci-003"},
        {"prompt", "Say bar() function called"}
    });
}

int main() {
    openai::OpenAI openai_instance{"your_api_key"};
    bar(openai_instance);
}

You can use a std::reference_wrapper as shown in examples/09-instances.cpp. This strategy is useful if you have to manage several OpenAI-CPP instances.