Comparing version 0.1.0 to 0.2.0
@@ -1,40 +0,7 @@ | ||
import { ApiType, GenerateRequest, StatusResponse } from './types'; | ||
export declare function generate(type: ApiType, request: GenerateRequest, apikey?: string): Promise<{ | ||
import { FindUserResponse, GenerateRequest, Model, StatusResponse } from './types'; | ||
export declare function generate(request: GenerateRequest, apikey?: string): Promise<{ | ||
id: string; | ||
}>; | ||
export declare function waitForResponse(type: ApiType, id: string, timeoutSecs?: number): Promise<StatusResponse>; | ||
/** | ||
* If the 'type' is not provided both APIs will be queried | ||
* | ||
* @param apikey | ||
* @param type | ||
* @returns | ||
*/ | ||
export declare function findUser(apikey: string, type?: ApiType): Promise<{ | ||
type: ApiType; | ||
kudos_details: { | ||
accumulated: number; | ||
gifted: number; | ||
admin: number; | ||
received: number; | ||
recurring: number; | ||
}; | ||
usage: { | ||
tokens: number; | ||
requests: number; | ||
}; | ||
contributions: { | ||
tokens: number; | ||
fulfillments: number; | ||
}; | ||
username: string; | ||
id: number; | ||
kudos: number; | ||
concurrency: number; | ||
worker_invited: number; | ||
moderator: boolean; | ||
worker_count: number; | ||
worker_ids: string[]; | ||
trusted: number; | ||
pseudonymous: number; | ||
}>; | ||
export declare function waitForResponse(id: string, timeoutSecs?: number): Promise<StatusResponse>; | ||
export declare function findUser(apikey: string): Promise<FindUserResponse>; | ||
export declare function getModels(type?: 'image' | 'text'): Promise<Model[]>; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.findUser = exports.waitForResponse = exports.generate = void 0; | ||
const needle_1 = __importDefault(require("needle")); | ||
exports.getModels = exports.findUser = exports.waitForResponse = exports.generate = void 0; | ||
const request_1 = require("./request"); | ||
const DEFAULT_API_KEY = '0000000000'; | ||
const urls = { | ||
horde: `https://stablehorde.net/api/v2`, | ||
kobold: `https://koboldai.net/api/v2`, | ||
}; | ||
async function generate(type, request, apikey = DEFAULT_API_KEY) { | ||
const url = type === 'kobold' ? `/generate/async` : `/generate/text/async`; | ||
const res = await post({ type, url, body: request, apikey }); | ||
async function generate(request, apikey = DEFAULT_API_KEY) { | ||
const url = `/generate/text/async`; | ||
const res = await (0, request_1.post)({ url, body: request, apikey }); | ||
return res; | ||
} | ||
exports.generate = generate; | ||
async function waitForResponse(type, id, timeoutSecs) { | ||
const url = type === 'horde' ? `/generate/text/status/${id}` : `/generate/check/${id}`; | ||
async function waitForResponse(id, timeoutSecs) { | ||
const url = `/generate/text/status/${id}`; | ||
const start = Date.now(); | ||
@@ -29,3 +22,3 @@ while (true) { | ||
} | ||
const res = await get({ url, type }); | ||
const res = await (0, request_1.get)({ url }); | ||
if (res.done) { | ||
@@ -43,55 +36,13 @@ return res; | ||
exports.waitForResponse = waitForResponse; | ||
/** | ||
* If the 'type' is not provided both APIs will be queried | ||
* | ||
* @param apikey | ||
* @param type | ||
* @returns | ||
*/ | ||
async function findUser(apikey, type) { | ||
if (type) { | ||
const user = await get({ url: '/find_user', type, apikey }); | ||
return { ...user, type }; | ||
} | ||
const [horde, kobold] = await Promise.allSettled([ | ||
get({ url: `/find_user`, type: 'horde', apikey }), | ||
get({ url: `/find_user`, type: 'kobold', apikey }), | ||
]); | ||
if (horde.status === 'fulfilled') { | ||
return { ...horde.value, type: 'horde' }; | ||
} | ||
if (kobold.status === 'fulfilled') { | ||
return { ...kobold.value, type: 'kobold' }; | ||
} | ||
throw new Error(`User not found`); | ||
async function findUser(apikey) { | ||
const user = await (0, request_1.get)({ url: `/find_user`, apikey }); | ||
return user; | ||
} | ||
exports.findUser = findUser; | ||
async function post({ type, url, apikey, body }) { | ||
const baseUrl = urls[type]; | ||
const headers = {}; | ||
if (apikey) { | ||
headers.apikey = apikey; | ||
} | ||
const res = await (0, needle_1.default)('post', `${baseUrl}${url}`, body, { json: true, headers }); | ||
if (res.statusCode && res.statusCode >= 400) { | ||
const error = new Error(`${res.statusMessage}: ${res.statusCode}`); | ||
error.body = res.body; | ||
throw error; | ||
} | ||
return res.body; | ||
async function getModels(type = 'image') { | ||
const url = `/status/models?type=${type}`; | ||
const models = await (0, request_1.get)({ url }); | ||
return models; | ||
} | ||
async function get({ type, url, apikey }) { | ||
const baseUrl = urls[type]; | ||
const headers = {}; | ||
if (apikey) { | ||
headers.apikey = apikey; | ||
} | ||
const res = await (0, needle_1.default)('get', `${baseUrl}${url}`, { json: true, headers }); | ||
if (res.statusCode && res.statusCode >= 400) { | ||
const error = new Error(`${res.statusMessage}: ${res.statusCode}`); | ||
error.body = res.body; | ||
throw error; | ||
} | ||
return res.body; | ||
} | ||
exports.getModels = getModels; | ||
async function wait(secs = 2) { | ||
@@ -98,0 +49,0 @@ return new Promise((resolve) => setTimeout(resolve, secs * 1000)); |
@@ -1,2 +0,1 @@ | ||
export type ApiType = 'horde' | 'kobold'; | ||
export type GenerateRequest = { | ||
@@ -77,1 +76,9 @@ prompt: string; | ||
}; | ||
export type Model = { | ||
name: string; | ||
count: number; | ||
performance: number; | ||
queued: number; | ||
eta: number; | ||
type?: string; | ||
}; |
{ | ||
"name": "ai-horde", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -20,3 +20,3 @@ "main": "dist/index.js", | ||
}, | ||
"readme": "# ai-horde\n\n> Stable Horde and Kobold Horde request library\n\n## Work in progress\n\n### TODO\n\n- Add image generation endpoints\n\n## Installation and Usage\n\n```sh\n> npm install ai-horde\n```\n\n```ts\nimport * as ai from 'ai-horde'\n\nconst user = await ai.findUser('some-api-key')\n```\n\n## API\n\n### generate\n\nDispatch an asynchronous text generation request\n\n```ts\n// Return: P\nfunction generate(\n type: 'horde' | 'kobold',\n request: GenerateRequest,\n apikey?: string\n): Promise<{ id: string }>\n\ntype GenerateRequest = {\n prompt: string\n params: {\n n?: number\n max_context_length: number\n max_length: number\n frmtadsnsp?: boolean\n frmtrmblln?: boolean\n frmtrmspch?: boolean\n frmttriminc?: boolean\n rep_pen?: number\n rep_pen_range?: number\n rep_pen_slope?: number\n singleline?: boolean\n soft_prompt?: string\n temperature?: number\n tfs?: number\n top_a?: number\n top_k?: number\n top_p?: number\n typical?: number\n sampler_order?: number\n }\n trusted_workers?: boolean\n nsfw?: boolean\n workers?: string[]\n models?: string[]\n}\n```\n\n### waitForResponse\n\nWait for a text generation response\n\nExample:\n\n```ts\nconst { id } = await generate('horde', params, apikey)\nconst response = await waitForResposne('horde', id)\n\nreturn response.generations\n```\n\n```ts\nfunction waitForResponse(\n type: 'horde' | 'kobold',\n id: string,\n timeoutSecs?: number\n): Promise<StatusResponse>\n\ntype StatusResponse = {\n finished: number\n processing: number\n restarted: number\n waiting: number\n done: boolean\n faulted: boolean\n wait_time: number\n queue_position: number\n kudos: number\n is_possible: boolean\n generations: Array<{\n worker_id: string\n worker_name: string\n model: string\n state: string\n text: string\n seed: number\n }>\n}\n```\n\n### findUser\n\nRetrieve user information. If `type` is not provided, both Horde APIs will be queried and the valid response will be returned.\n\n```ts\nfunction findUser(apikey: string, type?: 'horde' | 'kobold'): Promise<FindUserResponse>\n\ntype FindUserResponse = {\n type: 'horde' | 'kobold'\n kudos_details: {\n accumulated: number\n gifted: number\n admin: number\n received: number\n recurring: number\n }\n usage: {\n tokens: number\n requests: number\n }\n contributions: {\n tokens: number\n fulfillments: number\n }\n username: string\n id: number\n kudos: number\n concurrency: number\n worker_invited: number\n moderator: boolean\n worker_count: number\n worker_ids: string[]\n trusted: number\n pseudonymous: number\n}\n```\n" | ||
"readme": "# ai-horde\n\n> Stable Horde request library\n\n## Work in progress\n\n### TODO\n\n- Add image generation endpoints\n\n## Installation and Usage\n\n```sh\n> npm install ai-horde\n```\n\n```ts\nimport * as horde from 'ai-horde'\n\nconst user = await horde.findUser('some-api-key')\n```\n\n## API\n\n### generate\n\nDispatch an asynchronous text generation request\n\n```ts\nfunction generate(request: GenerateRequest, apikey?: string): Promise<{ id: string }>\n\ntype GenerateRequest = {\n prompt: string\n params: {\n n?: number\n max_context_length: number\n max_length: number\n frmtadsnsp?: boolean\n frmtrmblln?: boolean\n frmtrmspch?: boolean\n frmttriminc?: boolean\n rep_pen?: number\n rep_pen_range?: number\n rep_pen_slope?: number\n singleline?: boolean\n soft_prompt?: string\n temperature?: number\n tfs?: number\n top_a?: number\n top_k?: number\n top_p?: number\n typical?: number\n sampler_order?: number\n }\n trusted_workers?: boolean\n nsfw?: boolean\n workers?: string[]\n models?: string[]\n}\n```\n\n### waitForResponse\n\nWait for a text generation response\n\nExample:\n\n```ts\nconst models = await horde.getModels('text')\nconst { id } = await horde.generate(params, apikey)\nconst response = await horde.waitForResponse(id)\n\nreturn response.generations\n```\n\n```ts\nfunction waitForResponse(id: string, timeoutSecs?: number): Promise<StatusResponse>\n\ntype StatusResponse = {\n finished: number\n processing: number\n restarted: number\n waiting: number\n done: boolean\n faulted: boolean\n wait_time: number\n queue_position: number\n kudos: number\n is_possible: boolean\n generations: Array<{\n worker_id: string\n worker_name: string\n model: string\n state: string\n text: string\n seed: number\n }>\n}\n```\n\n### findUser\n\n```ts\nfunction findUser(apikey: string): Promise<FindUserResponse>\n\ntype FindUserResponse = {\n kudos_details: {\n accumulated: number\n gifted: number\n admin: number\n received: number\n recurring: number\n }\n usage: {\n tokens: number\n requests: number\n }\n contributions: {\n tokens: number\n fulfillments: number\n }\n username: string\n id: number\n kudos: number\n concurrency: number\n worker_invited: number\n moderator: boolean\n worker_count: number\n worker_ids: string[]\n trusted: number\n pseudonymous: number\n}\n```\n" | ||
} |
# ai-horde | ||
> Stable Horde and Kobold Horde request library | ||
> Stable Horde request library | ||
@@ -18,5 +18,5 @@ ## Work in progress | ||
```ts | ||
import * as ai from 'ai-horde' | ||
import * as horde from 'ai-horde' | ||
const user = await ai.findUser('some-api-key') | ||
const user = await horde.findUser('some-api-key') | ||
``` | ||
@@ -31,8 +31,3 @@ | ||
```ts | ||
// Return: P | ||
function generate( | ||
type: 'horde' | 'kobold', | ||
request: GenerateRequest, | ||
apikey?: string | ||
): Promise<{ id: string }> | ||
function generate(request: GenerateRequest, apikey?: string): Promise<{ id: string }> | ||
@@ -76,4 +71,5 @@ type GenerateRequest = { | ||
```ts | ||
const { id } = await generate('horde', params, apikey) | ||
const response = await waitForResposne('horde', id) | ||
const models = await horde.getModels('text') | ||
const { id } = await horde.generate(params, apikey) | ||
const response = await horde.waitForResponse(id) | ||
@@ -84,7 +80,3 @@ return response.generations | ||
```ts | ||
function waitForResponse( | ||
type: 'horde' | 'kobold', | ||
id: string, | ||
timeoutSecs?: number | ||
): Promise<StatusResponse> | ||
function waitForResponse(id: string, timeoutSecs?: number): Promise<StatusResponse> | ||
@@ -115,9 +107,6 @@ type StatusResponse = { | ||
Retrieve user information. If `type` is not provided, both Horde APIs will be queried and the valid response will be returned. | ||
```ts | ||
function findUser(apikey: string, type?: 'horde' | 'kobold'): Promise<FindUserResponse> | ||
function findUser(apikey: string): Promise<FindUserResponse> | ||
type FindUserResponse = { | ||
type: 'horde' | 'kobold' | ||
kudos_details: { | ||
@@ -124,0 +113,0 @@ accumulated: number |
@@ -1,19 +0,14 @@ | ||
import needle from 'needle' | ||
import { ApiType, FindUserResponse, GenerateRequest, StatusResponse } from './types' | ||
import { get, post } from './request' | ||
import { FindUserResponse, GenerateRequest, Model, StatusResponse } from './types' | ||
const DEFAULT_API_KEY = '0000000000' | ||
const urls = { | ||
horde: `https://stablehorde.net/api/v2`, | ||
kobold: `https://koboldai.net/api/v2`, | ||
} | ||
export async function generate(type: ApiType, request: GenerateRequest, apikey = DEFAULT_API_KEY) { | ||
const url = type === 'kobold' ? `/generate/async` : `/generate/text/async` | ||
const res = await post<{ id: string }>({ type, url, body: request, apikey }) | ||
export async function generate(request: GenerateRequest, apikey = DEFAULT_API_KEY) { | ||
const url = `/generate/text/async` | ||
const res = await post<{ id: string }>({ url, body: request, apikey }) | ||
return res | ||
} | ||
export async function waitForResponse(type: ApiType, id: string, timeoutSecs?: number) { | ||
const url = type === 'horde' ? `/generate/text/status/${id}` : `/generate/check/${id}` | ||
export async function waitForResponse(id: string, timeoutSecs?: number) { | ||
const url = `/generate/text/status/${id}` | ||
@@ -29,3 +24,3 @@ const start = Date.now() | ||
const res = await get<StatusResponse>({ url, type }) | ||
const res = await get<StatusResponse>({ url }) | ||
if (res.done) { | ||
@@ -45,72 +40,15 @@ return res | ||
/** | ||
* If the 'type' is not provided both APIs will be queried | ||
* | ||
* @param apikey | ||
* @param type | ||
* @returns | ||
*/ | ||
export async function findUser(apikey: string, type?: ApiType) { | ||
if (type) { | ||
const user = await get<FindUserResponse>({ url: '/find_user', type, apikey }) | ||
return { ...user, type } | ||
} | ||
const [horde, kobold] = await Promise.allSettled([ | ||
get<FindUserResponse>({ url: `/find_user`, type: 'horde', apikey }), | ||
get<FindUserResponse>({ url: `/find_user`, type: 'kobold', apikey }), | ||
]) | ||
if (horde.status === 'fulfilled') { | ||
return { ...horde.value, type: 'horde' as const } | ||
} | ||
if (kobold.status === 'fulfilled') { | ||
return { ...kobold.value, type: 'kobold' as const } | ||
} | ||
throw new Error(`User not found`) | ||
export async function findUser(apikey: string) { | ||
const user = await get<FindUserResponse>({ url: `/find_user`, apikey }) | ||
return user | ||
} | ||
async function post<T = any>({ type, url, apikey, body }: PostReq) { | ||
const baseUrl = urls[type] | ||
const headers: any = {} | ||
if (apikey) { | ||
headers.apikey = apikey | ||
} | ||
const res = await needle('post', `${baseUrl}${url}`, body, { json: true, headers }) | ||
if (res.statusCode && res.statusCode >= 400) { | ||
const error: any = new Error(`${res.statusMessage}: ${res.statusCode}`) | ||
error.body = res.body | ||
throw error | ||
} | ||
return res.body as T | ||
export async function getModels(type: 'image' | 'text' = 'image') { | ||
const url = `/status/models?type=${type}` | ||
const models = await get<Model[]>({ url }) | ||
return models | ||
} | ||
async function get<T = any>({ type, url, apikey }: Omit<PostReq, 'body'>) { | ||
const baseUrl = urls[type] | ||
const headers: any = {} | ||
if (apikey) { | ||
headers.apikey = apikey | ||
} | ||
const res = await needle('get', `${baseUrl}${url}`, { json: true, headers }) | ||
if (res.statusCode && res.statusCode >= 400) { | ||
const error: any = new Error(`${res.statusMessage}: ${res.statusCode}`) | ||
error.body = res.body | ||
throw error | ||
} | ||
return res.body as T | ||
} | ||
async function wait(secs = 2) { | ||
return new Promise((resolve) => setTimeout(resolve, secs * 1000)) | ||
} | ||
type PostReq = { | ||
type: ApiType | ||
url: string | ||
apikey?: string | ||
body: any | ||
} |
@@ -1,3 +0,1 @@ | ||
export type ApiType = 'horde' | 'kobold' | ||
export type GenerateRequest = { | ||
@@ -80,1 +78,10 @@ prompt: string | ||
} | ||
export type Model = { | ||
name: string | ||
count: number | ||
performance: number | ||
queued: number | ||
eta: number | ||
type?: string | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16
20114
356
134