Skip to content

Commit 4fa078b

Browse files
committed
Add OpenAI-compatible API endpoint and model configuration
1 parent a12fd56 commit 4fa078b

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

autoload/copilot.vim

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,17 +787,26 @@ function! s:commands.model(opts) abort
787787
return 'echoerr ' . string('Copilot: Error retrieving completions models: ' . response.error.message)
788788
endif
789789
let models = filter(response.result, { _, m -> index(m.scopes, 'completion') >= 0 })
790+
let current_model = get(g:, 'copilot_model', '')
790791
if len(models) == 0
791-
echo 'Copilot: Could not retrieve completions models'
792-
elseif len(models) == 1
792+
if !empty(current_model)
793+
echo 'Copilot: Current/only completions model is ' . current_model . ' (set via g:copilot_model)'
794+
else
795+
echo 'Copilot: Could not retrieve completions models'
796+
endif
797+
elseif len(models) == 1 && empty(current_model)
793798
echo 'Copilot: Current/only completions model is ' . s:FmtModel(models[0])
794799
else
800+
if !empty(current_model)
801+
call insert(models, {'modelName': current_model, 'id': current_model, 'scopes': ['completion']})
802+
endif
795803
let choices = map(copy(models), { i, m -> (i + 1) . '. ' . s:FmtModel(m) })
796804
let choice = inputlist(['Select a completions model:'] + choices)
797805
if choice < 1 || choice > len(models)
798806
return
799807
endif
800808
let model = models[choice - 1]
809+
let g:copilot_model = model.id
801810
if type(get(g:, 'copilot_settings')) != v:t_dict
802811
let g:copilot_settings = {}
803812
endif

autoload/copilot/client.vim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,21 @@ function! copilot#client#Settings() abort
596596
if type(settings.http.proxy) ==# v:t_string && settings.http.proxy =~# '^[^/]\+$'
597597
let settings.http.proxy = 'http://' . settings.http.proxy
598598
endif
599+
if type(get(g:, 'copilot_api_url')) == v:t_string
600+
let settings.api = {'url': g:copilot_api_url}
601+
endif
602+
if type(get(g:, 'copilot_api_key')) == v:t_string
603+
if !has_key(settings, 'api')
604+
let settings.api = {}
605+
endif
606+
let settings.api.key = g:copilot_api_key
607+
endif
608+
if type(get(g:, 'copilot_model')) == v:t_string
609+
if !has_key(settings, 'api')
610+
let settings.api = {}
611+
endif
612+
let settings.api.model = g:copilot_model
613+
endif
599614
if type(get(g:, 'copilot_settings')) == v:t_dict
600615
let settings.github = {'copilot': g:copilot_settings}
601616
endif
@@ -718,6 +733,12 @@ function! copilot#client#New() abort
718733
if type(get(g:, 'copilot_integration_id')) == v:t_string
719734
let opts.initializationOptions.copilotIntegrationId = g:copilot_integration_id
720735
endif
736+
if type(get(g:, 'copilot_model')) == v:t_string
737+
if type(get(g:, 'copilot_settings')) != v:t_dict
738+
let g:copilot_settings = {}
739+
endif
740+
let g:copilot_settings.selectedCompletionModel = g:copilot_model
741+
endif
721742
let opts.workspaceFolders = []
722743
let settings = copilot#client#Settings()
723744
if type(get(g:, 'copilot_workspace_folders')) == v:t_list

doc/copilot.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ COMMANDS *:Copilot*
3434
completions models is one, rendering this command
3535
inert.
3636

37+
The model can also be set via |g:copilot_model| in your
38+
vimrc to specify an arbitrary model without the
39+
interactive prompt.
40+
3741
*:Copilot_panel*
3842
:Copilot panel Open a window with up to 10 completions for the
3943
current buffer. Use <CR> to accept a completion.
@@ -125,6 +129,29 @@ g:copilot_proxy_strict_ssl
125129
by setting the $NODE_TLS_REJECT_UNAUTHORIZED
126130
environment variable to "0".
127131

132+
*g:copilot_api_url*
133+
g:copilot_api_url Specify an OpenAI API compatible endpoint URL for
134+
completions. When set, this URL will be passed to the
135+
language server for use with any OpenAI-compatible API.
136+
>
137+
let g:copilot_api_url = 'https://api.openai.com/v1'
138+
let g:copilot_api_url = 'http://localhost:11434/v1'
139+
<
140+
*g:copilot_api_key*
141+
g:copilot_api_key Specify an API key for use with the API endpoint
142+
configured via |g:copilot_api_url|.
143+
>
144+
let g:copilot_api_key = 'sk-...'
145+
<
146+
*g:copilot_model*
147+
g:copilot_model Specify the model to use for completions, bypassing the
148+
interactive |:Copilot_model| command. Setting this
149+
also populates |g:copilot_settings| with the given
150+
model.
151+
>
152+
let g:copilot_model = 'gpt-4o'
153+
let g:copilot_model = 'codellama'
154+
<
128155
*g:copilot_workspace_folders*
129156
g:copilot_workspace_folders
130157
A list of "workspace folders" or project roots that

0 commit comments

Comments
 (0)