Compare commits
3 Commits
9f881c6372
...
b1414c32d1
| Author | SHA1 | Date | |
|---|---|---|---|
| b1414c32d1 | |||
| e80f3457a1 | |||
| 557106f1be |
@@ -162,14 +162,69 @@ export type CompletionsRequest = {
|
|||||||
}?
|
}?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ModelsResponse = {
|
||||||
|
data : {
|
||||||
|
created: number,
|
||||||
|
id: string,
|
||||||
|
object: "model",
|
||||||
|
owned_by: string
|
||||||
|
},
|
||||||
|
object: "list"
|
||||||
|
}
|
||||||
|
|
||||||
|
type modality = 'text' | 'image' | string
|
||||||
|
|
||||||
|
export type LanguageModel = {
|
||||||
|
aliases: {
|
||||||
|
string
|
||||||
|
},
|
||||||
|
cached_prompt_text_token_price: number,
|
||||||
|
completion_text_token_price: number,
|
||||||
|
created: number,
|
||||||
|
fingerprint: string,
|
||||||
|
id: string,
|
||||||
|
input_modalities: {
|
||||||
|
modality
|
||||||
|
},
|
||||||
|
object: string,
|
||||||
|
output_modalities: {
|
||||||
|
modality
|
||||||
|
},
|
||||||
|
owned_by: string,
|
||||||
|
prompt_image_token_price: number,
|
||||||
|
prompt_text_token_price: number,
|
||||||
|
search_price: number,
|
||||||
|
version: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type LanguageModelsResponse = {
|
||||||
|
models: {
|
||||||
|
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"
|
||||||
|
|
||||||
function xai.create(api_key: string)
|
function xai.create(api_key: string)
|
||||||
local client = {}
|
local client = {}
|
||||||
|
|
||||||
function client.completions(self, request: CompletionsRequest): CompletionsResponse
|
function client:completions(request: CompletionsRequest): SuccessfulApiResponse | FailedApiResponse
|
||||||
local config = {
|
local config = {
|
||||||
url = "https://api.x.ai/v1/chat/completions",
|
url = `{baseUrl}/completions`,
|
||||||
method = "POST",
|
method = "POST",
|
||||||
headers = {
|
headers = {
|
||||||
Authorization = "Bearer " .. api_key,
|
Authorization = "Bearer " .. api_key,
|
||||||
@@ -180,16 +235,50 @@ 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
|
||||||
|
|
||||||
|
function client:models(): SuccessfulApiResponse | FailedApiResponse
|
||||||
|
local config = {
|
||||||
|
url = `{baseUrl}/models`,
|
||||||
|
method = "GET",
|
||||||
|
headers = {
|
||||||
|
["Content-Type"] = "application/json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local response = net.request(config)
|
||||||
|
if response.ok then
|
||||||
|
local decoded: ModelsResponse = serde.decode("json", response.body)
|
||||||
|
return { success = true, response = decoded } :: SuccessfulApiResponse
|
||||||
|
else
|
||||||
|
return { success = false, status = response.status, message = response.body } :: FailedApiResponse
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function client:language_models(): SuccessfulApiResponse | FailedApiResponse
|
||||||
|
local config = {
|
||||||
|
url = `{baseUrl}/language-models`,
|
||||||
|
method = "GET",
|
||||||
|
headers = {
|
||||||
|
["Content-Type"] = "application/json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
local response = net.request(config)
|
||||||
|
if response.ok then
|
||||||
|
local decoded: LanguageModelsResponse = serde.decode("json", response.body)
|
||||||
|
return { success = true, response = decoded } :: SuccessfulApiResponse
|
||||||
|
else
|
||||||
|
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