standardized api responses, added types
This commit is contained in:
@@ -197,19 +197,32 @@ export type LanguageModel = {
|
|||||||
version: string
|
version: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type LangaugeModelsResponse = {
|
export type LanguageModelsResponse = {
|
||||||
models: {
|
models: {
|
||||||
LanguageModel
|
LanguageModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ApiResponse = {
|
||||||
|
success: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SuccessfulApiResponse = ApiResponse & {
|
||||||
|
response: CompletionsResponse | ModelsResponse | LanguageModelsResponse
|
||||||
|
}
|
||||||
|
|
||||||
|
export type FailedApiResponse = ApiResponse & {
|
||||||
|
status: number,
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
|
||||||
local xai = {}
|
local xai = {}
|
||||||
local baseUrl = "https://api.x.ai/v1"
|
local baseUrl = "https://api.x.ai/v1"
|
||||||
|
|
||||||
function xai.create(api_key: string)
|
function xai.create(api_key: string)
|
||||||
local client = {}
|
local client = {}
|
||||||
|
|
||||||
function client:completions(request: CompletionsRequest): CompletionsResponse
|
function client:completions(request: CompletionsRequest): SuccessfulApiResponse | FailedApiResponse
|
||||||
local config = {
|
local config = {
|
||||||
url = `{baseUrl}/completions`,
|
url = `{baseUrl}/completions`,
|
||||||
method = "POST",
|
method = "POST",
|
||||||
@@ -222,13 +235,13 @@ function xai.create(api_key: string)
|
|||||||
local response = net.request(config)
|
local response = net.request(config)
|
||||||
if response.ok then
|
if response.ok then
|
||||||
local decoded: CompletionsResponse = serde.decode("json", response.body)
|
local decoded: CompletionsResponse = serde.decode("json", response.body)
|
||||||
return decoded
|
return { success = true, response = decoded } :: SuccessfulApiResponse
|
||||||
else
|
else
|
||||||
error(`API request failed with status {response.statusCode}: {response.statusMessage}`)
|
return { success = false, status = response.status, message = response.body } :: FailedApiResponse
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function client:models(): ModelsResponse
|
function client:models(): SuccessfulApiResponse | FailedApiResponse
|
||||||
local config = {
|
local config = {
|
||||||
url = `{baseUrl}/models`,
|
url = `{baseUrl}/models`,
|
||||||
method = "GET",
|
method = "GET",
|
||||||
@@ -239,13 +252,13 @@ function xai.create(api_key: string)
|
|||||||
local response = net.request(config)
|
local response = net.request(config)
|
||||||
if response.ok then
|
if response.ok then
|
||||||
local decoded: ModelsResponse = serde.decode("json", response.body)
|
local decoded: ModelsResponse = serde.decode("json", response.body)
|
||||||
return decoded
|
return { success = true, response = decoded } :: SuccessfulApiResponse
|
||||||
else
|
else
|
||||||
error(`API request failed with status {response.statusCode}: {response.statusMessage}`)
|
return { success = false, status = response.status, message = response.body } :: FailedApiResponse
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function client:language_models(): LangaugeModelsResponse
|
function client:language_models(): SuccessfulApiResponse | FailedApiResponse
|
||||||
local config = {
|
local config = {
|
||||||
url = `{baseUrl}/language-models`,
|
url = `{baseUrl}/language-models`,
|
||||||
method = "GET",
|
method = "GET",
|
||||||
@@ -255,17 +268,17 @@ function xai.create(api_key: string)
|
|||||||
}
|
}
|
||||||
local response = net.request(config)
|
local response = net.request(config)
|
||||||
if response.ok then
|
if response.ok then
|
||||||
local decoded: LangaugeModelsResponse = serde.decode("json", response.body)
|
local decoded: LanguageModelsResponse = serde.decode("json", response.body)
|
||||||
return decoded
|
return { success = true, response = decoded } :: SuccessfulApiResponse
|
||||||
else
|
else
|
||||||
error(`API request failed with status {response.statusCode}: {response.statusMessage}`)
|
return { success = false, status = response.status, message = response.body } :: FailedApiResponse
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return client
|
return client
|
||||||
end
|
end
|
||||||
|
|
||||||
function xai.completions(api_key: string, request: CompletionsRequest): CompletionsResponse
|
function xai.completions(api_key: string, request: CompletionsRequest): ApiResponse
|
||||||
return xai.create(api_key):completions(request)
|
return xai.create(api_key):completions(request)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user