promptlayer 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.mts +29 -2
- package/dist/index.d.ts +29 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +128 -0
- package/src/types.ts +26 -3
- package/src/utils.ts +187 -11
package/dist/esm/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var Q=Object.defineProperty,V=Object.defineProperties;var Z=Object.getOwnPropertyDescriptors;var I=Object.getOwnPropertySymbols;var ee=Object.prototype.hasOwnProperty,te=Object.prototype.propertyIsEnumerable;var S=(r,e)=>{if(e=Symbol[r])return e;throw Error("Symbol."+r+" is not defined")};var W=(r,e,t)=>e in r?Q(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,h=(r,e)=>{for(var t in e||(e={}))ee.call(e,t)&&W(r,t,e[t]);if(I)for(var t of I(e))te.call(e,t)&&W(r,t,e[t]);return r},w=(r,e)=>V(r,Z(e));var O=(r=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(r,{get:(e,t)=>(typeof require!="undefined"?require:e)[t]}):r)(function(r){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var u=(r,e,t)=>new Promise((o,a)=>{var n=s=>{try{i(t.next(s))}catch(m){a(m)}},p=s=>{try{i(t.throw(s))}catch(m){a(m)}},i=s=>s.done?o(s.value):Promise.resolve(s.value).then(n,p);i((t=t.apply(r,e)).next())}),_=function(r,e){this[0]=r,this[1]=e},q=(r,e,t)=>{var o=(p,i,s,m)=>{try{var l=t[p](i),d=(i=l.value)instanceof _,f=l.done;Promise.resolve(d?i[0]:i).then(c=>d?o(p==="return"?p:"next",i[1]?{done:c.done,value:c.value}:c,s,m):s({value:c,done:f})).catch(c=>o("throw",c,s,m))}catch(c){m(c)}},a=p=>n[p]=i=>new Promise((s,m)=>o(p,i,s,m)),n={};return t=t.apply(r,e),n[Symbol.asyncIterator]=()=>n,a("next"),a("throw"),a("return"),n};var x=(r,e,t)=>(e=r[S("asyncIterator")])?e.call(r):(r=r[S("iterator")](),e={},t=(o,a)=>(a=r[o])&&(e[o]=n=>new Promise((p,i,s)=>(n=a.call(r,n),s=n.done,Promise.resolve(n.value).then(m=>p({value:m,done:s}),i)))),t("next"),t("return"),e);var g="https://api.promptlayer.com",j=(r,e)=>u(void 0,null,function*(){return e.request_response[Symbol.asyncIterator]!==void 0?ne(r,e.request_response,e):yield v(r,e)}),v=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),o=yield t.json();if(t.status!==200&&y(o,"WARNING: While logging your request, PromptLayer experienced the following error:"),o&&e.return_pl_id)return[e.request_response,o.request_id]}catch(t){console.warn(`WARNING: While logging your request PromptLayer had the following error: ${t}`)}return e.request_response}),K=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-metadata`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(w(h({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return y(o,"WARNING: While logging metadata to your request, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While logging metadata to your request, PromptLayer experienced the following error: ${t}`),!1}return!0}),M=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-score`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(w(h({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return y(o,"WARNING: While scoring your request, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While scoring your request, PromptLayer experienced the following error: ${t}`),!1}return!0}),E=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-prompt`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(w(h({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return y(o,"WARNING: While associating your request with a prompt template, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While associating your request with a prompt template, PromptLayer experienced the following error: ${t}`),!1}return!0}),J=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/track-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(w(h({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return y(o,"WARNING: While associating your request with a group, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While associating your request with a group, PromptLayer experienced the following error: ${t}`),!1}return!0}),Y=r=>u(void 0,null,function*(){try{let e=yield fetch(`${g}/create-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({api_key:r})}),t=yield e.json();return e.status!==200?(y(t,"WARNING: While creating a group PromptLayer had the following error"),!1):t.id}catch(e){return console.warn(`WARNING: While creating a group PromptLayer had the following error: ${e}`),!1}}),D=(r,e,t)=>u(void 0,null,function*(){try{let o=new URL(`${g}/prompt-templates/${e}`),a=yield fetch(o,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":r},body:JSON.stringify(t)}),n=yield a.json();return a.status!==200?(y(n,"WARNING: While fetching a prompt template PromptLayer had the following error"),null):n}catch(o){return console.warn(`WARNING: While fetching a prompt template PromptLayer had the following error: ${o}`),null}}),B=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/rest/prompt-templates`,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":r},body:JSON.stringify({prompt_template:h({},e),prompt_version:h({},e),release_labels:e.release_labels?e.release_labels:void 0})}),o=yield t.json();return t.status===400&&y(o,"WARNING: While publishing a prompt template PromptLayer had the following error"),o}catch(t){console.warn(`WARNING: While publishing a prompt template PromptLayer had the following error: ${t}`)}}),H=(r,e)=>u(void 0,null,function*(){var t;try{let o=new URL(`${g}/prompt-templates`);Object.entries(e||{}).forEach(([p,i])=>o.searchParams.append(p,i.toString()));let a=yield fetch(o,{headers:{"Content-Type":"application/json","X-API-KEY":r}}),n=yield a.json();return a.status!==200?(y(n,"WARNING: While fetching all prompt templates PromptLayer had the following error"),null):(t=n.items)!=null?t:[]}catch(o){return console.warn(`WARNING: While fetching all prompt templates PromptLayer had the following error: ${o}`),null}}),re=r=>{var p,i,s,m,l;let e=null,t,o={id:"",choices:[],created:Date.now(),model:"",object:"chat.completion"},a=r.at(-1);if(!a)return o;let n;for(let d of r){let f=d.choices[0].delta;f.content&&(e=`${e||""}${f.content||""}`),f.function_call&&(t={name:`${t?t.name:""}${f.function_call.name||""}`,arguments:`${t?t.arguments:""}${f.function_call.arguments||""}`});let c=(p=f.tool_calls)==null?void 0:p[0];if(c){n=n||[];let P=n.at(-1);if(!P||c.id){n.push({id:c.id||"",type:c.type||"function",function:{name:((i=c.function)==null?void 0:i.name)||"",arguments:((s=c.function)==null?void 0:s.arguments)||""}});continue}P.function.name=`${P.function.name}${((m=c.function)==null?void 0:m.name)||""}`,P.function.arguments=`${P.function.arguments}${((l=c.function)==null?void 0:l.arguments)||""}`}}return o.choices.push({finish_reason:a.choices[0].finish_reason||"stop",index:a.choices[0].index||0,logprobs:a.choices[0].logprobs||null,message:{role:"assistant",content:e,function_call:t||void 0,tool_calls:n||void 0}}),o.id=a.id,o.model=a.model,o.created=a.created,o.system_fingerprint=a.system_fingerprint,o},oe=r=>{let e={id:"",model:"",content:[],role:"assistant",type:"message",stop_reason:"stop_sequence",stop_sequence:null,usage:{input_tokens:0,output_tokens:0}};if(!r.at(-1))return e;let o="";for(let a of r)switch(a.type){case"message_start":{e=h({},a.message);break}case"content_block_delta":a.delta.type==="text_delta"&&(o=`${o}${a.delta.text}`);case"message_delta":"usage"in a&&(e.usage.output_tokens=a.usage.output_tokens),"stop_reason"in a.delta&&(e.stop_reason=a.delta.stop_reason);default:break}return e.content.push({type:"text",text:o}),e},ae=(r,e="openai.chat.completions.create")=>{if("completion"in r[0])return r.reduce((t,o)=>w(h({},o),{completion:`${t.completion}${o.completion}`}),{});if(e==="anthropic.messages.create")return oe(r);if("text"in r[0].choices[0]){let t="";for(let a of r)t=`${t}${a.choices[0].text}`;let o=structuredClone(r.at(-1));return o.choices[0].text=t,o}if("delta"in r[0].choices[0]){let t=re(r);return t.choices[0]=h(h({},t.choices[0]),t.choices[0].message),t}return""};function ne(r,e,t){return q(this,null,function*(){let o=[];try{for(var p=x(e),i,s,m;i=!(s=yield new _(p.next())).done;i=!1){let l=s.value;yield t.return_pl_id?[l,null]:l,o.push(l)}}catch(s){m=[s]}finally{try{i&&(s=p.return)&&(yield new _(s.call(p)))}finally{if(m)throw m[0]}}let a=ae(o,t.function_name),n=yield new _(v(r,w(h({},t),{request_response:a,request_end_time:new Date().toISOString()})));if(n&&t.return_pl_id){let l=n[1];yield[o.at(-1),l]}})}var y=(r,e)=>{try{console.warn(`${e}: ${r.message}`)}catch(t){console.warn(`${e}: ${r}`)}};var T=class{constructor(e){this.create=()=>Y(this.apiKey);this.apiKey=e}};var C=(r,e,t="",o="openai")=>{let a={construct:(n,p)=>{let i=Reflect.construct(n,p);return Object.defineProperties(i,{function_name:{value:t,writable:!0},provider:{value:o}}),new Proxy(i,a)},get:(n,p,i)=>{let s=n[p],m=`${Reflect.get(n,"function_name")}.${p.toString()}`;return typeof s=="object"?(Object.defineProperties(s,{function_name:{value:m,writable:!0},provider:{value:o}}),new Proxy(s,a)):typeof s=="function"?(...l)=>{var $,A,L,G;let d=new Date().toISOString(),f=Reflect.get(n,"provider"),c=($=l[0])==null?void 0:$.return_pl_id,P=(A=l[0])==null?void 0:A.pl_tags;(L=l[0])==null||delete L.return_pl_id,(G=l[0])==null||delete G.pl_tags;let b=Reflect.apply(s,n,l);return b instanceof Promise?new Promise((X,z)=>{b.then(N=>u(void 0,null,function*(){let F=yield j(r,{api_key:r,provider_type:f,function_name:m,request_start_time:d,request_end_time:new Date().toISOString(),request_response:N,kwargs:l[0],return_pl_id:c,tags:P});X(F)})).catch(N=>{z(N)})}):b}:Reflect.get(n,p,i)}};return new Proxy(e,a)};var k=class{constructor(e){this.get=(e,t)=>D(this.apiKey,e,t);this.publish=e=>B(this.apiKey,e);this.all=e=>H(this.apiKey,e);this.apiKey=e}};var se=(r,e)=>{if(!(e.metadata instanceof Object))throw new Error("Please provide a dictionary of metadata.");for(let[t,o]of Object.entries(e.metadata))if(typeof t!="string"||typeof o!="string")throw new Error("Please provide a dictionary of metadata with key value pair of strings.");return K(r,e)},ie=(r,e)=>{if(typeof e.score!="number")throw new Error("Score must be a number");if(e.score<0||e.score>100)throw new Error("Score must be a number between 0 and 100.");return M(r,e)},pe=(r,e)=>{if(!(e.prompt_input_variables instanceof Object))throw new Error("Prompt template input variable dictionary not provided.");return E(r,e)},ce=(r,e)=>J(r,e),R=class{constructor(e){this.group=e=>ce(this.apiKey,e);this.metadata=e=>se(this.apiKey,e);this.prompt=e=>pe(this.apiKey,e);this.score=e=>ie(this.apiKey,e);this.apiKey=e}};var U=class{constructor({apiKey:e=process.env.PROMPTLAYER_API_KEY}={}){if(e===void 0)throw new Error("PromptLayer API key not provided. Please set the PROMPTLAYER_API_KEY environment variable or pass the api_key parameter.");this.apiKey=e,this.templates=new k(e),this.group=new T(e),this.track=new R(e)}get OpenAI(){try{let e=O("openai").default;return C(this.apiKey,e,"openai","openai")}catch(e){console.error("To use the OpenAI module, you must install the @openai/api package.")}}get Anthropic(){try{let e=O("@anthropic-ai/sdk").default;return C(this.apiKey,e,"anthropic","anthropic")}catch(e){console.error("To use the Anthropic module, you must install the @anthropic-ai/sdk package.")}}};export{U as PromptLayer};
|
|
1
|
+
var le=Object.defineProperty,me=Object.defineProperties;var ue=Object.getOwnPropertyDescriptors;var U=Object.getOwnPropertySymbols;var he=Object.prototype.hasOwnProperty,fe=Object.prototype.propertyIsEnumerable;var D=(r,e)=>{if(e=Symbol[r])return e;throw Error("Symbol."+r+" is not defined")};var Y=(r,e,t)=>e in r?le(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,d=(r,e)=>{for(var t in e||(e={}))he.call(e,t)&&Y(r,t,e[t]);if(U)for(var t of U(e))fe.call(e,t)&&Y(r,t,e[t]);return r},R=(r,e)=>me(r,ue(e));var N=(r=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(r,{get:(e,t)=>(typeof require!="undefined"?require:e)[t]}):r)(function(r){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var c=(r,e,t)=>new Promise((o,n)=>{var a=s=>{try{p(t.next(s))}catch(h){n(h)}},i=s=>{try{p(t.throw(s))}catch(h){n(h)}},p=s=>s.done?o(s.value):Promise.resolve(s.value).then(a,i);p((t=t.apply(r,e)).next())}),w=function(r,e){this[0]=r,this[1]=e},W=(r,e,t)=>{var o=(i,p,s,h)=>{try{var m=t[i](p),f=(p=m.value)instanceof w,u=m.done;Promise.resolve(f?p[0]:p).then(l=>f?o(i==="return"?i:"next",p[1]?{done:l.done,value:l.value}:l,s,h):s({value:l,done:u})).catch(l=>o("throw",l,s,h))}catch(l){h(l)}},n=i=>a[i]=p=>new Promise((s,h)=>o(i,p,s,h)),a={};return t=t.apply(r,e),a[Symbol.asyncIterator]=()=>a,n("next"),n("throw"),n("return"),a};var x=(r,e,t)=>(e=r[D("asyncIterator")])?e.call(r):(r=r[D("iterator")](),e={},t=(o,n)=>(n=r[o])&&(e[o]=a=>new Promise((i,p,s)=>(a=n.call(r,a),s=a.done,Promise.resolve(a.value).then(h=>i({value:h,done:s}),p)))),t("next"),t("return"),e);var g="https://api.promptlayer.com",J=(r,e)=>c(void 0,null,function*(){return e.request_response[Symbol.asyncIterator]!==void 0?ye(r,e.request_response,e):yield B(r,e)}),B=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),o=yield t.json();if(t.status!==200&&_(o,"WARNING: While logging your request, PromptLayer experienced the following error:"),o&&e.return_pl_id)return[e.request_response,o.request_id]}catch(t){console.warn(`WARNING: While logging your request PromptLayer had the following error: ${t}`)}return e.request_response}),F=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-metadata`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(d({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return _(o,"WARNING: While logging metadata to your request, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While logging metadata to your request, PromptLayer experienced the following error: ${t}`),!1}return!0}),H=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-score`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(d({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return _(o,"WARNING: While scoring your request, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While scoring your request, PromptLayer experienced the following error: ${t}`),!1}return!0}),X=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-prompt`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(d({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return _(o,"WARNING: While associating your request with a prompt template, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While associating your request with a prompt template, PromptLayer experienced the following error: ${t}`),!1}return!0}),V=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/track-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(d({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return _(o,"WARNING: While associating your request with a group, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While associating your request with a group, PromptLayer experienced the following error: ${t}`),!1}return!0}),z=r=>c(void 0,null,function*(){try{let e=yield fetch(`${g}/create-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({api_key:r})}),t=yield e.json();return e.status!==200?(_(t,"WARNING: While creating a group PromptLayer had the following error"),!1):t.id}catch(e){return console.warn(`WARNING: While creating a group PromptLayer had the following error: ${e}`),!1}}),Q=(r,e,t)=>c(void 0,null,function*(){try{let o=new URL(`${g}/prompt-templates/${e}`),n=yield fetch(o,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":r},body:JSON.stringify(t)}),a=yield n.json();return n.status!==200?(_(a,"WARNING: While fetching a prompt template PromptLayer had the following error"),null):a}catch(o){return console.warn(`WARNING: While fetching a prompt template PromptLayer had the following error: ${o}`),null}}),Z=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/rest/prompt-templates`,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":r},body:JSON.stringify({prompt_template:d({},e),prompt_version:d({},e),release_labels:e.release_labels?e.release_labels:void 0})}),o=yield t.json();return t.status===400&&_(o,"WARNING: While publishing a prompt template PromptLayer had the following error"),o}catch(t){console.warn(`WARNING: While publishing a prompt template PromptLayer had the following error: ${t}`)}}),ee=(r,e)=>c(void 0,null,function*(){var t;try{let o=new URL(`${g}/prompt-templates`);Object.entries(e||{}).forEach(([i,p])=>o.searchParams.append(i,p.toString()));let n=yield fetch(o,{headers:{"Content-Type":"application/json","X-API-KEY":r}}),a=yield n.json();return n.status!==200?(_(a,"WARNING: While fetching all prompt templates PromptLayer had the following error"),null):(t=a.items)!=null?t:[]}catch(o){return console.warn(`WARNING: While fetching all prompt templates PromptLayer had the following error: ${o}`),null}}),v=r=>{var i,p,s,h,m;let e=null,t,o={id:"",choices:[],created:Date.now(),model:"",object:"chat.completion"},n=r.at(-1);if(!n)return o;let a;for(let f of r){if(f.choices.length===0)continue;let u=f.choices[0].delta;u.content&&(e=`${e||""}${u.content||""}`),u.function_call&&(t={name:`${t?t.name:""}${u.function_call.name||""}`,arguments:`${t?t.arguments:""}${u.function_call.arguments||""}`});let l=(i=u.tool_calls)==null?void 0:i[0];if(l){a=a||[];let y=a.at(-1);if(!y||l.id){a.push({id:l.id||"",type:l.type||"function",function:{name:((p=l.function)==null?void 0:p.name)||"",arguments:((s=l.function)==null?void 0:s.arguments)||""}});continue}y.function.name=`${y.function.name}${((h=l.function)==null?void 0:h.name)||""}`,y.function.arguments=`${y.function.arguments}${((m=l.function)==null?void 0:m.arguments)||""}`}}return o.choices.push({finish_reason:r[0].choices[0].finish_reason||"stop",index:r[0].choices[0].index||0,logprobs:r[0].choices[0].logprobs||null,message:{role:"assistant",content:e,function_call:t||void 0,tool_calls:a||void 0}}),o.id=n.id,o.model=n.model,o.created=n.created,o.system_fingerprint=n.system_fingerprint,o.usage=n.usage,o},j=r=>{let e={id:"",model:"",content:[],role:"assistant",type:"message",stop_reason:"stop_sequence",stop_sequence:null,usage:{input_tokens:0,output_tokens:0}};if(!r.at(-1))return e;let o="";for(let n of r)switch(n.type){case"message_start":{e=d({},n.message);break}case"content_block_delta":n.delta.type==="text_delta"&&(o=`${o}${n.delta.text}`);case"message_delta":"usage"in n&&(e.usage.output_tokens=n.usage.output_tokens),"stop_reason"in n.delta&&(e.stop_reason=n.delta.stop_reason);default:break}return e.content.push({type:"text",text:o}),e},de=(r,e="openai.chat.completions.create")=>{if("completion"in r[0])return r.reduce((t,o)=>R(d({},o),{completion:`${t.completion}${o.completion}`}),{});if(e==="anthropic.messages.create")return j(r);if("text"in r[0].choices[0]){let t="";for(let n of r)t=`${t}${n.choices[0].text}`;let o=structuredClone(r.at(-1));return o.choices[0].text=t,o}if("delta"in r[0].choices[0]){let t=v(r);return t.choices[0]=d(d({},t.choices[0]),t.choices[0].message),t}return""};function ye(r,e,t){return W(this,null,function*(){let o=[];try{for(var i=x(e),p,s,h;p=!(s=yield new w(i.next())).done;p=!1){let m=s.value;yield t.return_pl_id?[m,null]:m,o.push(m)}}catch(s){h=[s]}finally{try{p&&(s=i.return)&&(yield new w(s.call(i)))}finally{if(h)throw h[0]}}let n=de(o,t.function_name),a=yield new w(B(r,R(d({},t),{request_response:n,request_end_time:new Date().toISOString()})));if(a&&t.return_pl_id){let m=a[1];yield[o.at(-1),m]}})}var _=(r,e)=>{try{console.warn(`${e}: ${r.message}`)}catch(t){console.warn(`${e}: ${r}`)}},te=r=>c(void 0,null,function*(){try{let e=yield fetch(`${g}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});return e.status!==200&&_(e,"WARNING: While logging your request, PromptLayer experienced the following error:"),e.json()}catch(e){console.warn(`WARNING: While logging your request PromptLayer had the following error: ${e}`)}return{}}),re=r=>{let e={id:"",choices:[{finish_reason:"stop",index:0,text:"",logprobs:null}],created:Date.now(),model:"",object:"text_completion"},t=r.at(-1);if(!t)return e;let o="";for(let n of r)n.choices.length>0&&n.choices[0].text&&(o=`${o}${n.choices[0].text}`);return e.choices[0].text=o,e.id=t.id,e.created=t.created,e.model=t.model,e.system_fingerprint=t.system_fingerprint,e.usage=t.usage,e},oe=r=>{let e={completion:"",id:"",model:"",stop_reason:"",type:"completion"},t=r.at(-1);if(!t)return e;let o="";for(let n of r)o=`${o}${n.completion}`;return e.completion=o,e.id=t.id,e.model=t.model,e.stop_reason=t.stop_reason,e};function ne(r,e,t){return W(this,null,function*(){let o={request_id:null,raw_response:null,prompt_blueprint:null},n=[];try{for(var p=x(r),s,h,m;s=!(h=yield new w(p.next())).done;s=!1){let f=h.value;n.push(f),o.raw_response=f,yield o}}catch(h){m=[h]}finally{try{s&&(h=p.return)&&(yield new w(h.call(p)))}finally{if(m)throw m[0]}}let a=t(n),i=yield new w(e({request_response:a}));o.request_id=i.request_id,o.prompt_blueprint=i.prompt_blueprint,yield o})}var ge=(r,e)=>c(void 0,null,function*(){return r.chat.completions.create(e)}),_e=(r,e)=>c(void 0,null,function*(){return r.completions.create(e)}),Pe={chat:ge,completion:_e},ae=(r,e)=>c(void 0,null,function*(){let t=N("openai").default,o=new t({baseURL:e.baseURL}),n=Pe[r.prompt_template.type];return n(o,e)}),we=(r,e)=>c(void 0,null,function*(){return r.messages.create(e)}),Te=(r,e)=>c(void 0,null,function*(){return r.completions.create(e)}),Re={chat:we,completion:Te},se=(r,e)=>c(void 0,null,function*(){let t=N("@anthropic-ai/sdk").default,o=new t({baseURL:e.baseURL}),n=Re[r.prompt_template.type];return n(o,e)});var I=class{constructor(e){this.create=()=>z(this.apiKey);this.apiKey=e}};var M=(r,e,t="",o="openai")=>{let n={construct:(a,i)=>{let p=Reflect.construct(a,i);return Object.defineProperties(p,{function_name:{value:t,writable:!0},provider:{value:o}}),new Proxy(p,n)},get:(a,i,p)=>{let s=a[i],h=`${Reflect.get(a,"function_name")}.${i.toString()}`;return typeof s=="object"?(Object.defineProperties(s,{function_name:{value:h,writable:!0},provider:{value:o}}),new Proxy(s,n)):typeof s=="function"?(...m)=>{var T,A,P,b;let f=new Date().toISOString(),u=Reflect.get(a,"provider"),l=(T=m[0])==null?void 0:T.return_pl_id,y=(A=m[0])==null?void 0:A.pl_tags;(P=m[0])==null||delete P.return_pl_id,(b=m[0])==null||delete b.pl_tags;let k=Reflect.apply(s,a,m);return k instanceof Promise?new Promise(($,L)=>{k.then(q=>c(void 0,null,function*(){let C=yield J(r,{api_key:r,provider_type:u,function_name:h,request_start_time:f,request_end_time:new Date().toISOString(),request_response:q,kwargs:m[0],return_pl_id:l,tags:y});$(C)})).catch(q=>{L(q)})}):k}:Reflect.get(a,i,p)}};return new Proxy(e,n)};var O=class{constructor(e){this.get=(e,t)=>Q(this.apiKey,e,t);this.publish=e=>Z(this.apiKey,e);this.all=e=>ee(this.apiKey,e);this.apiKey=e}};var ke=(r,e)=>{if(!(e.metadata instanceof Object))throw new Error("Please provide a dictionary of metadata.");for(let[t,o]of Object.entries(e.metadata))if(typeof t!="string"||typeof o!="string")throw new Error("Please provide a dictionary of metadata with key value pair of strings.");return F(r,e)},be=(r,e)=>{if(typeof e.score!="number")throw new Error("Score must be a number");if(e.score<0||e.score>100)throw new Error("Score must be a number between 0 and 100.");return H(r,e)},qe=(r,e)=>{if(!(e.prompt_input_variables instanceof Object))throw new Error("Prompt template input variable dictionary not provided.");return X(r,e)},Ae=(r,e)=>V(r,e),S=class{constructor(e){this.group=e=>Ae(this.apiKey,e);this.metadata=e=>ke(this.apiKey,e);this.prompt=e=>qe(this.apiKey,e);this.score=e=>be(this.apiKey,e);this.apiKey=e}};var Ce={openai:{chat:{function_name:"openai.chat.completions.create",stream_function:v},completion:{function_name:"openai.completions.create",stream_function:re}},anthropic:{chat:{function_name:"anthropic.messages.create",stream_function:j},completion:{function_name:"anthropic.completions.create",stream_function:oe}}},Ne={openai:ae,anthropic:se},ie=class{constructor({apiKey:e=process.env.PROMPTLAYER_API_KEY}={}){if(e===void 0)throw new Error("PromptLayer API key not provided. Please set the PROMPTLAYER_API_KEY environment variable or pass the api_key parameter.");this.apiKey=e,this.templates=new O(e),this.group=new I(e),this.track=new S(e)}get OpenAI(){try{let e=N("openai").default;return M(this.apiKey,e,"openai","openai")}catch(e){console.error("To use the OpenAI module, you must install the @openai/api package.")}}get Anthropic(){try{let e=N("@anthropic-ai/sdk").default;return M(this.apiKey,e,"anthropic","anthropic")}catch(e){console.error("To use the Anthropic module, you must install the @anthropic-ai/sdk package.")}}run(h){return c(this,arguments,function*({promptName:e,promptVersion:t,promptReleaseLabel:o,inputVariables:n,tags:a,metadata:i,groupId:p,stream:s=!1}){let m=n,f={label:o,version:t,metadata_filters:i};n&&(f.input_variables=n);let u=yield this.templates.get(e,f);if(!u)throw new Error("Prompt not found");let l=u.prompt_template;if(!u.llm_kwargs)throw new Error(`Prompt '${e}' does not have any LLM kwargs associated with it.`);let y=u.metadata;if(!y)throw new Error(`Prompt '${e}' does not have any metadata associated with it.`);let k=y.model;if(!k)throw new Error(`Prompt '${e}' does not have a model parameters associated with it.`);let T=k.provider,A=new Date().toISOString(),P=u.llm_kwargs,b=Ce[T][l.type],$=b.function_name,L=b.stream_function,q=Ne[T],C=u.provider_base_url;C&&(P.baseURL=C.url),P.stream=s,s&&T==="openai"&&(P.stream_options={include_usage:!0});let G=yield q(u,P),E=pe=>{let ce=new Date().toISOString();return te(d({function_name:$,provider_type:T,args:[],kwargs:P,tags:a,request_start_time:A,request_end_time:ce,api_key:this.apiKey,metadata:i,prompt_id:u.id,prompt_version:u.version,prompt_input_variables:m,group_id:p,return_prompt_blueprint:!0},pe))};if(s)return ne(G,E,L);let K=yield E({request_response:G});return{request_id:K.request_id,raw_response:G,prompt_blueprint:K.prompt_blueprint}})}};export{ie as PromptLayer};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils.ts","../../src/groups.ts","../../src/promptlayer.ts","../../src/templates.ts","../../src/track.ts","../../src/index.ts"],"sourcesContent":["import {\n GetPromptTemplateParams,\n GetPromptTemplateResponse,\n ListPromptTemplatesResponse,\n Pagination,\n PublishPromptTemplate,\n PublishPromptTemplateResponse,\n TrackGroup,\n TrackMetadata,\n TrackPrompt,\n TrackRequest,\n TrackScore,\n} from \"@/types\";\nimport { Message, MessageStreamEvent } from \"@anthropic-ai/sdk/resources\";\nimport { ChatCompletion, ChatCompletionChunk } from \"openai/resources\";\n\nconst URL_API_PROMPTLAYER = \"https://api.promptlayer.com\";\n\nconst promptlayerApiHandler = async <Item>(\n apiKey: string,\n body: TrackRequest & {\n request_response: AsyncIterable<Item> | any;\n }\n) => {\n const isGenerator = body.request_response[Symbol.asyncIterator] !== undefined;\n if (isGenerator) {\n return proxyGenerator(apiKey, body.request_response, body);\n }\n return await promptLayerApiRequest(apiKey, body);\n};\n\nconst promptLayerApiRequest = async (apiKey: string, body: TrackRequest) => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-request`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(body),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While logging your request, PromptLayer experienced the following error:\"\n );\n }\n if (data && body.return_pl_id) {\n return [body.request_response, data.request_id];\n }\n } catch (e) {\n console.warn(\n `WARNING: While logging your request PromptLayer had the following error: ${e}`\n );\n }\n return body.request_response;\n};\n\nconst promptLayerTrackMetadata = async (\n apiKey: string,\n body: TrackMetadata\n): Promise<boolean> => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/library-track-metadata`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n }\n );\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While logging metadata to your request, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While logging metadata to your request, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackScore = async (\n apiKey: string,\n body: TrackScore\n): Promise<boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/library-track-score`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While scoring your request, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While scoring your request, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackPrompt = async (\n apiKey: string,\n body: TrackPrompt\n): Promise<boolean> => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/library-track-prompt`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n }\n );\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While associating your request with a prompt template, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While associating your request with a prompt template, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackGroup = async (\n apiKey: string,\n body: TrackGroup\n): Promise<boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-group`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While associating your request with a group, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While associating your request with a group, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerCreateGroup = async (\n apiKey: string\n): Promise<number | boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/create-group`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While creating a group PromptLayer had the following error\"\n );\n return false;\n }\n return data.id;\n } catch (e) {\n console.warn(\n `WARNING: While creating a group PromptLayer had the following error: ${e}`\n );\n return false;\n }\n};\n\nconst getPromptTemplate = async (\n apiKey: string,\n promptName: string,\n params?: Partial<GetPromptTemplateParams>\n) => {\n try {\n const url = new URL(\n `${URL_API_PROMPTLAYER}/prompt-templates/${promptName}`\n );\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n body: JSON.stringify(params),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While fetching a prompt template PromptLayer had the following error\"\n );\n return null;\n }\n return data as Promise<GetPromptTemplateResponse>;\n } catch (e) {\n console.warn(\n `WARNING: While fetching a prompt template PromptLayer had the following error: ${e}`\n );\n return null;\n }\n};\n\nconst publishPromptTemplate = async (\n apiKey: string,\n body: PublishPromptTemplate\n) => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/rest/prompt-templates`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n body: JSON.stringify({\n prompt_template: { ...body },\n prompt_version: { ...body },\n release_labels: body.release_labels ? body.release_labels : undefined,\n }),\n }\n );\n const data = await response.json();\n if (response.status === 400) {\n warnOnBadResponse(\n data,\n \"WARNING: While publishing a prompt template PromptLayer had the following error\"\n );\n }\n return data as Promise<PublishPromptTemplateResponse>;\n } catch (e) {\n console.warn(\n `WARNING: While publishing a prompt template PromptLayer had the following error: ${e}`\n );\n }\n};\n\nconst getAllPromptTemplates = async (\n apiKey: string,\n params?: Partial<Pagination>\n) => {\n try {\n const url = new URL(`${URL_API_PROMPTLAYER}/prompt-templates`);\n Object.entries(params || {}).forEach(([key, value]) =>\n url.searchParams.append(key, value.toString())\n );\n const response = await fetch(url, {\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While fetching all prompt templates PromptLayer had the following error\"\n );\n return null;\n }\n return (data.items ?? []) as Promise<Array<ListPromptTemplatesResponse>>;\n } catch (e) {\n console.warn(\n `WARNING: While fetching all prompt templates PromptLayer had the following error: ${e}`\n );\n return null;\n }\n};\n\nconst mapChatCompletionChunk = (\n results: ChatCompletionChunk[]\n): ChatCompletion => {\n let content: ChatCompletion.Choice[\"message\"][\"content\"] = null;\n let functionCall: ChatCompletion.Choice[\"message\"][\"function_call\"] =\n undefined;\n const response: ChatCompletion = {\n id: \"\",\n choices: [],\n created: Date.now(),\n model: \"\",\n object: \"chat.completion\",\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let toolCalls: ChatCompletion.Choice[\"message\"][\"tool_calls\"] = undefined;\n for (const result of results) {\n const delta = result.choices[0].delta;\n\n if (delta.content) {\n content = `${content || \"\"}${delta.content || \"\"}`;\n }\n if (delta.function_call) {\n functionCall = {\n name: `${functionCall ? functionCall.name : \"\"}${\n delta.function_call.name || \"\"\n }`,\n arguments: `${functionCall ? functionCall.arguments : \"\"}${\n delta.function_call.arguments || \"\"\n }`,\n };\n }\n const toolCall = delta.tool_calls?.[0];\n if (toolCall) {\n toolCalls = toolCalls || [];\n const lastToolCall = toolCalls.at(-1);\n if (!lastToolCall || toolCall.id) {\n toolCalls.push({\n id: toolCall.id || \"\",\n type: toolCall.type || \"function\",\n function: {\n name: toolCall.function?.name || \"\",\n arguments: toolCall.function?.arguments || \"\",\n },\n });\n continue;\n }\n lastToolCall.function.name = `${lastToolCall.function.name}${\n toolCall.function?.name || \"\"\n }`;\n lastToolCall.function.arguments = `${lastToolCall.function.arguments}${\n toolCall.function?.arguments || \"\"\n }`;\n }\n }\n response.choices.push({\n finish_reason: lastResult.choices[0].finish_reason || \"stop\",\n index: lastResult.choices[0].index || 0,\n logprobs: lastResult.choices[0].logprobs || null,\n message: {\n role: \"assistant\",\n content,\n function_call: functionCall ? functionCall : undefined,\n tool_calls: toolCalls ? toolCalls : undefined,\n },\n });\n response.id = lastResult.id;\n response.model = lastResult.model;\n response.created = lastResult.created;\n response.system_fingerprint = lastResult.system_fingerprint;\n return response;\n};\n\nconst mapMessageStreamEvent = (results: MessageStreamEvent[]): Message => {\n let response: Message = {\n id: \"\",\n model: \"\",\n content: [],\n role: \"assistant\",\n type: \"message\",\n stop_reason: \"stop_sequence\",\n stop_sequence: null,\n usage: {\n input_tokens: 0,\n output_tokens: 0,\n },\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let content = \"\";\n for (const result of results) {\n switch (result.type) {\n case \"message_start\": {\n response = {\n ...result.message,\n };\n break;\n }\n case \"content_block_delta\": {\n if (result.delta.type === \"text_delta\")\n content = `${content}${result.delta.text}`;\n }\n case \"message_delta\": {\n if (\"usage\" in result)\n response.usage.output_tokens = result.usage.output_tokens;\n if (\"stop_reason\" in result.delta)\n response.stop_reason = result.delta.stop_reason;\n }\n default: {\n break;\n }\n }\n }\n response.content.push({\n type: \"text\",\n text: content,\n });\n return response;\n};\n\nconst cleaned_result = (\n results: any[],\n function_name = \"openai.chat.completions.create\"\n) => {\n if (\"completion\" in results[0]) {\n return results.reduce(\n (prev, current) => ({\n ...current,\n completion: `${prev.completion}${current.completion}`,\n }),\n {}\n );\n }\n\n if (function_name === \"anthropic.messages.create\")\n return mapMessageStreamEvent(results);\n\n if (\"text\" in results[0].choices[0]) {\n let response = \"\";\n for (const result of results) {\n response = `${response}${result.choices[0].text}`;\n }\n const final_result = structuredClone(results.at(-1));\n final_result.choices[0].text = response;\n return final_result;\n }\n\n if (\"delta\" in results[0].choices[0]) {\n const response = mapChatCompletionChunk(results);\n response.choices[0] = {\n ...response.choices[0],\n ...response.choices[0].message,\n };\n return response;\n }\n\n return \"\";\n};\n\nasync function* proxyGenerator<Item>(\n apiKey: string,\n generator: AsyncIterable<Item>,\n body: TrackRequest\n) {\n const results = [];\n for await (const value of generator) {\n yield body.return_pl_id ? [value, null] : value;\n results.push(value);\n }\n const request_response = cleaned_result(results, body.function_name);\n const response = await promptLayerApiRequest(apiKey, {\n ...body,\n request_response,\n request_end_time: new Date().toISOString(),\n });\n if (response) {\n if (body.return_pl_id) {\n const request_id = (response as any)[1];\n const lastResult = results.at(-1);\n yield [lastResult, request_id];\n }\n }\n}\n\nconst warnOnBadResponse = (request_response: any, main_message: string) => {\n try {\n console.warn(`${main_message}: ${request_response.message}`);\n } catch (e) {\n console.warn(`${main_message}: ${request_response}`);\n }\n};\n\nexport {\n getAllPromptTemplates,\n getPromptTemplate,\n promptLayerApiRequest,\n promptLayerCreateGroup,\n promptLayerTrackGroup,\n promptLayerTrackMetadata,\n promptLayerTrackPrompt,\n promptLayerTrackScore,\n promptlayerApiHandler,\n publishPromptTemplate,\n};\n","import { promptLayerCreateGroup } from \"@/utils\";\n\nexport class GroupManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n create = () => promptLayerCreateGroup(this.apiKey);\n}\n","import { promptlayerApiHandler } from \"@/utils\";\n\nexport const promptLayerBase = (\n apiKey: string,\n llm: object,\n function_name = \"\",\n provider = \"openai\"\n) => {\n const handler: ProxyHandler<any> = {\n construct: (target, args) => {\n const newTarget = Reflect.construct(target, args);\n Object.defineProperties(newTarget, {\n function_name: {\n value: function_name,\n writable: true,\n },\n provider: {\n value: provider,\n },\n });\n return new Proxy(newTarget, handler);\n },\n get: (target, prop, receiver) => {\n const value = target[prop];\n const function_name = `${Reflect.get(\n target,\n \"function_name\"\n )}.${prop.toString()}`;\n if (typeof value === \"object\") {\n Object.defineProperties(value, {\n function_name: {\n value: function_name,\n writable: true,\n },\n provider: {\n value: provider,\n },\n });\n return new Proxy(value, handler);\n }\n if (typeof value === \"function\") {\n return (...args: any[]) => {\n const request_start_time = new Date().toISOString();\n const provider_type = Reflect.get(target, \"provider\");\n const return_pl_id = args[0]?.return_pl_id;\n const pl_tags = args[0]?.pl_tags;\n delete args[0]?.return_pl_id;\n delete args[0]?.pl_tags;\n const response = Reflect.apply(value, target, args);\n if (response instanceof Promise) {\n return new Promise((resolve, reject) => {\n response\n .then(async (request_response) => {\n const response = await promptlayerApiHandler(apiKey, {\n api_key: apiKey,\n provider_type,\n function_name,\n request_start_time,\n request_end_time: new Date().toISOString(),\n request_response,\n kwargs: args[0],\n return_pl_id,\n tags: pl_tags,\n });\n resolve(response);\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n return response;\n };\n }\n return Reflect.get(target, prop, receiver);\n },\n };\n return new Proxy(llm, handler);\n};\n","import {\n GetPromptTemplateParams,\n Pagination,\n PublishPromptTemplate,\n} from \"@/types\";\nimport {\n getAllPromptTemplates,\n getPromptTemplate,\n publishPromptTemplate,\n} from \"@/utils\";\n\nexport class TemplateManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n get = (promptName: string, params?: Partial<GetPromptTemplateParams>) =>\n getPromptTemplate(this.apiKey, promptName, params);\n\n publish = (body: PublishPromptTemplate) =>\n publishPromptTemplate(this.apiKey, body);\n\n all = (params?: Pagination) => getAllPromptTemplates(this.apiKey, params);\n}\n","import { TrackGroup, TrackMetadata, TrackPrompt, TrackScore } from \"@/types\";\nimport {\n promptLayerTrackGroup,\n promptLayerTrackMetadata,\n promptLayerTrackPrompt,\n promptLayerTrackScore,\n} from \"@/utils\";\n\nconst metadata = (apiKey: string, body: TrackMetadata): Promise<boolean> => {\n if (!(body.metadata instanceof Object)) {\n throw new Error(\"Please provide a dictionary of metadata.\");\n }\n for (const [key, value] of Object.entries(body.metadata)) {\n if (typeof key !== \"string\" || typeof value !== \"string\") {\n throw new Error(\n \"Please provide a dictionary of metadata with key value pair of strings.\"\n );\n }\n }\n return promptLayerTrackMetadata(apiKey, body);\n};\n\nconst score = (apiKey: string, body: TrackScore): Promise<boolean> => {\n if (typeof body.score !== \"number\") {\n throw new Error(\"Score must be a number\");\n }\n if (body.score < 0 || body.score > 100) {\n throw new Error(\"Score must be a number between 0 and 100.\");\n }\n return promptLayerTrackScore(apiKey, body);\n};\n\nconst prompt = (apiKey: string, body: TrackPrompt): Promise<boolean> => {\n if (!(body.prompt_input_variables instanceof Object)) {\n throw new Error(\"Prompt template input variable dictionary not provided.\");\n }\n return promptLayerTrackPrompt(apiKey, body);\n};\n\nconst group = (apiKey: string, body: TrackGroup) =>\n promptLayerTrackGroup(apiKey, body);\n\nexport class TrackManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n group = (body: TrackGroup) => group(this.apiKey, body);\n\n metadata = (body: TrackMetadata) => metadata(this.apiKey, body);\n\n prompt = (body: TrackPrompt) => prompt(this.apiKey, body);\n\n score = (body: TrackScore) => score(this.apiKey, body);\n}\n","import { GroupManager } from \"@/groups\";\nimport { promptLayerBase } from \"@/promptlayer\";\nimport { TemplateManager } from \"@/templates\";\nimport { TrackManager } from \"@/track\";\n\nexport interface ClientOptions {\n apiKey?: string;\n}\n\nexport class PromptLayer {\n apiKey: string;\n templates: TemplateManager;\n group: GroupManager;\n track: TrackManager;\n\n constructor({\n apiKey = process.env.PROMPTLAYER_API_KEY,\n }: ClientOptions = {}) {\n if (apiKey === undefined) {\n throw new Error(\n \"PromptLayer API key not provided. Please set the PROMPTLAYER_API_KEY environment variable or pass the api_key parameter.\"\n );\n }\n this.apiKey = apiKey;\n this.templates = new TemplateManager(apiKey);\n this.group = new GroupManager(apiKey);\n this.track = new TrackManager(apiKey);\n }\n\n get OpenAI() {\n try {\n const module = require(\"openai\").default;\n return promptLayerBase(this.apiKey, module, \"openai\", \"openai\");\n } catch (e) {\n console.error(\n \"To use the OpenAI module, you must install the @openai/api package.\"\n );\n }\n }\n\n get Anthropic() {\n try {\n const module = require(\"@anthropic-ai/sdk\").default;\n return promptLayerBase(this.apiKey, module, \"anthropic\", \"anthropic\");\n } catch (e) {\n console.error(\n \"To use the Anthropic module, you must install the @anthropic-ai/sdk package.\"\n );\n }\n }\n}\n"],"mappings":"6nDAgBA,IAAMA,EAAsB,8BAEtBC,EAAwB,CAC5BC,EACAC,IAGGC,EAAA,wBAEH,OADoBD,EAAK,iBAAiB,OAAO,aAAa,IAAM,OAE3DE,GAAeH,EAAQC,EAAK,iBAAkBA,CAAI,EAEpD,MAAMG,EAAsBJ,EAAQC,CAAI,CACjD,GAEMG,EAAwB,CAAOJ,EAAgBC,IAAuBC,EAAA,wBAC1E,GAAI,CACF,IAAMG,EAAW,MAAM,MAAM,GAAGP,CAAmB,iBAAkB,CACnE,OAAQ,OACR,QAAS,CACP,eAAgB,kBAClB,EACA,KAAM,KAAK,UAAUG,CAAI,CAC3B,CAAC,EACKK,EAAO,MAAMD,EAAS,KAAK,EAOjC,GANIA,EAAS,SAAW,KACtBE,EACED,EACA,mFACF,EAEEA,GAAQL,EAAK,aACf,MAAO,CAACA,EAAK,iBAAkBK,EAAK,UAAU,CAElD,OAASE,EAAG,CACV,QAAQ,KACN,4EAA4EA,CAAC,EAC/E,CACF,CACA,OAAOP,EAAK,gBACd,GAEMQ,EAA2B,CAC/BT,EACAC,IACqBC,EAA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C,CAAI,CAC3C,EAEMM,GAAS,CAACP,EAAgBC,IAAwC,CACtE,GAAI,EAAEA,EAAK,kCAAkC,QAC3C,MAAM,IAAI,MAAM,yDAAyD,EAE3E,OAAOO,EAAuBR,EAAQC,CAAI,CAC5C,EAEMQ,GAAQ,CAACT,EAAgBC,IAC7BS,EAAsBV,EAAQC,CAAI,EAEvBU,EAAN,KAAmB,CAGxB,YAAYX,EAAgB,CAI5B,WAASC,GAAqBQ,GAAM,KAAK,OAAQR,CAAI,EAErD,cAAYA,GAAwBF,GAAS,KAAK,OAAQE,CAAI,EAE9D,YAAUA,GAAsBM,GAAO,KAAK,OAAQN,CAAI,EAExD,WAASA,GAAqBI,GAAM,KAAK,OAAQJ,CAAI,EATnD,KAAK,OAASD,CAChB,CASF,EC/CO,IAAMY,EAAN,KAAkB,CAMvB,YAAY,CACV,OAAAC,EAAS,QAAQ,IAAI,mBACvB,EAAmB,CAAC,EAAG,CACrB,GAAIA,IAAW,OACb,MAAM,IAAI,MACR,0HACF,EAEF,KAAK,OAASA,EACd,KAAK,UAAY,IAAIC,EAAgBD,CAAM,EAC3C,KAAK,MAAQ,IAAIE,EAAaF,CAAM,EACpC,KAAK,MAAQ,IAAIG,EAAaH,CAAM,CACtC,CAEA,IAAI,QAAS,CACX,GAAI,CACF,IAAMI,EAAS,EAAQ,QAAQ,EAAE,QACjC,OAAOC,EAAgB,KAAK,OAAQD,EAAQ,SAAU,QAAQ,CAChE,OAAS,EAAG,CACV,QAAQ,MACN,qEACF,CACF,CACF,CAEA,IAAI,WAAY,CACd,GAAI,CACF,IAAMA,EAAS,EAAQ,mBAAmB,EAAE,QAC5C,OAAOC,EAAgB,KAAK,OAAQD,EAAQ,YAAa,WAAW,CACtE,OAAS,EAAG,CACV,QAAQ,MACN,8EACF,CACF,CACF,CACF","names":["URL_API_PROMPTLAYER","promptlayerApiHandler","apiKey","body","__async","proxyGenerator","promptLayerApiRequest","response","data","warnOnBadResponse","e","promptLayerTrackMetadata","__spreadProps","__spreadValues","promptLayerTrackScore","promptLayerTrackPrompt","promptLayerTrackGroup","promptLayerCreateGroup","getPromptTemplate","promptName","params","url","publishPromptTemplate","getAllPromptTemplates","_a","key","value","mapChatCompletionChunk","results","_b","_c","_d","_e","content","functionCall","lastResult","toolCalls","result","delta","toolCall","lastToolCall","mapMessageStreamEvent","cleaned_result","function_name","prev","current","final_result","generator","__asyncGenerator","iter","__forAwait","more","temp","error","__await","request_response","request_id","main_message","GroupManager","apiKey","promptLayerCreateGroup","promptLayerBase","apiKey","llm","function_name","provider","handler","target","args","newTarget","prop","receiver","value","_a","_b","_c","_d","request_start_time","provider_type","return_pl_id","pl_tags","response","resolve","reject","request_response","__async","promptlayerApiHandler","error","TemplateManager","apiKey","promptName","params","getPromptTemplate","body","publishPromptTemplate","getAllPromptTemplates","metadata","apiKey","body","key","value","promptLayerTrackMetadata","score","promptLayerTrackScore","prompt","promptLayerTrackPrompt","group","promptLayerTrackGroup","TrackManager","PromptLayer","apiKey","TemplateManager","GroupManager","TrackManager","module","promptLayerBase"]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils.ts","../../src/groups.ts","../../src/promptlayer.ts","../../src/templates.ts","../../src/track.ts","../../src/index.ts"],"sourcesContent":["import {\n GetPromptTemplateParams,\n GetPromptTemplateResponse,\n ListPromptTemplatesResponse,\n Pagination,\n PublishPromptTemplate,\n PublishPromptTemplateResponse,\n TrackGroup,\n TrackMetadata,\n TrackPrompt,\n TrackRequest,\n TrackScore,\n} from \"@/types\";\nimport type TypeAnthropic from \"@anthropic-ai/sdk\";\nimport {\n Completion as AnthropicCompletion,\n Message,\n MessageStreamEvent,\n} from \"@anthropic-ai/sdk/resources\";\nimport type TypeOpenAI from \"openai\";\nimport {\n ChatCompletion,\n ChatCompletionChunk,\n Completion,\n} from \"openai/resources\";\n\nconst URL_API_PROMPTLAYER = \"https://api.promptlayer.com\";\n\nconst promptlayerApiHandler = async <Item>(\n apiKey: string,\n body: TrackRequest & {\n request_response: AsyncIterable<Item> | any;\n }\n) => {\n const isGenerator = body.request_response[Symbol.asyncIterator] !== undefined;\n if (isGenerator) {\n return proxyGenerator(apiKey, body.request_response, body);\n }\n return await promptLayerApiRequest(apiKey, body);\n};\n\nconst promptLayerApiRequest = async (apiKey: string, body: TrackRequest) => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-request`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(body),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While logging your request, PromptLayer experienced the following error:\"\n );\n }\n if (data && body.return_pl_id) {\n return [body.request_response, data.request_id];\n }\n } catch (e) {\n console.warn(\n `WARNING: While logging your request PromptLayer had the following error: ${e}`\n );\n }\n return body.request_response;\n};\n\nconst promptLayerTrackMetadata = async (\n apiKey: string,\n body: TrackMetadata\n): Promise<boolean> => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/library-track-metadata`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n }\n );\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While logging metadata to your request, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While logging metadata to your request, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackScore = async (\n apiKey: string,\n body: TrackScore\n): Promise<boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/library-track-score`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While scoring your request, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While scoring your request, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackPrompt = async (\n apiKey: string,\n body: TrackPrompt\n): Promise<boolean> => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/library-track-prompt`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n }\n );\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While associating your request with a prompt template, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While associating your request with a prompt template, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackGroup = async (\n apiKey: string,\n body: TrackGroup\n): Promise<boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-group`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While associating your request with a group, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While associating your request with a group, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerCreateGroup = async (\n apiKey: string\n): Promise<number | boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/create-group`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While creating a group PromptLayer had the following error\"\n );\n return false;\n }\n return data.id;\n } catch (e) {\n console.warn(\n `WARNING: While creating a group PromptLayer had the following error: ${e}`\n );\n return false;\n }\n};\n\nconst getPromptTemplate = async (\n apiKey: string,\n promptName: string,\n params?: Partial<GetPromptTemplateParams>\n) => {\n try {\n const url = new URL(\n `${URL_API_PROMPTLAYER}/prompt-templates/${promptName}`\n );\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n body: JSON.stringify(params),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While fetching a prompt template PromptLayer had the following error\"\n );\n return null;\n }\n return data as Promise<GetPromptTemplateResponse>;\n } catch (e) {\n console.warn(\n `WARNING: While fetching a prompt template PromptLayer had the following error: ${e}`\n );\n return null;\n }\n};\n\nconst publishPromptTemplate = async (\n apiKey: string,\n body: PublishPromptTemplate\n) => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/rest/prompt-templates`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n body: JSON.stringify({\n prompt_template: { ...body },\n prompt_version: { ...body },\n release_labels: body.release_labels ? body.release_labels : undefined,\n }),\n }\n );\n const data = await response.json();\n if (response.status === 400) {\n warnOnBadResponse(\n data,\n \"WARNING: While publishing a prompt template PromptLayer had the following error\"\n );\n }\n return data as Promise<PublishPromptTemplateResponse>;\n } catch (e) {\n console.warn(\n `WARNING: While publishing a prompt template PromptLayer had the following error: ${e}`\n );\n }\n};\n\nconst getAllPromptTemplates = async (\n apiKey: string,\n params?: Partial<Pagination>\n) => {\n try {\n const url = new URL(`${URL_API_PROMPTLAYER}/prompt-templates`);\n Object.entries(params || {}).forEach(([key, value]) =>\n url.searchParams.append(key, value.toString())\n );\n const response = await fetch(url, {\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While fetching all prompt templates PromptLayer had the following error\"\n );\n return null;\n }\n return (data.items ?? []) as Promise<Array<ListPromptTemplatesResponse>>;\n } catch (e) {\n console.warn(\n `WARNING: While fetching all prompt templates PromptLayer had the following error: ${e}`\n );\n return null;\n }\n};\n\nconst openaiStreamChat = (results: ChatCompletionChunk[]): ChatCompletion => {\n let content: ChatCompletion.Choice[\"message\"][\"content\"] = null;\n let functionCall: ChatCompletion.Choice[\"message\"][\"function_call\"] =\n undefined;\n const response: ChatCompletion = {\n id: \"\",\n choices: [],\n created: Date.now(),\n model: \"\",\n object: \"chat.completion\",\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let toolCalls: ChatCompletion.Choice[\"message\"][\"tool_calls\"] = undefined;\n for (const result of results) {\n if (result.choices.length === 0) continue;\n const delta = result.choices[0].delta;\n\n if (delta.content) {\n content = `${content || \"\"}${delta.content || \"\"}`;\n }\n if (delta.function_call) {\n functionCall = {\n name: `${functionCall ? functionCall.name : \"\"}${\n delta.function_call.name || \"\"\n }`,\n arguments: `${functionCall ? functionCall.arguments : \"\"}${\n delta.function_call.arguments || \"\"\n }`,\n };\n }\n const toolCall = delta.tool_calls?.[0];\n if (toolCall) {\n toolCalls = toolCalls || [];\n const lastToolCall = toolCalls.at(-1);\n if (!lastToolCall || toolCall.id) {\n toolCalls.push({\n id: toolCall.id || \"\",\n type: toolCall.type || \"function\",\n function: {\n name: toolCall.function?.name || \"\",\n arguments: toolCall.function?.arguments || \"\",\n },\n });\n continue;\n }\n lastToolCall.function.name = `${lastToolCall.function.name}${\n toolCall.function?.name || \"\"\n }`;\n lastToolCall.function.arguments = `${lastToolCall.function.arguments}${\n toolCall.function?.arguments || \"\"\n }`;\n }\n }\n response.choices.push({\n finish_reason: results[0].choices[0].finish_reason || \"stop\",\n index: results[0].choices[0].index || 0,\n logprobs: results[0].choices[0].logprobs || null,\n message: {\n role: \"assistant\",\n content,\n function_call: functionCall ? functionCall : undefined,\n tool_calls: toolCalls ? toolCalls : undefined,\n },\n });\n response.id = lastResult.id;\n response.model = lastResult.model;\n response.created = lastResult.created;\n response.system_fingerprint = lastResult.system_fingerprint;\n response.usage = lastResult.usage;\n return response;\n};\n\nconst anthropicStreamMessage = (results: MessageStreamEvent[]): Message => {\n let response: Message = {\n id: \"\",\n model: \"\",\n content: [],\n role: \"assistant\",\n type: \"message\",\n stop_reason: \"stop_sequence\",\n stop_sequence: null,\n usage: {\n input_tokens: 0,\n output_tokens: 0,\n },\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let content = \"\";\n for (const result of results) {\n switch (result.type) {\n case \"message_start\": {\n response = {\n ...result.message,\n };\n break;\n }\n case \"content_block_delta\": {\n if (result.delta.type === \"text_delta\")\n content = `${content}${result.delta.text}`;\n }\n case \"message_delta\": {\n if (\"usage\" in result)\n response.usage.output_tokens = result.usage.output_tokens;\n if (\"stop_reason\" in result.delta)\n response.stop_reason = result.delta.stop_reason;\n }\n default: {\n break;\n }\n }\n }\n response.content.push({\n type: \"text\",\n text: content,\n });\n return response;\n};\n\nconst cleaned_result = (\n results: any[],\n function_name = \"openai.chat.completions.create\"\n) => {\n if (\"completion\" in results[0]) {\n return results.reduce(\n (prev, current) => ({\n ...current,\n completion: `${prev.completion}${current.completion}`,\n }),\n {}\n );\n }\n\n if (function_name === \"anthropic.messages.create\")\n return anthropicStreamMessage(results);\n\n if (\"text\" in results[0].choices[0]) {\n let response = \"\";\n for (const result of results) {\n response = `${response}${result.choices[0].text}`;\n }\n const final_result = structuredClone(results.at(-1));\n final_result.choices[0].text = response;\n return final_result;\n }\n\n if (\"delta\" in results[0].choices[0]) {\n const response = openaiStreamChat(results);\n response.choices[0] = {\n ...response.choices[0],\n ...response.choices[0].message,\n };\n return response;\n }\n\n return \"\";\n};\n\nasync function* proxyGenerator<Item>(\n apiKey: string,\n generator: AsyncIterable<Item>,\n body: TrackRequest\n) {\n const results = [];\n for await (const value of generator) {\n yield body.return_pl_id ? [value, null] : value;\n results.push(value);\n }\n const request_response = cleaned_result(results, body.function_name);\n const response = await promptLayerApiRequest(apiKey, {\n ...body,\n request_response,\n request_end_time: new Date().toISOString(),\n });\n if (response) {\n if (body.return_pl_id) {\n const request_id = (response as any)[1];\n const lastResult = results.at(-1);\n yield [lastResult, request_id];\n }\n }\n}\n\nconst warnOnBadResponse = (request_response: any, main_message: string) => {\n try {\n console.warn(`${main_message}: ${request_response.message}`);\n } catch (e) {\n console.warn(`${main_message}: ${request_response}`);\n }\n};\n\nconst trackRequest = async (body: TrackRequest) => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-request`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(body),\n });\n if (response.status !== 200)\n warnOnBadResponse(\n response,\n \"WARNING: While logging your request, PromptLayer experienced the following error:\"\n );\n return response.json();\n } catch (e) {\n console.warn(\n `WARNING: While logging your request PromptLayer had the following error: ${e}`\n );\n }\n return {};\n};\n\nconst openaiStreamCompletion = (results: Completion[]) => {\n const response: Completion = {\n id: \"\",\n choices: [\n {\n finish_reason: \"stop\",\n index: 0,\n text: \"\",\n logprobs: null,\n },\n ],\n created: Date.now(),\n model: \"\",\n object: \"text_completion\",\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let text = \"\";\n for (const result of results) {\n if (result.choices.length > 0 && result.choices[0].text) {\n text = `${text}${result.choices[0].text}`;\n }\n }\n response.choices[0].text = text;\n response.id = lastResult.id;\n response.created = lastResult.created;\n response.model = lastResult.model;\n response.system_fingerprint = lastResult.system_fingerprint;\n response.usage = lastResult.usage;\n return response;\n};\n\nconst anthropicStreamCompletion = (results: AnthropicCompletion[]) => {\n const response: AnthropicCompletion = {\n completion: \"\",\n id: \"\",\n model: \"\",\n stop_reason: \"\",\n type: \"completion\",\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let completion = \"\";\n for (const result of results) {\n completion = `${completion}${result.completion}`;\n }\n response.completion = completion;\n response.id = lastResult.id;\n response.model = lastResult.model;\n response.stop_reason = lastResult.stop_reason;\n return response;\n};\n\nasync function* streamResponse<Item>(\n generator: AsyncIterable<Item>,\n afterStream: (body: object) => any,\n mapResults: any\n) {\n const data: {\n request_id: number | null;\n raw_response: any;\n prompt_blueprint: any;\n } = {\n request_id: null,\n raw_response: null,\n prompt_blueprint: null,\n };\n const results = [];\n for await (const result of generator) {\n results.push(result);\n data.raw_response = result;\n yield data;\n }\n const request_response = mapResults(results);\n const response = await afterStream({ request_response });\n data.request_id = response.request_id;\n data.prompt_blueprint = response.prompt_blueprint;\n yield data;\n}\n\nconst openaiChatRequest = async (client: TypeOpenAI, kwargs: any) => {\n return client.chat.completions.create(kwargs);\n};\n\nconst openaiCompletionsRequest = async (client: TypeOpenAI, kwargs: any) => {\n return client.completions.create(kwargs);\n};\n\nconst MAP_TYPE_TO_OPENAI_FUNCTION = {\n chat: openaiChatRequest,\n completion: openaiCompletionsRequest,\n};\n\nconst openaiRequest = async (\n promptBlueprint: GetPromptTemplateResponse,\n kwargs: any\n) => {\n const OpenAI = require(\"openai\").default;\n const client = new OpenAI({\n baseURL: kwargs.baseURL,\n });\n const requestToMake =\n MAP_TYPE_TO_OPENAI_FUNCTION[promptBlueprint.prompt_template.type];\n return requestToMake(client, kwargs);\n};\n\nconst anthropicChatRequest = async (client: TypeAnthropic, kwargs: any) => {\n return client.messages.create(kwargs);\n};\n\nconst anthropicCompletionsRequest = async (\n client: TypeAnthropic,\n kwargs: any\n) => {\n return client.completions.create(kwargs);\n};\n\nconst MAP_TYPE_TO_ANTHROPIC_FUNCTION = {\n chat: anthropicChatRequest,\n completion: anthropicCompletionsRequest,\n};\n\nconst anthropicRequest = async (\n promptBlueprint: GetPromptTemplateResponse,\n kwargs: any\n) => {\n const Anthropic = require(\"@anthropic-ai/sdk\").default;\n const client = new Anthropic({\n baseURL: kwargs.baseURL,\n });\n const requestToMake =\n MAP_TYPE_TO_ANTHROPIC_FUNCTION[promptBlueprint.prompt_template.type];\n return requestToMake(client, kwargs);\n};\n\nexport {\n anthropicRequest,\n anthropicStreamCompletion,\n anthropicStreamMessage,\n getAllPromptTemplates,\n getPromptTemplate,\n openaiRequest,\n openaiStreamChat,\n openaiStreamCompletion,\n promptLayerApiRequest,\n promptLayerCreateGroup,\n promptLayerTrackGroup,\n promptLayerTrackMetadata,\n promptLayerTrackPrompt,\n promptLayerTrackScore,\n promptlayerApiHandler,\n publishPromptTemplate,\n streamResponse,\n trackRequest,\n};\n","import { promptLayerCreateGroup } from \"@/utils\";\n\nexport class GroupManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n create = () => promptLayerCreateGroup(this.apiKey);\n}\n","import { promptlayerApiHandler } from \"@/utils\";\n\nexport const promptLayerBase = (\n apiKey: string,\n llm: object,\n function_name = \"\",\n provider = \"openai\"\n) => {\n const handler: ProxyHandler<any> = {\n construct: (target, args) => {\n const newTarget = Reflect.construct(target, args);\n Object.defineProperties(newTarget, {\n function_name: {\n value: function_name,\n writable: true,\n },\n provider: {\n value: provider,\n },\n });\n return new Proxy(newTarget, handler);\n },\n get: (target, prop, receiver) => {\n const value = target[prop];\n const function_name = `${Reflect.get(\n target,\n \"function_name\"\n )}.${prop.toString()}`;\n if (typeof value === \"object\") {\n Object.defineProperties(value, {\n function_name: {\n value: function_name,\n writable: true,\n },\n provider: {\n value: provider,\n },\n });\n return new Proxy(value, handler);\n }\n if (typeof value === \"function\") {\n return (...args: any[]) => {\n const request_start_time = new Date().toISOString();\n const provider_type = Reflect.get(target, \"provider\");\n const return_pl_id = args[0]?.return_pl_id;\n const pl_tags = args[0]?.pl_tags;\n delete args[0]?.return_pl_id;\n delete args[0]?.pl_tags;\n const response = Reflect.apply(value, target, args);\n if (response instanceof Promise) {\n return new Promise((resolve, reject) => {\n response\n .then(async (request_response) => {\n const response = await promptlayerApiHandler(apiKey, {\n api_key: apiKey,\n provider_type,\n function_name,\n request_start_time,\n request_end_time: new Date().toISOString(),\n request_response,\n kwargs: args[0],\n return_pl_id,\n tags: pl_tags,\n });\n resolve(response);\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n return response;\n };\n }\n return Reflect.get(target, prop, receiver);\n },\n };\n return new Proxy(llm, handler);\n};\n","import {\n GetPromptTemplateParams,\n Pagination,\n PublishPromptTemplate,\n} from \"@/types\";\nimport {\n getAllPromptTemplates,\n getPromptTemplate,\n publishPromptTemplate,\n} from \"@/utils\";\n\nexport class TemplateManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n get = (promptName: string, params?: Partial<GetPromptTemplateParams>) =>\n getPromptTemplate(this.apiKey, promptName, params);\n\n publish = (body: PublishPromptTemplate) =>\n publishPromptTemplate(this.apiKey, body);\n\n all = (params?: Pagination) => getAllPromptTemplates(this.apiKey, params);\n}\n","import { TrackGroup, TrackMetadata, TrackPrompt, TrackScore } from \"@/types\";\nimport {\n promptLayerTrackGroup,\n promptLayerTrackMetadata,\n promptLayerTrackPrompt,\n promptLayerTrackScore,\n} from \"@/utils\";\n\nconst metadata = (apiKey: string, body: TrackMetadata): Promise<boolean> => {\n if (!(body.metadata instanceof Object)) {\n throw new Error(\"Please provide a dictionary of metadata.\");\n }\n for (const [key, value] of Object.entries(body.metadata)) {\n if (typeof key !== \"string\" || typeof value !== \"string\") {\n throw new Error(\n \"Please provide a dictionary of metadata with key value pair of strings.\"\n );\n }\n }\n return promptLayerTrackMetadata(apiKey, body);\n};\n\nconst score = (apiKey: string, body: TrackScore): Promise<boolean> => {\n if (typeof body.score !== \"number\") {\n throw new Error(\"Score must be a number\");\n }\n if (body.score < 0 || body.score > 100) {\n throw new Error(\"Score must be a number between 0 and 100.\");\n }\n return promptLayerTrackScore(apiKey, body);\n};\n\nconst prompt = (apiKey: string, body: TrackPrompt): Promise<boolean> => {\n if (!(body.prompt_input_variables instanceof Object)) {\n throw new Error(\"Prompt template input variable dictionary not provided.\");\n }\n return promptLayerTrackPrompt(apiKey, body);\n};\n\nconst group = (apiKey: string, body: TrackGroup) =>\n promptLayerTrackGroup(apiKey, body);\n\nexport class TrackManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n group = (body: TrackGroup) => group(this.apiKey, body);\n\n metadata = (body: TrackMetadata) => metadata(this.apiKey, body);\n\n prompt = (body: TrackPrompt) => prompt(this.apiKey, body);\n\n score = (body: TrackScore) => score(this.apiKey, body);\n}\n","import { GroupManager } from \"@/groups\";\nimport { promptLayerBase } from \"@/promptlayer\";\nimport { TemplateManager } from \"@/templates\";\nimport { TrackManager } from \"@/track\";\nimport { GetPromptTemplateParams, RunRequest } from \"@/types\";\nimport {\n anthropicRequest,\n anthropicStreamCompletion,\n anthropicStreamMessage,\n openaiRequest,\n openaiStreamChat,\n openaiStreamCompletion,\n streamResponse,\n trackRequest,\n} from \"@/utils\";\n\nconst MAP_PROVIDER_TO_FUNCTION_NAME = {\n openai: {\n chat: {\n function_name: \"openai.chat.completions.create\",\n stream_function: openaiStreamChat,\n },\n completion: {\n function_name: \"openai.completions.create\",\n stream_function: openaiStreamCompletion,\n },\n },\n anthropic: {\n chat: {\n function_name: \"anthropic.messages.create\",\n stream_function: anthropicStreamMessage,\n },\n completion: {\n function_name: \"anthropic.completions.create\",\n stream_function: anthropicStreamCompletion,\n },\n },\n};\n\nconst MAP_PROVIDER_TO_FUNCTION: Record<string, any> = {\n openai: openaiRequest,\n anthropic: anthropicRequest,\n};\n\nexport interface ClientOptions {\n apiKey?: string;\n}\n\nexport class PromptLayer {\n apiKey: string;\n templates: TemplateManager;\n group: GroupManager;\n track: TrackManager;\n\n constructor({\n apiKey = process.env.PROMPTLAYER_API_KEY,\n }: ClientOptions = {}) {\n if (apiKey === undefined) {\n throw new Error(\n \"PromptLayer API key not provided. Please set the PROMPTLAYER_API_KEY environment variable or pass the api_key parameter.\"\n );\n }\n this.apiKey = apiKey;\n this.templates = new TemplateManager(apiKey);\n this.group = new GroupManager(apiKey);\n this.track = new TrackManager(apiKey);\n }\n\n get OpenAI() {\n try {\n const module = require(\"openai\").default;\n return promptLayerBase(this.apiKey, module, \"openai\", \"openai\");\n } catch (e) {\n console.error(\n \"To use the OpenAI module, you must install the @openai/api package.\"\n );\n }\n }\n\n get Anthropic() {\n try {\n const module = require(\"@anthropic-ai/sdk\").default;\n return promptLayerBase(this.apiKey, module, \"anthropic\", \"anthropic\");\n } catch (e) {\n console.error(\n \"To use the Anthropic module, you must install the @anthropic-ai/sdk package.\"\n );\n }\n }\n\n async run({\n promptName,\n promptVersion,\n promptReleaseLabel,\n inputVariables,\n tags,\n metadata,\n groupId,\n stream = false,\n }: RunRequest) {\n const prompt_input_variables = inputVariables;\n const templateGetParams: GetPromptTemplateParams = {\n label: promptReleaseLabel,\n version: promptVersion,\n metadata_filters: metadata,\n };\n if (inputVariables) templateGetParams.input_variables = inputVariables;\n const promptBlueprint = await this.templates.get(\n promptName,\n templateGetParams\n );\n if (!promptBlueprint) throw new Error(\"Prompt not found\");\n const promptTemplate = promptBlueprint.prompt_template;\n if (!promptBlueprint.llm_kwargs) {\n throw new Error(\n `Prompt '${promptName}' does not have any LLM kwargs associated with it.`\n );\n }\n const promptBlueprintMetadata = promptBlueprint.metadata;\n if (!promptBlueprintMetadata) {\n throw new Error(\n `Prompt '${promptName}' does not have any metadata associated with it.`\n );\n }\n const promptBlueprintModel = promptBlueprintMetadata.model;\n if (!promptBlueprintModel) {\n throw new Error(\n `Prompt '${promptName}' does not have a model parameters associated with it.`\n );\n }\n const provider_type = promptBlueprintModel.provider;\n const request_start_time = new Date().toISOString();\n const kwargs = promptBlueprint.llm_kwargs;\n const config =\n MAP_PROVIDER_TO_FUNCTION_NAME[\n provider_type as keyof typeof MAP_PROVIDER_TO_FUNCTION_NAME\n ][promptTemplate.type];\n const function_name = config.function_name;\n const stream_function = config.stream_function;\n const request_function = MAP_PROVIDER_TO_FUNCTION[provider_type];\n const provider_base_url = promptBlueprint.provider_base_url;\n if (provider_base_url) {\n kwargs[\"baseURL\"] = provider_base_url.url;\n }\n kwargs[\"stream\"] = stream;\n if (stream && provider_type === \"openai\") {\n kwargs[\"stream_options\"] = { include_usage: true };\n }\n const response = await request_function(promptBlueprint, kwargs);\n const _trackRequest = (body: object) => {\n const request_end_time = new Date().toISOString();\n return trackRequest({\n function_name,\n provider_type,\n args: [],\n kwargs,\n tags,\n request_start_time,\n request_end_time,\n api_key: this.apiKey,\n metadata,\n prompt_id: promptBlueprint.id,\n prompt_version: promptBlueprint.version,\n prompt_input_variables,\n group_id: groupId,\n return_prompt_blueprint: true,\n ...body,\n });\n };\n if (stream) return streamResponse(response, _trackRequest, stream_function);\n const requestLog = await _trackRequest({ request_response: response });\n const data = {\n request_id: requestLog.request_id,\n raw_response: response,\n prompt_blueprint: requestLog.prompt_blueprint,\n };\n return data;\n }\n}\n"],"mappings":"moDA0BA,IAAMA,EAAsB,8BAEtBC,EAAwB,CAC5BC,EACAC,IAGGC,EAAA,wBAEH,OADoBD,EAAK,iBAAiB,OAAO,aAAa,IAAM,OAE3DE,GAAeH,EAAQC,EAAK,iBAAkBA,CAAI,EAEpD,MAAMG,EAAsBJ,EAAQC,CAAI,CACjD,GAEMG,EAAwB,CAAOJ,EAAgBC,IAAuBC,EAAA,wBAC1E,GAAI,CACF,IAAMG,EAAW,MAAM,MAAM,GAAGP,CAAmB,iBAAkB,CACnE,OAAQ,OACR,QAAS,CACP,eAAgB,kBAClB,EACA,KAAM,KAAK,UAAUG,CAAI,CAC3B,CAAC,EACKK,EAAO,MAAMD,EAAS,KAAK,EAOjC,GANIA,EAAS,SAAW,KACtBE,EACED,EACA,mFACF,EAEEA,GAAQL,EAAK,aACf,MAAO,CAACA,EAAK,iBAAkBK,EAAK,UAAU,CAElD,OAASE,EAAG,CACV,QAAQ,KACN,4EAA4EA,CAAC,EAC/E,CACF,CACA,OAAOP,EAAK,gBACd,GAEMQ,EAA2B,CAC/BT,EACAC,IACqBC,EAAA,wBACrB,GAAI,CACF,IAAMG,EAAW,MAAM,MACrB,GAAGP,CAAmB,0BACtB,CACE,OAAQ,OACR,QAAS,CACP,eAAgB,kBAClB,EACA,KAAM,KAAK,UAAUY,EAAAC,EAAA,GAChBV,GADgB,CAEnB,QAASD,CACX,EAAC,C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AAqB,SACrCE,EAAqB,IAAI,KAAK,EAAE,YAAY,EAC5CC,EAASN,EAAgB,WACzBO,EACJlC,GACE+B,CACF,EAAEH,EAAe,IAAI,EACjBO,EAAgBD,EAAO,cACvBE,EAAkBF,EAAO,gBACzBG,EAAmBhC,GAAyB0B,CAAa,EACzDO,EAAoBX,EAAgB,kBACtCW,IACFL,EAAO,QAAaK,EAAkB,KAExCL,EAAO,OAAYT,EACfA,GAAUO,IAAkB,WAC9BE,EAAO,eAAoB,CAAE,cAAe,EAAK,GAEnD,IAAMM,EAAW,MAAMF,EAAiBV,EAAiBM,CAAM,EACzDO,EAAiBC,IAAiB,CACtC,IAAMC,GAAmB,IAAI,KAAK,EAAE,YAAY,EAChD,OAAOC,GAAaC,EAAA,CAClB,cAAAT,EACA,cAAAJ,EACA,KAAM,CAAC,EACP,OAAAE,EACA,KAAAZ,EACA,mBAAAW,EACA,iBAAAU,GACA,QAAS,KAAK,OACd,SAAApB,EACA,UAAWK,EAAgB,GAC3B,eAAgBA,EAAgB,QAChC,uBAAAF,EACA,SAAUF,EACV,wBAAyB,IACtBkB,GACJ,CACH,EACA,GAAIjB,EAAQ,OAAOqB,GAAeN,EAAUC,EAAeJ,CAAe,EAC1E,IAAMU,EAAa,MAAMN,EAAc,CAAE,iBAAkBD,CAAS,CAAC,EAMrE,MALa,CACX,WAAYO,EAAW,WACvB,aAAcP,EACd,iBAAkBO,EAAW,gBAC/B,CAEF,GACF","names":["URL_API_PROMPTLAYER","promptlayerApiHandler","apiKey","body","__async","proxyGenerator","promptLayerApiRequest","response","data","warnOnBadResponse","e","promptLayerTrackMetadata","__spreadProps","__spreadValues","promptLayerTrackScore","promptLayerTrackPrompt","promptLayerTrackGroup","promptLayerCreateGroup","getPromptTemplate","promptName","params","url","publishPromptTemplate","getAllPromptTemplates","_a","key","value","openaiStreamChat","results","_b","_c","_d","_e","content","functionCall","lastResult","toolCalls","result","delta","toolCall","lastToolCall","anthropicStreamMessage","cleaned_result","function_name","prev","current","final_result","generator","__asyncGenerator","iter","__forAwait","more","temp","error","__await","request_response","request_id","main_message","trackRequest","openaiStreamCompletion","text","anthropicStreamCompletion","completion","streamResponse","afterStream","mapResults","openaiChatRequest","client","kwargs","openaiCompletionsRequest","MAP_TYPE_TO_OPENAI_FUNCTION","openaiRequest","promptBlueprint","OpenAI","requestToMake","anthropicChatRequest","anthropicCompletionsRequest","MAP_TYPE_TO_ANTHROPIC_FUNCTION","anthropicRequest","Anthropic","GroupManager","apiKey","promptLayerCreateGroup","promptLayerBase","apiKey","llm","function_name","provider","handler","target","args","newTarget","prop","receiver","value","_a","_b","_c","_d","request_start_time","provider_type","return_pl_id","pl_tags","response","resolve","reject","request_response","__async","promptlayerApiHandler","error","TemplateManager","apiKey","promptName","params","getPromptTemplate","body","publishPromptTemplate","getAllPromptTemplates","metadata","apiKey","body","key","value","promptLayerTrackMetadata","score","promptLayerTrackScore","prompt","promptLayerTrackPrompt","group","promptLayerTrackGroup","TrackManager","MAP_PROVIDER_TO_FUNCTION_NAME","openaiStreamChat","openaiStreamCompletion","anthropicStreamMessage","anthropicStreamCompletion","MAP_PROVIDER_TO_FUNCTION","openaiRequest","anthropicRequest","PromptLayer","apiKey","TemplateManager","GroupManager","TrackManager","module","promptLayerBase","_0","__async","promptName","promptVersion","promptReleaseLabel","inputVariables","tags","metadata","groupId","stream","prompt_input_variables","templateGetParams","promptBlueprint","promptTemplate","promptBlueprintMetadata","promptBlueprintModel","provider_type","request_start_time","kwargs","config","function_name","stream_function","request_function","provider_base_url","response","_trackRequest","body","request_end_time","trackRequest","__spreadValues","streamResponse","requestLog"]}
|
package/dist/index.d.mts
CHANGED
|
@@ -30,8 +30,9 @@ interface Pagination {
|
|
|
30
30
|
interface GetPromptTemplateParams {
|
|
31
31
|
version?: number;
|
|
32
32
|
label?: string;
|
|
33
|
-
provider
|
|
34
|
-
input_variables
|
|
33
|
+
provider?: string;
|
|
34
|
+
input_variables?: Record<string, string>;
|
|
35
|
+
metadata_filters?: Record<string, string>;
|
|
35
36
|
}
|
|
36
37
|
declare const templateFormat: readonly ["f-string", "jinja2"];
|
|
37
38
|
type TemplateFormat = (typeof templateFormat)[number];
|
|
@@ -148,6 +149,12 @@ type PromptVersion = {
|
|
|
148
149
|
type PublishPromptTemplate = BasePromptTemplate & PromptVersion & {
|
|
149
150
|
release_labels?: string[];
|
|
150
151
|
};
|
|
152
|
+
interface ProviderBaseURL {
|
|
153
|
+
id: number;
|
|
154
|
+
name: string;
|
|
155
|
+
provider: string;
|
|
156
|
+
url: string;
|
|
157
|
+
}
|
|
151
158
|
interface BasePromptTemplateResponse {
|
|
152
159
|
id: number;
|
|
153
160
|
prompt_name: string;
|
|
@@ -155,6 +162,7 @@ interface BasePromptTemplateResponse {
|
|
|
155
162
|
prompt_template: PromptTemplate;
|
|
156
163
|
commit_message?: string;
|
|
157
164
|
metadata?: Metadata;
|
|
165
|
+
provider_base_url?: ProviderBaseURL;
|
|
158
166
|
}
|
|
159
167
|
interface PublishPromptTemplateResponse extends BasePromptTemplateResponse {
|
|
160
168
|
}
|
|
@@ -165,6 +173,16 @@ interface GetPromptTemplateResponse extends BasePromptTemplateResponse {
|
|
|
165
173
|
interface ListPromptTemplatesResponse extends BasePromptTemplateResponse {
|
|
166
174
|
version: number;
|
|
167
175
|
}
|
|
176
|
+
interface RunRequest {
|
|
177
|
+
promptName: string;
|
|
178
|
+
tags?: string[];
|
|
179
|
+
metadata?: Record<string, string>;
|
|
180
|
+
groupId?: number;
|
|
181
|
+
stream?: boolean;
|
|
182
|
+
promptVersion?: number;
|
|
183
|
+
promptReleaseLabel?: string;
|
|
184
|
+
inputVariables?: Record<string, string>;
|
|
185
|
+
}
|
|
168
186
|
|
|
169
187
|
declare class TemplateManager {
|
|
170
188
|
apiKey: string;
|
|
@@ -194,6 +212,15 @@ declare class PromptLayer {
|
|
|
194
212
|
constructor({ apiKey, }?: ClientOptions);
|
|
195
213
|
get OpenAI(): any;
|
|
196
214
|
get Anthropic(): any;
|
|
215
|
+
run({ promptName, promptVersion, promptReleaseLabel, inputVariables, tags, metadata, groupId, stream, }: RunRequest): Promise<AsyncGenerator<{
|
|
216
|
+
request_id: number | null;
|
|
217
|
+
raw_response: any;
|
|
218
|
+
prompt_blueprint: any;
|
|
219
|
+
}, void, unknown> | {
|
|
220
|
+
request_id: any;
|
|
221
|
+
raw_response: any;
|
|
222
|
+
prompt_blueprint: any;
|
|
223
|
+
}>;
|
|
197
224
|
}
|
|
198
225
|
|
|
199
226
|
export { ClientOptions, PromptLayer };
|
package/dist/index.d.ts
CHANGED
|
@@ -30,8 +30,9 @@ interface Pagination {
|
|
|
30
30
|
interface GetPromptTemplateParams {
|
|
31
31
|
version?: number;
|
|
32
32
|
label?: string;
|
|
33
|
-
provider
|
|
34
|
-
input_variables
|
|
33
|
+
provider?: string;
|
|
34
|
+
input_variables?: Record<string, string>;
|
|
35
|
+
metadata_filters?: Record<string, string>;
|
|
35
36
|
}
|
|
36
37
|
declare const templateFormat: readonly ["f-string", "jinja2"];
|
|
37
38
|
type TemplateFormat = (typeof templateFormat)[number];
|
|
@@ -148,6 +149,12 @@ type PromptVersion = {
|
|
|
148
149
|
type PublishPromptTemplate = BasePromptTemplate & PromptVersion & {
|
|
149
150
|
release_labels?: string[];
|
|
150
151
|
};
|
|
152
|
+
interface ProviderBaseURL {
|
|
153
|
+
id: number;
|
|
154
|
+
name: string;
|
|
155
|
+
provider: string;
|
|
156
|
+
url: string;
|
|
157
|
+
}
|
|
151
158
|
interface BasePromptTemplateResponse {
|
|
152
159
|
id: number;
|
|
153
160
|
prompt_name: string;
|
|
@@ -155,6 +162,7 @@ interface BasePromptTemplateResponse {
|
|
|
155
162
|
prompt_template: PromptTemplate;
|
|
156
163
|
commit_message?: string;
|
|
157
164
|
metadata?: Metadata;
|
|
165
|
+
provider_base_url?: ProviderBaseURL;
|
|
158
166
|
}
|
|
159
167
|
interface PublishPromptTemplateResponse extends BasePromptTemplateResponse {
|
|
160
168
|
}
|
|
@@ -165,6 +173,16 @@ interface GetPromptTemplateResponse extends BasePromptTemplateResponse {
|
|
|
165
173
|
interface ListPromptTemplatesResponse extends BasePromptTemplateResponse {
|
|
166
174
|
version: number;
|
|
167
175
|
}
|
|
176
|
+
interface RunRequest {
|
|
177
|
+
promptName: string;
|
|
178
|
+
tags?: string[];
|
|
179
|
+
metadata?: Record<string, string>;
|
|
180
|
+
groupId?: number;
|
|
181
|
+
stream?: boolean;
|
|
182
|
+
promptVersion?: number;
|
|
183
|
+
promptReleaseLabel?: string;
|
|
184
|
+
inputVariables?: Record<string, string>;
|
|
185
|
+
}
|
|
168
186
|
|
|
169
187
|
declare class TemplateManager {
|
|
170
188
|
apiKey: string;
|
|
@@ -194,6 +212,15 @@ declare class PromptLayer {
|
|
|
194
212
|
constructor({ apiKey, }?: ClientOptions);
|
|
195
213
|
get OpenAI(): any;
|
|
196
214
|
get Anthropic(): any;
|
|
215
|
+
run({ promptName, promptVersion, promptReleaseLabel, inputVariables, tags, metadata, groupId, stream, }: RunRequest): Promise<AsyncGenerator<{
|
|
216
|
+
request_id: number | null;
|
|
217
|
+
raw_response: any;
|
|
218
|
+
prompt_blueprint: any;
|
|
219
|
+
}, void, unknown> | {
|
|
220
|
+
request_id: any;
|
|
221
|
+
raw_response: any;
|
|
222
|
+
prompt_blueprint: any;
|
|
223
|
+
}>;
|
|
197
224
|
}
|
|
198
225
|
|
|
199
226
|
export { ClientOptions, PromptLayer };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var T=Object.defineProperty,V=Object.defineProperties,Z=Object.getOwnPropertyDescriptor,ee=Object.getOwnPropertyDescriptors,te=Object.getOwnPropertyNames,W=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable;var O=(r,e)=>{if(e=Symbol[r])return e;throw Error("Symbol."+r+" is not defined")};var q=(r,e,t)=>e in r?T(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,h=(r,e)=>{for(var t in e||(e={}))x.call(e,t)&&q(r,t,e[t]);if(W)for(var t of W(e))re.call(e,t)&&q(r,t,e[t]);return r},w=(r,e)=>V(r,ee(e));var oe=(r,e)=>{for(var t in e)T(r,t,{get:e[t],enumerable:!0})},ae=(r,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of te(e))!x.call(r,a)&&a!==t&&T(r,a,{get:()=>e[a],enumerable:!(o=Z(e,a))||o.enumerable});return r};var ne=r=>ae(T({},"__esModule",{value:!0}),r);var u=(r,e,t)=>new Promise((o,a)=>{var n=s=>{try{i(t.next(s))}catch(m){a(m)}},p=s=>{try{i(t.throw(s))}catch(m){a(m)}},i=s=>s.done?o(s.value):Promise.resolve(s.value).then(n,p);i((t=t.apply(r,e)).next())}),_=function(r,e){this[0]=r,this[1]=e},j=(r,e,t)=>{var o=(p,i,s,m)=>{try{var l=t[p](i),d=(i=l.value)instanceof _,f=l.done;Promise.resolve(d?i[0]:i).then(c=>d?o(p==="return"?p:"next",i[1]?{done:c.done,value:c.value}:c,s,m):s({value:c,done:f})).catch(c=>o("throw",c,s,m))}catch(c){m(c)}},a=p=>n[p]=i=>new Promise((s,m)=>o(p,i,s,m)),n={};return t=t.apply(r,e),n[Symbol.asyncIterator]=()=>n,a("next"),a("throw"),a("return"),n};var v=(r,e,t)=>(e=r[O("asyncIterator")])?e.call(r):(r=r[O("iterator")](),e={},t=(o,a)=>(a=r[o])&&(e[o]=n=>new Promise((p,i,s)=>(n=a.call(r,n),s=n.done,Promise.resolve(n.value).then(m=>p({value:m,done:s}),i)))),t("next"),t("return"),e);var fe={};oe(fe,{PromptLayer:()=>A});module.exports=ne(fe);var g="https://api.promptlayer.com",K=(r,e)=>u(void 0,null,function*(){return e.request_response[Symbol.asyncIterator]!==void 0?ce(r,e.request_response,e):yield M(r,e)}),M=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),o=yield t.json();if(t.status!==200&&y(o,"WARNING: While logging your request, PromptLayer experienced the following error:"),o&&e.return_pl_id)return[e.request_response,o.request_id]}catch(t){console.warn(`WARNING: While logging your request PromptLayer had the following error: ${t}`)}return e.request_response}),E=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-metadata`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(w(h({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return y(o,"WARNING: While logging metadata to your request, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While logging metadata to your request, PromptLayer experienced the following error: ${t}`),!1}return!0}),J=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-score`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(w(h({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return y(o,"WARNING: While scoring your request, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While scoring your request, PromptLayer experienced the following error: ${t}`),!1}return!0}),Y=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-prompt`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(w(h({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return y(o,"WARNING: While associating your request with a prompt template, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While associating your request with a prompt template, PromptLayer experienced the following error: ${t}`),!1}return!0}),D=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/track-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(w(h({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return y(o,"WARNING: While associating your request with a group, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While associating your request with a group, PromptLayer experienced the following error: ${t}`),!1}return!0}),B=r=>u(void 0,null,function*(){try{let e=yield fetch(`${g}/create-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({api_key:r})}),t=yield e.json();return e.status!==200?(y(t,"WARNING: While creating a group PromptLayer had the following error"),!1):t.id}catch(e){return console.warn(`WARNING: While creating a group PromptLayer had the following error: ${e}`),!1}}),H=(r,e,t)=>u(void 0,null,function*(){try{let o=new URL(`${g}/prompt-templates/${e}`),a=yield fetch(o,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":r},body:JSON.stringify(t)}),n=yield a.json();return a.status!==200?(y(n,"WARNING: While fetching a prompt template PromptLayer had the following error"),null):n}catch(o){return console.warn(`WARNING: While fetching a prompt template PromptLayer had the following error: ${o}`),null}}),U=(r,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${g}/rest/prompt-templates`,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":r},body:JSON.stringify({prompt_template:h({},e),prompt_version:h({},e),release_labels:e.release_labels?e.release_labels:void 0})}),o=yield t.json();return t.status===400&&y(o,"WARNING: While publishing a prompt template PromptLayer had the following error"),o}catch(t){console.warn(`WARNING: While publishing a prompt template PromptLayer had the following error: ${t}`)}}),X=(r,e)=>u(void 0,null,function*(){var t;try{let o=new URL(`${g}/prompt-templates`);Object.entries(e||{}).forEach(([p,i])=>o.searchParams.append(p,i.toString()));let a=yield fetch(o,{headers:{"Content-Type":"application/json","X-API-KEY":r}}),n=yield a.json();return a.status!==200?(y(n,"WARNING: While fetching all prompt templates PromptLayer had the following error"),null):(t=n.items)!=null?t:[]}catch(o){return console.warn(`WARNING: While fetching all prompt templates PromptLayer had the following error: ${o}`),null}}),se=r=>{var p,i,s,m,l;let e=null,t,o={id:"",choices:[],created:Date.now(),model:"",object:"chat.completion"},a=r.at(-1);if(!a)return o;let n;for(let d of r){let f=d.choices[0].delta;f.content&&(e=`${e||""}${f.content||""}`),f.function_call&&(t={name:`${t?t.name:""}${f.function_call.name||""}`,arguments:`${t?t.arguments:""}${f.function_call.arguments||""}`});let c=(p=f.tool_calls)==null?void 0:p[0];if(c){n=n||[];let P=n.at(-1);if(!P||c.id){n.push({id:c.id||"",type:c.type||"function",function:{name:((i=c.function)==null?void 0:i.name)||"",arguments:((s=c.function)==null?void 0:s.arguments)||""}});continue}P.function.name=`${P.function.name}${((m=c.function)==null?void 0:m.name)||""}`,P.function.arguments=`${P.function.arguments}${((l=c.function)==null?void 0:l.arguments)||""}`}}return o.choices.push({finish_reason:a.choices[0].finish_reason||"stop",index:a.choices[0].index||0,logprobs:a.choices[0].logprobs||null,message:{role:"assistant",content:e,function_call:t||void 0,tool_calls:n||void 0}}),o.id=a.id,o.model=a.model,o.created=a.created,o.system_fingerprint=a.system_fingerprint,o},ie=r=>{let e={id:"",model:"",content:[],role:"assistant",type:"message",stop_reason:"stop_sequence",stop_sequence:null,usage:{input_tokens:0,output_tokens:0}};if(!r.at(-1))return e;let o="";for(let a of r)switch(a.type){case"message_start":{e=h({},a.message);break}case"content_block_delta":a.delta.type==="text_delta"&&(o=`${o}${a.delta.text}`);case"message_delta":"usage"in a&&(e.usage.output_tokens=a.usage.output_tokens),"stop_reason"in a.delta&&(e.stop_reason=a.delta.stop_reason);default:break}return e.content.push({type:"text",text:o}),e},pe=(r,e="openai.chat.completions.create")=>{if("completion"in r[0])return r.reduce((t,o)=>w(h({},o),{completion:`${t.completion}${o.completion}`}),{});if(e==="anthropic.messages.create")return ie(r);if("text"in r[0].choices[0]){let t="";for(let a of r)t=`${t}${a.choices[0].text}`;let o=structuredClone(r.at(-1));return o.choices[0].text=t,o}if("delta"in r[0].choices[0]){let t=se(r);return t.choices[0]=h(h({},t.choices[0]),t.choices[0].message),t}return""};function ce(r,e,t){return j(this,null,function*(){let o=[];try{for(var p=v(e),i,s,m;i=!(s=yield new _(p.next())).done;i=!1){let l=s.value;yield t.return_pl_id?[l,null]:l,o.push(l)}}catch(s){m=[s]}finally{try{i&&(s=p.return)&&(yield new _(s.call(p)))}finally{if(m)throw m[0]}}let a=pe(o,t.function_name),n=yield new _(M(r,w(h({},t),{request_response:a,request_end_time:new Date().toISOString()})));if(n&&t.return_pl_id){let l=n[1];yield[o.at(-1),l]}})}var y=(r,e)=>{try{console.warn(`${e}: ${r.message}`)}catch(t){console.warn(`${e}: ${r}`)}};var k=class{constructor(e){this.create=()=>B(this.apiKey);this.apiKey=e}};var $=(r,e,t="",o="openai")=>{let a={construct:(n,p)=>{let i=Reflect.construct(n,p);return Object.defineProperties(i,{function_name:{value:t,writable:!0},provider:{value:o}}),new Proxy(i,a)},get:(n,p,i)=>{let s=n[p],m=`${Reflect.get(n,"function_name")}.${p.toString()}`;return typeof s=="object"?(Object.defineProperties(s,{function_name:{value:m,writable:!0},provider:{value:o}}),new Proxy(s,a)):typeof s=="function"?(...l)=>{var L,G,I,S;let d=new Date().toISOString(),f=Reflect.get(n,"provider"),c=(L=l[0])==null?void 0:L.return_pl_id,P=(G=l[0])==null?void 0:G.pl_tags;(I=l[0])==null||delete I.return_pl_id,(S=l[0])==null||delete S.pl_tags;let N=Reflect.apply(s,n,l);return N instanceof Promise?new Promise((z,F)=>{N.then(C=>u(void 0,null,function*(){let Q=yield K(r,{api_key:r,provider_type:f,function_name:m,request_start_time:d,request_end_time:new Date().toISOString(),request_response:C,kwargs:l[0],return_pl_id:c,tags:P});z(Q)})).catch(C=>{F(C)})}):N}:Reflect.get(n,p,i)}};return new Proxy(e,a)};var R=class{constructor(e){this.get=(e,t)=>H(this.apiKey,e,t);this.publish=e=>U(this.apiKey,e);this.all=e=>X(this.apiKey,e);this.apiKey=e}};var le=(r,e)=>{if(!(e.metadata instanceof Object))throw new Error("Please provide a dictionary of metadata.");for(let[t,o]of Object.entries(e.metadata))if(typeof t!="string"||typeof o!="string")throw new Error("Please provide a dictionary of metadata with key value pair of strings.");return E(r,e)},me=(r,e)=>{if(typeof e.score!="number")throw new Error("Score must be a number");if(e.score<0||e.score>100)throw new Error("Score must be a number between 0 and 100.");return J(r,e)},ue=(r,e)=>{if(!(e.prompt_input_variables instanceof Object))throw new Error("Prompt template input variable dictionary not provided.");return Y(r,e)},he=(r,e)=>D(r,e),b=class{constructor(e){this.group=e=>he(this.apiKey,e);this.metadata=e=>le(this.apiKey,e);this.prompt=e=>ue(this.apiKey,e);this.score=e=>me(this.apiKey,e);this.apiKey=e}};var A=class{constructor({apiKey:e=process.env.PROMPTLAYER_API_KEY}={}){if(e===void 0)throw new Error("PromptLayer API key not provided. Please set the PROMPTLAYER_API_KEY environment variable or pass the api_key parameter.");this.apiKey=e,this.templates=new R(e),this.group=new k(e),this.track=new b(e)}get OpenAI(){try{let e=require("openai").default;return $(this.apiKey,e,"openai","openai")}catch(e){console.error("To use the OpenAI module, you must install the @openai/api package.")}}get Anthropic(){try{let e=require("@anthropic-ai/sdk").default;return $(this.apiKey,e,"anthropic","anthropic")}catch(e){console.error("To use the Anthropic module, you must install the @anthropic-ai/sdk package.")}}};0&&(module.exports={PromptLayer});
|
|
1
|
+
"use strict";var N=Object.defineProperty,me=Object.defineProperties,ue=Object.getOwnPropertyDescriptor,he=Object.getOwnPropertyDescriptors,fe=Object.getOwnPropertyNames,D=Object.getOwnPropertySymbols;var B=Object.prototype.hasOwnProperty,de=Object.prototype.propertyIsEnumerable;var Y=(r,e)=>{if(e=Symbol[r])return e;throw Error("Symbol."+r+" is not defined")};var J=(r,e,t)=>e in r?N(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,d=(r,e)=>{for(var t in e||(e={}))B.call(e,t)&&J(r,t,e[t]);if(D)for(var t of D(e))de.call(e,t)&&J(r,t,e[t]);return r},R=(r,e)=>me(r,he(e));var ye=(r,e)=>{for(var t in e)N(r,t,{get:e[t],enumerable:!0})},ge=(r,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of fe(e))!B.call(r,n)&&n!==t&&N(r,n,{get:()=>e[n],enumerable:!(o=ue(e,n))||o.enumerable});return r};var _e=r=>ge(N({},"__esModule",{value:!0}),r);var c=(r,e,t)=>new Promise((o,n)=>{var a=s=>{try{p(t.next(s))}catch(h){n(h)}},i=s=>{try{p(t.throw(s))}catch(h){n(h)}},p=s=>s.done?o(s.value):Promise.resolve(s.value).then(a,i);p((t=t.apply(r,e)).next())}),w=function(r,e){this[0]=r,this[1]=e},W=(r,e,t)=>{var o=(i,p,s,h)=>{try{var m=t[i](p),f=(p=m.value)instanceof w,u=m.done;Promise.resolve(f?p[0]:p).then(l=>f?o(i==="return"?i:"next",p[1]?{done:l.done,value:l.value}:l,s,h):s({value:l,done:u})).catch(l=>o("throw",l,s,h))}catch(l){h(l)}},n=i=>a[i]=p=>new Promise((s,h)=>o(i,p,s,h)),a={};return t=t.apply(r,e),a[Symbol.asyncIterator]=()=>a,n("next"),n("throw"),n("return"),a};var x=(r,e,t)=>(e=r[Y("asyncIterator")])?e.call(r):(r=r[Y("iterator")](),e={},t=(o,n)=>(n=r[o])&&(e[o]=a=>new Promise((i,p,s)=>(a=n.call(r,a),s=a.done,Promise.resolve(a.value).then(h=>i({value:h,done:s}),p)))),t("next"),t("return"),e);var Le={};ye(Le,{PromptLayer:()=>E});module.exports=_e(Le);var g="https://api.promptlayer.com",F=(r,e)=>c(void 0,null,function*(){return e.request_response[Symbol.asyncIterator]!==void 0?we(r,e.request_response,e):yield H(r,e)}),H=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),o=yield t.json();if(t.status!==200&&_(o,"WARNING: While logging your request, PromptLayer experienced the following error:"),o&&e.return_pl_id)return[e.request_response,o.request_id]}catch(t){console.warn(`WARNING: While logging your request PromptLayer had the following error: ${t}`)}return e.request_response}),X=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-metadata`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(d({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return _(o,"WARNING: While logging metadata to your request, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While logging metadata to your request, PromptLayer experienced the following error: ${t}`),!1}return!0}),V=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-score`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(d({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return _(o,"WARNING: While scoring your request, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While scoring your request, PromptLayer experienced the following error: ${t}`),!1}return!0}),z=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/library-track-prompt`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(d({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return _(o,"WARNING: While associating your request with a prompt template, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While associating your request with a prompt template, PromptLayer experienced the following error: ${t}`),!1}return!0}),Q=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/track-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(d({},e),{api_key:r}))}),o=yield t.json();if(t.status!==200)return _(o,"WARNING: While associating your request with a group, PromptLayer experienced the following error"),!1}catch(t){return console.warn(`WARNING: While associating your request with a group, PromptLayer experienced the following error: ${t}`),!1}return!0}),Z=r=>c(void 0,null,function*(){try{let e=yield fetch(`${g}/create-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({api_key:r})}),t=yield e.json();return e.status!==200?(_(t,"WARNING: While creating a group PromptLayer had the following error"),!1):t.id}catch(e){return console.warn(`WARNING: While creating a group PromptLayer had the following error: ${e}`),!1}}),ee=(r,e,t)=>c(void 0,null,function*(){try{let o=new URL(`${g}/prompt-templates/${e}`),n=yield fetch(o,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":r},body:JSON.stringify(t)}),a=yield n.json();return n.status!==200?(_(a,"WARNING: While fetching a prompt template PromptLayer had the following error"),null):a}catch(o){return console.warn(`WARNING: While fetching a prompt template PromptLayer had the following error: ${o}`),null}}),te=(r,e)=>c(void 0,null,function*(){try{let t=yield fetch(`${g}/rest/prompt-templates`,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":r},body:JSON.stringify({prompt_template:d({},e),prompt_version:d({},e),release_labels:e.release_labels?e.release_labels:void 0})}),o=yield t.json();return t.status===400&&_(o,"WARNING: While publishing a prompt template PromptLayer had the following error"),o}catch(t){console.warn(`WARNING: While publishing a prompt template PromptLayer had the following error: ${t}`)}}),re=(r,e)=>c(void 0,null,function*(){var t;try{let o=new URL(`${g}/prompt-templates`);Object.entries(e||{}).forEach(([i,p])=>o.searchParams.append(i,p.toString()));let n=yield fetch(o,{headers:{"Content-Type":"application/json","X-API-KEY":r}}),a=yield n.json();return n.status!==200?(_(a,"WARNING: While fetching all prompt templates PromptLayer had the following error"),null):(t=a.items)!=null?t:[]}catch(o){return console.warn(`WARNING: While fetching all prompt templates PromptLayer had the following error: ${o}`),null}}),v=r=>{var i,p,s,h,m;let e=null,t,o={id:"",choices:[],created:Date.now(),model:"",object:"chat.completion"},n=r.at(-1);if(!n)return o;let a;for(let f of r){if(f.choices.length===0)continue;let u=f.choices[0].delta;u.content&&(e=`${e||""}${u.content||""}`),u.function_call&&(t={name:`${t?t.name:""}${u.function_call.name||""}`,arguments:`${t?t.arguments:""}${u.function_call.arguments||""}`});let l=(i=u.tool_calls)==null?void 0:i[0];if(l){a=a||[];let y=a.at(-1);if(!y||l.id){a.push({id:l.id||"",type:l.type||"function",function:{name:((p=l.function)==null?void 0:p.name)||"",arguments:((s=l.function)==null?void 0:s.arguments)||""}});continue}y.function.name=`${y.function.name}${((h=l.function)==null?void 0:h.name)||""}`,y.function.arguments=`${y.function.arguments}${((m=l.function)==null?void 0:m.arguments)||""}`}}return o.choices.push({finish_reason:r[0].choices[0].finish_reason||"stop",index:r[0].choices[0].index||0,logprobs:r[0].choices[0].logprobs||null,message:{role:"assistant",content:e,function_call:t||void 0,tool_calls:a||void 0}}),o.id=n.id,o.model=n.model,o.created=n.created,o.system_fingerprint=n.system_fingerprint,o.usage=n.usage,o},j=r=>{let e={id:"",model:"",content:[],role:"assistant",type:"message",stop_reason:"stop_sequence",stop_sequence:null,usage:{input_tokens:0,output_tokens:0}};if(!r.at(-1))return e;let o="";for(let n of r)switch(n.type){case"message_start":{e=d({},n.message);break}case"content_block_delta":n.delta.type==="text_delta"&&(o=`${o}${n.delta.text}`);case"message_delta":"usage"in n&&(e.usage.output_tokens=n.usage.output_tokens),"stop_reason"in n.delta&&(e.stop_reason=n.delta.stop_reason);default:break}return e.content.push({type:"text",text:o}),e},Pe=(r,e="openai.chat.completions.create")=>{if("completion"in r[0])return r.reduce((t,o)=>R(d({},o),{completion:`${t.completion}${o.completion}`}),{});if(e==="anthropic.messages.create")return j(r);if("text"in r[0].choices[0]){let t="";for(let n of r)t=`${t}${n.choices[0].text}`;let o=structuredClone(r.at(-1));return o.choices[0].text=t,o}if("delta"in r[0].choices[0]){let t=v(r);return t.choices[0]=d(d({},t.choices[0]),t.choices[0].message),t}return""};function we(r,e,t){return W(this,null,function*(){let o=[];try{for(var i=x(e),p,s,h;p=!(s=yield new w(i.next())).done;p=!1){let m=s.value;yield t.return_pl_id?[m,null]:m,o.push(m)}}catch(s){h=[s]}finally{try{p&&(s=i.return)&&(yield new w(s.call(i)))}finally{if(h)throw h[0]}}let n=Pe(o,t.function_name),a=yield new w(H(r,R(d({},t),{request_response:n,request_end_time:new Date().toISOString()})));if(a&&t.return_pl_id){let m=a[1];yield[o.at(-1),m]}})}var _=(r,e)=>{try{console.warn(`${e}: ${r.message}`)}catch(t){console.warn(`${e}: ${r}`)}},oe=r=>c(void 0,null,function*(){try{let e=yield fetch(`${g}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)});return e.status!==200&&_(e,"WARNING: While logging your request, PromptLayer experienced the following error:"),e.json()}catch(e){console.warn(`WARNING: While logging your request PromptLayer had the following error: ${e}`)}return{}}),ne=r=>{let e={id:"",choices:[{finish_reason:"stop",index:0,text:"",logprobs:null}],created:Date.now(),model:"",object:"text_completion"},t=r.at(-1);if(!t)return e;let o="";for(let n of r)n.choices.length>0&&n.choices[0].text&&(o=`${o}${n.choices[0].text}`);return e.choices[0].text=o,e.id=t.id,e.created=t.created,e.model=t.model,e.system_fingerprint=t.system_fingerprint,e.usage=t.usage,e},ae=r=>{let e={completion:"",id:"",model:"",stop_reason:"",type:"completion"},t=r.at(-1);if(!t)return e;let o="";for(let n of r)o=`${o}${n.completion}`;return e.completion=o,e.id=t.id,e.model=t.model,e.stop_reason=t.stop_reason,e};function se(r,e,t){return W(this,null,function*(){let o={request_id:null,raw_response:null,prompt_blueprint:null},n=[];try{for(var p=x(r),s,h,m;s=!(h=yield new w(p.next())).done;s=!1){let f=h.value;n.push(f),o.raw_response=f,yield o}}catch(h){m=[h]}finally{try{s&&(h=p.return)&&(yield new w(h.call(p)))}finally{if(m)throw m[0]}}let a=t(n),i=yield new w(e({request_response:a}));o.request_id=i.request_id,o.prompt_blueprint=i.prompt_blueprint,yield o})}var Te=(r,e)=>c(void 0,null,function*(){return r.chat.completions.create(e)}),Re=(r,e)=>c(void 0,null,function*(){return r.completions.create(e)}),ke={chat:Te,completion:Re},ie=(r,e)=>c(void 0,null,function*(){let t=require("openai").default,o=new t({baseURL:e.baseURL}),n=ke[r.prompt_template.type];return n(o,e)}),be=(r,e)=>c(void 0,null,function*(){return r.messages.create(e)}),qe=(r,e)=>c(void 0,null,function*(){return r.completions.create(e)}),Ae={chat:be,completion:qe},pe=(r,e)=>c(void 0,null,function*(){let t=require("@anthropic-ai/sdk").default,o=new t({baseURL:e.baseURL}),n=Ae[r.prompt_template.type];return n(o,e)});var I=class{constructor(e){this.create=()=>Z(this.apiKey);this.apiKey=e}};var M=(r,e,t="",o="openai")=>{let n={construct:(a,i)=>{let p=Reflect.construct(a,i);return Object.defineProperties(p,{function_name:{value:t,writable:!0},provider:{value:o}}),new Proxy(p,n)},get:(a,i,p)=>{let s=a[i],h=`${Reflect.get(a,"function_name")}.${i.toString()}`;return typeof s=="object"?(Object.defineProperties(s,{function_name:{value:h,writable:!0},provider:{value:o}}),new Proxy(s,n)):typeof s=="function"?(...m)=>{var T,A,P,b;let f=new Date().toISOString(),u=Reflect.get(a,"provider"),l=(T=m[0])==null?void 0:T.return_pl_id,y=(A=m[0])==null?void 0:A.pl_tags;(P=m[0])==null||delete P.return_pl_id,(b=m[0])==null||delete b.pl_tags;let k=Reflect.apply(s,a,m);return k instanceof Promise?new Promise(($,L)=>{k.then(q=>c(void 0,null,function*(){let C=yield F(r,{api_key:r,provider_type:u,function_name:h,request_start_time:f,request_end_time:new Date().toISOString(),request_response:q,kwargs:m[0],return_pl_id:l,tags:y});$(C)})).catch(q=>{L(q)})}):k}:Reflect.get(a,i,p)}};return new Proxy(e,n)};var O=class{constructor(e){this.get=(e,t)=>ee(this.apiKey,e,t);this.publish=e=>te(this.apiKey,e);this.all=e=>re(this.apiKey,e);this.apiKey=e}};var Ce=(r,e)=>{if(!(e.metadata instanceof Object))throw new Error("Please provide a dictionary of metadata.");for(let[t,o]of Object.entries(e.metadata))if(typeof t!="string"||typeof o!="string")throw new Error("Please provide a dictionary of metadata with key value pair of strings.");return X(r,e)},Ne=(r,e)=>{if(typeof e.score!="number")throw new Error("Score must be a number");if(e.score<0||e.score>100)throw new Error("Score must be a number between 0 and 100.");return V(r,e)},Ie=(r,e)=>{if(!(e.prompt_input_variables instanceof Object))throw new Error("Prompt template input variable dictionary not provided.");return z(r,e)},Oe=(r,e)=>Q(r,e),S=class{constructor(e){this.group=e=>Oe(this.apiKey,e);this.metadata=e=>Ce(this.apiKey,e);this.prompt=e=>Ie(this.apiKey,e);this.score=e=>Ne(this.apiKey,e);this.apiKey=e}};var Se={openai:{chat:{function_name:"openai.chat.completions.create",stream_function:v},completion:{function_name:"openai.completions.create",stream_function:ne}},anthropic:{chat:{function_name:"anthropic.messages.create",stream_function:j},completion:{function_name:"anthropic.completions.create",stream_function:ae}}},$e={openai:ie,anthropic:pe},E=class{constructor({apiKey:e=process.env.PROMPTLAYER_API_KEY}={}){if(e===void 0)throw new Error("PromptLayer API key not provided. Please set the PROMPTLAYER_API_KEY environment variable or pass the api_key parameter.");this.apiKey=e,this.templates=new O(e),this.group=new I(e),this.track=new S(e)}get OpenAI(){try{let e=require("openai").default;return M(this.apiKey,e,"openai","openai")}catch(e){console.error("To use the OpenAI module, you must install the @openai/api package.")}}get Anthropic(){try{let e=require("@anthropic-ai/sdk").default;return M(this.apiKey,e,"anthropic","anthropic")}catch(e){console.error("To use the Anthropic module, you must install the @anthropic-ai/sdk package.")}}run(h){return c(this,arguments,function*({promptName:e,promptVersion:t,promptReleaseLabel:o,inputVariables:n,tags:a,metadata:i,groupId:p,stream:s=!1}){let m=n,f={label:o,version:t,metadata_filters:i};n&&(f.input_variables=n);let u=yield this.templates.get(e,f);if(!u)throw new Error("Prompt not found");let l=u.prompt_template;if(!u.llm_kwargs)throw new Error(`Prompt '${e}' does not have any LLM kwargs associated with it.`);let y=u.metadata;if(!y)throw new Error(`Prompt '${e}' does not have any metadata associated with it.`);let k=y.model;if(!k)throw new Error(`Prompt '${e}' does not have a model parameters associated with it.`);let T=k.provider,A=new Date().toISOString(),P=u.llm_kwargs,b=Se[T][l.type],$=b.function_name,L=b.stream_function,q=$e[T],C=u.provider_base_url;C&&(P.baseURL=C.url),P.stream=s,s&&T==="openai"&&(P.stream_options={include_usage:!0});let G=yield q(u,P),K=ce=>{let le=new Date().toISOString();return oe(d({function_name:$,provider_type:T,args:[],kwargs:P,tags:a,request_start_time:A,request_end_time:le,api_key:this.apiKey,metadata:i,prompt_id:u.id,prompt_version:u.version,prompt_input_variables:m,group_id:p,return_prompt_blueprint:!0},ce))};if(s)return se(G,K,L);let U=yield K({request_response:G});return{request_id:U.request_id,raw_response:G,prompt_blueprint:U.prompt_blueprint}})}};0&&(module.exports={PromptLayer});
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/groups.ts","../src/promptlayer.ts","../src/templates.ts","../src/track.ts"],"sourcesContent":["import { GroupManager } from \"@/groups\";\nimport { promptLayerBase } from \"@/promptlayer\";\nimport { TemplateManager } from \"@/templates\";\nimport { TrackManager } from \"@/track\";\n\nexport interface ClientOptions {\n apiKey?: string;\n}\n\nexport class PromptLayer {\n apiKey: string;\n templates: TemplateManager;\n group: GroupManager;\n track: TrackManager;\n\n constructor({\n apiKey = process.env.PROMPTLAYER_API_KEY,\n }: ClientOptions = {}) {\n if (apiKey === undefined) {\n throw new Error(\n \"PromptLayer API key not provided. Please set the PROMPTLAYER_API_KEY environment variable or pass the api_key parameter.\"\n );\n }\n this.apiKey = apiKey;\n this.templates = new TemplateManager(apiKey);\n this.group = new GroupManager(apiKey);\n this.track = new TrackManager(apiKey);\n }\n\n get OpenAI() {\n try {\n const module = require(\"openai\").default;\n return promptLayerBase(this.apiKey, module, \"openai\", \"openai\");\n } catch (e) {\n console.error(\n \"To use the OpenAI module, you must install the @openai/api package.\"\n );\n }\n }\n\n get Anthropic() {\n try {\n const module = require(\"@anthropic-ai/sdk\").default;\n return promptLayerBase(this.apiKey, module, \"anthropic\", \"anthropic\");\n } catch (e) {\n console.error(\n \"To use the Anthropic module, you must install the @anthropic-ai/sdk package.\"\n );\n }\n }\n}\n","import {\n GetPromptTemplateParams,\n GetPromptTemplateResponse,\n ListPromptTemplatesResponse,\n Pagination,\n PublishPromptTemplate,\n PublishPromptTemplateResponse,\n TrackGroup,\n TrackMetadata,\n TrackPrompt,\n TrackRequest,\n TrackScore,\n} from \"@/types\";\nimport { Message, MessageStreamEvent } from \"@anthropic-ai/sdk/resources\";\nimport { ChatCompletion, ChatCompletionChunk } from \"openai/resources\";\n\nconst URL_API_PROMPTLAYER = \"https://api.promptlayer.com\";\n\nconst promptlayerApiHandler = async <Item>(\n apiKey: string,\n body: TrackRequest & {\n request_response: AsyncIterable<Item> | any;\n }\n) => {\n const isGenerator = body.request_response[Symbol.asyncIterator] !== undefined;\n if (isGenerator) {\n return proxyGenerator(apiKey, body.request_response, body);\n }\n return await promptLayerApiRequest(apiKey, body);\n};\n\nconst promptLayerApiRequest = async (apiKey: string, body: TrackRequest) => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-request`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(body),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While logging your request, PromptLayer experienced the following error:\"\n );\n }\n if (data && body.return_pl_id) {\n return [body.request_response, data.request_id];\n }\n } catch (e) {\n console.warn(\n `WARNING: While logging your request PromptLayer had the following error: ${e}`\n );\n }\n return body.request_response;\n};\n\nconst promptLayerTrackMetadata = async (\n apiKey: string,\n body: TrackMetadata\n): Promise<boolean> => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/library-track-metadata`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n }\n );\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While logging metadata to your request, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While logging metadata to your request, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackScore = async (\n apiKey: string,\n body: TrackScore\n): Promise<boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/library-track-score`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While scoring your request, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While scoring your request, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackPrompt = async (\n apiKey: string,\n body: TrackPrompt\n): Promise<boolean> => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/library-track-prompt`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n }\n );\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While associating your request with a prompt template, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While associating your request with a prompt template, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackGroup = async (\n apiKey: string,\n body: TrackGroup\n): Promise<boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-group`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While associating your request with a group, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While associating your request with a group, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerCreateGroup = async (\n apiKey: string\n): Promise<number | boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/create-group`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While creating a group PromptLayer had the following error\"\n );\n return false;\n }\n return data.id;\n } catch (e) {\n console.warn(\n `WARNING: While creating a group PromptLayer had the following error: ${e}`\n );\n return false;\n }\n};\n\nconst getPromptTemplate = async (\n apiKey: string,\n promptName: string,\n params?: Partial<GetPromptTemplateParams>\n) => {\n try {\n const url = new URL(\n `${URL_API_PROMPTLAYER}/prompt-templates/${promptName}`\n );\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n body: JSON.stringify(params),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While fetching a prompt template PromptLayer had the following error\"\n );\n return null;\n }\n return data as Promise<GetPromptTemplateResponse>;\n } catch (e) {\n console.warn(\n `WARNING: While fetching a prompt template PromptLayer had the following error: ${e}`\n );\n return null;\n }\n};\n\nconst publishPromptTemplate = async (\n apiKey: string,\n body: PublishPromptTemplate\n) => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/rest/prompt-templates`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n body: JSON.stringify({\n prompt_template: { ...body },\n prompt_version: { ...body },\n release_labels: body.release_labels ? body.release_labels : undefined,\n }),\n }\n );\n const data = await response.json();\n if (response.status === 400) {\n warnOnBadResponse(\n data,\n \"WARNING: While publishing a prompt template PromptLayer had the following error\"\n );\n }\n return data as Promise<PublishPromptTemplateResponse>;\n } catch (e) {\n console.warn(\n `WARNING: While publishing a prompt template PromptLayer had the following error: ${e}`\n );\n }\n};\n\nconst getAllPromptTemplates = async (\n apiKey: string,\n params?: Partial<Pagination>\n) => {\n try {\n const url = new URL(`${URL_API_PROMPTLAYER}/prompt-templates`);\n Object.entries(params || {}).forEach(([key, value]) =>\n url.searchParams.append(key, value.toString())\n );\n const response = await fetch(url, {\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While fetching all prompt templates PromptLayer had the following error\"\n );\n return null;\n }\n return (data.items ?? []) as Promise<Array<ListPromptTemplatesResponse>>;\n } catch (e) {\n console.warn(\n `WARNING: While fetching all prompt templates PromptLayer had the following error: ${e}`\n );\n return null;\n }\n};\n\nconst mapChatCompletionChunk = (\n results: ChatCompletionChunk[]\n): ChatCompletion => {\n let content: ChatCompletion.Choice[\"message\"][\"content\"] = null;\n let functionCall: ChatCompletion.Choice[\"message\"][\"function_call\"] =\n undefined;\n const response: ChatCompletion = {\n id: \"\",\n choices: [],\n created: Date.now(),\n model: \"\",\n object: \"chat.completion\",\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let toolCalls: ChatCompletion.Choice[\"message\"][\"tool_calls\"] = undefined;\n for (const result of results) {\n const delta = result.choices[0].delta;\n\n if (delta.content) {\n content = `${content || \"\"}${delta.content || \"\"}`;\n }\n if (delta.function_call) {\n functionCall = {\n name: `${functionCall ? functionCall.name : \"\"}${\n delta.function_call.name || \"\"\n }`,\n arguments: `${functionCall ? functionCall.arguments : \"\"}${\n delta.function_call.arguments || \"\"\n }`,\n };\n }\n const toolCall = delta.tool_calls?.[0];\n if (toolCall) {\n toolCalls = toolCalls || [];\n const lastToolCall = toolCalls.at(-1);\n if (!lastToolCall || toolCall.id) {\n toolCalls.push({\n id: toolCall.id || \"\",\n type: toolCall.type || \"function\",\n function: {\n name: toolCall.function?.name || \"\",\n arguments: toolCall.function?.arguments || \"\",\n },\n });\n continue;\n }\n lastToolCall.function.name = `${lastToolCall.function.name}${\n toolCall.function?.name || \"\"\n }`;\n lastToolCall.function.arguments = `${lastToolCall.function.arguments}${\n toolCall.function?.arguments || \"\"\n }`;\n }\n }\n response.choices.push({\n finish_reason: lastResult.choices[0].finish_reason || \"stop\",\n index: lastResult.choices[0].index || 0,\n logprobs: lastResult.choices[0].logprobs || null,\n message: {\n role: \"assistant\",\n content,\n function_call: functionCall ? functionCall : undefined,\n tool_calls: toolCalls ? toolCalls : undefined,\n },\n });\n response.id = lastResult.id;\n response.model = lastResult.model;\n response.created = lastResult.created;\n response.system_fingerprint = lastResult.system_fingerprint;\n return response;\n};\n\nconst mapMessageStreamEvent = (results: MessageStreamEvent[]): Message => {\n let response: Message = {\n id: \"\",\n model: \"\",\n content: [],\n role: \"assistant\",\n type: \"message\",\n stop_reason: \"stop_sequence\",\n stop_sequence: null,\n usage: {\n input_tokens: 0,\n output_tokens: 0,\n },\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let content = \"\";\n for (const result of results) {\n switch (result.type) {\n case \"message_start\": {\n response = {\n ...result.message,\n };\n break;\n }\n case \"content_block_delta\": {\n if (result.delta.type === \"text_delta\")\n content = `${content}${result.delta.text}`;\n }\n case \"message_delta\": {\n if (\"usage\" in result)\n response.usage.output_tokens = result.usage.output_tokens;\n if (\"stop_reason\" in result.delta)\n response.stop_reason = result.delta.stop_reason;\n }\n default: {\n break;\n }\n }\n }\n response.content.push({\n type: \"text\",\n text: content,\n });\n return response;\n};\n\nconst cleaned_result = (\n results: any[],\n function_name = \"openai.chat.completions.create\"\n) => {\n if (\"completion\" in results[0]) {\n return results.reduce(\n (prev, current) => ({\n ...current,\n completion: `${prev.completion}${current.completion}`,\n }),\n {}\n );\n }\n\n if (function_name === \"anthropic.messages.create\")\n return mapMessageStreamEvent(results);\n\n if (\"text\" in results[0].choices[0]) {\n let response = \"\";\n for (const result of results) {\n response = `${response}${result.choices[0].text}`;\n }\n const final_result = structuredClone(results.at(-1));\n final_result.choices[0].text = response;\n return final_result;\n }\n\n if (\"delta\" in results[0].choices[0]) {\n const response = mapChatCompletionChunk(results);\n response.choices[0] = {\n ...response.choices[0],\n ...response.choices[0].message,\n };\n return response;\n }\n\n return \"\";\n};\n\nasync function* proxyGenerator<Item>(\n apiKey: string,\n generator: AsyncIterable<Item>,\n body: TrackRequest\n) {\n const results = [];\n for await (const value of generator) {\n yield body.return_pl_id ? [value, null] : value;\n results.push(value);\n }\n const request_response = cleaned_result(results, body.function_name);\n const response = await promptLayerApiRequest(apiKey, {\n ...body,\n request_response,\n request_end_time: new Date().toISOString(),\n });\n if (response) {\n if (body.return_pl_id) {\n const request_id = (response as any)[1];\n const lastResult = results.at(-1);\n yield [lastResult, request_id];\n }\n }\n}\n\nconst warnOnBadResponse = (request_response: any, main_message: string) => {\n try {\n console.warn(`${main_message}: ${request_response.message}`);\n } catch (e) {\n console.warn(`${main_message}: ${request_response}`);\n }\n};\n\nexport {\n getAllPromptTemplates,\n getPromptTemplate,\n promptLayerApiRequest,\n promptLayerCreateGroup,\n promptLayerTrackGroup,\n promptLayerTrackMetadata,\n promptLayerTrackPrompt,\n promptLayerTrackScore,\n promptlayerApiHandler,\n publishPromptTemplate,\n};\n","import { promptLayerCreateGroup } from \"@/utils\";\n\nexport class GroupManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n create = () => promptLayerCreateGroup(this.apiKey);\n}\n","import { promptlayerApiHandler } from \"@/utils\";\n\nexport const promptLayerBase = (\n apiKey: string,\n llm: object,\n function_name = \"\",\n provider = \"openai\"\n) => {\n const handler: ProxyHandler<any> = {\n construct: (target, args) => {\n const newTarget = Reflect.construct(target, args);\n Object.defineProperties(newTarget, {\n function_name: {\n value: function_name,\n writable: true,\n },\n provider: {\n value: provider,\n },\n });\n return new Proxy(newTarget, handler);\n },\n get: (target, prop, receiver) => {\n const value = target[prop];\n const function_name = `${Reflect.get(\n target,\n \"function_name\"\n )}.${prop.toString()}`;\n if (typeof value === \"object\") {\n Object.defineProperties(value, {\n function_name: {\n value: function_name,\n writable: true,\n },\n provider: {\n value: provider,\n },\n });\n return new Proxy(value, handler);\n }\n if (typeof value === \"function\") {\n return (...args: any[]) => {\n const request_start_time = new Date().toISOString();\n const provider_type = Reflect.get(target, \"provider\");\n const return_pl_id = args[0]?.return_pl_id;\n const pl_tags = args[0]?.pl_tags;\n delete args[0]?.return_pl_id;\n delete args[0]?.pl_tags;\n const response = Reflect.apply(value, target, args);\n if (response instanceof Promise) {\n return new Promise((resolve, reject) => {\n response\n .then(async (request_response) => {\n const response = await promptlayerApiHandler(apiKey, {\n api_key: apiKey,\n provider_type,\n function_name,\n request_start_time,\n request_end_time: new Date().toISOString(),\n request_response,\n kwargs: args[0],\n return_pl_id,\n tags: pl_tags,\n });\n resolve(response);\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n return response;\n };\n }\n return Reflect.get(target, prop, receiver);\n },\n };\n return new Proxy(llm, handler);\n};\n","import {\n GetPromptTemplateParams,\n Pagination,\n PublishPromptTemplate,\n} from \"@/types\";\nimport {\n getAllPromptTemplates,\n getPromptTemplate,\n publishPromptTemplate,\n} from \"@/utils\";\n\nexport class TemplateManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n get = (promptName: string, params?: Partial<GetPromptTemplateParams>) =>\n getPromptTemplate(this.apiKey, promptName, params);\n\n publish = (body: PublishPromptTemplate) =>\n publishPromptTemplate(this.apiKey, body);\n\n all = (params?: Pagination) => getAllPromptTemplates(this.apiKey, params);\n}\n","import { TrackGroup, TrackMetadata, TrackPrompt, TrackScore } from \"@/types\";\nimport {\n promptLayerTrackGroup,\n promptLayerTrackMetadata,\n promptLayerTrackPrompt,\n promptLayerTrackScore,\n} from \"@/utils\";\n\nconst metadata = (apiKey: string, body: TrackMetadata): Promise<boolean> => {\n if (!(body.metadata instanceof Object)) {\n throw new Error(\"Please provide a dictionary of metadata.\");\n }\n for (const [key, value] of Object.entries(body.metadata)) {\n if (typeof key !== \"string\" || typeof value !== \"string\") {\n throw new Error(\n \"Please provide a dictionary of metadata with key value pair of strings.\"\n );\n }\n }\n return promptLayerTrackMetadata(apiKey, body);\n};\n\nconst score = (apiKey: string, body: TrackScore): Promise<boolean> => {\n if (typeof body.score !== \"number\") {\n throw new Error(\"Score must be a number\");\n }\n if (body.score < 0 || body.score > 100) {\n throw new Error(\"Score must be a number between 0 and 100.\");\n }\n return promptLayerTrackScore(apiKey, body);\n};\n\nconst prompt = (apiKey: string, body: TrackPrompt): Promise<boolean> => {\n if (!(body.prompt_input_variables instanceof Object)) {\n throw new Error(\"Prompt template input variable dictionary not provided.\");\n }\n return promptLayerTrackPrompt(apiKey, body);\n};\n\nconst group = (apiKey: string, body: TrackGroup) =>\n promptLayerTrackGroup(apiKey, body);\n\nexport class TrackManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n group = (body: TrackGroup) => group(this.apiKey, body);\n\n metadata = (body: TrackMetadata) => metadata(this.apiKey, body);\n\n prompt = (body: TrackPrompt) => prompt(this.apiKey, body);\n\n score = (body: TrackScore) => score(this.apiKey, body);\n}\n"],"mappings":"6rDAAA,IAAAA,GAAA,GAAAC,GAAAD,GAAA,iBAAAE,IAAA,eAAAC,GAAAH,ICgBA,IAAMI,EAAsB,8BAEtBC,EAAwB,CAC5BC,EACAC,IAGGC,EAAA,wBAEH,OADoBD,EAAK,iBAAiB,OAAO,aAAa,IAAM,OAE3DE,GAAeH,EAAQC,EAAK,iBAAkBA,CAAI,EAEpD,MAAMG,EAAsBJ,EAAQC,CAAI,CACjD,GAEMG,EAAwB,CAAOJ,EAAgBC,IAAuBC,EAAA,wBAC1E,GAAI,CACF,IAAMG,EAAW,MAAM,MAAM,GAAGP,CAAmB,iBAAkB,CACnE,OAAQ,OACR,QAAS,CACP,eAAgB,kBAClB,EACA,KAAM,KAAK,UAAUG,CAAI,CAC3B,CAAC,EACKK,EAAO,MAAMD,EAAS,KAAK,EAOjC,GANIA,EAAS,SAAW,KACtBE,EACED,EACA,mFACF,EAEEA,GAAQL,EAAK,aACf,MAAO,CAACA,EAAK,iBAAkBK,EAAK,UAAU,CAElD,OAASE,EAAG,CACV,QAAQ,KACN,4EAA4EA,CAAC,EAC/E,CACF,CACA,OAAOP,EAAK,gBACd,GAEMQ,EAA2B,CAC/BT,EACAC,IACqBC,EAAA,wBACrB,GAAI,CACF,IAAMG,EAAW,MAAM,MACrB,GAAGP,CAAmB,0BACtB,CACE,OAAQ,OACR,QAAS,CACP,eAAgB,kBAClB,EACA,KAAM,KAAK,UAAUY,EAAAC,EAAA,GAChBV,GADgB,CAEnB,QAASD,CACX,EAAC,CACH,CACF,EACMM,EAAO,MAAMD,EAAS,KAAK,EACjC,GAAIA,EAAS,SAAW,IACtB,OAAAE,EACED,EACA,8FACF,EACO,EAEX,OAASE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AAK,OAAQR,CAAI,EAErD,cAAYA,GAAwBF,GAAS,KAAK,OAAQE,CAAI,EAE9D,YAAUA,GAAsBM,GAAO,KAAK,OAAQN,CAAI,EAExD,WAASA,GAAqBI,GAAM,KAAK,OAAQJ,CAAI,EATnD,KAAK,OAASD,CAChB,CASF,EL/CO,IAAMY,EAAN,KAAkB,CAMvB,YAAY,CACV,OAAAC,EAAS,QAAQ,IAAI,mBACvB,EAAmB,CAAC,EAAG,CACrB,GAAIA,IAAW,OACb,MAAM,IAAI,MACR,0HACF,EAEF,KAAK,OAASA,EACd,KAAK,UAAY,IAAIC,EAAgBD,CAAM,EAC3C,KAAK,MAAQ,IAAIE,EAAaF,CAAM,EACpC,KAAK,MAAQ,IAAIG,EAAaH,CAAM,CACtC,CAEA,IAAI,QAAS,CACX,GAAI,CACF,IAAMI,EAAS,QAAQ,QAAQ,EAAE,QACjC,OAAOC,EAAgB,KAAK,OAAQD,EAAQ,SAAU,QAAQ,CAChE,OAAS,EAAG,CACV,QAAQ,MACN,qEACF,CACF,CACF,CAEA,IAAI,WAAY,CACd,GAAI,CACF,IAAMA,EAAS,QAAQ,mBAAmB,EAAE,QAC5C,OAAOC,EAAgB,KAAK,OAAQD,EAAQ,YAAa,WAAW,CACtE,OAAS,EAAG,CACV,QAAQ,MACN,8EACF,CACF,CACF,CACF","names":["src_exports","__export","PromptLayer","__toCommonJS","URL_API_PROMPTLAYER","promptlayerApiHandler","apiKey","body","__async","proxyGenerator","promptLayerApiRequest","response","data","warnOnBadResponse","e","promptLayerTrackMetadata","__spreadProps","__spreadValues","promptLayerTrackScore","promptLayerTrackPrompt","promptLayerTrackGroup","promptLayerCreateGroup","getPromptTemplate","promptName","params","url","publishPromptTemplate","getAllPromptTemplates","_a","key","value","mapChatCompletionChunk","results","_b","_c","_d","_e","content","functionCall","lastResult","toolCalls","result","delta","toolCall","lastToolCall","mapMessageStreamEvent","cleaned_result","function_name","prev","current","final_result","generator","__asyncGenerator","iter","__forAwait","more","temp","error","__await","request_response","request_id","main_message","GroupManager","apiKey","promptLayerCreateGroup","promptLayerBase","apiKey","llm","function_name","provider","handler","target","args","newTarget","prop","receiver","value","_a","_b","_c","_d","request_start_time","provider_type","return_pl_id","pl_tags","response","resolve","reject","request_response","__async","promptlayerApiHandler","error","TemplateManager","apiKey","promptName","params","getPromptTemplate","body","publishPromptTemplate","getAllPromptTemplates","metadata","apiKey","body","key","value","promptLayerTrackMetadata","score","promptLayerTrackScore","prompt","promptLayerTrackPrompt","group","promptLayerTrackGroup","TrackManager","PromptLayer","apiKey","TemplateManager","GroupManager","TrackManager","module","promptLayerBase"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/groups.ts","../src/promptlayer.ts","../src/templates.ts","../src/track.ts"],"sourcesContent":["import { GroupManager } from \"@/groups\";\nimport { promptLayerBase } from \"@/promptlayer\";\nimport { TemplateManager } from \"@/templates\";\nimport { TrackManager } from \"@/track\";\nimport { GetPromptTemplateParams, RunRequest } from \"@/types\";\nimport {\n anthropicRequest,\n anthropicStreamCompletion,\n anthropicStreamMessage,\n openaiRequest,\n openaiStreamChat,\n openaiStreamCompletion,\n streamResponse,\n trackRequest,\n} from \"@/utils\";\n\nconst MAP_PROVIDER_TO_FUNCTION_NAME = {\n openai: {\n chat: {\n function_name: \"openai.chat.completions.create\",\n stream_function: openaiStreamChat,\n },\n completion: {\n function_name: \"openai.completions.create\",\n stream_function: openaiStreamCompletion,\n },\n },\n anthropic: {\n chat: {\n function_name: \"anthropic.messages.create\",\n stream_function: anthropicStreamMessage,\n },\n completion: {\n function_name: \"anthropic.completions.create\",\n stream_function: anthropicStreamCompletion,\n },\n },\n};\n\nconst MAP_PROVIDER_TO_FUNCTION: Record<string, any> = {\n openai: openaiRequest,\n anthropic: anthropicRequest,\n};\n\nexport interface ClientOptions {\n apiKey?: string;\n}\n\nexport class PromptLayer {\n apiKey: string;\n templates: TemplateManager;\n group: GroupManager;\n track: TrackManager;\n\n constructor({\n apiKey = process.env.PROMPTLAYER_API_KEY,\n }: ClientOptions = {}) {\n if (apiKey === undefined) {\n throw new Error(\n \"PromptLayer API key not provided. Please set the PROMPTLAYER_API_KEY environment variable or pass the api_key parameter.\"\n );\n }\n this.apiKey = apiKey;\n this.templates = new TemplateManager(apiKey);\n this.group = new GroupManager(apiKey);\n this.track = new TrackManager(apiKey);\n }\n\n get OpenAI() {\n try {\n const module = require(\"openai\").default;\n return promptLayerBase(this.apiKey, module, \"openai\", \"openai\");\n } catch (e) {\n console.error(\n \"To use the OpenAI module, you must install the @openai/api package.\"\n );\n }\n }\n\n get Anthropic() {\n try {\n const module = require(\"@anthropic-ai/sdk\").default;\n return promptLayerBase(this.apiKey, module, \"anthropic\", \"anthropic\");\n } catch (e) {\n console.error(\n \"To use the Anthropic module, you must install the @anthropic-ai/sdk package.\"\n );\n }\n }\n\n async run({\n promptName,\n promptVersion,\n promptReleaseLabel,\n inputVariables,\n tags,\n metadata,\n groupId,\n stream = false,\n }: RunRequest) {\n const prompt_input_variables = inputVariables;\n const templateGetParams: GetPromptTemplateParams = {\n label: promptReleaseLabel,\n version: promptVersion,\n metadata_filters: metadata,\n };\n if (inputVariables) templateGetParams.input_variables = inputVariables;\n const promptBlueprint = await this.templates.get(\n promptName,\n templateGetParams\n );\n if (!promptBlueprint) throw new Error(\"Prompt not found\");\n const promptTemplate = promptBlueprint.prompt_template;\n if (!promptBlueprint.llm_kwargs) {\n throw new Error(\n `Prompt '${promptName}' does not have any LLM kwargs associated with it.`\n );\n }\n const promptBlueprintMetadata = promptBlueprint.metadata;\n if (!promptBlueprintMetadata) {\n throw new Error(\n `Prompt '${promptName}' does not have any metadata associated with it.`\n );\n }\n const promptBlueprintModel = promptBlueprintMetadata.model;\n if (!promptBlueprintModel) {\n throw new Error(\n `Prompt '${promptName}' does not have a model parameters associated with it.`\n );\n }\n const provider_type = promptBlueprintModel.provider;\n const request_start_time = new Date().toISOString();\n const kwargs = promptBlueprint.llm_kwargs;\n const config =\n MAP_PROVIDER_TO_FUNCTION_NAME[\n provider_type as keyof typeof MAP_PROVIDER_TO_FUNCTION_NAME\n ][promptTemplate.type];\n const function_name = config.function_name;\n const stream_function = config.stream_function;\n const request_function = MAP_PROVIDER_TO_FUNCTION[provider_type];\n const provider_base_url = promptBlueprint.provider_base_url;\n if (provider_base_url) {\n kwargs[\"baseURL\"] = provider_base_url.url;\n }\n kwargs[\"stream\"] = stream;\n if (stream && provider_type === \"openai\") {\n kwargs[\"stream_options\"] = { include_usage: true };\n }\n const response = await request_function(promptBlueprint, kwargs);\n const _trackRequest = (body: object) => {\n const request_end_time = new Date().toISOString();\n return trackRequest({\n function_name,\n provider_type,\n args: [],\n kwargs,\n tags,\n request_start_time,\n request_end_time,\n api_key: this.apiKey,\n metadata,\n prompt_id: promptBlueprint.id,\n prompt_version: promptBlueprint.version,\n prompt_input_variables,\n group_id: groupId,\n return_prompt_blueprint: true,\n ...body,\n });\n };\n if (stream) return streamResponse(response, _trackRequest, stream_function);\n const requestLog = await _trackRequest({ request_response: response });\n const data = {\n request_id: requestLog.request_id,\n raw_response: response,\n prompt_blueprint: requestLog.prompt_blueprint,\n };\n return data;\n }\n}\n","import {\n GetPromptTemplateParams,\n GetPromptTemplateResponse,\n ListPromptTemplatesResponse,\n Pagination,\n PublishPromptTemplate,\n PublishPromptTemplateResponse,\n TrackGroup,\n TrackMetadata,\n TrackPrompt,\n TrackRequest,\n TrackScore,\n} from \"@/types\";\nimport type TypeAnthropic from \"@anthropic-ai/sdk\";\nimport {\n Completion as AnthropicCompletion,\n Message,\n MessageStreamEvent,\n} from \"@anthropic-ai/sdk/resources\";\nimport type TypeOpenAI from \"openai\";\nimport {\n ChatCompletion,\n ChatCompletionChunk,\n Completion,\n} from \"openai/resources\";\n\nconst URL_API_PROMPTLAYER = \"https://api.promptlayer.com\";\n\nconst promptlayerApiHandler = async <Item>(\n apiKey: string,\n body: TrackRequest & {\n request_response: AsyncIterable<Item> | any;\n }\n) => {\n const isGenerator = body.request_response[Symbol.asyncIterator] !== undefined;\n if (isGenerator) {\n return proxyGenerator(apiKey, body.request_response, body);\n }\n return await promptLayerApiRequest(apiKey, body);\n};\n\nconst promptLayerApiRequest = async (apiKey: string, body: TrackRequest) => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-request`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(body),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While logging your request, PromptLayer experienced the following error:\"\n );\n }\n if (data && body.return_pl_id) {\n return [body.request_response, data.request_id];\n }\n } catch (e) {\n console.warn(\n `WARNING: While logging your request PromptLayer had the following error: ${e}`\n );\n }\n return body.request_response;\n};\n\nconst promptLayerTrackMetadata = async (\n apiKey: string,\n body: TrackMetadata\n): Promise<boolean> => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/library-track-metadata`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n }\n );\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While logging metadata to your request, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While logging metadata to your request, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackScore = async (\n apiKey: string,\n body: TrackScore\n): Promise<boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/library-track-score`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While scoring your request, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While scoring your request, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackPrompt = async (\n apiKey: string,\n body: TrackPrompt\n): Promise<boolean> => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/library-track-prompt`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n }\n );\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While associating your request with a prompt template, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While associating your request with a prompt template, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerTrackGroup = async (\n apiKey: string,\n body: TrackGroup\n): Promise<boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-group`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n ...body,\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While associating your request with a group, PromptLayer experienced the following error\"\n );\n return false;\n }\n } catch (e) {\n console.warn(\n `WARNING: While associating your request with a group, PromptLayer experienced the following error: ${e}`\n );\n return false;\n }\n return true;\n};\n\nconst promptLayerCreateGroup = async (\n apiKey: string\n): Promise<number | boolean> => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/create-group`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n api_key: apiKey,\n }),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While creating a group PromptLayer had the following error\"\n );\n return false;\n }\n return data.id;\n } catch (e) {\n console.warn(\n `WARNING: While creating a group PromptLayer had the following error: ${e}`\n );\n return false;\n }\n};\n\nconst getPromptTemplate = async (\n apiKey: string,\n promptName: string,\n params?: Partial<GetPromptTemplateParams>\n) => {\n try {\n const url = new URL(\n `${URL_API_PROMPTLAYER}/prompt-templates/${promptName}`\n );\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n body: JSON.stringify(params),\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While fetching a prompt template PromptLayer had the following error\"\n );\n return null;\n }\n return data as Promise<GetPromptTemplateResponse>;\n } catch (e) {\n console.warn(\n `WARNING: While fetching a prompt template PromptLayer had the following error: ${e}`\n );\n return null;\n }\n};\n\nconst publishPromptTemplate = async (\n apiKey: string,\n body: PublishPromptTemplate\n) => {\n try {\n const response = await fetch(\n `${URL_API_PROMPTLAYER}/rest/prompt-templates`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n body: JSON.stringify({\n prompt_template: { ...body },\n prompt_version: { ...body },\n release_labels: body.release_labels ? body.release_labels : undefined,\n }),\n }\n );\n const data = await response.json();\n if (response.status === 400) {\n warnOnBadResponse(\n data,\n \"WARNING: While publishing a prompt template PromptLayer had the following error\"\n );\n }\n return data as Promise<PublishPromptTemplateResponse>;\n } catch (e) {\n console.warn(\n `WARNING: While publishing a prompt template PromptLayer had the following error: ${e}`\n );\n }\n};\n\nconst getAllPromptTemplates = async (\n apiKey: string,\n params?: Partial<Pagination>\n) => {\n try {\n const url = new URL(`${URL_API_PROMPTLAYER}/prompt-templates`);\n Object.entries(params || {}).forEach(([key, value]) =>\n url.searchParams.append(key, value.toString())\n );\n const response = await fetch(url, {\n headers: {\n \"Content-Type\": \"application/json\",\n \"X-API-KEY\": apiKey,\n },\n });\n const data = await response.json();\n if (response.status !== 200) {\n warnOnBadResponse(\n data,\n \"WARNING: While fetching all prompt templates PromptLayer had the following error\"\n );\n return null;\n }\n return (data.items ?? []) as Promise<Array<ListPromptTemplatesResponse>>;\n } catch (e) {\n console.warn(\n `WARNING: While fetching all prompt templates PromptLayer had the following error: ${e}`\n );\n return null;\n }\n};\n\nconst openaiStreamChat = (results: ChatCompletionChunk[]): ChatCompletion => {\n let content: ChatCompletion.Choice[\"message\"][\"content\"] = null;\n let functionCall: ChatCompletion.Choice[\"message\"][\"function_call\"] =\n undefined;\n const response: ChatCompletion = {\n id: \"\",\n choices: [],\n created: Date.now(),\n model: \"\",\n object: \"chat.completion\",\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let toolCalls: ChatCompletion.Choice[\"message\"][\"tool_calls\"] = undefined;\n for (const result of results) {\n if (result.choices.length === 0) continue;\n const delta = result.choices[0].delta;\n\n if (delta.content) {\n content = `${content || \"\"}${delta.content || \"\"}`;\n }\n if (delta.function_call) {\n functionCall = {\n name: `${functionCall ? functionCall.name : \"\"}${\n delta.function_call.name || \"\"\n }`,\n arguments: `${functionCall ? functionCall.arguments : \"\"}${\n delta.function_call.arguments || \"\"\n }`,\n };\n }\n const toolCall = delta.tool_calls?.[0];\n if (toolCall) {\n toolCalls = toolCalls || [];\n const lastToolCall = toolCalls.at(-1);\n if (!lastToolCall || toolCall.id) {\n toolCalls.push({\n id: toolCall.id || \"\",\n type: toolCall.type || \"function\",\n function: {\n name: toolCall.function?.name || \"\",\n arguments: toolCall.function?.arguments || \"\",\n },\n });\n continue;\n }\n lastToolCall.function.name = `${lastToolCall.function.name}${\n toolCall.function?.name || \"\"\n }`;\n lastToolCall.function.arguments = `${lastToolCall.function.arguments}${\n toolCall.function?.arguments || \"\"\n }`;\n }\n }\n response.choices.push({\n finish_reason: results[0].choices[0].finish_reason || \"stop\",\n index: results[0].choices[0].index || 0,\n logprobs: results[0].choices[0].logprobs || null,\n message: {\n role: \"assistant\",\n content,\n function_call: functionCall ? functionCall : undefined,\n tool_calls: toolCalls ? toolCalls : undefined,\n },\n });\n response.id = lastResult.id;\n response.model = lastResult.model;\n response.created = lastResult.created;\n response.system_fingerprint = lastResult.system_fingerprint;\n response.usage = lastResult.usage;\n return response;\n};\n\nconst anthropicStreamMessage = (results: MessageStreamEvent[]): Message => {\n let response: Message = {\n id: \"\",\n model: \"\",\n content: [],\n role: \"assistant\",\n type: \"message\",\n stop_reason: \"stop_sequence\",\n stop_sequence: null,\n usage: {\n input_tokens: 0,\n output_tokens: 0,\n },\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let content = \"\";\n for (const result of results) {\n switch (result.type) {\n case \"message_start\": {\n response = {\n ...result.message,\n };\n break;\n }\n case \"content_block_delta\": {\n if (result.delta.type === \"text_delta\")\n content = `${content}${result.delta.text}`;\n }\n case \"message_delta\": {\n if (\"usage\" in result)\n response.usage.output_tokens = result.usage.output_tokens;\n if (\"stop_reason\" in result.delta)\n response.stop_reason = result.delta.stop_reason;\n }\n default: {\n break;\n }\n }\n }\n response.content.push({\n type: \"text\",\n text: content,\n });\n return response;\n};\n\nconst cleaned_result = (\n results: any[],\n function_name = \"openai.chat.completions.create\"\n) => {\n if (\"completion\" in results[0]) {\n return results.reduce(\n (prev, current) => ({\n ...current,\n completion: `${prev.completion}${current.completion}`,\n }),\n {}\n );\n }\n\n if (function_name === \"anthropic.messages.create\")\n return anthropicStreamMessage(results);\n\n if (\"text\" in results[0].choices[0]) {\n let response = \"\";\n for (const result of results) {\n response = `${response}${result.choices[0].text}`;\n }\n const final_result = structuredClone(results.at(-1));\n final_result.choices[0].text = response;\n return final_result;\n }\n\n if (\"delta\" in results[0].choices[0]) {\n const response = openaiStreamChat(results);\n response.choices[0] = {\n ...response.choices[0],\n ...response.choices[0].message,\n };\n return response;\n }\n\n return \"\";\n};\n\nasync function* proxyGenerator<Item>(\n apiKey: string,\n generator: AsyncIterable<Item>,\n body: TrackRequest\n) {\n const results = [];\n for await (const value of generator) {\n yield body.return_pl_id ? [value, null] : value;\n results.push(value);\n }\n const request_response = cleaned_result(results, body.function_name);\n const response = await promptLayerApiRequest(apiKey, {\n ...body,\n request_response,\n request_end_time: new Date().toISOString(),\n });\n if (response) {\n if (body.return_pl_id) {\n const request_id = (response as any)[1];\n const lastResult = results.at(-1);\n yield [lastResult, request_id];\n }\n }\n}\n\nconst warnOnBadResponse = (request_response: any, main_message: string) => {\n try {\n console.warn(`${main_message}: ${request_response.message}`);\n } catch (e) {\n console.warn(`${main_message}: ${request_response}`);\n }\n};\n\nconst trackRequest = async (body: TrackRequest) => {\n try {\n const response = await fetch(`${URL_API_PROMPTLAYER}/track-request`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(body),\n });\n if (response.status !== 200)\n warnOnBadResponse(\n response,\n \"WARNING: While logging your request, PromptLayer experienced the following error:\"\n );\n return response.json();\n } catch (e) {\n console.warn(\n `WARNING: While logging your request PromptLayer had the following error: ${e}`\n );\n }\n return {};\n};\n\nconst openaiStreamCompletion = (results: Completion[]) => {\n const response: Completion = {\n id: \"\",\n choices: [\n {\n finish_reason: \"stop\",\n index: 0,\n text: \"\",\n logprobs: null,\n },\n ],\n created: Date.now(),\n model: \"\",\n object: \"text_completion\",\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let text = \"\";\n for (const result of results) {\n if (result.choices.length > 0 && result.choices[0].text) {\n text = `${text}${result.choices[0].text}`;\n }\n }\n response.choices[0].text = text;\n response.id = lastResult.id;\n response.created = lastResult.created;\n response.model = lastResult.model;\n response.system_fingerprint = lastResult.system_fingerprint;\n response.usage = lastResult.usage;\n return response;\n};\n\nconst anthropicStreamCompletion = (results: AnthropicCompletion[]) => {\n const response: AnthropicCompletion = {\n completion: \"\",\n id: \"\",\n model: \"\",\n stop_reason: \"\",\n type: \"completion\",\n };\n const lastResult = results.at(-1);\n if (!lastResult) return response;\n let completion = \"\";\n for (const result of results) {\n completion = `${completion}${result.completion}`;\n }\n response.completion = completion;\n response.id = lastResult.id;\n response.model = lastResult.model;\n response.stop_reason = lastResult.stop_reason;\n return response;\n};\n\nasync function* streamResponse<Item>(\n generator: AsyncIterable<Item>,\n afterStream: (body: object) => any,\n mapResults: any\n) {\n const data: {\n request_id: number | null;\n raw_response: any;\n prompt_blueprint: any;\n } = {\n request_id: null,\n raw_response: null,\n prompt_blueprint: null,\n };\n const results = [];\n for await (const result of generator) {\n results.push(result);\n data.raw_response = result;\n yield data;\n }\n const request_response = mapResults(results);\n const response = await afterStream({ request_response });\n data.request_id = response.request_id;\n data.prompt_blueprint = response.prompt_blueprint;\n yield data;\n}\n\nconst openaiChatRequest = async (client: TypeOpenAI, kwargs: any) => {\n return client.chat.completions.create(kwargs);\n};\n\nconst openaiCompletionsRequest = async (client: TypeOpenAI, kwargs: any) => {\n return client.completions.create(kwargs);\n};\n\nconst MAP_TYPE_TO_OPENAI_FUNCTION = {\n chat: openaiChatRequest,\n completion: openaiCompletionsRequest,\n};\n\nconst openaiRequest = async (\n promptBlueprint: GetPromptTemplateResponse,\n kwargs: any\n) => {\n const OpenAI = require(\"openai\").default;\n const client = new OpenAI({\n baseURL: kwargs.baseURL,\n });\n const requestToMake =\n MAP_TYPE_TO_OPENAI_FUNCTION[promptBlueprint.prompt_template.type];\n return requestToMake(client, kwargs);\n};\n\nconst anthropicChatRequest = async (client: TypeAnthropic, kwargs: any) => {\n return client.messages.create(kwargs);\n};\n\nconst anthropicCompletionsRequest = async (\n client: TypeAnthropic,\n kwargs: any\n) => {\n return client.completions.create(kwargs);\n};\n\nconst MAP_TYPE_TO_ANTHROPIC_FUNCTION = {\n chat: anthropicChatRequest,\n completion: anthropicCompletionsRequest,\n};\n\nconst anthropicRequest = async (\n promptBlueprint: GetPromptTemplateResponse,\n kwargs: any\n) => {\n const Anthropic = require(\"@anthropic-ai/sdk\").default;\n const client = new Anthropic({\n baseURL: kwargs.baseURL,\n });\n const requestToMake =\n MAP_TYPE_TO_ANTHROPIC_FUNCTION[promptBlueprint.prompt_template.type];\n return requestToMake(client, kwargs);\n};\n\nexport {\n anthropicRequest,\n anthropicStreamCompletion,\n anthropicStreamMessage,\n getAllPromptTemplates,\n getPromptTemplate,\n openaiRequest,\n openaiStreamChat,\n openaiStreamCompletion,\n promptLayerApiRequest,\n promptLayerCreateGroup,\n promptLayerTrackGroup,\n promptLayerTrackMetadata,\n promptLayerTrackPrompt,\n promptLayerTrackScore,\n promptlayerApiHandler,\n publishPromptTemplate,\n streamResponse,\n trackRequest,\n};\n","import { promptLayerCreateGroup } from \"@/utils\";\n\nexport class GroupManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n create = () => promptLayerCreateGroup(this.apiKey);\n}\n","import { promptlayerApiHandler } from \"@/utils\";\n\nexport const promptLayerBase = (\n apiKey: string,\n llm: object,\n function_name = \"\",\n provider = \"openai\"\n) => {\n const handler: ProxyHandler<any> = {\n construct: (target, args) => {\n const newTarget = Reflect.construct(target, args);\n Object.defineProperties(newTarget, {\n function_name: {\n value: function_name,\n writable: true,\n },\n provider: {\n value: provider,\n },\n });\n return new Proxy(newTarget, handler);\n },\n get: (target, prop, receiver) => {\n const value = target[prop];\n const function_name = `${Reflect.get(\n target,\n \"function_name\"\n )}.${prop.toString()}`;\n if (typeof value === \"object\") {\n Object.defineProperties(value, {\n function_name: {\n value: function_name,\n writable: true,\n },\n provider: {\n value: provider,\n },\n });\n return new Proxy(value, handler);\n }\n if (typeof value === \"function\") {\n return (...args: any[]) => {\n const request_start_time = new Date().toISOString();\n const provider_type = Reflect.get(target, \"provider\");\n const return_pl_id = args[0]?.return_pl_id;\n const pl_tags = args[0]?.pl_tags;\n delete args[0]?.return_pl_id;\n delete args[0]?.pl_tags;\n const response = Reflect.apply(value, target, args);\n if (response instanceof Promise) {\n return new Promise((resolve, reject) => {\n response\n .then(async (request_response) => {\n const response = await promptlayerApiHandler(apiKey, {\n api_key: apiKey,\n provider_type,\n function_name,\n request_start_time,\n request_end_time: new Date().toISOString(),\n request_response,\n kwargs: args[0],\n return_pl_id,\n tags: pl_tags,\n });\n resolve(response);\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n return response;\n };\n }\n return Reflect.get(target, prop, receiver);\n },\n };\n return new Proxy(llm, handler);\n};\n","import {\n GetPromptTemplateParams,\n Pagination,\n PublishPromptTemplate,\n} from \"@/types\";\nimport {\n getAllPromptTemplates,\n getPromptTemplate,\n publishPromptTemplate,\n} from \"@/utils\";\n\nexport class TemplateManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n get = (promptName: string, params?: Partial<GetPromptTemplateParams>) =>\n getPromptTemplate(this.apiKey, promptName, params);\n\n publish = (body: PublishPromptTemplate) =>\n publishPromptTemplate(this.apiKey, body);\n\n all = (params?: Pagination) => getAllPromptTemplates(this.apiKey, params);\n}\n","import { TrackGroup, TrackMetadata, TrackPrompt, TrackScore } from \"@/types\";\nimport {\n promptLayerTrackGroup,\n promptLayerTrackMetadata,\n promptLayerTrackPrompt,\n promptLayerTrackScore,\n} from \"@/utils\";\n\nconst metadata = (apiKey: string, body: TrackMetadata): Promise<boolean> => {\n if (!(body.metadata instanceof Object)) {\n throw new Error(\"Please provide a dictionary of metadata.\");\n }\n for (const [key, value] of Object.entries(body.metadata)) {\n if (typeof key !== \"string\" || typeof value !== \"string\") {\n throw new Error(\n \"Please provide a dictionary of metadata with key value pair of strings.\"\n );\n }\n }\n return promptLayerTrackMetadata(apiKey, body);\n};\n\nconst score = (apiKey: string, body: TrackScore): Promise<boolean> => {\n if (typeof body.score !== \"number\") {\n throw new Error(\"Score must be a number\");\n }\n if (body.score < 0 || body.score > 100) {\n throw new Error(\"Score must be a number between 0 and 100.\");\n }\n return promptLayerTrackScore(apiKey, body);\n};\n\nconst prompt = (apiKey: string, body: TrackPrompt): Promise<boolean> => {\n if (!(body.prompt_input_variables instanceof Object)) {\n throw new Error(\"Prompt template input variable dictionary not provided.\");\n }\n return promptLayerTrackPrompt(apiKey, body);\n};\n\nconst group = (apiKey: string, body: TrackGroup) =>\n promptLayerTrackGroup(apiKey, body);\n\nexport class TrackManager {\n apiKey: string;\n\n constructor(apiKey: string) {\n this.apiKey = apiKey;\n }\n\n group = (body: TrackGroup) => group(this.apiKey, body);\n\n metadata = (body: TrackMetadata) => metadata(this.apiKey, body);\n\n prompt = (body: TrackPrompt) => prompt(this.apiKey, body);\n\n score = (body: TrackScore) => score(this.apiKey, body);\n}\n"],"mappings":"isDAAA,IAAAA,GAAA,GAAAC,GAAAD,GAAA,iBAAAE,IAAA,eAAAC,GAAAH,IC0BA,IAAMI,EAAsB,8BAEtBC,EAAwB,CAC5BC,EACAC,IAGGC,EAAA,wBAEH,OADoBD,EAAK,iBAAiB,OAAO,aAAa,IAAM,OAE3DE,GAAeH,EAAQC,EAAK,iBAAkBA,CAAI,EAEpD,MAAMG,EAAsBJ,EAAQC,CAAI,CACjD,GAEMG,EAAwB,CAAOJ,EAAgBC,IAAuBC,EAAA,wBAC1E,GAAI,CACF,IAAMG,EAAW,MAAM,MAAM,GAAGP,CAAmB,iBAAkB,CACnE,OAAQ,OACR,QAAS,CACP,eAAgB,kBAClB,EACA,KAAM,KAAK,UAAUG,CAAI,CAC3B,CAAC,EACKK,EAAO,MAAMD,EAAS,KAAK,EAOjC,GANIA,EAAS,SAAW,KACtBE,EACED,EACA,mFACF,EAEEA,GAAQL,EAAK,aACf,MAAO,CAACA,EAAK,iBAAkBK,EAAK,UAAU,CAElD,OAASE,EAAG,CACV,QAAQ,KACN,4EAA4EA,CAAC,EAC/E,CACF,CACA,OAAOP,EAAK,gBACd,GAEMQ,EAA2B,CAC/BT,EACAC,IACqBC,EAAA,wBACrB,GAAI,CACF,IAAMG,EAAW,MAAM,MACrB,GAAGP,CAAmB,0BACtB,CACE,OAAQ,OACR,QAAS,CACP,eAAgB,kBAClB,EACA,KAAM,KAAK,UAAUY,EAAAC,EAAA,GAChBV,GADgB,CAEnB,QAASD,CACX,EAAC,CACH,CACF,EACMM,EAAO,MAAMD,EAAS,KAAK,EACjC,GAAIA,EAAS,SAAW,IACtB,OAAAE,EACED,EACA,8FACF,EACO,EAEX,OAASE,EAAG,CACV,eAAQ,KACN,iGAAiGA,CAAC,EACpG,EACO,EACT,CACA,MAAO,EACT,GAEMI,EAAwB,CAC5BZ,E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AAgB,kBACtCW,IACFL,EAAO,QAAaK,EAAkB,KAExCL,EAAO,OAAYT,EACfA,GAAUO,IAAkB,WAC9BE,EAAO,eAAoB,CAAE,cAAe,EAAK,GAEnD,IAAMM,EAAW,MAAMF,EAAiBV,EAAiBM,CAAM,EACzDO,EAAiBC,IAAiB,CACtC,IAAMC,GAAmB,IAAI,KAAK,EAAE,YAAY,EAChD,OAAOC,GAAaC,EAAA,CAClB,cAAAT,EACA,cAAAJ,EACA,KAAM,CAAC,EACP,OAAAE,EACA,KAAAZ,EACA,mBAAAW,EACA,iBAAAU,GACA,QAAS,KAAK,OACd,SAAApB,EACA,UAAWK,EAAgB,GAC3B,eAAgBA,EAAgB,QAChC,uBAAAF,EACA,SAAUF,EACV,wBAAyB,IACtBkB,GACJ,CACH,EACA,GAAIjB,EAAQ,OAAOqB,GAAeN,EAAUC,EAAeJ,CAAe,EAC1E,IAAMU,EAAa,MAAMN,EAAc,CAAE,iBAAkBD,CAAS,CAAC,EAMrE,MALa,CACX,WAAYO,EAAW,WACvB,aAAcP,EACd,iBAAkBO,EAAW,gBAC/B,CAEF,GACF","names":["src_exports","__export","PromptLayer","__toCommonJS","URL_API_PROMPTLAYER","promptlayerApiHandler","apiKey","body","__async","proxyGenerator","promptLayerApiRequest","response","data","warnOnBadResponse","e","promptLayerTrackMetadata","__spreadProps","__spreadValues","promptLayerTrackScore","promptLayerTrackPrompt","promptLayerTrackGroup","promptLayerCreateGroup","getPromptTemplate","promptName","params","url","publishPromptTemplate","getAllPromptTemplates","_a","key","value","openaiStreamChat","results","_b","_c","_d","_e","content","functionCall","lastResult","toolCalls","result","delta","toolCall","lastToolCall","anthropicStreamMessage","cleaned_result","function_name","prev","current","final_result","generator","__asyncGenerator","iter","__forAwait","more","temp","error","__await","request_response","request_id","main_message","trackRequest","openaiStreamCompletion","text","anthropicStreamCompletion","completion","streamResponse","afterStream","mapResults","openaiChatRequest","client","kwargs","openaiCompletionsRequest","MAP_TYPE_TO_OPENAI_FUNCTION","openaiRequest","promptBlueprint","OpenAI","requestToMake","anthropicChatRequest","anthropicCompletionsRequest","MAP_TYPE_TO_ANTHROPIC_FUNCTION","anthropicRequest","Anthropic","GroupManager","apiKey","promptLayerCreateGroup","promptLayerBase","apiKey","llm","function_name","provider","handler","target","args","newTarget","prop","receiver","value","_a","_b","_c","_d","request_start_time","provider_type","return_pl_id","pl_tags","response","resolve","reject","request_response","__async","promptlayerApiHandler","error","TemplateManager","apiKey","promptName","params","getPromptTemplate","body","publishPromptTemplate","getAllPromptTemplates","metadata","apiKey","body","key","value","promptLayerTrackMetadata","score","promptLayerTrackScore","prompt","promptLayerTrackPrompt","group","promptLayerTrackGroup","TrackManager","MAP_PROVIDER_TO_FUNCTION_NAME","openaiStreamChat","openaiStreamCompletion","anthropicStreamMessage","anthropicStreamCompletion","MAP_PROVIDER_TO_FUNCTION","openaiRequest","anthropicRequest","PromptLayer","apiKey","TemplateManager","GroupManager","TrackManager","module","promptLayerBase","_0","__async","promptName","promptVersion","promptReleaseLabel","inputVariables","tags","metadata","groupId","stream","prompt_input_variables","templateGetParams","promptBlueprint","promptTemplate","promptBlueprintMetadata","promptBlueprintModel","provider_type","request_start_time","kwargs","config","function_name","stream_function","request_function","provider_base_url","response","_trackRequest","body","request_end_time","trackRequest","__spreadValues","streamResponse","requestLog"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "promptlayer",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@anthropic-ai/sdk": "^0.20.8",
|
|
20
20
|
"@types/node": "^20.8.0",
|
|
21
|
-
"openai": "^4.
|
|
21
|
+
"openai": "^4.48.1",
|
|
22
22
|
"tsup": "^7.2.0",
|
|
23
23
|
"typescript": "^5.2.2"
|
|
24
24
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,45 @@ import { GroupManager } from "@/groups";
|
|
|
2
2
|
import { promptLayerBase } from "@/promptlayer";
|
|
3
3
|
import { TemplateManager } from "@/templates";
|
|
4
4
|
import { TrackManager } from "@/track";
|
|
5
|
+
import { GetPromptTemplateParams, RunRequest } from "@/types";
|
|
6
|
+
import {
|
|
7
|
+
anthropicRequest,
|
|
8
|
+
anthropicStreamCompletion,
|
|
9
|
+
anthropicStreamMessage,
|
|
10
|
+
openaiRequest,
|
|
11
|
+
openaiStreamChat,
|
|
12
|
+
openaiStreamCompletion,
|
|
13
|
+
streamResponse,
|
|
14
|
+
trackRequest,
|
|
15
|
+
} from "@/utils";
|
|
16
|
+
|
|
17
|
+
const MAP_PROVIDER_TO_FUNCTION_NAME = {
|
|
18
|
+
openai: {
|
|
19
|
+
chat: {
|
|
20
|
+
function_name: "openai.chat.completions.create",
|
|
21
|
+
stream_function: openaiStreamChat,
|
|
22
|
+
},
|
|
23
|
+
completion: {
|
|
24
|
+
function_name: "openai.completions.create",
|
|
25
|
+
stream_function: openaiStreamCompletion,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
anthropic: {
|
|
29
|
+
chat: {
|
|
30
|
+
function_name: "anthropic.messages.create",
|
|
31
|
+
stream_function: anthropicStreamMessage,
|
|
32
|
+
},
|
|
33
|
+
completion: {
|
|
34
|
+
function_name: "anthropic.completions.create",
|
|
35
|
+
stream_function: anthropicStreamCompletion,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const MAP_PROVIDER_TO_FUNCTION: Record<string, any> = {
|
|
41
|
+
openai: openaiRequest,
|
|
42
|
+
anthropic: anthropicRequest,
|
|
43
|
+
};
|
|
5
44
|
|
|
6
45
|
export interface ClientOptions {
|
|
7
46
|
apiKey?: string;
|
|
@@ -48,4 +87,93 @@ export class PromptLayer {
|
|
|
48
87
|
);
|
|
49
88
|
}
|
|
50
89
|
}
|
|
90
|
+
|
|
91
|
+
async run({
|
|
92
|
+
promptName,
|
|
93
|
+
promptVersion,
|
|
94
|
+
promptReleaseLabel,
|
|
95
|
+
inputVariables,
|
|
96
|
+
tags,
|
|
97
|
+
metadata,
|
|
98
|
+
groupId,
|
|
99
|
+
stream = false,
|
|
100
|
+
}: RunRequest) {
|
|
101
|
+
const prompt_input_variables = inputVariables;
|
|
102
|
+
const templateGetParams: GetPromptTemplateParams = {
|
|
103
|
+
label: promptReleaseLabel,
|
|
104
|
+
version: promptVersion,
|
|
105
|
+
metadata_filters: metadata,
|
|
106
|
+
};
|
|
107
|
+
if (inputVariables) templateGetParams.input_variables = inputVariables;
|
|
108
|
+
const promptBlueprint = await this.templates.get(
|
|
109
|
+
promptName,
|
|
110
|
+
templateGetParams
|
|
111
|
+
);
|
|
112
|
+
if (!promptBlueprint) throw new Error("Prompt not found");
|
|
113
|
+
const promptTemplate = promptBlueprint.prompt_template;
|
|
114
|
+
if (!promptBlueprint.llm_kwargs) {
|
|
115
|
+
throw new Error(
|
|
116
|
+
`Prompt '${promptName}' does not have any LLM kwargs associated with it.`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const promptBlueprintMetadata = promptBlueprint.metadata;
|
|
120
|
+
if (!promptBlueprintMetadata) {
|
|
121
|
+
throw new Error(
|
|
122
|
+
`Prompt '${promptName}' does not have any metadata associated with it.`
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
const promptBlueprintModel = promptBlueprintMetadata.model;
|
|
126
|
+
if (!promptBlueprintModel) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`Prompt '${promptName}' does not have a model parameters associated with it.`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
const provider_type = promptBlueprintModel.provider;
|
|
132
|
+
const request_start_time = new Date().toISOString();
|
|
133
|
+
const kwargs = promptBlueprint.llm_kwargs;
|
|
134
|
+
const config =
|
|
135
|
+
MAP_PROVIDER_TO_FUNCTION_NAME[
|
|
136
|
+
provider_type as keyof typeof MAP_PROVIDER_TO_FUNCTION_NAME
|
|
137
|
+
][promptTemplate.type];
|
|
138
|
+
const function_name = config.function_name;
|
|
139
|
+
const stream_function = config.stream_function;
|
|
140
|
+
const request_function = MAP_PROVIDER_TO_FUNCTION[provider_type];
|
|
141
|
+
const provider_base_url = promptBlueprint.provider_base_url;
|
|
142
|
+
if (provider_base_url) {
|
|
143
|
+
kwargs["baseURL"] = provider_base_url.url;
|
|
144
|
+
}
|
|
145
|
+
kwargs["stream"] = stream;
|
|
146
|
+
if (stream && provider_type === "openai") {
|
|
147
|
+
kwargs["stream_options"] = { include_usage: true };
|
|
148
|
+
}
|
|
149
|
+
const response = await request_function(promptBlueprint, kwargs);
|
|
150
|
+
const _trackRequest = (body: object) => {
|
|
151
|
+
const request_end_time = new Date().toISOString();
|
|
152
|
+
return trackRequest({
|
|
153
|
+
function_name,
|
|
154
|
+
provider_type,
|
|
155
|
+
args: [],
|
|
156
|
+
kwargs,
|
|
157
|
+
tags,
|
|
158
|
+
request_start_time,
|
|
159
|
+
request_end_time,
|
|
160
|
+
api_key: this.apiKey,
|
|
161
|
+
metadata,
|
|
162
|
+
prompt_id: promptBlueprint.id,
|
|
163
|
+
prompt_version: promptBlueprint.version,
|
|
164
|
+
prompt_input_variables,
|
|
165
|
+
group_id: groupId,
|
|
166
|
+
return_prompt_blueprint: true,
|
|
167
|
+
...body,
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
if (stream) return streamResponse(response, _trackRequest, stream_function);
|
|
171
|
+
const requestLog = await _trackRequest({ request_response: response });
|
|
172
|
+
const data = {
|
|
173
|
+
request_id: requestLog.request_id,
|
|
174
|
+
raw_response: response,
|
|
175
|
+
prompt_blueprint: requestLog.prompt_blueprint,
|
|
176
|
+
};
|
|
177
|
+
return data;
|
|
178
|
+
}
|
|
51
179
|
}
|
package/src/types.ts
CHANGED
|
@@ -31,6 +31,8 @@ export interface TrackRequest {
|
|
|
31
31
|
tags?: string[];
|
|
32
32
|
request_response?: Record<string, unknown>;
|
|
33
33
|
prompt_input_variables?: Record<string, string> | string[];
|
|
34
|
+
return_data?: boolean;
|
|
35
|
+
group_id?: number;
|
|
34
36
|
[k: string]: unknown;
|
|
35
37
|
}
|
|
36
38
|
|
|
@@ -65,8 +67,9 @@ export interface Pagination {
|
|
|
65
67
|
export interface GetPromptTemplateParams {
|
|
66
68
|
version?: number;
|
|
67
69
|
label?: string;
|
|
68
|
-
provider
|
|
69
|
-
input_variables
|
|
70
|
+
provider?: string;
|
|
71
|
+
input_variables?: Record<string, string>;
|
|
72
|
+
metadata_filters?: Record<string, string>;
|
|
70
73
|
}
|
|
71
74
|
|
|
72
75
|
const templateFormat = ["f-string", "jinja2"] as const;
|
|
@@ -212,7 +215,15 @@ export type PromptVersion = {
|
|
|
212
215
|
metadata?: Metadata;
|
|
213
216
|
};
|
|
214
217
|
|
|
215
|
-
export type PublishPromptTemplate = BasePromptTemplate &
|
|
218
|
+
export type PublishPromptTemplate = BasePromptTemplate &
|
|
219
|
+
PromptVersion & { release_labels?: string[] };
|
|
220
|
+
|
|
221
|
+
export interface ProviderBaseURL {
|
|
222
|
+
id: number;
|
|
223
|
+
name: string;
|
|
224
|
+
provider: string;
|
|
225
|
+
url: string;
|
|
226
|
+
}
|
|
216
227
|
|
|
217
228
|
export interface BasePromptTemplateResponse {
|
|
218
229
|
id: number;
|
|
@@ -221,6 +232,7 @@ export interface BasePromptTemplateResponse {
|
|
|
221
232
|
prompt_template: PromptTemplate;
|
|
222
233
|
commit_message?: string;
|
|
223
234
|
metadata?: Metadata;
|
|
235
|
+
provider_base_url?: ProviderBaseURL;
|
|
224
236
|
}
|
|
225
237
|
|
|
226
238
|
export interface PublishPromptTemplateResponse
|
|
@@ -235,3 +247,14 @@ export interface ListPromptTemplatesResponse
|
|
|
235
247
|
extends BasePromptTemplateResponse {
|
|
236
248
|
version: number;
|
|
237
249
|
}
|
|
250
|
+
|
|
251
|
+
export interface RunRequest {
|
|
252
|
+
promptName: string;
|
|
253
|
+
tags?: string[];
|
|
254
|
+
metadata?: Record<string, string>;
|
|
255
|
+
groupId?: number;
|
|
256
|
+
stream?: boolean;
|
|
257
|
+
promptVersion?: number;
|
|
258
|
+
promptReleaseLabel?: string;
|
|
259
|
+
inputVariables?: Record<string, string>;
|
|
260
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -11,8 +11,18 @@ import {
|
|
|
11
11
|
TrackRequest,
|
|
12
12
|
TrackScore,
|
|
13
13
|
} from "@/types";
|
|
14
|
-
import
|
|
15
|
-
import {
|
|
14
|
+
import type TypeAnthropic from "@anthropic-ai/sdk";
|
|
15
|
+
import {
|
|
16
|
+
Completion as AnthropicCompletion,
|
|
17
|
+
Message,
|
|
18
|
+
MessageStreamEvent,
|
|
19
|
+
} from "@anthropic-ai/sdk/resources";
|
|
20
|
+
import type TypeOpenAI from "openai";
|
|
21
|
+
import {
|
|
22
|
+
ChatCompletion,
|
|
23
|
+
ChatCompletionChunk,
|
|
24
|
+
Completion,
|
|
25
|
+
} from "openai/resources";
|
|
16
26
|
|
|
17
27
|
const URL_API_PROMPTLAYER = "https://api.promptlayer.com";
|
|
18
28
|
|
|
@@ -321,9 +331,7 @@ const getAllPromptTemplates = async (
|
|
|
321
331
|
}
|
|
322
332
|
};
|
|
323
333
|
|
|
324
|
-
const
|
|
325
|
-
results: ChatCompletionChunk[]
|
|
326
|
-
): ChatCompletion => {
|
|
334
|
+
const openaiStreamChat = (results: ChatCompletionChunk[]): ChatCompletion => {
|
|
327
335
|
let content: ChatCompletion.Choice["message"]["content"] = null;
|
|
328
336
|
let functionCall: ChatCompletion.Choice["message"]["function_call"] =
|
|
329
337
|
undefined;
|
|
@@ -338,6 +346,7 @@ const mapChatCompletionChunk = (
|
|
|
338
346
|
if (!lastResult) return response;
|
|
339
347
|
let toolCalls: ChatCompletion.Choice["message"]["tool_calls"] = undefined;
|
|
340
348
|
for (const result of results) {
|
|
349
|
+
if (result.choices.length === 0) continue;
|
|
341
350
|
const delta = result.choices[0].delta;
|
|
342
351
|
|
|
343
352
|
if (delta.content) {
|
|
@@ -377,9 +386,9 @@ const mapChatCompletionChunk = (
|
|
|
377
386
|
}
|
|
378
387
|
}
|
|
379
388
|
response.choices.push({
|
|
380
|
-
finish_reason:
|
|
381
|
-
index:
|
|
382
|
-
logprobs:
|
|
389
|
+
finish_reason: results[0].choices[0].finish_reason || "stop",
|
|
390
|
+
index: results[0].choices[0].index || 0,
|
|
391
|
+
logprobs: results[0].choices[0].logprobs || null,
|
|
383
392
|
message: {
|
|
384
393
|
role: "assistant",
|
|
385
394
|
content,
|
|
@@ -391,10 +400,11 @@ const mapChatCompletionChunk = (
|
|
|
391
400
|
response.model = lastResult.model;
|
|
392
401
|
response.created = lastResult.created;
|
|
393
402
|
response.system_fingerprint = lastResult.system_fingerprint;
|
|
403
|
+
response.usage = lastResult.usage;
|
|
394
404
|
return response;
|
|
395
405
|
};
|
|
396
406
|
|
|
397
|
-
const
|
|
407
|
+
const anthropicStreamMessage = (results: MessageStreamEvent[]): Message => {
|
|
398
408
|
let response: Message = {
|
|
399
409
|
id: "",
|
|
400
410
|
model: "",
|
|
@@ -456,7 +466,7 @@ const cleaned_result = (
|
|
|
456
466
|
}
|
|
457
467
|
|
|
458
468
|
if (function_name === "anthropic.messages.create")
|
|
459
|
-
return
|
|
469
|
+
return anthropicStreamMessage(results);
|
|
460
470
|
|
|
461
471
|
if ("text" in results[0].choices[0]) {
|
|
462
472
|
let response = "";
|
|
@@ -469,7 +479,7 @@ const cleaned_result = (
|
|
|
469
479
|
}
|
|
470
480
|
|
|
471
481
|
if ("delta" in results[0].choices[0]) {
|
|
472
|
-
const response =
|
|
482
|
+
const response = openaiStreamChat(results);
|
|
473
483
|
response.choices[0] = {
|
|
474
484
|
...response.choices[0],
|
|
475
485
|
...response.choices[0].message,
|
|
@@ -513,9 +523,173 @@ const warnOnBadResponse = (request_response: any, main_message: string) => {
|
|
|
513
523
|
}
|
|
514
524
|
};
|
|
515
525
|
|
|
526
|
+
const trackRequest = async (body: TrackRequest) => {
|
|
527
|
+
try {
|
|
528
|
+
const response = await fetch(`${URL_API_PROMPTLAYER}/track-request`, {
|
|
529
|
+
method: "POST",
|
|
530
|
+
headers: {
|
|
531
|
+
"Content-Type": "application/json",
|
|
532
|
+
},
|
|
533
|
+
body: JSON.stringify(body),
|
|
534
|
+
});
|
|
535
|
+
if (response.status !== 200)
|
|
536
|
+
warnOnBadResponse(
|
|
537
|
+
response,
|
|
538
|
+
"WARNING: While logging your request, PromptLayer experienced the following error:"
|
|
539
|
+
);
|
|
540
|
+
return response.json();
|
|
541
|
+
} catch (e) {
|
|
542
|
+
console.warn(
|
|
543
|
+
`WARNING: While logging your request PromptLayer had the following error: ${e}`
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
return {};
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const openaiStreamCompletion = (results: Completion[]) => {
|
|
550
|
+
const response: Completion = {
|
|
551
|
+
id: "",
|
|
552
|
+
choices: [
|
|
553
|
+
{
|
|
554
|
+
finish_reason: "stop",
|
|
555
|
+
index: 0,
|
|
556
|
+
text: "",
|
|
557
|
+
logprobs: null,
|
|
558
|
+
},
|
|
559
|
+
],
|
|
560
|
+
created: Date.now(),
|
|
561
|
+
model: "",
|
|
562
|
+
object: "text_completion",
|
|
563
|
+
};
|
|
564
|
+
const lastResult = results.at(-1);
|
|
565
|
+
if (!lastResult) return response;
|
|
566
|
+
let text = "";
|
|
567
|
+
for (const result of results) {
|
|
568
|
+
if (result.choices.length > 0 && result.choices[0].text) {
|
|
569
|
+
text = `${text}${result.choices[0].text}`;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
response.choices[0].text = text;
|
|
573
|
+
response.id = lastResult.id;
|
|
574
|
+
response.created = lastResult.created;
|
|
575
|
+
response.model = lastResult.model;
|
|
576
|
+
response.system_fingerprint = lastResult.system_fingerprint;
|
|
577
|
+
response.usage = lastResult.usage;
|
|
578
|
+
return response;
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
const anthropicStreamCompletion = (results: AnthropicCompletion[]) => {
|
|
582
|
+
const response: AnthropicCompletion = {
|
|
583
|
+
completion: "",
|
|
584
|
+
id: "",
|
|
585
|
+
model: "",
|
|
586
|
+
stop_reason: "",
|
|
587
|
+
type: "completion",
|
|
588
|
+
};
|
|
589
|
+
const lastResult = results.at(-1);
|
|
590
|
+
if (!lastResult) return response;
|
|
591
|
+
let completion = "";
|
|
592
|
+
for (const result of results) {
|
|
593
|
+
completion = `${completion}${result.completion}`;
|
|
594
|
+
}
|
|
595
|
+
response.completion = completion;
|
|
596
|
+
response.id = lastResult.id;
|
|
597
|
+
response.model = lastResult.model;
|
|
598
|
+
response.stop_reason = lastResult.stop_reason;
|
|
599
|
+
return response;
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
async function* streamResponse<Item>(
|
|
603
|
+
generator: AsyncIterable<Item>,
|
|
604
|
+
afterStream: (body: object) => any,
|
|
605
|
+
mapResults: any
|
|
606
|
+
) {
|
|
607
|
+
const data: {
|
|
608
|
+
request_id: number | null;
|
|
609
|
+
raw_response: any;
|
|
610
|
+
prompt_blueprint: any;
|
|
611
|
+
} = {
|
|
612
|
+
request_id: null,
|
|
613
|
+
raw_response: null,
|
|
614
|
+
prompt_blueprint: null,
|
|
615
|
+
};
|
|
616
|
+
const results = [];
|
|
617
|
+
for await (const result of generator) {
|
|
618
|
+
results.push(result);
|
|
619
|
+
data.raw_response = result;
|
|
620
|
+
yield data;
|
|
621
|
+
}
|
|
622
|
+
const request_response = mapResults(results);
|
|
623
|
+
const response = await afterStream({ request_response });
|
|
624
|
+
data.request_id = response.request_id;
|
|
625
|
+
data.prompt_blueprint = response.prompt_blueprint;
|
|
626
|
+
yield data;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
const openaiChatRequest = async (client: TypeOpenAI, kwargs: any) => {
|
|
630
|
+
return client.chat.completions.create(kwargs);
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
const openaiCompletionsRequest = async (client: TypeOpenAI, kwargs: any) => {
|
|
634
|
+
return client.completions.create(kwargs);
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
const MAP_TYPE_TO_OPENAI_FUNCTION = {
|
|
638
|
+
chat: openaiChatRequest,
|
|
639
|
+
completion: openaiCompletionsRequest,
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
const openaiRequest = async (
|
|
643
|
+
promptBlueprint: GetPromptTemplateResponse,
|
|
644
|
+
kwargs: any
|
|
645
|
+
) => {
|
|
646
|
+
const OpenAI = require("openai").default;
|
|
647
|
+
const client = new OpenAI({
|
|
648
|
+
baseURL: kwargs.baseURL,
|
|
649
|
+
});
|
|
650
|
+
const requestToMake =
|
|
651
|
+
MAP_TYPE_TO_OPENAI_FUNCTION[promptBlueprint.prompt_template.type];
|
|
652
|
+
return requestToMake(client, kwargs);
|
|
653
|
+
};
|
|
654
|
+
|
|
655
|
+
const anthropicChatRequest = async (client: TypeAnthropic, kwargs: any) => {
|
|
656
|
+
return client.messages.create(kwargs);
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
const anthropicCompletionsRequest = async (
|
|
660
|
+
client: TypeAnthropic,
|
|
661
|
+
kwargs: any
|
|
662
|
+
) => {
|
|
663
|
+
return client.completions.create(kwargs);
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
const MAP_TYPE_TO_ANTHROPIC_FUNCTION = {
|
|
667
|
+
chat: anthropicChatRequest,
|
|
668
|
+
completion: anthropicCompletionsRequest,
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
const anthropicRequest = async (
|
|
672
|
+
promptBlueprint: GetPromptTemplateResponse,
|
|
673
|
+
kwargs: any
|
|
674
|
+
) => {
|
|
675
|
+
const Anthropic = require("@anthropic-ai/sdk").default;
|
|
676
|
+
const client = new Anthropic({
|
|
677
|
+
baseURL: kwargs.baseURL,
|
|
678
|
+
});
|
|
679
|
+
const requestToMake =
|
|
680
|
+
MAP_TYPE_TO_ANTHROPIC_FUNCTION[promptBlueprint.prompt_template.type];
|
|
681
|
+
return requestToMake(client, kwargs);
|
|
682
|
+
};
|
|
683
|
+
|
|
516
684
|
export {
|
|
685
|
+
anthropicRequest,
|
|
686
|
+
anthropicStreamCompletion,
|
|
687
|
+
anthropicStreamMessage,
|
|
517
688
|
getAllPromptTemplates,
|
|
518
689
|
getPromptTemplate,
|
|
690
|
+
openaiRequest,
|
|
691
|
+
openaiStreamChat,
|
|
692
|
+
openaiStreamCompletion,
|
|
519
693
|
promptLayerApiRequest,
|
|
520
694
|
promptLayerCreateGroup,
|
|
521
695
|
promptLayerTrackGroup,
|
|
@@ -524,4 +698,6 @@ export {
|
|
|
524
698
|
promptLayerTrackScore,
|
|
525
699
|
promptlayerApiHandler,
|
|
526
700
|
publishPromptTemplate,
|
|
701
|
+
streamResponse,
|
|
702
|
+
trackRequest,
|
|
527
703
|
};
|