AgentGPT icon indicating copy to clipboard operation
AgentGPT copied to clipboard

Enhancement: Option to change OpenAI API Endpoint

Open hahayusuf opened this issue 1 year ago • 16 comments

You can now set a custom API base URL through the env file using the API_BASE_URL variable. When kept blank (""), it will use the default base URL as per the previous implementation.

The README, and setup scripts were updated to incorporate the new variable.

hahayusuf avatar Apr 13 '23 13:04 hahayusuf

@hahayusuf is attempting to deploy a commit to the reworkd Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Apr 13 '23 13:04 vercel[bot]

dude, this commit is very valuable for the countries that cannot connect to openai api url

pengmaochang avatar Apr 13 '23 15:04 pengmaochang

@hahayusuf the file "setup.sh" write like this may be better

#!/bin/bash
cd "$(dirname "$0")" || exit

echo -n "Enter your OpenAI Key (eg: sk...): "
read OPENAI_API_KEY
echo -n "Enter your API_BASE_URL IS YOU NEEDED ( press enter for default): "
read API_BASE_URL

NEXTAUTH_SECRET=$(openssl rand -base64 32)

ENV="NODE_ENV=development\n\
NEXTAUTH_SECRET=$NEXTAUTH_SECRET\n\
NEXTAUTH_URL=http://localhost:3000\n\
OPENAI_API_KEY=$OPENAI_API_KEY\n\
DATABASE_URL=file:../db/db.sqlite\n\
API_BASE_URL=$API_BASE_URL\n"
...

pengmaochang avatar Apr 13 '23 16:04 pengmaochang

I am interested

Mohamedhoussein avatar Apr 14 '23 01:04 Mohamedhoussein

This is also useful for analytics and caching layers such as https://helicone.ai

jondwillis avatar Apr 16 '23 01:04 jondwillis

Hi @hahayusuf, thanks for making this PR :) this is a very useful change

I have included some feedback. Also, there will likely be merge conflicts since a lot has changed since this PR was made

Thanks for your feedback!

Will let you know when it's ready to merge.

hahayusuf avatar Apr 16 '23 21:04 hahayusuf

@Jshen123 @pengmaochang

The merge conflicts have now been resolved and can be merged automatically.

I have refactored this to use the ModelSettings interface for setting the customBaseUrl.

  • Input field created for customBaseUrl under advanced settings in settings dialog.
  • Fixed other comments related to "Copy!" message when clicking the copy button.

hahayusuf avatar Apr 18 '23 20:04 hahayusuf

@Jshen123

Thanks for your feedback.

I have added your "minor tweaks" commit.

Then I noticed some merge conflicts, which I have now resolved.

hahayusuf avatar Apr 19 '23 08:04 hahayusuf

I will take a look at this shortly!

awtkns avatar Apr 19 '23 16:04 awtkns

I have a slight security concern about allowing users to change the base url in our production environments. Doing so could potentially expose our keys if we allow them to change the baseurl + not have to input their own key. Ideally we will probably want to keep this client side if possible. Thoughts @asim-shrestha?

It is a valid concern @awtkns. We can protect against it by implementing two safeguards: -Frontend: hiding the custom url input unless a customApiKey is set. -Backend: when settings.customBaseUrl is set, env.OPENAI_API_KEY cannot be used, only customApiKey. A simple guard clause should do the trick.

hahayusuf avatar Apr 19 '23 20:04 hahayusuf

I have a slight security concern about allowing users to change the base url in our production environments. Doing so could potentially expose our keys if we allow them to change the baseurl + not have to input their own key. Ideally we will probably want to keep this client side if possible. Thoughts @asim-shrestha?

It is a valid concern @awtkns. We can protect against it by implementing two safeguards: -Frontend: hiding the custom url input unless a customApiKey is set. -Backend: when settings.customBaseUrl is set, env.OPENAI_API_KEY cannot be used, only customApiKey. A simple guard clause should do the trick.

@awtkns that's a really good callout. @hahayusuf agree with your suggested solution (only change here would be to use either disable or disable + hide)

@awtkns @asim-shrestha, if we are not comfortable with custom URL in production, perhaps we can rollback URL input feature and release the rest of the code (so users can at least use their own URLs locally). Thoughts?

Jshen123 avatar Apr 20 '23 00:04 Jshen123

Update: I have implemented both safeguards discussed.

Thanks for your feedback @Jshen123

Look forward to hearing what others think.

hahayusuf avatar Apr 20 '23 01:04 hahayusuf

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
agent-gpt ❌ Failed (Inspect) Apr 24, 2023 2:41am

vercel[bot] avatar Apr 24 '23 02:04 vercel[bot]

Hey sorry @hahayusuf, this has flown under the radar. Unfortunate because it seems we have a bunch more conflicts. I can have a look into manually fixing this stuff if you're not able.

Configuring endpoint locally via ENV seems like a good win. For endpoint in settings, given the safe guards it should be ok but maybe some unit tests are in order to validate this. Otherwise, perhaps we can provide a dropdown of options people can use. Not sure what other endpoints they might try.

asim-shrestha avatar Apr 24 '23 02:04 asim-shrestha

Hey @asim-shrestha

Thanks for offering to resolve some of the conflicts.

I agree, using env to set a custom base url should be a good enough win.

Will work on that. May need help ironing out conflicts.

hahayusuf avatar May 01 '23 18:05 hahayusuf

Awesome!

asim-shrestha avatar May 01 '23 23:05 asim-shrestha

Closed as this has been already implemented

awtkns avatar Jun 27 '23 18:06 awtkns