Search code examples
shelljupyter-notebookcommand-linemultilinemultilinestring

How do I run a GPT API shell command in Jupyter Notebook without having to put a multi-line string argument in one line?


I have an API key that can ask the GPT bot from the command line, from Jupyter Notebook, or from other interfaces. I follow the guide at Open AI - Getting Started - Making Requests. The code works in a command line interface:

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
     "model": "gpt-3.5-turbo",
     "messages": [{"role": "user", "content": "Say this is a test!"}],
     "temperature": 0.7
   }'

Output:

(base) user@admin:~$ curl https://api.openai.com/v1/chat/completions \
>   -H "Content-Type: application/json" \
>   -H "Authorization: Bearer sk-xyzxyzxyz............................" \
>   -d '{
>      "model": "gpt-3.5-turbo",
>      "messages": [{"role": "user", "content": "Say this is a test!"}],
>      "temperature": 0.7
>    }'
{
  "id": "chatcmpl-1a............................",
  "object": "chat.completion",
  "created": 1706212345,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "This is a test!"
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 13,
    "completion_tokens": 5,
    "total_tokens": 18
  },
  "system_fingerprint": null
}

We see that the bot understands the command and answers with:

"content": "This is a test!"

This seems like the "Hello World!" for tests of a chat bot.

In the Jupyter Notebook cell, the command needs to begin with "!curl ...", thus, "!" in front. That is clear.

But the string over more than one line throws an error:

  File <tokenize>:5
    }'
    ^
IndentationError: unindent does not match any outer indentation level

Clearly, the multi-line string does not work, as I can run:

!curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-xyzxyzxyz............................" \
  -d '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7}'

I saw in some threads that everything was written in one line, example from Open AI community:

!curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
  "model": "gpt-4-1106-preview",
  "messages": [
        {"role": "user", "content": "<prompt-start> <prompt-before-input1> <input1> <prompt-before-input2><input2> <prompt-before-input3> <input3> <prompt-before-input4> <input4> <prompt-before-input5> <input5>"}
     ],
  "max_tokens": <max-tokens>,
  "n": <number-of-renditions>
}

That might be best practice or not, but such very long lines are awkward to read.

How do I run a GPT API shell command in Jupyter Notebook without having to put a multi-line string argument in one line?


Solution

  • Here is the working code, you only need to add " \" or "\" to each line. It does not work to put """ at the beginning and at the end as you would guess from Running command line command in Jupyter notebook.

    And I thought this would not work since there are already "\" signs in the first rows.

    curl https://api.openai.com/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer $OPENAI_API_KEY" \
      -d '{ \
         "model": "gpt-3.5-turbo", \
         "messages": [{"role": "user", "content": "Say this is a test!"}], \
         "temperature": 0.7 \
       }'
    

    This runs through with the same output.