Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow OpenAI endpoint configuration via config #18

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add base_url to config, implement usage of base_url in requests
  • Loading branch information
Karim Shakirov authored and Karim Shakirov committed Feb 20, 2024
commit f27a96453935bace264944a003cf542ff7172aff
2 changes: 2 additions & 0 deletions lua/wtf/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function M.setup(opts)
local default_opts = {
openai_api_key = nil,
openai_model_id = "gpt-3.5-turbo",
openai_base_url = "https://api.openai.com",
language = "english",
search_engine = "google",
context = true,
Expand All @@ -27,6 +28,7 @@ function M.setup(opts)
winhighlight = { opts.winhighlight, "string" },
openai_api_key = { opts.openai_api_key, { "string", "nil" } },
openai_model_id = { opts.openai_model_id, "string" },
openai_base_url = { opts.openai_base_url, { "string", "nil" } },
language = { opts.language, "string" },
search_engine = {
opts.search_engine,
Expand Down
25 changes: 23 additions & 2 deletions lua/wtf/gpt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,29 @@ local function get_api_key()
return api_key
end

local function get_base_url()
local url = config.options.openai_base_url
if url == nil then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the nil check necessary here? As we already have the default API endpoint set in the default_ops as a fallback for if one is not specified? Even if you intentionally set it to be nil in your config, the default will still be set in this case?

if vim.g.wtf_base_url_complained == nil then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we refactor to not use vim globals?

local message =
"No OpenAI Base URL foun. Please set openai_base_url in the setup table. Defaulting to https://api.openai.com for now"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny typo with foun!

vim.fn.confirm(message, "&OK", 1, "Warning")
vim.g.wtf_base_url_complained = 1
end
return "https://api.openai.com"
end
return url
end

function M.request(messages, callback, callbackTable)
local api_key = get_api_key()

if api_key == nil then
return nil
end

local base_url = get_base_url()

-- Check if curl is installed
if vim.fn.executable("curl") == 0 then
vim.fn.confirm("curl installation not found. Please install curl to use Wtf", "&OK", 1, "Warning")
Expand Down Expand Up @@ -110,7 +126,10 @@ function M.request(messages, callback, callbackTable)
if isWindows ~= true then
-- Linux
curlRequest = string.format(
'curl -s https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer '
--'curl -s https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer '
'curl -s '
.. base_url
.. '/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer '
.. api_key
.. '" --data-binary "@'
.. tempFilePathEscaped
Expand All @@ -121,7 +140,9 @@ function M.request(messages, callback, callbackTable)
else
-- Windows
curlRequest = string.format(
'curl -s https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer '
'curl -s '
.. base_url
.. '/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer '
.. api_key
.. '" --data-binary "@'
.. tempFilePathEscaped
Expand Down