1"use strict";(self.webpackChunkintellidocs=self.webpackChunkintellidocs||[]).push([[8726],{9600:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>l,contentTitle:()=>s,default:()=>p,frontMatter:()=>a,metadata:()=>r,toc:()=>c});var i=t(4848),o=t(8453);const a={sidebar_position:4,title:"Compare and Evaluate LLMs in Node.js",sidebar_label:"LLM evaluation",description:"Learn to compare LLM responses in Node.js with IntelliNode using cosine similarity and Euclidean distance across OpenAI, Cohere, Replicate, and Gemini.",keywords:["node.js llm evaluation","intellinode llm evaluation","compare llm responses","cosine similarity llm","euclidean distance llm","openai cohere replicate gemini"]},s="LLM evaluation",r={id:"npm/functions/llm-evaluation",title:"Compare and Evaluate LLMs in Node.js",description:"Learn to compare LLM responses in Node.js with IntelliNode using cosine similarity and Euclidean distance across OpenAI, Cohere, Replicate, and Gemini.",source:"@site/docs/npm/functions/llm-evaluation.md",sourceDirName:"npm/functions",slug:"/npm/functions/llm-evaluation",permalink:"/docs/npm/functions/llm-evaluation",draft:!1,unlisted:!1,editUrl:"https://github.com/intelligentnode/docs/edit/main/intellidocs/docs/npm/functions/llm-evaluation.md",tags:[],version:"current",sidebarPosition:4,frontMatter:{sidebar_position:4,title:"Compare and Evaluate LLMs in Node.js",sidebar_label:"LLM evaluation",description:"Learn to compare LLM responses in Node.js with IntelliNode using cosine similarity and Euclidean distance across OpenAI, Cohere, Replicate, and Gemini.",keywords:["node.js llm evaluation","intellinode llm evaluation","compare llm responses","cosine similarity llm","euclidean distance llm","openai cohere replicate gemini"]},sidebar:"npmSidebar",previous:{title:"Text analyzer",permalink:"/docs/npm/functions/text-analyzer"},next:{title:"Support ticket triage",permalink:"/docs/npm/use-cases/support-triage"}},l={},c=[{value:"Usage",id:"usage",level:3}];function d(e){const n={code:"code",h1:"h1",h3:"h3",p:"p",pre:"pre",strong:"strong",...(0,o.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.h1,{id:"llm-evaluation",children:"LLM evaluation"}),"\n",(0,i.jsx)(n.p,{children:"The Language Model Evaluation feature is used to compare different Language Learning Models (LLMs) such as ChatGPT, LLaMA, Cohere, etc. The evaluation is performed by comparing the cosine similarity and Euclidean distance scores of predicted responses against an array of target answers."}),"\n",(0,i.jsx)(n.h3,{id:"usage",children:"Usage"}),"\n",(0,i.jsxs)(n.p,{children:["Here is an example to demonstrate the usage of LLM Evaluation. In this case, we are comparing ",(0,i.jsx)(n.strong,{children:"OpenAI's ChatGPT"})," language model with ",(0,i.jsx)(n.strong,{children:"Cohere's command"})," completion model:"]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.strong,{children:"Step 1: Imports"})," ",(0,i.jsx)("br",{}),"\nThe first thing is to import the required modules from the IntelliNode library. The main module that we need for this is ",(0,i.jsx)(n.code,{children:"LLMEvaluation"}),", along with ",(0,i.jsx)(n.code,{children:"SupportedChatModels"})," and ",(0,i.jsx)(n.code,{children:"SupportedLangModels"}),"."]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-javascript",children:"const { LLMEvaluation, SupportedChatModels, SupportedLangModels } = require('intellinode');\n"})}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.strong,{children:"Step 2: Create Providers Set"})," ",(0,i.jsx)("br",{}),"\nCreate a list of the chat or language models you want to evaluate with each provider\u2019s API key, the provider name, the model name, and the maximum tokens that the model is allowed to generate in a single call."]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-javascript",children:"const openaiChat = { \n apiKey: openaiKey, \n provider: SupportedChatModels.OPENAI, \n type: 'chat', \n model: 'gpt-3.5-turbo', \n maxTokens: 100\n};\n\nconst cohereCompletion = { \n apiKey: cohereKey, \n provider: SupportedLangModels.COHERE, \n type: 'completion', \n model: 'command', \n maxTokens: 100 \n};
1\n\nconst llamaChat = {\n apiKey: replicateKey, \n provider: SupportedChatModels.REPLICATE,\n type:'chat', \n model: '13b-chat', \n maxTokens: 100\n}\n\n\nconst geminiChat = {\n apiKey: geminiKey, \n provider: SupportedChatModels.GEMINI,\n type: 'chat', \n model: 'gemini'\n};\n\n// the models set to compare\nconst providerSets = [openaiChat, cohereCompletion, llamaChat, geminiChat];\n"})}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.strong,{children:"Step 3: Call LLMEvaluation"})," ",(0,i.jsx)("br",{}),"\nCreate an instance of LLMEvaluation and provide it with the models set:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-javascript",children:'// create the LLMEvaluation with the desired embedding algorithm for unified comparison.\nconst llmEvaluation = new LLMEvaluation(openaiKey, \'openai\');\n\n// the model input\nconst inputString = "Explain the process of photosynthesis in simple terms.";\n\n// target answers to calculate the average distance\nconst targetAnswers = [\n "Photosynthesis is the process where green plants use sunlight to...",\n "Photosynthesis is how plants make their own food. They take in water..."\n];\n\n// execute the evaluation\nconst results = await llmEvaluation.compareModels(inputString, targetAnswers, providerSets);\n'})}),"\n",(0,i.jsx)(n.p,{children:"The function will return an object that contains the prediction from each model, along with its cosine similarity score and euclidean distance score compared to the target answers."}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{children:"{\n 'openai/gpt-3.5-turbo': [\n {\n prediction: 'Photosynthesis is how plants make food for themselves....',\n score_cosine_similarity: 0.9566836802012463,\n score_euclidean_distance: 0.29175853870023755\n }\n ],\n 'replicate/13b-chat': [\n prediction: \"Here's an explanation of photosynthesis in simple terms .....\",\n score_cosine_similarity: 0.9096764395396765,\n score_euclidean_distance: 0.4248874961328429\n ],\n ...\n lookup: {\n cosine_similarity: 'a value closer to 1 indicates a higher degree of similarity between two vectors',\n euclidean_distance: 'the lower the value, the closer the two points'\n }\n}\n"})})]})}function p(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(d,{...e})}):d(e)}},8453:(e,n,t)=>{t.d(n,{R:()=>s,x:()=>r});var i=t(6540);const o={},a=i.createContext(o);function s(e){const n=i.useContext(a);return i.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:s(e.components),i.createElement(a.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.