Is there any way to pass the user’s key to API calls so that it overrides the one in configuration?
The idea is that, if I develop a SaaS service using TaskingAI, to be able to offer users either the option of using my API key, paying a subscription, or even using the service for free with their API keys.
I don’t seem to have seen anything among the examples and code on Github, so in that case could I have some suggestions for implementing this?
I’m not entirely sure if I understood you correctly. Are you referring to the TASKING_API_KEY? If you are developing with Python, you can use taskingai.init(api_key=TASKING_API_KEY) to configure the TASKING_API_KEY independently. This variable can be read from the environment or obtained from your users. Relevant documentation: Quickstart | TaskingAI
If you are referring to the model’s API key, then TaskingAI natively supports users entering their own API key.
I talk about the API model key, I give an example.
I registered for the free hosted version, and I created an OpenAI gpt-4o model using my API key.
Tomorrow I create a web app that does something, using this model. I make my app usable for free to other users. But this way those who use the tool, they will be using my API key and I would be paying for them.
What I would like is to be able to use the Tasking API, choosing the model I created, but overriding the key used by that model.
So users in the app enter their API keys, and when the app runs it calls the Tasking API using the model I created, but with their API keys instead of mine that I entered when I created the model.
Now I understand your needs. An ideal approach would be for you to create a model for each user using their key, rather than using a single model to manage all users.
Currently, TaskingAI allows credentials to be updated for individual models only on the cloud platform.
I was thinking of doing something like this by modifying the backend:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_TASKINGAI_API_KEY",
user_model_key="MODEL_API_KEY_PROVIDED_FROM_USER"
base_url="https://oapi.tasking.ai/v1",
)
response = client.chat.completions.create(
model="YOUR_TASKINGAI_MODEL_ID",
messages=[
{"role": "user", "content": "Hello, how are you?"},
]
)
when the parameter user_model_key is passed, the backend should use that to call the requested model, instead of the credentials associated with the model configuration.
This doesn’t seem like a complex change to make on the open-source backend code. But I may be wrong, because I have no much experience with Python, so a hint on where exactly to go about doing it would be very helpful