UNPKG

dalal

Version:

A CLI Tool for Interacting with Ollama Language Models

27 lines (22 loc) 2.37 kB
import{a as d,b as h}from"./chunk-UWMHM37F.js";import k from"node:fs";import{parseArgs as D}from"node:util";var f="dalal",E="0.0.1-canary.15";var $={name:"Neeraj Dalal",email:"admin@nrjdalal.com",url:"https://nrjdalal.com"};import{glob as j}from"tinyglobby";var u=async({patterns:s=["**"],ignore:e=[]}={})=>{let l=[".DS_Store","*.lock","**/*.map","LICENSE*"],i=[".git","node_modules","dist"];e=Array.isArray(e)?e:[e],e=[...l,...i,...e];let n=Array.isArray(s)?s:[s];return e=e.filter(p=>!n.some(a=>a===p)),await j(n,{ignore:e})};var y=`ollama (https://ollama.com) Description: Get up and running with large language models Usage: $ ${f} ollama <prompt> [options] Options: -b, --base Base URL Default: http://localhost:11434 -d, --dir Prefix <prompt> with files in CWD -l, --list List local models -m, --model The model name -h, --help Display help message -v, --version Display version`,N=async s=>{try{let{values:e,positionals:l}=D({allowPositionals:!0,options:{dir:{type:"boolean",short:"d"},base:{type:"string",short:"b"},list:{type:"boolean",short:"l"},model:{type:"string"},help:{type:"boolean",short:"h"},version:{type:"boolean",short:"v"}},args:s});l.length||(e.list&&console.log(await d()),e.version&&console.log(await h()),e.help&&console.log(y),process.exit(0));let i=e.model;if(!e.model){let t=await d();if(!t.models.length)throw new Error("No model found!");i=t.models[0].name}console.log(`Generating text with model: ${i} `);let n="";if(e.dir){let t=await u();console.log(` Files attached:`,t);for(let r of t)n+=` --- ${r} --- ${k.readFileSync(r,"utf-8")}`;console.log(` `)}let p=n+l.join(" "),a=await fetch((e.base||"http://localhost:11434")+"/api/generate",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({model:i,prompt:p,stream:!0})});if(!a.body)throw new Error("Response body is null");let b=a.body.getReader(),w=new TextDecoder("utf-8"),o="";for(;;){let{done:t,value:r}=await b.read();if(t)break;let x=w.decode(r,{stream:!0});o+=x;let c=o.indexOf(` `);for(;c!==-1;){let v=o.slice(0,c);o=o.slice(c+1);let g=JSON.parse(v);g.response&&process.stdout.write(g.response),c=o.indexOf(` `)}}process.exit(0)}catch(e){console.error(y),console.error(` ${e.message} `),process.exit(1)}};process.argv[2]!=="ollama"&&N(process.argv.slice(2));export{f as a,E as b,$ as c,N as d};