-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
252 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
local openai = require("codecompanion.adapters.openai") | ||
|
||
---@class AzureOpenAI.Adapter: CodeCompanion.Adapter | ||
return { | ||
name = "azure_openai", | ||
roles = { | ||
llm = "assistant", | ||
user = "user", | ||
}, | ||
opts = { | ||
stream = true, | ||
}, | ||
features = { | ||
text = true, | ||
tokens = true, | ||
vision = true, | ||
}, | ||
url = "${endpoint}/openai/deployments/${deployment}/chat/completions?api-version=${api_version}", | ||
env = { | ||
api_key = "AZURE_OPENAI_API_KEY", | ||
endpoint = "AZURE_OPENAI_ENDPOINT", | ||
api_version = "2024-06-01", | ||
deployment = "schema.model", | ||
}, | ||
raw = { | ||
"--no-buffer", | ||
"--silent", | ||
}, | ||
headers = { | ||
["Content-Type"] = "application/json", | ||
["api-key"] = "${api_key}", | ||
}, | ||
handlers = { | ||
--- Use the OpenAI adapter for the bulk of the work | ||
setup = function(self) | ||
return openai.handlers.setup(self) | ||
end, | ||
tokens = function(self, data) | ||
return openai.handlers.tokens(self, data) | ||
end, | ||
form_parameters = function(self, params, messages) | ||
return openai.handlers.form_parameters(self, params, messages) | ||
end, | ||
form_messages = function(self, messages) | ||
return openai.handlers.form_messages(self, messages) | ||
end, | ||
chat_output = function(self, data) | ||
return openai.handlers.chat_output(self, data) | ||
end, | ||
inline_output = function(self, data, context) | ||
return openai.handlers.inline_output(self, data, context) | ||
end, | ||
on_exit = function(self, data) | ||
return openai.handlers.on_exit(self, data) | ||
end, | ||
}, | ||
schema = { | ||
-- See https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions | ||
-- model does not exist in the Azure OpenAI API, but this is used as the deployment in the URL. | ||
model = { | ||
order = 1, | ||
mapping = "parameters", | ||
type = "enum", | ||
desc = "ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API.", | ||
default = "gpt-4o", | ||
choices = { | ||
"gpt-4o", | ||
"gpt-4o-mini", | ||
"gpt-4-turbo-preview", | ||
"gpt-4", | ||
"gpt-3.5-turbo", | ||
}, | ||
}, | ||
temperature = { | ||
order = 2, | ||
mapping = "parameters", | ||
type = "number", | ||
optional = true, | ||
default = 1, | ||
desc = "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or top_p but not both.", | ||
validate = function(n) | ||
return n >= 0 and n <= 2, "Must be between 0 and 2" | ||
end, | ||
}, | ||
top_p = { | ||
order = 3, | ||
mapping = "parameters", | ||
type = "number", | ||
optional = true, | ||
default = 1, | ||
desc = "An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.", | ||
validate = function(n) | ||
return n >= 0 and n <= 1, "Must be between 0 and 1" | ||
end, | ||
}, | ||
stop = { | ||
order = 4, | ||
mapping = "parameters", | ||
type = "list", | ||
optional = true, | ||
default = nil, | ||
subtype = { | ||
type = "string", | ||
}, | ||
desc = "Up to 4 sequences where the API will stop generating further tokens.", | ||
validate = function(l) | ||
return #l >= 1 and #l <= 4, "Must have between 1 and 4 elements" | ||
end, | ||
}, | ||
max_tokens = { | ||
order = 5, | ||
mapping = "parameters", | ||
type = "integer", | ||
optional = true, | ||
default = nil, | ||
desc = "The maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length.", | ||
validate = function(n) | ||
return n > 0, "Must be greater than 0" | ||
end, | ||
}, | ||
presence_penalty = { | ||
order = 6, | ||
mapping = "parameters", | ||
type = "number", | ||
optional = true, | ||
default = 0, | ||
desc = "Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.", | ||
validate = function(n) | ||
return n >= -2 and n <= 2, "Must be between -2 and 2" | ||
end, | ||
}, | ||
frequency_penalty = { | ||
order = 7, | ||
mapping = "parameters", | ||
type = "number", | ||
optional = true, | ||
default = 0, | ||
desc = "Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.", | ||
validate = function(n) | ||
return n >= -2 and n <= 2, "Must be between -2 and 2" | ||
end, | ||
}, | ||
logit_bias = { | ||
order = 8, | ||
mapping = "parameters", | ||
type = "map", | ||
optional = true, | ||
default = nil, | ||
desc = "Modify the likelihood of specified tokens appearing in the completion. Maps tokens (specified by their token ID) to an associated bias value from -100 to 100. Use https://platform.openai.com/tokenizer to find token IDs.", | ||
subtype_key = { | ||
type = "integer", | ||
}, | ||
subtype = { | ||
type = "integer", | ||
validate = function(n) | ||
return n >= -100 and n <= 100, "Must be between -100 and 100" | ||
end, | ||
}, | ||
}, | ||
user = { | ||
order = 9, | ||
mapping = "parameters", | ||
type = "string", | ||
optional = true, | ||
default = nil, | ||
desc = "A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.", | ||
validate = function(u) | ||
return u:len() < 100, "Cannot be longer than 100 characters" | ||
end, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters