marineenergy / ba

process Biological Assessments using AI
https://marineenergy.github.io/ba/
MIT License
0 stars 0 forks source link

get ChatGPT working again with new OPENAI_API_KEY #1

Open bbest opened 3 months ago

bbest commented 3 months ago

Hi @jaschallert,

I'm going to log here so there's a record for future. For the Edit-BA app, the OPENAI_API_KEY gets used for calling ChatGPT and supplying tags to a user-submitted excerpt in the form like so:

image The user chekcs Auto Tag (with OpenAI GPT) and after clicking Save tags from the excerpt are added by ChatGPT.

Here are the most succinct links for this process that I previously provided over email:

marineenergy/ba: tag_excerpt_gpt.py called by Shiny app shiny.marineenergy.app/edit-ba (source: global.R)

Let's look at this in more detail to try and update the OPENAI_API_KEY so as to get this working again...

bbest commented 3 months ago

So the tag_excerpt_gpt.py is referenced by the Edit-BA's app's global.R (line 31):

gpt_py       <- "/share/github/ba/tag_excerpt_gpt.py"

And that gets called from the Shiny app when the ck_gpt is True in global.R (lines 159-168):

if (d$ck_gpt){
    tmp_txt <- tempfile(fileext=".txt")
    writeLines(d$excerpt, tmp_txt)
    cmd <- glue('python "{gpt_py}" "{tmp_txt}" "{d$gpt_version}"')
    message(cmd)
    res <- system(cmd, intern = T)
    unlink(tmp_txt)
    if (!str_detect(res, "Error|Sorry|sorry"))
      d$tag_named[[1]] <- union(str_split(res, ", ")[[1]], d$tag_named[[1]])
  }

So let's create a test excerpt and run the command:

# change directory to Shiny app
cd /share/github/ba

# create test excerpt
echo 'whales dolphins noise' > /tmp/excerpt.txt

# run command
python tag_excerpt_gpt.py /tmp/excerpt.txt 4

Response

Error with ChatGPT request: The model `gpt-4` does not exist or you do not have access to it.

Ok, so let's look at tag_excerpt_gpt.py file where the OPENAI_API_KEY gets loaded from the .env file in the same folder.

https://github.com/marineenergy/ba/blob/4cb9df32d266a5a8c1ac0b3a415dc96e3b5c1736/tag_excerpt_gpt.py#L89-L90

Looking at the .env, I can see that it has not been updated with your latest emailed OPENAI_API_KEY:

# change directory
cat /share/github/ba

# show .env
cat .env

# edit .env
vi .env

After inserting my best translation from the image of the new value for OPENAI_API_KEY you shared with me over email, it still doesn't seem to work, but I do get a different response:

python tag_excerpt_gpt.py /tmp/test.txt 4

Response:

Error with ChatGPT request: Incorrect API key provided: skvMnRe***************************************oLi9. You can find your API key at https://platform.openai.com/account/api-keys.
bbest commented 3 months ago

Models got renamed

curl https://api.openai.com/v1/models -H "Authorization: Bearer $OPENAI_API_KEY"
{
  "object": "list",
  "data": [
    {
      "id": "dall-e-3",
      "object": "model",
      "created": 1698785189,
      "owned_by": "system"
    },
    {
      "id": "whisper-1",
      "object": "model",
      "created": 1677532384,
      "owned_by": "openai-internal"
    },
    {
      "id": "dall-e-2",
      "object": "model",
      "created": 1698798177,
      "owned_by": "system"
    },
    {
      "id": "tts-1-hd-1106",
      "object": "model",
      "created": 1699053533,
      "owned_by": "system"
    },
    {
      "id": "tts-1-hd",
      "object": "model",
      "created": 1699046015,
      "owned_by": "system"
    },
    {
      "id": "tts-1",
      "object": "model",
      "created": 1681940951,
      "owned_by": "openai-internal"
    },
    {
      "id": "gpt-4o-mini-2024-07-18",
      "object": "model",
      "created": 1721172717,
      "owned_by": "system"
    },
    {
      "id": "babbage-002",
      "object": "model",
      "created": 1692634615,
      "owned_by": "system"
    },
    {
      "id": "gpt-4o-mini",
      "object": "model",
      "created": 1721172741,
      "owned_by": "system"
    },
    {
      "id": "text-embedding-3-small",
      "object": "model",
      "created": 1705948997,
      "owned_by": "system"
    },
    {
      "id": "text-embedding-3-large",
      "object": "model",
      "created": 1705953180,
      "owned_by": "system"
    },
    {
      "id": "tts-1-1106",
      "object": "model",
      "created": 1699053241,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo",
      "object": "model",
      "created": 1677610602,
      "owned_by": "openai"
    },
    {
      "id": "gpt-3.5-turbo-instruct",
      "object": "model",
      "created": 1692901427,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo-instruct-0914",
      "object": "model",
      "created": 1694122472,
      "owned_by": "system"
    },
    {
      "id": "text-embedding-ada-002",
      "object": "model",
      "created": 1671217299,
      "owned_by": "openai-internal"
    },
    {
      "id": "gpt-3.5-turbo-16k",
      "object": "model",
      "created": 1683758102,
      "owned_by": "openai-internal"
    },
    {
      "id": "davinci-002",
      "object": "model",
      "created": 1692634301,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo-0125",
      "object": "model",
      "created": 1706048358,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo-1106",
      "object": "model",
      "created": 1698959748,
      "owned_by": "system"
    }
  ]
}

