|
51 | 51 | ---@param chat_history ChatHistory |
52 | 52 | ---@param on_stdout_chunk fun(chunk: string) Function to call whenever a stdout chunk occurs |
53 | 53 | ---@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 |
56 | 60 |
|
57 | 61 | local data = { |
58 | 62 | model = chat_history.model, |
59 | | - stream = true, |
60 | | - messages = chat_history.messages, |
| 63 | + stream = true, |
| 64 | + messages = chat_history.messages |
61 | 65 | } |
62 | | - data = vim.tbl_deep_extend("force", {}, data, chat_history.params) |
63 | | - |
64 | 66 | chunks = {} |
65 | 67 | raw_chunks = {} |
66 | 68 | 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", |
71 | 70 | "-H", |
72 | 71 | "Content-Type: application/json", |
73 | 72 | "-H", |
74 | 73 | "Authorization: Bearer " .. api_key, |
75 | 74 | "-d", |
76 | 75 | vim.json.encode(data), |
77 | | - }, function(chunk) |
| 76 | + }, function (chunk) |
78 | 77 | M._recieve_chunk(chunk, on_stdout_chunk) |
79 | | - end, function(err, _) |
| 78 | + end, function (err, _) |
| 79 | + |
80 | 80 | local total_message = table.concat(raw_chunks, "") |
81 | 81 | local ok, json = pcall(vim.json.decode, total_message) |
82 | 82 | if ok then |
|
0 commit comments