Skip to content

Commit

Permalink
rework AZURE constants
Browse files Browse the repository at this point in the history
  • Loading branch information
algunion committed Nov 7, 2024
1 parent 78103eb commit 4237fc1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
7 changes: 2 additions & 5 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,13 @@ struct AZUREServiceEndpoint <: ServiceEndpoint end
chat = Chat()
Creates a new `Chat` object with default settings:
- `model` is set to `gpt-4o-2024-08-06`
- `model` is set to `gpt-4o`
- `messages` is set to an empty `Vector{Message}`
- `history` is set to `true`
"""
@kwdef struct Chat
service::Type{<:ServiceEndpoint} = OPENAIServiceEndpoint #AZUREServiceEndpoint #OPENAIServiceEndpoint
model::String = "gpt-4o"
api_version::String = "2024-08-01-preview" # for Azure only
messages::Conversation = Message[]
history::Bool = true
tools::Union{Vector{GPTTool},Nothing} = nothing
Expand All @@ -179,7 +178,6 @@ Creates a new `Chat` object with default settings:
function Chat(
service,
model,
api_version,
messages,
history,
tools,
Expand All @@ -202,7 +200,6 @@ Creates a new `Chat` object with default settings:
return new(
service,
model,
api_version,
messages,
history,
tools,
Expand All @@ -226,7 +223,7 @@ end

StructTypes.StructType(::Type{Chat}) = StructTypes.Struct()
StructTypes.omitempties(::Type{Chat}) = true
StructTypes.excludes(::Type{Chat}) = (:history, :service, :api_version)
StructTypes.excludes(::Type{Chat}) = (:history, :service)

Base.length(chat::Chat) = length(chat.messages)
Base.isempty(chat::Chat) = isempty(chat.messages)
Expand Down
13 changes: 11 additions & 2 deletions src/constants.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const OPENAI_BASE_URL::String = "https://api.openai.com"
const OPENAI_API_KEY::String = "OPENAI_API_KEY"
const AZURE_OPENAI_BASE_URL::String = "AZURE_OPENAI_BASE_URL"
const AZURE_OPENAI_DEPLOY_NAME::String = "AZURE_OPENAI_DEPLOY_NAME"
const AZURE_OPENAI_API_KEY::String = "AZURE_OPENAI_API_KEY"
const AZURE_OPENAI_API_VERSION::String = "AZURE_OPENAI_API_VERSION"

"""
Convenience mapping of model names to their respective endpoints.
Convenience mapping of OpenAI model names to their respective endpoints.
"""
const _MODEL_ENDPOINTS_OPENAI::Dict{String,String} = Dict(
Expand Down Expand Up @@ -39,4 +39,13 @@ const _MODEL_ENDPOINTS_OPENAI::Dict{String,String} = Dict(
"text-search-ada-doc-001" => "/v1/embeddings",
"text-moderation-stable" => "/v1/moderations",
"text-moderation-latest" => "/v1/moderations"
)

"""
Convenience mapping of OpenAI model names to their respective endpoints.
"""
const _MODEL_ENDPOINTS_AZURE_OPENAI::Dict{String,String} = Dict(
"gpt-4o" => "/openai/deployments/" * ENV["AZURE_OPENAI_DEPLOY_NAME_GPT_4O"],
"gpt-4o-mini" => "/openai/deployments/" * ENV["AZURE_OPENAI_DEPLOY_NAME_GPT_4O_MINI"]
)
13 changes: 5 additions & 8 deletions src/requests.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
get_url(model::String) = OPENAI_BASE_URL * _MODEL_ENDPOINTS_OPENAI[model]
get_url(params::Chat) = params.service == OPENAIServiceEndpoint ? get_url(params.model) : get_url(AZUREServiceEndpoint, params)
get_url(params::Chat) = get_url(params.service, params.model)
get_url(emb::Embeddings) = get_url(emb.model)
get_url(::Type{OPENAIServiceEndpoint}, params::Chat) = get_url(params)
get_url(::Type{OPENAIServiceEndpoint}, model::String) = OPENAI_BASE_URL * _MODEL_ENDPOINTS_OPENAI[model]
get_url(::Type{OPENAIServiceEndpoint}, emb::Embeddings) = get_url(emb)
get_url(::Type{OPENAIServiceEndpoint}, model::String) = get_url(model)
function get_url(::Type{AZUREServiceEndpoint}, params::Chat)
ENV[AZURE_OPENAI_BASE_URL] * "/openai/deployments/" * ENV[AZURE_OPENAI_DEPLOY_NAME] * "/chat/completions?api-version=$(getfield(params, :api_version))"
end
get_url(::Type{AZUREServiceEndpoint}, model::String) = ENV[AZURE_OPENAI_BASE_URL] * "/openai/deployments/" * _MODEL_ENDPOINTS_AZURE_OPENAI[model] * "/chat/completions?api-version=$(ENV[AZURE_OPENAI_API_VERSION])"


function auth_header(::Type{OPENAIServiceEndpoint})
[
"Authorization" => "Bearer $(ENV["OPENAI_API_KEY"])",
"Authorization" => "Bearer $(ENV[OPENAI_API_KEY])",
"Content-Type" => "application/json"
]
end
Expand Down

0 comments on commit 4237fc1

Please sign in to comment.