UNPKG

@isdk/ai-tool-llm-openai

Version:

llama.cpp LLM Provider - OpenAI Compatible

1 lines 3.9 kB
"use strict";var e,t=Object.create,n=Object.defineProperty,o=Object.getOwnPropertyDescriptor,i=Object.getOwnPropertyNames,s=Object.getPrototypeOf,r=Object.prototype.hasOwnProperty,a=(e,t,s,a)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let c of i(t))r.call(e,c)||c===s||n(e,c,{get:()=>t[c],enumerable:!(a=o(t,c))||a.enumerable});return e},c={};function l(e,t){if(e instanceof Uint8Array){const t=(new TextDecoder).decode(e);e=JSON.parse(t)}if(0===e.choices.length)return{content:"",options:e,finishReason:"error"};const n=e.choices[0],o={...t,...e};delete o.choices;const i={content:n.delta.content||"",options:{generation_settings:o},finishReason:n.finish_reason};return n.finish_reason&&(i.stop=!0),i}function p(e,t){if(0===e.choices.length)return{content:"",options:e,finishReason:"error"};const n=e.choices[0],o={...t,...e};delete o.choices;return{content:n.message.content||"",role:n.message.role,options:{generation_settings:o},finishReason:n.finish_reason}}((e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})})(c,{OpenaiProvider:()=>h,OpenaiProviderName:()=>f,openai:()=>v,openaiToAIResult:()=>p,openaiToAIResultChunk:()=>l}),module.exports=(e=c,a(n({},"__esModule",{value:!0}),e));var u=((e,o,i)=>(i=null!=e?t(s(e)):{},a(!o&&e&&e.__esModule?i:n(i,"default",{value:e,enumerable:!0}),e)))(require("openai")),m=require("@isdk/ai-tool"),d=require("@isdk/ai-tool-llm"),f="openai",g=["gpt-4o-2024-08-06","gpt-4o-mini-2024-07-18","gpt-4o-mini","chatgpt-4o-latest","o1-preview","o1-preview-2024-09-12","o1-mini"],h=class extends d.LLMProvider{constructor(){super(...arguments),this.rule=[/^gpt-4o(-2024-(05-13|08-06)|-mini(-2024-07-18)?)?$/,/^chatgpt-4o-latest$/,/^o1-preview(-2024-09-12)?$/,/^o1-mini(-2024-09-12)?$/,/^gpt-(4|3\.5)(-[\w\d]+)*?$/,/^text-embedding-3-(large|small)/,"text-embedding-ada-002"]}async processModelOptions(e,t,n){const o="string"==typeof t,i=n.apiUrl||this.apiUrl;if(!e&&this.model&&(e=this.model),!e)throw new m.NotFoundError("missing model name","OpenaiProvider");if(e.startsWith(f+"://")&&(e=e.slice(f.length+3)),n.model=e,t&&(o||Array.isArray(t))||(0,m.throwError)("missing prompt value","OpenaiProvider"),n.stop&&"string"==typeof n.stop&&(n.stop=[n.stop]),n.stop_words){let e=n.stop_words||[];"string"==typeof n.stop_words&&e.push(n.stop_words),n.stop?n.stop=[...new Set([...e,...n.stop])]:n.stop=e}if("json_object"===n.response_format?.type&&n.response_format.schema){i&&!i.includes("api.openai.com")||(n.response_format.type="json_schema");const e=n.response_format.schema;n.response_format.name?e.name=n.response_format.name:e.name="result",n.response_format.description&&(e.description=n.response_format.description),delete n.response_format.description,void 0!==n.response_format.strict&&(e.strict=!!n.response_format.strict),delete n.response_format.strict,n.response_format.json_schema={schema:e},delete n.response_format.schema}return"json_schema"===n.response_format?.type&&(g.includes(e)||console.error(`WARN: The json schema response_format is only supported for latest gpt-4o models, not for ${e}.`)),o&&(t=[{role:"user",content:t}]),n.value=t,n}func({model:e,value:t,options:n}){return this.runAsyncCancelableTask(n,(async o=>{const i=o.aborter.signal;delete(o={...o}).aborter,o=await this.processModelOptions(e,t,o);const s=n.apiUrl||this.apiUrl,r=n.apiKey||this.apiKey,a={};s&&(a.baseURL=s),r&&(a.apiKey=r);const c=new u.default(a);o.chatTemplateId&&delete o.chatTemplateId,t=o.value,delete o.value;const m={...o,messages:t};let d=await c.chat.completions.create(m,{signal:i});return o=Object.fromEntries(Object.entries(o).filter((([e,t])=>void 0!==t&&!["name","signal","messages"].includes(e)))),d=d.toReadableStream?d.toReadableStream().pipeThrough(function(e){return new TransformStream({transform(t,n){n.enqueue(l(t,e))}})}(o)):p(d,o),d}))}};(0,m.makeToolFuncCancelable)(h,{asyncFeatures:m.AsyncFeatures.MultiTask});var v=new h(f);