Skip to content

Commit

Permalink
feat: add runOption, use with new groq chat
Browse files Browse the repository at this point in the history
groq is openai compatible but requires auth token,
this lets us add an auth token after parsing chat prompt so we don't
need to stick tokens in plaintext
  • Loading branch information
gsuuon committed May 25, 2024
1 parent a7438aa commit 77e891d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lua/model/core/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local M = {}
---@field provider Provider The API provider for this prompt
---@field create fun(input: string, context: Context): string | ChatContents Converts input and context to the first message text or ChatContents
---@field run fun(messages: ChatMessage[], config: ChatConfig): table | fun(resolve: fun(params: table): nil ) ) Converts chat messages and config into completion request params
---@field runOptions? fun(): table Builds additional options to merge into chat prompt options. E.g. for auth tokens that shouldn't be written to the chat config header.
---@field system? string System instruction
---@field params? table Static request parameters
---@field options? table Provider options
Expand Down Expand Up @@ -336,6 +337,10 @@ function M.run_chat(opts)
local options = parsed.contents.config.options or {}
local params = parsed.contents.config.params or {}

if type(chat_prompt.runOptions) == 'function' then
options = vim.tbl_deep_extend('force', options, chat_prompt.runOptions())
end

if type(run_params) == 'function' then
run_params(function(async_params)
local merged_params = vim.tbl_deep_extend('force', params, async_params)
Expand Down
13 changes: 13 additions & 0 deletions lua/model/prompts/chats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ local anthropic = require('model.providers.anthropic')
local zephyr_fmt = require('model.format.zephyr')
local starling_fmt = require('model.format.starling')

local util = require('model.util')

local function input_if_selection(input, context)
return context.selection and input or ''
end
Expand Down Expand Up @@ -213,6 +215,17 @@ local chats = {
})
end,
},
groq = vim.tbl_deep_extend('force', openai_chat, {
params = {
model = 'llama3-70b-8192',
},
runOptions = function()
return {
url = 'https://api.groq.com/openai/v1/',
authorization = 'Bearer ' .. util.env('GROQ_API_KEY'),
}
end,
}),
}

return chats
2 changes: 1 addition & 1 deletion lua/model/util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ local get_secret_once = M.memo(function(name)
end)

function M.env(name)
if M.secrets[name] then
if type(M.secrets[name]) == 'function' then
return get_secret_once(name)
else
local value = vim.env[name]
Expand Down

0 comments on commit 77e891d

Please sign in to comment.