@convo-lang/convo-lang
Version:
The language of AI
107 lines • 4.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpConvoCompletionService = exports.convoHttpRelayModule = exports.httpConvoCompletionEndpointParam = exports.defaultConvoHttpApiEndpointPrefix = exports.defaultConvoHttpEndpointPrefix = void 0;
const common_1 = require("@iyio/common");
const convo_lib_1 = require("./convo-lib");
const convo_rag_lib_1 = require("./convo-rag-lib");
const convo_deps_1 = require("./convo.deps");
exports.defaultConvoHttpEndpointPrefix = '/convo-lang';
exports.defaultConvoHttpApiEndpointPrefix = '/api' + exports.defaultConvoHttpEndpointPrefix;
exports.httpConvoCompletionEndpointParam = (0, common_1.defineStringParam)('httpConvoCompletionEndpoint', exports.defaultConvoHttpApiEndpointPrefix);
const convoHttpRelayModule = (scope) => {
scope.implementService(convo_deps_1.convoCompletionService, scope => HttpConvoCompletionService.fromScope(scope));
scope.implementService(convo_rag_lib_1.convoRagService, scope => HttpConvoCompletionService.fromScope(scope));
};
exports.convoHttpRelayModule = convoHttpRelayModule;
/**
* Forwards messages to an convo-lang api endpoint or pool of convo-lang api endpoints.
*
* ## Endpoint structure
*
* ### POST /completion (flat:FlatConvoConversationBase) => ConvoCompletionMessage[]
* Completes a posted flat conversation and returns the completed messages.
*
* ### POST /convo (convo:string) => string
* Completes a convo conversation as a string and returns the completed messages as a string
*
* ### GET /models ()=>ConvoModelInfo[]
* Returns all models known to the server
*/
class HttpConvoCompletionService {
serviceId = 'http';
static fromScope(scope, endpoint) {
if (!endpoint) {
const ep = (0, exports.httpConvoCompletionEndpointParam)().split(',').map(e => e.trim()).filter(e => e);
endpoint = ep.length === 1 ? ep[0] : ep;
}
if (!endpoint) {
throw new Error('Empty HttpConvoCompletionService provided');
}
return new HttpConvoCompletionService({
endpoint
});
}
inputType = convo_lib_1.passthroughConvoInputType;
outputType = convo_lib_1.passthroughConvoOutputType;
endpoint;
constructor({ endpoint }) {
if (Array.isArray(endpoint)) {
endpoint = [...endpoint];
(0, common_1.aryRandomize)(endpoint);
}
this.endpoint = endpoint;
}
canComplete(model, flat) {
return true;
}
endpointIndex = 0;
getEndpoint() {
if (Array.isArray(this.endpoint)) {
const e = this.endpoint[this.endpointIndex];
this.endpointIndex++;
if (this.endpointIndex >= this.endpoint.length) {
this.endpointIndex = 0;
(0, common_1.aryRandomize)(this.endpoint);
}
return e ?? '';
}
else {
return this.endpoint;
}
}
async completeConvoAsync(flat) {
const r = await (0, common_1.httpClient)().postAsync((0, common_1.joinPaths)(this.getEndpoint(), '/completion'), (0, convo_lib_1.getSerializableFlatConvoConversation)(flat));
if (!r) {
throw new Error('convo-lang ai endpoint returned empty response');
}
if (!Array.isArray(r) && r.messages) {
return r.messages;
}
else {
return r;
}
}
getModelsAsync() {
return (0, common_1.httpClient)().getAsync((0, common_1.joinPaths)(this.getEndpoint(), '/models'));
}
async relayConvertConvoToInputAsync(flat, inputType) {
const request = {
flat: (0, convo_lib_1.getSerializableFlatConvoConversation)(flat),
inputType
};
const r = await (0, common_1.httpClient)().postAsync((0, common_1.joinPaths)(this.getEndpoint(), '/convert'), request);
if (!r) {
throw new common_1.NotFoundError();
}
return r;
}
async searchAsync(search) {
const r = await (0, common_1.httpClient)().postAsync((0, common_1.joinPaths)(this.getEndpoint(), '/rag/search'), search);
if (!r) {
throw new Error('convo-lang ai endpoint returned empty response');
}
return r;
}
}
exports.HttpConvoCompletionService = HttpConvoCompletionService;
//# sourceMappingURL=HttpConvoCompletionService.js.map