UNPKG

@isdk/ai-tool-llm-openai

Version:

llama.cpp LLM Provider - OpenAI Compatible

1 lines 3.33 kB
function e(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 s={content:n.delta.content||"",options:{generation_settings:o},finishReason:n.finish_reason};return n.finish_reason&&(s.stop=!0),s}function t(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}}import n from"openai";import{AsyncFeatures as o,NotFoundError as s,makeToolFuncCancelable as i,throwError as r}from"@isdk/ai-tool";import{LLMProvider as a}from"@isdk/ai-tool-llm";var c="openai",p=["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"],l=class extends a{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 s("missing model name","OpenaiProvider");if(e.startsWith(c+"://")&&(e=e.slice(9)),n.model=e,t&&(o||Array.isArray(t))||r("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&&(p.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:o,value:s,options:i}){return this.runAsyncCancelableTask(i,(async r=>{const a=r.aborter.signal;delete(r={...r}).aborter,r=await this.processModelOptions(o,s,r);const c=i.apiUrl||this.apiUrl,p=i.apiKey||this.apiKey,l={};c&&(l.baseURL=c),p&&(l.apiKey=p);const m=new n(l);r.chatTemplateId&&delete r.chatTemplateId,s=r.value,delete r.value;const d={...r,messages:s};let f=await m.chat.completions.create(d,{signal:a});return r=Object.fromEntries(Object.entries(r).filter((([e,t])=>void 0!==t&&!["name","signal","messages"].includes(e)))),f=f.toReadableStream?f.toReadableStream().pipeThrough(function(t){return new TransformStream({transform(n,o){o.enqueue(e(n,t))}})}(r)):t(f,r),f}))}};i(l,{asyncFeatures:o.MultiTask});var m=new l(c);export{l as OpenaiProvider,c as OpenaiProviderName,m as openai,t as openaiToAIResult,e as openaiToAIResultChunk};