Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creation of master API keys to be shared across users #236

Closed
krschacht opened this issue Mar 24, 2024 · 8 comments
Closed

Allow creation of master API keys to be shared across users #236

krschacht opened this issue Mar 24, 2024 · 8 comments
Assignees
Milestone

Comments

@krschacht
Copy link
Contributor

krschacht commented Mar 24, 2024

Some people want to setup an instance of HostedGPT and let a couple special people use it. In this scenario, they don’t want everyone to have to enter their own API keys they want to let them just piggy back on some master API keys and foot the bill. In this scenario, my assumption was: we could read the API keys out of some central config.

If we implemented it this way then we would want to allow trusted people to register but prevent random people from registering. Maybe there needs to be an option to set a registration code? And, when I tell a trusted friend my registration code and they sign up, if that friend goes to their account settings how should the API key fields behave? Maybe the fields should still be there so they can input their own keys instead of using the master ones. Probably that’s the case.

Note: We’ll need some way to lock down user registration once this is implemented.

@krschacht krschacht added this to the 0.8 milestone Mar 24, 2024
@krschacht
Copy link
Contributor Author

@olimart I mentioned this in a PR, but I’m pulling it out into a real issue. FYI in case you want to tackle it.

@olimart
Copy link
Contributor

olimart commented Mar 28, 2024

Food for thoughts:

  • Implement a new model ApiKey with type/provider Claude, ChatGPT,... & shared attribute.
  • Update logic for current user key to become user's key || shared ApiKey

This implies:

  • To keep fields on user settings to override shared key
  • New admin/api_keys controller & views to manage keys
    • Add admin attribute to the very first user created to restrict keys management
  • Migrate user keys to the new ApiKey model
  • Eventually, new onboarding screen to input API key(s)

@krschacht
Copy link
Contributor Author

I like how you’re thinking. What you laid out makes sense as the place to end up. My inclination would be to get there in a least a couple PRs. One PR could lay down the core logic and just use an ENV as the fallback “shared key” and then a second PR could implement the new admin views & routes.

Want to take this one?

@krschacht krschacht modified the milestones: 0.8, 0.7 Apr 20, 2024
@krschacht krschacht changed the title Allow creation of master API keys Allow creation of master API keys to be shared across users Apr 20, 2024
@krschacht krschacht changed the title Allow creation of master API keys to be shared across users Allow creation of master API keys to be shared across users & add some token limits & cost tracking May 7, 2024
@stephan-buckmaster
Copy link
Contributor

Has this got anywhere? More api-key configurations coming with my next changes, for #341

@krschacht
Copy link
Contributor Author

Nope, no progress on this one so you're safe to changes in this arena

@matthewbennink
Copy link
Contributor

This is interesting for our use case where we'll have a handful of students who are learning about AI chat interfaces. We'll be providing the API keys and yet we want to be sure we can keep usage within reasonable limits on a per-user basis.

As for cost tracking, is that something like keeping track of token usage per conversation? It seems like it'd be hard to do per message, especially if there's a moment where HostedGPT is unable to send all of the preceding messages, but instead sends summarized messages or perhaps only the last N messages of a conversation to keep the token count underneath the max_tokens limit. Or were you imagining tracking in terms of USD based on a pricing table?

Also, fwiw, we have a very small change that uses two env variables: DEFAULT_OPENAI_KEY and DEFAULT_ANTHROPIC_KEY. I suppose those names could be changed. We've also got REGISTRATION_FEATURE set to false to ensure we're in control of user account creation. I'd be happy to open a PR that adds those couple of ENV variables.

@krschacht
Copy link
Contributor Author

krschacht commented Jun 10, 2024

@matthewbennink My initial thought on cost tracking was:

  • Add a column to the messages table such as estimated_cost
  • At the time of new message creation we count the number of tokens and update this on the message
  • Then in various places such as account settings we can estimate total $ incurred this month with a simple Message.created_after(Date.beginning_of_month).sum(estimated_cost)
  • I think we'd need some cost-per-token added to our LanguageModel model
  • Then within backend/anthropic & backend/open_ai when we send the API call to the server, in that moment we count how many tokens we are sending (I have the tiktoken ruby gem already in the app for doing this). At this point, the new message currently is blank but we go ahead and save a preliminary estimated_cost based on how many tokens we're sending.
  • Then in get_next_message_job near the bottom where we do the final message.save we count the total number of tokens received back from the API (I think it's just Tiktoken.count(message.content_text) and += to add that onto the preliminary estimated_cost

@krschacht krschacht changed the title Allow creation of master API keys to be shared across users & add some token limits & cost tracking Allow creation of master API keys to be shared across users Jun 11, 2024
@krschacht
Copy link
Contributor Author

Closing this and opening #402 as a placeholder for token cost tracking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants