UNPKG

llms-client

Version:

A powerful package that enables users to effortlessly connect to various Language Models, including Claude, Bard, and ChatGPT. Stay tuned for upcoming support for Vicuna and Alpaca!

3 lines (2 loc) 11.3 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("axios"))&&"object"==typeof t&&"default"in t?t.default:t;function r(){r=function(){return t};var t={},e=Object.prototype,n=e.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},a="function"==typeof Symbol?Symbol:{},i=a.iterator||"@@iterator",c=a.asyncIterator||"@@asyncIterator",s=a.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function p(t,e,r,n){var a=Object.create((e&&e.prototype instanceof f?e:f).prototype),i=new j(n||[]);return o(a,"_invoke",{value:L(t,r,i)}),a}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=p;var h={};function f(){}function d(){}function v(){}var m={};u(m,i,(function(){return this}));var y=Object.getPrototypeOf,g=y&&y(y(C([])));g&&g!==e&&n.call(g,i)&&(m=g);var w=v.prototype=f.prototype=Object.create(m);function x(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function b(t,e){var r;o(this,"_invoke",{value:function(o,a){function i(){return new e((function(r,i){!function r(o,a,i,c){var s=l(t[o],t,a);if("throw"!==s.type){var u=s.arg,p=u.value;return p&&"object"==typeof p&&n.call(p,"__await")?e.resolve(p.__await).then((function(t){r("next",t,i,c)}),(function(t){r("throw",t,i,c)})):e.resolve(p).then((function(t){u.value=t,i(u)}),(function(t){return r("throw",t,i,c)}))}c(s.arg)}(o,a,r,i)}))}return r=r?r.then(i,i):i()}})}function L(t,e,r){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var c=E(i,r);if(c){if(c===h)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=l(t,e,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===h)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}function E(t,e){var r=e.method,n=t.iterator[r];if(void 0===n)return e.delegate=null,"throw"===r&&t.iterator.return&&(e.method="return",e.arg=void 0,E(t,e),"throw"===e.method)||"return"!==r&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+r+"' method")),h;var o=l(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,h;var a=o.arg;return a?a.done?(e[t.resultName]=a.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,h):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,h)}function _(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function k(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function j(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function C(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,o=function e(){for(;++r<t.length;)if(n.call(t,r))return e.value=t[r],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:O}}function O(){return{value:void 0,done:!0}}return d.prototype=v,o(w,"constructor",{value:v,configurable:!0}),o(v,"constructor",{value:d,configurable:!0}),d.displayName=u(v,s,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===d||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,v):(t.__proto__=v,u(t,s,"GeneratorFunction")),t.prototype=Object.create(w),t},t.awrap=function(t){return{__await:t}},x(b.prototype),u(b.prototype,c,(function(){return this})),t.AsyncIterator=b,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new b(p(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},x(w),u(w,s,"Generator"),u(w,i,(function(){return this})),u(w,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},t.values=C,j.prototype={constructor:j,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!t)for(var e in this)"t"===e.charAt(0)&&n.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function r(r,n){return i.type="throw",i.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var c=n.call(a,"catchLoc"),s=n.call(a,"finallyLoc");if(c&&s){if(this.prev<a.catchLoc)return r(a.catchLoc,!0);if(this.prev<a.finallyLoc)return r(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return r(a.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return r(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=e&&e<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=e,a?(this.method="next",this.next=a.finallyLoc,h):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),h},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),k(r),h}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;k(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:C(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),h}},t}function n(t,e,r,n,o,a,i){try{var c=t[a](i),s=c.value}catch(t){return void r(t)}c.done?e(s):Promise.resolve(s).then(n,o)}function o(t){return function(){var e=this,r=arguments;return new Promise((function(o,a){var i=t.apply(e,r);function c(t){n(i,o,a,c,s,"next",t)}function s(t){n(i,o,a,c,s,"throw",t)}c(void 0)}))}}var a=function(){function t(t){this.apiUrl=t.baseurl,this.apiKey=t.apiKey}var n=t.prototype;return n.complete=function(){var t=o(r().mark((function t(n,o){var a;return r().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,e.defaults.headers.common.Authorization="Bearer "+this.apiKey,e.defaults.headers.common["Content-Type"]="application/json",t.next=5,e.post(this.apiUrl,{prompt:n,maxTokens:o});case 5:if(200!==(a=t.sent).status){t.next=10;break}return t.abrupt("return",a.data.completion);case 10:throw new Error("Error completing prompt: "+a.status);case 11:t.next=17;break;case 13:throw t.prev=13,t.t0=t.catch(0),console.error("Error completing prompt:",t.t0),t.t0;case 17:case"end":return t.stop()}}),t,this,[[0,13]])})));return function(e,r){return t.apply(this,arguments)}}(),n.getChatCompletions=function(t,e){throw console.log({messages:t,maxTokens:e}),new Error("Method not implemented.")},t}(),i=function(){function t(t){this.axiosInstance=e.create({timeout:t.timeout||1e4,baseURL:t.baseurl,headers:{"Content-Type":"application/json",Authorization:"Bearer "+t.apiKey}}),this.model=t.model}var n=t.prototype;return n.complete=function(){var t=o(r().mark((function t(e,n){return r().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,this.axiosInstance.post("/v1/completions",{model:this.model,prompt:e,max_tokens:n,temperature:0});case 3:return t.abrupt("return",t.sent.data.choices[0].text);case 7:throw t.prev=7,t.t0=t.catch(0),console.log(t.t0),t.t0;case 11:case"end":return t.stop()}}),t,this,[[0,7]])})));return function(e,r){return t.apply(this,arguments)}}(),n.getChatCompletions=function(){var t=o(r().mark((function t(e,n){var o;return r().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return console.log({messages:e,maxTokens:n}),o={model:"gpt-3.5-turbo",messages:e,temperature:.7},t.prev=2,t.next=5,this.axiosInstance.post("/v1/chat/completions",o);case 5:return t.abrupt("return",t.sent.data.choices[0].message.content);case 9:throw t.prev=9,t.t0=t.catch(2),console.error(t.t0),t.t0;case 13:case"end":return t.stop()}}),t,this,[[2,9]])})));return function(e,r){return t.apply(this,arguments)}}(),t}(),c=function(){function t(t){this.axiosInstance=e.create({timeout:t.timeout||1e4,baseURL:t.baseurl,headers:{"x-api-key":t.apiKey,"content-type":"application/json"},maxBodyLength:1/0}),this.model=t.model}var n=t.prototype;return n.complete=function(){var t=o(r().mark((function t(e,n){var o;return r().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return console.log("Claude adapter called ..."),t.prev=1,o={method:"post",data:{prompt:"\n\nHuman: "+e+"\n\nAssistant: ",model:this.model,max_tokens_to_sample:n,stop_sequences:["\n\nHuman:"]}},t.next=6,this.axiosInstance.request(o);case 6:return t.abrupt("return",t.sent.data.completion);case 10:throw t.prev=10,t.t0=t.catch(1),console.error("Error completing prompt:",t.t0),t.t0;case 14:case"end":return t.stop()}}),t,this,[[1,10]])})));return function(e,r){return t.apply(this,arguments)}}(),n.getChatCompletions=function(){var t=o(r().mark((function t(e,n){var o,a;return r().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=e.map((function(t){return t.role+": "+t.content})).join("\n\n"),a={prompt:o,model:this.model,max_tokens_to_sample:n,stop_sequences:["\n\nHuman:"]},console.log({data:a}),t.prev=3,t.next=6,this.axiosInstance.post("",a);case 6:return t.abrupt("return",t.sent.data.completion);case 10:throw t.prev=10,t.t0=t.catch(3),console.error("Error getting chat completions:",t.t0),t.t0;case 14:case"end":return t.stop()}}),t,this,[[3,10]])})));return function(e,r){return t.apply(this,arguments)}}(),t}();exports.CompletionServiceSelector=function(){function t(t){switch(t.type){case"chatGPT":if(!t.model)throw Error("Model is required for ChatGPT adapter");this.adapter=new i(t);break;case"claudeAI":this.adapter=new c(t);break;case"bard":this.adapter=new a(t);break;default:throw Error("Invalid adapter: "+t.type)}}var e=t.prototype;return e.getChatCompletions=function(t,e){return this.adapter.getChatCompletions(t,e)},e.complete=function(t,e){return this.adapter.complete(t,e)},t}(); //# sourceMappingURL=llms-client.cjs.production.min.js.map