Skip to content

Commit 33723e1

Browse files
wookayinBryley
andauthored
fix: Handle error when OPENAI_API_KEY is not set. (#24)
Co-authored-by: Bryley <bryleyhayter@gmail.com>
1 parent 248c200 commit 33723e1

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

lua/neoai/chat/models/openai.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,32 @@ end
5151
---@param chat_history ChatHistory
5252
---@param on_stdout_chunk fun(chunk: string) Function to call whenever a stdout chunk occurs
5353
---@param on_complete fun(err?: string, output?: string) Function to call when model has finished
54-
M.send_to_model = function(chat_history, on_stdout_chunk, on_complete)
55-
local api_key = config.options.open_ai.api_key.get()
54+
M.send_to_model = function (chat_history, on_stdout_chunk, on_complete)
55+
local api_key = os.getenv(config.options.open_api_key_env)
56+
if not api_key then
57+
on_complete("OpenAI API Key is missing.", nil)
58+
return
59+
end
5660

5761
local data = {
5862
model = chat_history.model,
59-
stream = true,
60-
messages = chat_history.messages,
63+
stream = true,
64+
messages = chat_history.messages
6165
}
62-
data = vim.tbl_deep_extend("force", {}, data, chat_history.params)
63-
6466
chunks = {}
6567
raw_chunks = {}
6668
utils.exec("curl", {
67-
"--silent",
68-
"--show-error",
69-
"--no-buffer",
70-
"https://api.openai.com/v1/chat/completions",
69+
"--silent", "--show-error", "--no-buffer", "https://api.openai.com/v1/chat/completions",
7170
"-H",
7271
"Content-Type: application/json",
7372
"-H",
7473
"Authorization: Bearer " .. api_key,
7574
"-d",
7675
vim.json.encode(data),
77-
}, function(chunk)
76+
}, function (chunk)
7877
M._recieve_chunk(chunk, on_stdout_chunk)
79-
end, function(err, _)
78+
end, function (err, _)
79+
8080
local total_message = table.concat(raw_chunks, "")
8181
local ok, json = pcall(vim.json.decode, total_message)
8282
if ok then

0 commit comments

Comments
 (0)