promptlayer 1.0.39 → 1.0.40
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 +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/index.ts +1 -1
- package/src/utils/blueprint-builder.ts +198 -0
- package/src/utils/utils.ts +57 -12
package/dist/esm/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var De=Object.defineProperty,Ye=Object.defineProperties;var Fe=Object.getOwnPropertyDescriptors;var L=Object.getOwnPropertySymbols;var re=Object.prototype.hasOwnProperty,se=Object.prototype.propertyIsEnumerable;var ne=(n,e)=>{if(e=Symbol[n])return e;throw Error("Symbol."+n+" is not defined")};var oe=(n,e,t)=>e in n?De(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,h=(n,e)=>{for(var t in e||(e={}))re.call(e,t)&&oe(n,t,e[t]);if(L)for(var t of L(e))se.call(e,t)&&oe(n,t,e[t]);return n},T=(n,e)=>Ye(n,Fe(e));var C=(n=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(n,{get:(e,t)=>(typeof require!="undefined"?require:e)[t]}):n)(function(n){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var ae=(n,e)=>{var t={};for(var o in n)re.call(n,o)&&e.indexOf(o)<0&&(t[o]=n[o]);if(n!=null&&L)for(var o of L(n))e.indexOf(o)<0&&se.call(n,o)&&(t[o]=n[o]);return t};var u=(n,e,t)=>new Promise((o,r)=>{var s=i=>{try{p(t.next(i))}catch(c){r(c)}},a=i=>{try{p(t.throw(i))}catch(c){r(c)}},p=i=>i.done?o(i.value):Promise.resolve(i.value).then(s,a);p((t=t.apply(n,e)).next())}),A=function(n,e){this[0]=n,this[1]=e},J=(n,e,t)=>{var o=(a,p,i,c)=>{try{var l=t[a](p),m=(p=l.value)instanceof A,y=l.done;Promise.resolve(m?p[0]:p).then(f=>m?o(a==="return"?a:"next",p[1]?{done:f.done,value:f.value}:f,i,c):i({value:f,done:y})).catch(f=>o("throw",f,i,c))}catch(f){c(f)}},r=a=>s[a]=p=>new Promise((i,c)=>o(a,p,i,c)),s={};return t=t.apply(n,e),s[Symbol.asyncIterator]=()=>s,r("next"),r("throw"),r("return"),s};var D=(n,e,t)=>(e=n[ne("asyncIterator")])?e.call(n):(n=n[ne("iterator")](),e={},t=(o,r)=>(r=n[o])&&(e[o]=s=>new Promise((a,p,i)=>(s=r.call(n,s),i=s.done,Promise.resolve(s.value).then(c=>a({value:c,done:i}),p)))),t("next"),t("return"),e);import Ve from"ably";var Y="SET_WORKFLOW_COMPLETE";function Xe(n,e,t){return u(this,null,function*(){let o=yield fetch(`${R}/workflow-version-execution-results?workflow_version_execution_id=${n}&return_all_outputs=${e}`,{headers:t});if(!o.ok)throw new Error("Failed to fetch final output");return o.json()})}function ze(n,e,t,o){return function(r){return u(this,null,function*(){if(r.name===Y)try{let s=JSON.parse(r.data),a=s.result_code,p;if(a==="OK"||a==null)p=s.final_output;else if(a==="EXCEEDS_SIZE_LIMIT")p=yield Xe(e,t,o);else throw new Error(`Unsupported final output code: ${a}`);n.resolve(p)}catch(s){n.reject(s)}})}}function He(a){return u(this,arguments,function*({token:n,channelName:e,executionId:t,returnAllOutputs:o,headers:r,timeout:s}){let p=new Ve.Realtime(n),i=p.channels.get(e),c={},l=new Promise((y,f)=>{c.resolve=y,c.reject=f}),m=ze(c,t,o,r);yield i.subscribe(Y,m);try{return yield new Promise((y,f)=>{let _=setTimeout(()=>{f(new Error("Workflow execution did not complete properly (timeout)"))},s);l.then(w=>{clearTimeout(_),y(w)}).catch(w=>{clearTimeout(_),f(w)})})}finally{console.log("Closing client"),i.unsubscribe(Y,m),p.close(),console.log("Closed client")}})}var R=process.env.PROMPTLAYER_API_URL||"https://api.promptlayer.com",ce=(n,e)=>u(void 0,null,function*(){return e.request_response[Symbol.asyncIterator]!==void 0?Ze(n,e.request_response,e):yield pe(n,e)}),pe=(n,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${R}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),o=yield t.json();if(t.status!==200&&S(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}),le=(n,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${R}/library-track-metadata`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(T(h({},e),{api_key:n}))}),o=yield t.json();if(t.status!==200)return S(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}),ue=(n,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${R}/library-track-score`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(T(h({},e),{api_key:n}))}),o=yield t.json();if(t.status!==200)return S(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}),me=(n,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${R}/library-track-prompt`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(T(h({},e),{api_key:n}))}),o=yield t.json();if(t.status!==200)return S(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}),fe=(n,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${R}/track-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(T(h({},e),{api_key:n}))}),o=yield t.json();if(t.status!==200)return S(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}),de=n=>u(void 0,null,function*(){try{let e=yield fetch(`${R}/create-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({api_key:n})}),t=yield e.json();return e.status!==200?(S(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}}),he=(n,e,t)=>u(void 0,null,function*(){try{let o=new URL(`${R}/prompt-templates/${e}`),r=yield fetch(o,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":n},body:JSON.stringify(t)}),s=yield r.json();return r.status!==200?(S(s,"WARNING: While fetching a prompt template PromptLayer had the following error"),null):(s.warning&&console.warn(`WARNING: While fetching your prompt PromptLayer had the following error: ${s.warning}`),s)}catch(o){return console.warn(`WARNING: While fetching a prompt template PromptLayer had the following error: ${o}`),null}}),ye=(n,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${R}/rest/prompt-templates`,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":n},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&&S(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}`)}}),ge=(n,e)=>u(void 0,null,function*(){var t;try{let o=new URL(`${R}/prompt-templates`);Object.entries(e||{}).forEach(([a,p])=>o.searchParams.append(a,p.toString()));let r=yield fetch(o,{headers:{"Content-Type":"application/json","X-API-KEY":n}}),s=yield r.json();return r.status!==200?(S(s,"WARNING: While fetching all prompt templates PromptLayer had the following error"),null):(t=s.items)!=null?t:[]}catch(o){return console.warn(`WARNING: While fetching all prompt templates PromptLayer had the following error: ${o}`),null}}),_e=i=>u(void 0,[i],function*({workflow_name:n,input_variables:e,metadata:t={},workflow_label_name:o=null,workflow_version_number:r=null,return_all_outputs:s=!1,api_key:a,timeout:p=36e5}){let c={input_variables:e,metadata:t,workflow_label_name:o,workflow_version_number:r,return_all_outputs:s},l={"X-API-KEY":a,"Content-Type":"application/json"};try{let m=yield fetch(`${R}/workflows/${encodeURIComponent(n)}/run`,{method:"POST",headers:l,body:JSON.stringify(c)});if(m.status!==201)return{success:!1,message:`Failed to run workflow: ${(yield m.json().catch(()=>({}))).error||m.statusText}`};let y=yield m.json();y.warning&&console.warn(`WARNING: ${y.warning}`);let f=y.workflow_version_execution_id;if(!f)return console.log("No execution ID returned from workflow run"),{success:!1,message:"Failed to run workflow"};let _=`workflow_updates:${f}`,O=(yield(yield fetch(`${R}/ws-token-request-library?capability=${_}`,{method:"POST",headers:l})).json()).token_details.token;return yield He({token:O,channelName:_,executionId:f,returnAllOutputs:s,headers:l,timeout:p})}catch(m){throw console.error(`Failed to run workflow: ${m instanceof Error?m.message:m}`),m}}),F=n=>{var p,i,c,l,m,y,f,_,w,b;let e=null,t,o={id:"",choices:[],created:Date.now(),model:"",object:"chat.completion"},r=n.at(-1);if(!r)return o;let s;for(let O of n){if(O.choices.length===0)continue;let g=O.choices[0].delta;g.content&&(e=`${e||""}${g.content||""}`),g.function_call&&(t={name:`${t?t.name:""}${g.function_call.name||""}`,arguments:`${t?t.arguments:""}${g.function_call.arguments||""}`});let d=(p=g.tool_calls)==null?void 0:p[0];if(d){s=s||[];let P=s.at(-1);if(!P||d.id){s.push({id:d.id||"",type:d.type||"function",function:{name:((i=d.function)==null?void 0:i.name)||"",arguments:((c=d.function)==null?void 0:c.arguments)||""}});continue}P.function.name=`${P.function.name}${((l=d.function)==null?void 0:l.name)||""}`,P.function.arguments=`${P.function.arguments}${((m=d.function)==null?void 0:m.arguments)||""}`}}let a=n[0].choices.at(0);return o.choices.push({finish_reason:(y=a==null?void 0:a.finish_reason)!=null?y:"stop",index:(f=a==null?void 0:a.index)!=null?f:0,logprobs:(_=a==null?void 0:a.logprobs)!=null?_:null,message:{role:"assistant",content:e,function_call:t||void 0,tool_calls:s||void 0,refusal:(w=a==null?void 0:a.delta.refusal)!=null?w:null}}),o.id=r.id,o.model=r.model,o.created=r.created,o.system_fingerprint=r.system_fingerprint,o.usage=(b=r.usage)!=null?b:void 0,o},we=n=>{var i;let e={id:"",model:"",content:[],role:"assistant",type:"message",stop_reason:"stop_sequence",stop_sequence:null,usage:{input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0,server_tool_use:null,service_tier:null}};if(!n.at(-1))return e;let o=null,r="",s="",a="",p="";for(let c of n)if(c.type==="message_start")e=h({},c.message);else if(c.type==="content_block_start")o=h({},c.content_block),o.type==="thinking"?(r="",s=""):o.type==="text"?a="":(o.type==="tool_use"||o.type==="server_tool_use")&&(p="");else if(c.type==="content_block_delta"&&o!==null){if(o.type==="thinking")"signature"in c.delta&&(r=c.delta.signature||""),"thinking"in c.delta&&(s+=c.delta.thinking||"");else if(o.type==="text")"text"in c.delta&&(a+=c.delta.text||"");else if((o.type==="tool_use"||o.type==="server_tool_use")&&c.delta.type==="input_json_delta"){let l=c.delta;p+=l.partial_json||""}}else if(c.type==="content_block_stop"&&o!==null){if(o.type==="thinking")o.signature=r,o.thinking=s;else if(o.type==="text")o.text=a,o.citations=null;else if(o.type==="tool_use"||o.type==="server_tool_use")try{o.input=p?JSON.parse(p):{}}catch(l){o.input={}}e.content.push(o),o=null,r="",s="",a="",p=""}else c.type==="message_delta"&&("usage"in c&&c.usage&&(e.usage=T(h({},e.usage),{output_tokens:(i=c.usage.output_tokens)!=null?i:0})),"delta"in c&&c.delta&&("stop_reason"in c.delta&&c.delta.stop_reason!==void 0&&(e.stop_reason=c.delta.stop_reason),"stop_sequence"in c.delta&&c.delta.stop_sequence!==void 0&&(e.stop_sequence=c.delta.stop_sequence)));return e},Be=(n,e="openai.chat.completions.create")=>{if("completion"in n[0])return n.reduce((t,o)=>T(h({},o),{completion:`${t.completion}${o.completion}`}),{});if(e==="anthropic.messages.create")return we(n);if("text"in n[0].choices[0]){let t="";for(let r of n)t=`${t}${r.choices[0].text}`;let o=structuredClone(n.at(-1));return o.choices[0].text=t,o}if("delta"in n[0].choices[0]){let t=F(n);return t.choices[0]=h(h({},t.choices[0]),t.choices[0].message),t}return""};function Ze(n,e,t){return J(this,null,function*(){let o=[];try{for(var a=D(e),p,i,c;p=!(i=yield new A(a.next())).done;p=!1){let l=i.value;yield t.return_pl_id?[l,null]:l,o.push(l)}}catch(i){c=[i]}finally{try{p&&(i=a.return)&&(yield new A(i.call(a)))}finally{if(c)throw c[0]}}let r=Be(o,t.function_name),s=yield new A(pe(n,T(h({},t),{request_response:r,request_end_time:new Date().toISOString()})));if(s&&t.return_pl_id){let l=s[1];yield[o.at(-1),l]}})}var S=(n,e)=>{try{console.warn(`${e}: ${n.message}`)}catch(t){console.warn(`${e}: ${n}`)}},Pe=n=>u(void 0,null,function*(){try{let e=yield fetch(`${R}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)}),t=yield e.json();return e.status!==200&&S(t,"WARNING: While logging your request, PromptLayer experienced the following error:"),t}catch(e){console.warn(`WARNING: While logging your request PromptLayer had the following error: ${e}`)}return{}}),ie=n=>{let e={id:"",choices:[{finish_reason:"stop",index:0,text:"",logprobs:null}],created:Date.now(),model:"",object:"text_completion"},t=n.at(-1);if(!t)return e;let o="";for(let r of n)r.choices.length>0&&r.choices[0].text&&(o=`${o}${r.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},Qe=n=>{let e={completion:"",id:"",model:"",stop_reason:"",type:"completion"},t=n.at(-1);if(!t)return e;let o="";for(let r of n)o=`${o}${r.completion}`;return e.completion=o,e.id=t.id,e.model=t.model,e.stop_reason=t.stop_reason,e};function Re(n,e,t){return J(this,null,function*(){let o={request_id:null,raw_response:null,prompt_blueprint:null},r=[];try{for(var p=D(n),i,c,l;i=!(c=yield new A(p.next())).done;i=!1){let m=c.value;r.push(m),o.raw_response=m,yield o}}catch(c){l=[c]}finally{try{i&&(c=p.return)&&(yield new A(c.call(p)))}finally{if(l)throw l[0]}}let s=t(r),a=yield new A(e({request_response:s}));o.request_id=a.request_id,o.prompt_blueprint=a.prompt_blueprint,yield o})}var et=(n,e)=>u(void 0,null,function*(){return n.chat.completions.create(e)}),tt=(n,e)=>u(void 0,null,function*(){return n.completions.create(e)}),Te={chat:et,completion:tt},Se=(n,e)=>u(void 0,null,function*(){let t=C("openai").default,o=new t({baseURL:e.baseURL,apiKey:e.apiKey}),r=Te[n.prompt_template.type];return r(o,e)}),Oe=(n,e)=>u(void 0,null,function*(){let{AzureOpenAI:t}=C("openai"),o=new t({endpoint:process.env.AZURE_OPENAI_ENDPOINT||e.baseURL,apiVersion:process.env.OPENAI_API_VERSION||e.apiVersion,apiKey:process.env.AZURE_OPENAI_API_KEY||e.apiKey});e==null||delete e.baseURL,e==null||delete e.apiVersion,e==null||delete e.apiKey;let r=Te[n.prompt_template.type];return r(o,e)}),Ee=(n,e)=>u(void 0,null,function*(){return n.messages.create(e)}),nt=(n,e)=>u(void 0,null,function*(){return n.completions.create(e)}),ot={chat:Ee,completion:nt},be=(n,e)=>u(void 0,null,function*(){let t=C("@anthropic-ai/sdk").default,o=new t({baseURL:e.baseURL}),r=ot[n.prompt_template.type];return r(o,e)}),Ae=(n,e)=>u(void 0,null,function*(){try{let t=yield fetch(`${R}/log-request`,{method:"POST",headers:{"X-API-KEY":n,"Content-Type":"application/json"},body:JSON.stringify(e)}),o=yield t.json();return t.status!==201?(S(o,"WARNING: While logging your request PromptLayer had the following error"),null):o}catch(t){return console.warn(`WARNING: While tracking your prompt PromptLayer had the following error: ${t}`),null}}),rt=(n,e,t,o)=>{var a,p;let r=h({},o),s=[];if(n){let i={text:n,thought:!0};s.push(i)}if(e){let i={text:e,thought:null};s.push(i)}for(let i of t){let c={function_call:i};s.push(c)}return s.length>0&&((p=(a=r.candidates)==null?void 0:a[0])!=null&&p.content)&&(r.candidates[0].content.parts=s),r},Ce=n=>{var s,a;let{GenerateContentResponse:e}=C("@google/genai");if(!n.length)return new e;let t="",o="",r=[];for(let p of n)if(p.candidates&&((a=(s=p.candidates[0])==null?void 0:s.content)!=null&&a.parts))for(let i of p.candidates[0].content.parts)i.text?i.thought===!0?t+=i.text:o+=i.text:i.functionCall&&r.push(i.functionCall);return rt(t,o,r,n[n.length-1])},st=n=>Ce(n),at=n=>Ce(n),it=(n,e)=>u(void 0,null,function*(){var a,p;let t=e==null?void 0:e.history,o=e==null?void 0:e.generationConfig,r=t.length>0?(a=t[t.length-1])==null?void 0:a.parts:"",s=n.chats.create({model:e==null?void 0:e.model,history:(p=t.slice(0,-1))!=null?p:[],config:o});return e!=null&&e.stream?yield s.sendMessageStream({message:r}):yield s.sendMessage({message:r})}),ct=(n,o)=>u(void 0,null,function*(){var r=o,{stream:e}=r,t=ae(r,["stream"]);return e?yield n.generateContentStream(h({},t)):yield n.generateContent(h({},t))}),pt={chat:it,completion:ct},X=(n,e)=>u(void 0,null,function*(){let{GoogleGenAI:t}=yield import("@google/genai"),o=process.env.GOOGLE_API_KEY||process.env.GEMINI_API_KEY,r=process.env.VERTEX_AI_PROJECT_ID||process.env.GOOGLE_PROJECT_ID||process.env.GOOGLE_CLOUD_PROJECT,s=process.env.VERTEX_AI_PROJECT_LOCATION||process.env.GOOGLE_PROJECT_LOCATION||process.env.GOOGLE_CLOUD_PROJECT_LOCATION,a={keyFilename:process.env.GOOGLE_APPLICATION_CREDENTIALS,projectId:r,scopes:["https://www.googleapis.com/auth/cloud-platform"]},p=o?new t({apiKey:o}):new t({vertexai:!0,project:r,location:s,googleAuthOptions:a}),i=pt[n.prompt_template.type],c=V(e);return c.generationConfig&&(c.generationConfig=V(c.generationConfig)),yield i(p,c)}),lt=n=>n.replace(/_([a-z])/g,(e,t)=>t.toUpperCase()),V=n=>!n||typeof n!="object"?n:Array.isArray(n)?n.map(V):Object.fromEntries(Object.entries(n).map(([e,t])=>[lt(e),t])),ut=["openai","openai.azure"],mt={openai:{chat:{function_name:"openai.chat.completions.create",stream_function:F},completion:{function_name:"openai.completions.create",stream_function:ie}},anthropic:{chat:{function_name:"anthropic.messages.create",stream_function:we},completion:{function_name:"anthropic.completions.create",stream_function:Qe}},"openai.azure":{chat:{function_name:"openai.AzureOpenAI.chat.completions.create",stream_function:F},completion:{function_name:"openai.AzureOpenAI.completions.create",stream_function:ie}},google:{chat:{function_name:"google.convo.send_message",stream_function:st},completion:{function_name:"google.model.generate_content",stream_function:at}}},Ie=(n,e,t={},o=!1)=>{var p,i,c,l,m;let r=(c=e==null?void 0:e.client)!=null?c:(i=(p=n.metadata)==null?void 0:p.model)==null?void 0:i.provider;if(!r)throw new Error("Provider type not found in prompt blueprint or custom provider");let s=T(h(h({},n.llm_kwargs||{}),t),{stream:o}),a={baseURL:(m=e==null?void 0:e.base_url)!=null?m:(l=n.provider_base_url)==null?void 0:l.url,apiKey:e==null?void 0:e.api_key};return Object.entries(a).forEach(([y,f])=>{f!==void 0&&(s[y]=f)}),o&&ut.includes(r)&&(s.stream_options={include_usage:!0}),{provider_type:r,kwargs:s}},Ne=(n,e)=>{let t=mt[n];if(!t)throw new Error(`Unsupported provider type: ${n}`);let o=e.type,r=t[o];if(!r)throw new Error(`Unsupported template type '${e.type}' for provider '${n}'`);return r},ke=(n,e)=>u(void 0,null,function*(){var o;let t=(o=n.metadata)==null?void 0:o.model;if(!t)throw new Error("Model metadata not found in prompt blueprint");if(t.name.startsWith("gemini"))return X(n,e);if(t.name.startsWith("claude")){let{AnthropicVertex:r}=yield import("@anthropic-ai/vertex-sdk"),s=new r({baseURL:e.baseURL});if(n.prompt_template.type==="chat")return Ee(s,e);throw new Error(`Unsupported prompt template type '${n.prompt_template.type}' for Anthropic Vertex AI`)}throw new Error(`Unsupported model name '${t.name}' for Vertex AI request`)});var v=class{constructor(e){this.create=()=>de(this.apiKey);this.apiKey=e}};import*as Le from"@opentelemetry/api";import{SimpleSpanProcessor as ft}from"@opentelemetry/sdk-trace-base";import{NodeTracerProvider as dt}from"@opentelemetry/sdk-trace-node";import{SpanKind as q,SpanStatusCode as z}from"@opentelemetry/api";import{ExportResultCode as x}from"@opentelemetry/core";var H=class{constructor(e,t){this.apiKey=t||process.env.PROMPTLAYER_API_KEY,this.enableTracing=e,this.url=`${R}/spans-bulk`}attributesToObject(e){return e?Object.fromEntries(Object.entries(e)):{}}spanKindToString(e){return{[q.INTERNAL]:"SpanKind.INTERNAL",[q.SERVER]:"SpanKind.SERVER",[q.CLIENT]:"SpanKind.CLIENT",[q.PRODUCER]:"SpanKind.PRODUCER",[q.CONSUMER]:"SpanKind.CONSUMER"}[e]||"SpanKind.INTERNAL"}statusCodeToString(e){return{[z.ERROR]:"StatusCode.ERROR",[z.OK]:"StatusCode.OK",[z.UNSET]:"StatusCode.UNSET"}[e]||"StatusCode.UNSET"}toNanoseconds(e){return(BigInt(e[0])*BigInt(1e9)+BigInt(e[1])).toString()}export(e){if(!this.enableTracing)return Promise.resolve(x.SUCCESS);let t=e.map(o=>{var r;return{name:o.name,context:{trace_id:o.spanContext().traceId,span_id:o.spanContext().spanId,trace_state:((r=o.spanContext().traceState)==null?void 0:r.serialize())||""},kind:this.spanKindToString(o.kind),parent_id:o.parentSpanId||null,start_time:this.toNanoseconds(o.startTime),end_time:this.toNanoseconds(o.endTime),status:{status_code:this.statusCodeToString(o.status.code),description:o.status.message},attributes:this.attributesToObject(o.attributes),events:o.events.map(s=>({name:s.name,timestamp:this.toNanoseconds(s.time),attributes:this.attributesToObject(s.attributes)})),links:o.links.map(s=>({context:s.context,attributes:this.attributesToObject(s.attributes)})),resource:{attributes:T(h({},o.resource.attributes),{"service.name":"prompt-layer-js"}),schema_url:""}}});return fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":this.apiKey||""},body:JSON.stringify({spans:t})}).then(o=>o.ok?x.SUCCESS:(console.error(`Error exporting spans
|
|
2
|
-
HTTP error! status: ${o.status}`)
|
|
1
|
+
var Ze=Object.defineProperty,Qe=Object.defineProperties;var et=Object.getOwnPropertyDescriptors;var L=Object.getOwnPropertySymbols;var pe=Object.prototype.hasOwnProperty,le=Object.prototype.propertyIsEnumerable;var ie=(t,e)=>{if(e=Symbol[t])return e;throw Error("Symbol."+t+" is not defined")};var ce=(t,e,n)=>e in t?Ze(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,y=(t,e)=>{for(var n in e||(e={}))pe.call(e,n)&&ce(t,n,e[n]);if(L)for(var n of L(e))le.call(e,n)&&ce(t,n,e[n]);return t},R=(t,e)=>Qe(t,et(e));var I=(t=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(t,{get:(e,n)=>(typeof require!="undefined"?require:e)[n]}):t)(function(t){if(typeof require!="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')});var v=(t,e)=>{var n={};for(var o in t)pe.call(t,o)&&e.indexOf(o)<0&&(n[o]=t[o]);if(t!=null&&L)for(var o of L(t))e.indexOf(o)<0&&le.call(t,o)&&(n[o]=t[o]);return n};var m=(t,e,n)=>new Promise((o,r)=>{var s=i=>{try{c(n.next(i))}catch(p){r(p)}},a=i=>{try{c(n.throw(i))}catch(p){r(p)}},c=i=>i.done?o(i.value):Promise.resolve(i.value).then(s,a);c((n=n.apply(t,e)).next())}),A=function(t,e){this[0]=t,this[1]=e},Y=(t,e,n)=>{var o=(a,c,i,p)=>{try{var u=n[a](c),f=(c=u.value)instanceof A,l=u.done;Promise.resolve(f?c[0]:c).then(d=>f?o(a==="return"?a:"next",c[1]?{done:d.done,value:d.value}:d,i,p):i({value:d,done:l})).catch(d=>o("throw",d,i,p))}catch(d){p(d)}},r=a=>s[a]=c=>new Promise((i,p)=>o(a,c,i,p)),s={};return n=n.apply(t,e),s[Symbol.asyncIterator]=()=>s,r("next"),r("throw"),r("return"),s};var V=(t,e,n)=>(e=t[ie("asyncIterator")])?e.call(t):(t=t[ie("iterator")](),e={},n=(o,r)=>(r=t[o])&&(e[o]=s=>new Promise((a,c,i)=>(s=r.call(t,s),i=s.done,Promise.resolve(s.value).then(p=>a({value:p,done:i}),c)))),n("next"),n("return"),e);var G=(t,e,n)=>({id:t,function:{name:e,input:n}}),C=n=>{var o=n,{type:t}=o,e=v(o,["type"]);return y({type:t},e)},B=(t,e)=>({input_variables:[],template_format:"f-string",content:t,role:"assistant",function_call:null,name:null,tool_calls:e}),X=(t,e)=>({prompt_template:{messages:[t],type:"chat",input_variables:[]},metadata:e}),ue=(t,e)=>{let n=[],o=[];t.type==="content_block_start"?t.content_block.type==="thinking"?n.push(C({type:"thinking",thinking:"",signature:""})):t.content_block.type==="text"?n.push(C({type:"text",text:""})):t.content_block.type==="tool_use"&&o.push(G(t.content_block.id||"",t.content_block.name||"",{})):t.type==="content_block_delta"&&(t.delta.type==="thinking_delta"?n.push(C({type:"thinking",thinking:t.delta.thinking||"",signature:""})):t.delta.type==="text_delta"?n.push(C({type:"text",text:t.delta.text||""})):t.delta.type==="signature_delta"?n.push(C({type:"thinking",thinking:"",signature:t.delta.signature||""})):t.delta.type==="input_json_delta"&&o.push(G("","",t.delta.partial_json)));let r=B(n,o);return X(r,e)},me=(t,e)=>{let n=[],o=[];for(let s of t.candidates)if(s.content&&s.content.parts&&Array.isArray(s.content.parts))for(let a of s.content.parts)a.text?a.thought===!0?n.push(C({type:"thinking",thinking:a.text,signature:a.thoughtSignature||""})):n.push(C({type:"text",text:a.text})):a.functionCall&&o.push(G(a.functionCall.id||"",a.functionCall.name||"",a.functionCall.args||{}));let r=B(n,o);return X(r,e)},fe=(t,e)=>{let n=[],o=[];for(let s of t.choices)if(s.delta&&(s.delta.content&&n.push(C({type:"text",text:s.delta.content})),s.delta.tool_calls&&Array.isArray(s.delta.tool_calls)))for(let a of s.delta.tool_calls)a.function&&o.push(G(a.id||"",a.function.name||"",a.function.arguments||""));let r=B(n,o);return X(r,e)};import tt from"ably";var z="SET_WORKFLOW_COMPLETE";function nt(t,e,n){return m(this,null,function*(){let o=yield fetch(`${T}/workflow-version-execution-results?workflow_version_execution_id=${t}&return_all_outputs=${e}`,{headers:n});if(!o.ok)throw new Error("Failed to fetch final output");return o.json()})}function ot(t,e,n,o){return function(r){return m(this,null,function*(){if(r.name===z)try{let s=JSON.parse(r.data),a=s.result_code,c;if(a==="OK"||a==null)c=s.final_output;else if(a==="EXCEEDS_SIZE_LIMIT")c=yield nt(e,n,o);else throw new Error(`Unsupported final output code: ${a}`);t.resolve(c)}catch(s){t.reject(s)}})}}function rt(a){return m(this,arguments,function*({token:t,channelName:e,executionId:n,returnAllOutputs:o,headers:r,timeout:s}){let c=new tt.Realtime(t),i=c.channels.get(e),p={},u=new Promise((l,d)=>{p.resolve=l,p.reject=d}),f=ot(p,n,o,r);yield i.subscribe(z,f);try{return yield new Promise((l,d)=>{let w=setTimeout(()=>{d(new Error("Workflow execution did not complete properly (timeout)"))},s);u.then(_=>{clearTimeout(w),l(_)}).catch(_=>{clearTimeout(w),d(_)})})}finally{console.log("Closing client"),i.unsubscribe(z,f),c.close(),console.log("Closed client")}})}var T=process.env.PROMPTLAYER_API_URL||"https://api.promptlayer.com",ye=(t,e)=>m(void 0,null,function*(){return e.request_response[Symbol.asyncIterator]!==void 0?at(t,e.request_response,e):yield ge(t,e)}),ge=(t,e)=>m(void 0,null,function*(){try{let n=yield fetch(`${T}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)}),o=yield n.json();if(n.status!==200&&b(o,"WARNING: While logging your request, PromptLayer experienced the following error:"),o&&e.return_pl_id)return[e.request_response,o.request_id]}catch(n){console.warn(`WARNING: While logging your request PromptLayer had the following error: ${n}`)}return e.request_response}),_e=(t,e)=>m(void 0,null,function*(){try{let n=yield fetch(`${T}/library-track-metadata`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(y({},e),{api_key:t}))}),o=yield n.json();if(n.status!==200)return b(o,"WARNING: While logging metadata to your request, PromptLayer experienced the following error"),!1}catch(n){return console.warn(`WARNING: While logging metadata to your request, PromptLayer experienced the following error: ${n}`),!1}return!0}),we=(t,e)=>m(void 0,null,function*(){try{let n=yield fetch(`${T}/library-track-score`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(y({},e),{api_key:t}))}),o=yield n.json();if(n.status!==200)return b(o,"WARNING: While scoring your request, PromptLayer experienced the following error"),!1}catch(n){return console.warn(`WARNING: While scoring your request, PromptLayer experienced the following error: ${n}`),!1}return!0}),Pe=(t,e)=>m(void 0,null,function*(){try{let n=yield fetch(`${T}/library-track-prompt`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(y({},e),{api_key:t}))}),o=yield n.json();if(n.status!==200)return b(o,"WARNING: While associating your request with a prompt template, PromptLayer experienced the following error"),!1}catch(n){return console.warn(`WARNING: While associating your request with a prompt template, PromptLayer experienced the following error: ${n}`),!1}return!0}),Te=(t,e)=>m(void 0,null,function*(){try{let n=yield fetch(`${T}/track-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(R(y({},e),{api_key:t}))}),o=yield n.json();if(n.status!==200)return b(o,"WARNING: While associating your request with a group, PromptLayer experienced the following error"),!1}catch(n){return console.warn(`WARNING: While associating your request with a group, PromptLayer experienced the following error: ${n}`),!1}return!0}),Re=t=>m(void 0,null,function*(){try{let e=yield fetch(`${T}/create-group`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({api_key:t})}),n=yield e.json();return e.status!==200?(b(n,"WARNING: While creating a group PromptLayer had the following error"),!1):n.id}catch(e){return console.warn(`WARNING: While creating a group PromptLayer had the following error: ${e}`),!1}}),be=(t,e,n)=>m(void 0,null,function*(){try{let o=new URL(`${T}/prompt-templates/${e}`),r=yield fetch(o,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":t},body:JSON.stringify(n)}),s=yield r.json();return r.status!==200?(b(s,"WARNING: While fetching a prompt template PromptLayer had the following error"),null):(s.warning&&console.warn(`WARNING: While fetching your prompt PromptLayer had the following error: ${s.warning}`),s)}catch(o){return console.warn(`WARNING: While fetching a prompt template PromptLayer had the following error: ${o}`),null}}),Se=(t,e)=>m(void 0,null,function*(){try{let n=yield fetch(`${T}/rest/prompt-templates`,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":t},body:JSON.stringify({prompt_template:y({},e),prompt_version:y({},e),release_labels:e.release_labels?e.release_labels:void 0})}),o=yield n.json();return n.status===400&&b(o,"WARNING: While publishing a prompt template PromptLayer had the following error"),o}catch(n){console.warn(`WARNING: While publishing a prompt template PromptLayer had the following error: ${n}`)}}),Oe=(t,e)=>m(void 0,null,function*(){var n;try{let o=new URL(`${T}/prompt-templates`);Object.entries(e||{}).forEach(([a,c])=>o.searchParams.append(a,c.toString()));let r=yield fetch(o,{headers:{"Content-Type":"application/json","X-API-KEY":t}}),s=yield r.json();return r.status!==200?(b(s,"WARNING: While fetching all prompt templates PromptLayer had the following error"),null):(n=s.items)!=null?n:[]}catch(o){return console.warn(`WARNING: While fetching all prompt templates PromptLayer had the following error: ${o}`),null}}),Ee=i=>m(void 0,[i],function*({workflow_name:t,input_variables:e,metadata:n={},workflow_label_name:o=null,workflow_version_number:r=null,return_all_outputs:s=!1,api_key:a,timeout:c=36e5}){let p={input_variables:e,metadata:n,workflow_label_name:o,workflow_version_number:r,return_all_outputs:s},u={"X-API-KEY":a,"Content-Type":"application/json"};try{let f=yield fetch(`${T}/workflows/${encodeURIComponent(t)}/run`,{method:"POST",headers:u,body:JSON.stringify(p)});if(f.status!==201)return{success:!1,message:`Failed to run workflow: ${(yield f.json().catch(()=>({}))).error||f.statusText}`};let l=yield f.json();l.warning&&console.warn(`WARNING: ${l.warning}`);let d=l.workflow_version_execution_id;if(!d)return console.log("No execution ID returned from workflow run"),{success:!1,message:"Failed to run workflow"};let w=`workflow_updates:${d}`,S=(yield(yield fetch(`${T}/ws-token-request-library?capability=${w}`,{method:"POST",headers:u})).json()).token_details.token;return yield rt({token:S,channelName:w,executionId:d,returnAllOutputs:s,headers:u,timeout:c})}catch(f){throw console.error(`Failed to run workflow: ${f instanceof Error?f.message:f}`),f}}),H=t=>{var c,i,p,u,f,l,d,w,_,E;let e=null,n,o={id:"",choices:[],created:Date.now(),model:"",object:"chat.completion"},r=t.at(-1);if(!r)return o;let s;for(let S of t){if(S.choices.length===0)continue;let g=S.choices[0].delta;g.content&&(e=`${e||""}${g.content||""}`),g.function_call&&(n={name:`${n?n.name:""}${g.function_call.name||""}`,arguments:`${n?n.arguments:""}${g.function_call.arguments||""}`});let h=(c=g.tool_calls)==null?void 0:c[0];if(h){s=s||[];let P=s.at(-1);if(!P||h.id){s.push({id:h.id||"",type:h.type||"function",function:{name:((i=h.function)==null?void 0:i.name)||"",arguments:((p=h.function)==null?void 0:p.arguments)||""}});continue}P.function.name=`${P.function.name}${((u=h.function)==null?void 0:u.name)||""}`,P.function.arguments=`${P.function.arguments}${((f=h.function)==null?void 0:f.arguments)||""}`}}let a=t[0].choices.at(0);return o.choices.push({finish_reason:(l=a==null?void 0:a.finish_reason)!=null?l:"stop",index:(d=a==null?void 0:a.index)!=null?d:0,logprobs:(w=a==null?void 0:a.logprobs)!=null?w:null,message:{role:"assistant",content:e,function_call:n||void 0,tool_calls:s||void 0,refusal:(_=a==null?void 0:a.delta.refusal)!=null?_:null}}),o.id=r.id,o.model=r.model,o.created=r.created,o.system_fingerprint=r.system_fingerprint,o.usage=(E=r.usage)!=null?E:void 0,o},Ae=t=>{var i;let e={id:"",model:"",content:[],role:"assistant",type:"message",stop_reason:"stop_sequence",stop_sequence:null,usage:{input_tokens:0,output_tokens:0,cache_creation_input_tokens:0,cache_read_input_tokens:0,server_tool_use:null,service_tier:null}};if(!t.at(-1))return e;let o=null,r="",s="",a="",c="";for(let p of t)if(p.type==="message_start")e=y({},p.message);else if(p.type==="content_block_start")o=y({},p.content_block),o.type==="thinking"?(r="",s=""):o.type==="text"?a="":(o.type==="tool_use"||o.type==="server_tool_use")&&(c="");else if(p.type==="content_block_delta"&&o!==null){if(o.type==="thinking")"signature"in p.delta&&(r=p.delta.signature||""),"thinking"in p.delta&&(s+=p.delta.thinking||"");else if(o.type==="text")"text"in p.delta&&(a+=p.delta.text||"");else if((o.type==="tool_use"||o.type==="server_tool_use")&&p.delta.type==="input_json_delta"){let u=p.delta;c+=u.partial_json||""}}else if(p.type==="content_block_stop"&&o!==null){if(o.type==="thinking")o.signature=r,o.thinking=s;else if(o.type==="text")o.text=a,o.citations=null;else if(o.type==="tool_use"||o.type==="server_tool_use")try{o.input=c?JSON.parse(c):{}}catch(u){o.input={}}e.content.push(o),o=null,r="",s="",a="",c=""}else p.type==="message_delta"&&("usage"in p&&p.usage&&(e.usage=R(y({},e.usage),{output_tokens:(i=p.usage.output_tokens)!=null?i:0})),"delta"in p&&p.delta&&("stop_reason"in p.delta&&p.delta.stop_reason!==void 0&&(e.stop_reason=p.delta.stop_reason),"stop_sequence"in p.delta&&p.delta.stop_sequence!==void 0&&(e.stop_sequence=p.delta.stop_sequence)));return e},st=(t,e="openai.chat.completions.create")=>{if("completion"in t[0])return t.reduce((n,o)=>R(y({},o),{completion:`${n.completion}${o.completion}`}),{});if(e==="anthropic.messages.create")return Ae(t);if("text"in t[0].choices[0]){let n="";for(let r of t)n=`${n}${r.choices[0].text}`;let o=structuredClone(t.at(-1));return o.choices[0].text=n,o}if("delta"in t[0].choices[0]){let n=H(t);return n.choices[0]=y(y({},n.choices[0]),n.choices[0].message),n}return""};function at(t,e,n){return Y(this,null,function*(){let o=[];try{for(var a=V(e),c,i,p;c=!(i=yield new A(a.next())).done;c=!1){let u=i.value;yield n.return_pl_id?[u,null]:u,o.push(u)}}catch(i){p=[i]}finally{try{c&&(i=a.return)&&(yield new A(i.call(a)))}finally{if(p)throw p[0]}}let r=st(o,n.function_name),s=yield new A(ge(t,R(y({},n),{request_response:r,request_end_time:new Date().toISOString()})));if(s&&n.return_pl_id){let u=s[1];yield[o.at(-1),u]}})}var b=(t,e)=>{try{console.warn(`${e}: ${t.message}`)}catch(n){console.warn(`${e}: ${t}`)}},Ce=t=>m(void 0,null,function*(){try{let e=yield fetch(`${T}/track-request`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}),n=yield e.json();return e.status!==200&&b(n,"WARNING: While logging your request, PromptLayer experienced the following error:"),n}catch(e){console.warn(`WARNING: While logging your request PromptLayer had the following error: ${e}`)}return{}}),de=t=>{let e={id:"",choices:[{finish_reason:"stop",index:0,text:"",logprobs:null}],created:Date.now(),model:"",object:"text_completion"},n=t.at(-1);if(!n)return e;let o="";for(let r of t)r.choices.length>0&&r.choices[0].text&&(o=`${o}${r.choices[0].text}`);return e.choices[0].text=o,e.id=n.id,e.created=n.created,e.model=n.model,e.system_fingerprint=n.system_fingerprint,e.usage=n.usage,e},it=t=>{let e={completion:"",id:"",model:"",stop_reason:"",type:"completion"},n=t.at(-1);if(!n)return e;let o="";for(let r of t)o=`${o}${r.completion}`;return e.completion=o,e.id=n.id,e.model=n.model,e.stop_reason=n.stop_reason,e};function Ie(t,e,n,o){return Y(this,null,function*(){let r={request_id:null,raw_response:null,prompt_blueprint:null},s=[];try{for(var i=V(t),p,u,f;p=!(u=yield new A(i.next())).done;p=!1){let l=u.value;s.push(l),r.raw_response=l,l&&typeof l=="object"&&"type"in l&&(r.prompt_blueprint=ue(l,o)),l&&typeof l=="object"&&"candidates"in l&&(r.prompt_blueprint=me(l,o)),l&&typeof l=="object"&&"choices"in l&&(r.prompt_blueprint=fe(l,o)),yield r}}catch(u){f=[u]}finally{try{p&&(u=i.return)&&(yield new A(u.call(i)))}finally{if(f)throw f[0]}}let a=n(s),c=yield new A(e({request_response:a}));r.request_id=c.request_id,r.prompt_blueprint=c.prompt_blueprint,yield r})}var ct=(t,e)=>m(void 0,null,function*(){return t.chat.completions.create(e)}),pt=(t,e)=>m(void 0,null,function*(){return t.completions.create(e)}),ke={chat:ct,completion:pt},Ne=(t,e)=>m(void 0,null,function*(){let n=I("openai").default,o=new n({baseURL:e.baseURL,apiKey:e.apiKey}),r=ke[t.prompt_template.type];return r(o,e)}),xe=(t,e)=>m(void 0,null,function*(){let{AzureOpenAI:n}=I("openai"),o=new n({endpoint:process.env.AZURE_OPENAI_ENDPOINT||e.baseURL,apiVersion:process.env.OPENAI_API_VERSION||e.apiVersion,apiKey:process.env.AZURE_OPENAI_API_KEY||e.apiKey});e==null||delete e.baseURL,e==null||delete e.apiVersion,e==null||delete e.apiKey;let r=ke[t.prompt_template.type];return r(o,e)}),qe=(t,e)=>m(void 0,null,function*(){return t.messages.create(e)}),lt=(t,e)=>m(void 0,null,function*(){return t.completions.create(e)}),ut={chat:qe,completion:lt},Le=(t,e)=>m(void 0,null,function*(){let n=I("@anthropic-ai/sdk").default,o=new n({baseURL:e.baseURL}),r=ut[t.prompt_template.type];return r(o,e)}),ve=(t,e)=>m(void 0,null,function*(){try{let n=yield fetch(`${T}/log-request`,{method:"POST",headers:{"X-API-KEY":t,"Content-Type":"application/json"},body:JSON.stringify(e)}),o=yield n.json();return n.status!==201?(b(o,"WARNING: While logging your request PromptLayer had the following error"),null):o}catch(n){return console.warn(`WARNING: While tracking your prompt PromptLayer had the following error: ${n}`),null}}),mt=(t,e,n,o)=>{var a,c;let r=y({},o),s=[];if(t){let i={text:t,thought:!0};s.push(i)}if(e){let i={text:e,thought:null};s.push(i)}for(let i of n){let p={function_call:i};s.push(p)}return s.length>0&&((c=(a=r.candidates)==null?void 0:a[0])!=null&&c.content)&&(r.candidates[0].content.parts=s),r},Ge=t=>{var s,a;let{GenerateContentResponse:e}=I("@google/genai");if(!t.length)return new e;let n="",o="",r=[];for(let c of t)if(c.candidates&&((a=(s=c.candidates[0])==null?void 0:s.content)!=null&&a.parts))for(let i of c.candidates[0].content.parts)i.text?i.thought===!0?n+=i.text:o+=i.text:i.functionCall&&r.push(i.functionCall);return mt(n,o,r,t[t.length-1])},ft=t=>Ge(t),dt=t=>Ge(t),ht=(t,e)=>m(void 0,null,function*(){var a,c;let n=e==null?void 0:e.history,o=e==null?void 0:e.generationConfig,r=n.length>0?(a=n[n.length-1])==null?void 0:a.parts:"",s=t.chats.create({model:e==null?void 0:e.model,history:(c=n.slice(0,-1))!=null?c:[],config:o});return e!=null&&e.stream?yield s.sendMessageStream({message:r}):yield s.sendMessage({message:r})}),yt=(t,o)=>m(void 0,null,function*(){var r=o,{stream:e}=r,n=v(r,["stream"]);return e?yield t.generateContentStream(y({},n)):yield t.generateContent(y({},n))}),gt={chat:ht,completion:yt},Q=(t,e)=>m(void 0,null,function*(){let{GoogleGenAI:n}=yield import("@google/genai"),o=process.env.GOOGLE_API_KEY||process.env.GEMINI_API_KEY,r=process.env.VERTEX_AI_PROJECT_ID||process.env.GOOGLE_PROJECT_ID||process.env.GOOGLE_CLOUD_PROJECT,s=process.env.VERTEX_AI_PROJECT_LOCATION||process.env.GOOGLE_PROJECT_LOCATION||process.env.GOOGLE_CLOUD_PROJECT_LOCATION,a={keyFilename:process.env.GOOGLE_APPLICATION_CREDENTIALS,projectId:r,scopes:["https://www.googleapis.com/auth/cloud-platform"]},c=o?new n({apiKey:o}):new n({vertexai:!0,project:r,location:s,googleAuthOptions:a}),i=gt[t.prompt_template.type];return yield i(c,e)}),he=t=>t.replace(/_([a-z])/g,(e,n)=>n.toUpperCase()),Z=(t,e=new Set)=>!t||typeof t!="object"?t:Array.isArray(t)?t.map(n=>Z(n,e)):Object.fromEntries(Object.entries(t).map(([n,o])=>e.has(n)?[he(n),o]:[he(n),Z(o,e)])),_t=["openai","openai.azure"],wt={openai:{chat:{function_name:"openai.chat.completions.create",stream_function:H},completion:{function_name:"openai.completions.create",stream_function:de}},anthropic:{chat:{function_name:"anthropic.messages.create",stream_function:Ae},completion:{function_name:"anthropic.completions.create",stream_function:it}},"openai.azure":{chat:{function_name:"openai.AzureOpenAI.chat.completions.create",stream_function:H},completion:{function_name:"openai.AzureOpenAI.completions.create",stream_function:de}},google:{chat:{function_name:"google.convo.send_message",stream_function:ft},completion:{function_name:"google.model.generate_content",stream_function:dt}}},We=(t,e,n={},o=!1)=>{var c,i,p,u,f,l,d;let r=(p=e==null?void 0:e.client)!=null?p:(i=(c=t.metadata)==null?void 0:c.model)==null?void 0:i.provider;if(!r)throw new Error("Provider type not found in prompt blueprint or custom provider");let s=R(y(y({},t.llm_kwargs||{}),n),{stream:o});["google","vertexai"].includes(r)&&((f=(u=t.metadata)==null?void 0:u.model)!=null&&f.name.startsWith("gemini"))&&(s=Z(s,new Set(["function_declarations"])));let a={baseURL:(d=e==null?void 0:e.base_url)!=null?d:(l=t.provider_base_url)==null?void 0:l.url,apiKey:e==null?void 0:e.api_key};return Object.entries(a).forEach(([w,_])=>{_!==void 0&&(s[w]=_)}),o&&_t.includes(r)&&(s.stream_options={include_usage:!0}),{provider_type:r,kwargs:s}},$e=(t,e)=>{let n=wt[t];if(!n)throw new Error(`Unsupported provider type: ${t}`);let o=e.type,r=n[o];if(!r)throw new Error(`Unsupported template type '${e.type}' for provider '${t}'`);return r},Me=(t,e)=>m(void 0,null,function*(){var o;let n=(o=t.metadata)==null?void 0:o.model;if(!n)throw new Error("Model metadata not found in prompt blueprint");if(n.name.startsWith("gemini"))return Q(t,e);if(n.name.startsWith("claude")){let{AnthropicVertex:r}=yield import("@anthropic-ai/vertex-sdk"),s=new r({baseURL:e.baseURL});if(t.prompt_template.type==="chat")return qe(s,e);throw new Error(`Unsupported prompt template type '${t.prompt_template.type}' for Anthropic Vertex AI`)}throw new Error(`Unsupported model name '${n.name}' for Vertex AI request`)});var W=class{constructor(e){this.create=()=>Re(this.apiKey);this.apiKey=e}};import*as Ke from"@opentelemetry/api";import{SimpleSpanProcessor as Pt}from"@opentelemetry/sdk-trace-base";import{NodeTracerProvider as Tt}from"@opentelemetry/sdk-trace-node";import{SpanKind as q,SpanStatusCode as ee}from"@opentelemetry/api";import{ExportResultCode as $}from"@opentelemetry/core";var te=class{constructor(e,n){this.apiKey=n||process.env.PROMPTLAYER_API_KEY,this.enableTracing=e,this.url=`${T}/spans-bulk`}attributesToObject(e){return e?Object.fromEntries(Object.entries(e)):{}}spanKindToString(e){return{[q.INTERNAL]:"SpanKind.INTERNAL",[q.SERVER]:"SpanKind.SERVER",[q.CLIENT]:"SpanKind.CLIENT",[q.PRODUCER]:"SpanKind.PRODUCER",[q.CONSUMER]:"SpanKind.CONSUMER"}[e]||"SpanKind.INTERNAL"}statusCodeToString(e){return{[ee.ERROR]:"StatusCode.ERROR",[ee.OK]:"StatusCode.OK",[ee.UNSET]:"StatusCode.UNSET"}[e]||"StatusCode.UNSET"}toNanoseconds(e){return(BigInt(e[0])*BigInt(1e9)+BigInt(e[1])).toString()}export(e){if(!this.enableTracing)return Promise.resolve($.SUCCESS);let n=e.map(o=>{var r;return{name:o.name,context:{trace_id:o.spanContext().traceId,span_id:o.spanContext().spanId,trace_state:((r=o.spanContext().traceState)==null?void 0:r.serialize())||""},kind:this.spanKindToString(o.kind),parent_id:o.parentSpanId||null,start_time:this.toNanoseconds(o.startTime),end_time:this.toNanoseconds(o.endTime),status:{status_code:this.statusCodeToString(o.status.code),description:o.status.message},attributes:this.attributesToObject(o.attributes),events:o.events.map(s=>({name:s.name,timestamp:this.toNanoseconds(s.time),attributes:this.attributesToObject(s.attributes)})),links:o.links.map(s=>({context:s.context,attributes:this.attributesToObject(s.attributes)})),resource:{attributes:R(y({},o.resource.attributes),{"service.name":"prompt-layer-js"}),schema_url:""}}});return fetch(this.url,{method:"POST",headers:{"Content-Type":"application/json","X-API-KEY":this.apiKey||""},body:JSON.stringify({spans:n})}).then(o=>o.ok?$.SUCCESS:(console.error(`Error exporting spans
|
|
2
|
+
HTTP error! status: ${o.status}`),$.FAILED)).catch(o=>(console.error("Error exporting spans:",o),$.FAILED))}shutdown(){return Promise.resolve()}},je=te;var k=(t="promptlayer-tracer")=>Ke.trace.getTracer(t),Ue=(t,e)=>{let n=new Tt,o=new je(t,e),r=new Pt(o);n.addSpanProcessor(r),n.register()};var Rt=k(),ne=(t,e,n="",o="openai")=>{let r={construct:(s,a)=>{let c=Reflect.construct(s,a);return Object.defineProperties(c,{function_name:{value:n,writable:!0},provider:{value:o}}),new Proxy(c,r)},get:(s,a,c)=>{let i=s[a],p=`${Reflect.get(s,"function_name")}.${a.toString()}`;return typeof i=="object"?(Object.defineProperties(i,{function_name:{value:p,writable:!0},provider:{value:o}}),new Proxy(i,r)):typeof i=="function"?(...u)=>{var _,E,S,g;let f=new Date().toISOString(),l=Reflect.get(s,"provider"),d=(_=u[0])==null?void 0:_.return_pl_id,w=(E=u[0])==null?void 0:E.pl_tags;return(S=u[0])==null||delete S.return_pl_id,(g=u[0])==null||delete g.pl_tags,Rt.startActiveSpan(`${l}.${p}`,h=>m(void 0,null,function*(){try{h.setAttribute("function_input",JSON.stringify(u));let P=Reflect.apply(i,s,u),N=h.spanContext().spanId;return P instanceof Promise?new Promise((U,J)=>{P.then(O=>m(void 0,null,function*(){let x=yield ye(t,{api_key:t,provider_type:l,function_name:p,request_start_time:f,request_end_time:new Date().toISOString(),request_response:O,kwargs:u[0],return_pl_id:d,tags:w,span_id:N});h.setAttribute("function_output",JSON.stringify(x)),h.setAttribute("response_status","success"),h.end(),U(x)})).catch(O=>{h.recordException(O),h.setAttribute("response_status","error"),h.end(),J(O)})}):(h.setAttribute("function_output",JSON.stringify(P)),h.setAttribute("response_status","success"),h.end(),P)}catch(P){throw h.recordException(P),h.setAttribute("response_status","error"),h.end(),P}}))}:Reflect.get(s,a,c)}};return new Proxy(e,r)};import*as M from"@opentelemetry/api";var De=(t,e,n)=>function(...o){let r=k(),s=a=>{try{n&&Object.entries(n).forEach(([i,p])=>{a.setAttribute(i,p)}),a.setAttribute("function_input",JSON.stringify(o));let c=e(...o);return c instanceof Promise?c.then(i=>(a.setAttribute("function_output",JSON.stringify(i)),a.setStatus({code:M.SpanStatusCode.OK}),i)).catch(i=>{throw Je(a,i,o),i}).finally(()=>a.end()):(a.setAttribute("function_output",JSON.stringify(c)),a.setStatus({code:M.SpanStatusCode.OK}),a.end(),c)}catch(c){throw Je(a,c,o),c}};return r.startActiveSpan(t,s)},Je=(t,e,n)=>{t.setAttribute("function_input",JSON.stringify(n)),t.setStatus({code:M.SpanStatusCode.ERROR,message:e instanceof Error?e.message:"Unknown error"}),t.end()};var j=class{constructor(e){this.get=(e,n)=>be(this.apiKey,e,n);this.publish=e=>Se(this.apiKey,e);this.all=e=>Oe(this.apiKey,e);this.apiKey=e}};var bt=(t,e)=>{if(!(e.metadata instanceof Object))throw new Error("Please provide a dictionary of metadata.");for(let[n,o]of Object.entries(e.metadata))if(typeof n!="string"||typeof o!="string")throw new Error("Please provide a dictionary of metadata with key value pair of strings.");return _e(t,e)},St=(t,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 we(t,e)},Ot=(t,e)=>{if(!(e.prompt_input_variables instanceof Object))throw new Error("Prompt template input variable dictionary not provided.");return Pe(t,e)},Et=(t,e)=>Te(t,e),K=class{constructor(e){this.group=e=>Et(this.apiKey,e);this.metadata=e=>bt(this.apiKey,e);this.prompt=e=>Ot(this.apiKey,e);this.score=e=>St(this.apiKey,e);this.apiKey=e}};import*as Ye from"@opentelemetry/api";var At={openai:Ne,anthropic:Le,"openai.azure":xe,google:Q,vertexai:Me},Ct=t=>{if(!t||typeof t!="object"||Array.isArray(t))return!1;let e=["status","value","error_message","raw_error_message","is_output_node"];return Object.values(t).every(o=>typeof o!="object"||o===null?!1:e.every(r=>r in o))},Fe=class{constructor({apiKey:e=process.env.PROMPTLAYER_API_KEY,enableTracing:n=!1}={}){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.enableTracing=n,this.templates=new j(e),this.group=new W(e),this.track=new K(e),this.wrapWithSpan=De,n&&Ue(n,e)}get Anthropic(){try{let e=I("@anthropic-ai/sdk").default;return ne(this.apiKey,e,"anthropic","anthropic")}catch(e){console.error("To use the Anthropic module, you must install the @anthropic-ai/sdk package.")}}get OpenAI(){try{let e=I("openai").default;return ne(this.apiKey,e,"openai","openai")}catch(e){console.error("To use the OpenAI module, you must install the @openai/api package.")}}run(l){return m(this,arguments,function*({promptName:e,promptVersion:n,promptReleaseLabel:o,inputVariables:r,tags:s,metadata:a,groupId:c,modelParameterOverrides:i,stream:p=!1,provider:u,model:f}){return k().startActiveSpan("PromptLayer Run",w=>m(this,null,function*(){try{let _={promptName:e,promptVersion:n,promptReleaseLabel:o,inputVariables:r,tags:s,metadata:a,groupId:c,modelParameterOverrides:i,stream:p};w.setAttribute("function_input",JSON.stringify(_));let E=r,S={label:o,version:n,metadata_filters:a,provider:u,model:f};r&&(S.input_variables=r);let g=yield this.templates.get(e,S);if(!g)throw new Error("Prompt not found");let h=g.prompt_template;g.llm_kwargs||console.warn(`Prompt '${e}' does not have any LLM kwargs associated with it. Please set your model parameters in the registry in the PromptLayer dashbaord.`);let P=g.metadata;if(!P)throw new Error(`Prompt '${e}' does not have any metadata associated with it.`);let N=P.model;if(!N)throw new Error(`Prompt '${e}' does not have a model parameters associated with it.`);let U=g.custom_provider,J=new Date().toISOString(),{provider_type:O,kwargs:x}=We(g,U,i,p),D=O;N.name.startsWith("gemini")?D="google":N.name.startsWith("claude")&&(D="anthropic");let Ve=$e(D,h),{function_name:Be,stream_function:Xe}=Ve,oe=At[O];if(!oe)throw new Error(`No request function found for provider: ${O}`);let F=yield oe(g,x),re=ze=>{let He=new Date().toISOString();return Ce(y({function_name:Be,provider_type:O,args:[],kwargs:x,tags:s,request_start_time:J,request_end_time:He,api_key:this.apiKey,metadata:a,prompt_id:g.id,prompt_version:g.version,prompt_input_variables:E,group_id:c,return_prompt_blueprint:!0,span_id:w.spanContext().spanId},ze))};if(p)return Ie(F,re,Xe,a);let se=yield re({request_response:F}),ae={request_id:se.request_id,raw_response:F,prompt_blueprint:se.prompt_blueprint};return w.setAttribute("function_output",JSON.stringify(ae)),ae}catch(_){throw w.setStatus({code:Ye.SpanStatusCode.ERROR,message:_ instanceof Error?_.message:"Unknown error"}),_}finally{w.end()}}))})}runWorkflow(c){return m(this,arguments,function*({workflowName:e,inputVariables:n={},metadata:o={},workflowLabelName:r=null,workflowVersion:s=null,returnAllOutputs:a=!1}){try{let i=yield Ee({workflow_name:e,input_variables:n,metadata:o,workflow_label_name:r,workflow_version_number:s,return_all_outputs:a,api_key:this.apiKey});if(!a&&Ct(i)){let u=Object.values(i).filter(l=>l.is_output_node===!0);if(u.length===0)throw new Error(JSON.stringify(i,null,2));if(!u.some(l=>l.status==="SUCCESS"))throw new Error(JSON.stringify(i,null,2))}return i}catch(i){throw i instanceof Error?(console.error("Error running workflow:",i.message),new Error(`Error running workflow: ${i.message}`)):(console.error("Unknown error running workflow:",i),new Error("Unknown error running workflow"))}})}logRequest(e){return m(this,null,function*(){return ve(this.apiKey,e)})}};export{Fe as PromptLayer};
|
|
3
3
|
//# sourceMappingURL=index.js.map
|