Trying model 3.5

Trying in Terminal of https://rstudio.marineenergy.app

cd /share/github/ba
python tag_excerpt_gpt.py /tmp/test.txt 3.5

Response:

Error with ChatGPT request: You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.
bbest commented 3 months ago

There were two main fixes:

1. Updated Deprecated Models

Per Deprecations - OpenAI API, made fixes:

2. API Key with a balance

I setup a temporary API Key with a $10.00 balance on the default project and we updated the appropriate .env files for Shiny (ba) and Docker (cstories).

TODO: update OPENAI_API_KEY to one with balance

Right now the CStories website https://cstories.integral-corp.com/beta/ still uses the non-working OPENAI_API_KEY. Here's what it looked like when @nsimon and I were debugging with my temporary funded API key:

Screenshot 2024-07-22 at 6 42 30 PM

bbest commented 3 months ago

Hi @jaschallert,

You can always test using the examples in the OpenAI docs platform.openai.com/docs/api-reference. For example, in the bash Terminal of RStudio (local or rstudio.marineenergy.app):

# set your funded key
OPENAI_API_KEY=sk-proj-SECRETsequence

# test it with command
curl https://api.openai.com/v1/models \
  -H "Authorization: Bearer $OPENAI_API_KEY"

e.g. to get list of models as commented above

jaschallert commented 3 months ago

Thank you Ben - I will get back to this with Neil hopefully today or tomorrow - we had a couple of other urgent things come up. Really appreciate the documentation and your help. I also did not get to talking about the application updates themselves in which it doesn’t look like I have write access to overwrite the google sheets when I run the update script. I will be out of town next week but can we talk about that when I get back?Thank you,JenSent from my iPhoneOn Jul 23, 2024, at 8:30 AM, Ben Best @.***> wrote: Hi @jaschallert, You can always test using the examples in the OpenAI docs platform.openai.com/docs/api-reference. For example, in the bash Terminal of RStudio (local or rstudio.marineenergy.app):

set your funded key

OPENAI_API_KEY=sk-proj-SECRETsequence

test it with command

curl https://api.openai.com/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY" e.g. to get list of models as commented above

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

bbest commented 3 months ago

Hi @jaschallert,

I'm happy to look at the other issue with you. Please feel free to suggest a time.

It would be especially helpful for you to share what you did where and the exact response you were getting. For instance, were you successful in updating one of the Google Sheets (ba-docs, or data | marineenergy.app) but then ran into an error running one of the update_*() functions in apps repo /scripts/ update_all.R or update.R?

I suspect you ran update_ba() function in update.R from your laptop (and not the server rstudio.marineenergy.app), which probably does not have the correct authorization file path available per this line (with comments) of R code:

  # google sheet key from Google Console service account
  #   https://console.cloud.google.com/iam-admin/serviceaccounts/details/111453668228815650069/keys?authuser=2&organizationId=651265387478&project=marineenergy4gargle
  gs4_auth_json <- "/share/data/marineenergy4gargle.json" 

Note above uses the private Google Console service account authorization JSON file based on read & write permissions being granted to shares@marineenergy4gargle.iam.gserviceaccount.com visible when you click on the Share button of the Google Sheet ba-docs (or data | marineenergy.app):

image
jaschallert commented 3 months ago

Hi Ben -Thank you! I am out of town this week but will re-try and schedule something when I’m back. I had logged into the server when I got my error but will double check to make sure. Thanks again,JenSent from my iPhoneOn Jul 31, 2024, at 4:03 AM, Ben Best @.**> wrote: Hi @jaschallert, I'm happy to look at the other issue with you. Please feel free to suggest a time. It would be especially helpful for you to share what you did where and the exact response you were getting. For instance, were you successful in updating one of the Google Sheets (ba-docs, or data | marineenergy.app) but then ran into an error running one of the update_() functions in apps repo /scripts/ update_all.R or update.R? I suspect you ran update_ba() function in update.R from your laptop (and not the server rstudio.marineenergy.app), which probably does not have the correct authorization file path available per this line (with comments) of R code:

google sheet key from Google Console service account

https://console.cloud.google.com/iam-admin/serviceaccounts/details/111453668228815650069/keys?authuser=2&organizationId=651265387478&project=marineenergy4gargle

gs4_auth_json <- "/share/data/marineenergy4gargle.json" Note above uses the private Google Console service account authorization JSON file based on read & write permissions being granted to @.*** visible when you click on the Share button of the Google Sheet ba-docs (or data | marineenergy.app): image.png (view on web)

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>