ChatGPT.nvim
                                
                                
                                
                                    ChatGPT.nvim copied to clipboard
                            
                            
                            
                        Attempt to index field `usage` (a nil value)
I ran :ChatGPT and entered some query then this error occurred:
Error executing vim.schedule lua callback: ....local/share/nvim/lazy/ChatGPT.nvim/lua/chatgpt/chat.lua:97: attempt to index field 'usage' (a nil value)                                                                                 
stack traceback:                                                                                                                                                                                                                        
        ....local/share/nvim/lazy/ChatGPT.nvim/lua/chatgpt/chat.lua:97: in function 'get_total_tokens'                                                                                                                                  
        ...ocal/share/nvim/lazy/ChatGPT.nvim/lua/chatgpt/module.lua:75: in function 'cb'                                                                                                                                                
        .../.local/share/nvim/lazy/ChatGPT.nvim/lua/chatgpt/api.lua:57: in function ''                                                                                                                                                  
        vim/_editor.lua: in function <vim/_editor.lua:0>  
Might be a problem with my API key but if that's the case I think the error handling could be improved. Related: #10
You probably have to pay for it now, you only get a months worth of free API calls
@ChristianChiarulli so that was the issue in your case? could you paste the output of sample API call e.g.:
curl https://api.openai.com/v1/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"model": "code-davinci-002", "prompt": "test", "max_tokens": 10 }'
                                    
                                    
                                    
                                
Here is the output I get:
{"id":"cmpl-6QVU76IfN5u35VXfgoVK9y6CA81Zh","object":"text_completion","created":1671775575,"model":"code-davinci-002","choices":[{"text":" = False\ndevelopment = False\n\nimport","index":0,"logprobs":null,"finish_reason":"length"}],"usage":{"prompt_tokens":1,"completion_tokens":10,"total_tokens":11}}
                                    
                                    
                                    
                                
Looks good, but issue still occurs?
Oh, the issue indeed seems to be that I've apparently sent too many requests. This is what vim.pretty_print(self:get_last_answer()) prints:
{                                                                                                                                                                                                                                       
  end_line = 1,                                                                                                                                                                                                                         
  lines = { "// API ERROR: You exceeded your current quota, please check your plan and billing details." },                                                                                                                             
  nr_of_lines = 1,                                                                                                                                                                                                                      
  start_line = 1,                                                                                                                                                                                                                       
  text = "// API ERROR: You exceeded your current quota, please check your plan and billing details.\n",                                                                                                                                
  type = 2                                                                                                                                                                                                                              
}  
                                    
                                    
                                    
                                
I got the same issue and the response of the API call was:
{
    "error": {
        "message": "The model: `code-davinci-002` does not exist",
        "type": "invalid_request_error",
        "param": null,
        "code": "model_not_found"
    }
}
I suppose that I have to change config to use another model myself?
code-davinci-002 is deprecated now.
My bad. I mistook code-davinci-002 as the default code-davinci-edit-002. However, API call responsed that I didn't have this model. I tried code-davinci-edit-001 and API call returned:
{
  "error": {
    "message": "This model is not supported in the v1/completions endpoint.",
    "type": "invalid_request_error",
    "param": "model",
    "code": null
  }
}
My bad again. The test command should adapt tocurl https://api.openai.com/v1/edits -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{"model": "code-davinci-edit-001", "prompt": "test", "max_tokens": 10 }' to use the correct API endpoint and the current code-davinci-edit-001` model. Altought it complains that instruction is missed, the model should work anyway.