Skip to content

AI-powered number operations `npm` package using OpenAI, Groq, and other LLM providers. Generate random numbers, discover patterns, and perform intelligent math operations.

License

Notifications You must be signed in to change notification settings

hozaifa4you/number-ai

Repository files navigation

number-ai

npm version License: MIT

AI-powered number operations using OpenAI, Groq, and other LLM providers.

Install

npm install number-ai

Usage

OpenAI

import { NumberAiWithOpenAi } from 'number-ai'

const ai = new NumberAiWithOpenAi()

// Generate random integer
const result = await ai.randomInt(1, 100)
console.log(result) // { num: 42 }

Groq

import { NumberAiWithGroq } from 'number-ai'

const ai = new NumberAiWithGroq()

const result = await ai.randomInt(1, 100)
console.log(result) // { num: 87 }

With Environment Variables

// Set OPENAI_API_KEY or GROQ_API_KEY in .env
const ai = new NumberAiWithOpenAi() // uses process.env.OPENAI_API_KEY

Custom Model & pass api key

// OpenAI with custom model
const ai = new NumberAiWithOpenAi({
	apiKey: '<your api key>',
	model: '<model>', // default: gpt-4o-mini
})

// Groq with custom model
const ai = new NumberAiWithGroq({
	apiKey: '<your api key>',
	model: '<model>', // default: openai/gpt-oss-20b
})

APIs

randomInt(min?, max?)

Generate a random integer using AI.

Parameters:

  • min (optional) - Minimum value (inclusive)
  • max (optional) - Maximum value (inclusive)

Returns: Promise<{ num?: number, error?: string }>

Examples:

// Between 1 and 100
await ai.randomInt(1, 100)
// { num: 42 }

// Greater than or equal to 50
await ai.randomInt(50)
// { num: 73 }

// Less than or equal to 20
await ai.randomInt(undefined, 20)
// { num: 15 }

// Any random integer
await ai.randomInt()
// { num: -127 }

// Error handling
const result = await ai.randomInt(1, 10)
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Number:', result.num)
}

randomFloat(min?, max?)

Generate a random float using AI.

Parameters:

  • min (optional) - Minimum value (inclusive)
  • max (optional) - Maximum value (inclusive)

Returns: Promise<{ num?: number, error?: string }>

Examples:

// Between 1 and 100
await ai.randomFloat(1, 100)
// { num: 42.50 }

// Greater than or equal to 50
await ai.randomFloat(50)
// { num: 73.92 }

// Less than or equal to 20
await ai.randomFloat(undefined, 20)
// { num: 15.64 }

// Any random float
await ai.randomFloat()
// { num: -127.65 }

// Error handling
const result = await ai.randomFloat(1, 10)
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Number:', result.num)
}

randomIntArray(count!, min?, max?)

Generate a random array integer.

Parameters:

  • count (required) - Count/length of array
  • min (optional) - Minimum value (inclusive)
  • max (optional) - Maximum value (inclusive)

Returns: Promise<{ nums?: [number], error?: string }>

Examples:

// Between 1 and 100
await ai.randomIntArray(5, 1, 100)
// { nums: [42, 10, 1, 5, 11] }

// Greater than or equal to 50
await ai.randomIntArray(6, 50)
// { nums: [73, 60, 69, 99, 954, 847727432] }

// Less than or equal to 20
await ai.randomIntArray(10, undefined, 20)
// { nums: [15, 1, 2, 4, 9, 19, 6, 11, 18, 14]}

// Any random integer
await ai.randomIntArray(3)
// { nums: [93764362, 9133, 9]}

// Error handling
const result = await ai.randomIntArray(10, 1, 10)
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Number:', result.nums)
}

randomFloatArray(count!, min?, max?)

Generate a random array float.

Parameters:

  • count (required) - Count/length of array
  • min (optional) - Minimum value (inclusive)
  • max (optional) - Maximum value (inclusive)

Returns: Promise<{ nums?: [number], error?: string }>

Examples:

// Between 1 and 100
await ai.randomFloatArray(5, 1, 100)
// { nums: [42.85, 10.85, 1.875, 5.85, 11.85] }

// Greater than or equal to 50
await ai.randomFloatArray(6, 50)
// { nums: [73.74, 60.10, 69.82, 99.04, 954.84, 847727432.19] }

// Less than or equal to 20
await ai.randomFloatArray(10, undefined, 20)
// { nums: [15.31, 1.10, 2.84, 4.95, 9.30, 19.95, 6.20, 11.65, 18.82, 14.45]}

// Any random integer
await ai.randomFloatArray(3)
// { nums: [93764362.84, 9133.34, 9.01]}

// Error handling
const result = await ai.randomFloatArray(10, 1, 10)
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Number:', result.nums)
}

isPrime(n!)

Check is the integer is prime number or not.

Parameters:

  • n (required) - Which number want to check

Returns: Promise<{ is_prime?: number, error?: string }>

Examples:

// Is prime
await ai.isPrime(10)
// { is_prime: true }

// Is not prime
await ai.isPrime(5)
// { is_prime: false }

// Error handling
const result = await ai.isPrime(10)
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Number:', result.is_prime)
}

describeNumber(n!)

User gives a number → get a human-readable description

Parameters:

  • n (required) - Which number want to describe

Returns: Promise<{ description?: string, error?: string }>

Examples:

// Is prime
await ai.describeNumber(19)
// { description: "<details about 19>" }

// Error handling
const result = await ai.describeNumber(19)
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Number:', result.description)
}

patternDetection(pattern!)

User gives a number → get a human-readable description

Parameters:

  • pattern: number[] | string[] - (required) - Pattern you want to detect

Returns: Promise<{ pattern?: string, error?: string }>

Examples:

// Is prime
await ai.patternDetection([1, 3, 4, 5, 7])
// { pattern: "<details about the given pattern>" }

// Error handling
const result = await ai.patternDetection([2, 4, 6])
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Number:', result.pattern)
}

unitConversion(value!, from!, to!)

Convert unit from an unit to other unit if possible.

Parameters:

  • value: number | string - (Required) The numeric value to convert.
  • from: string - (Required) The unit to convert from.
  • to: string - (Required) The unit to convert to.

Returns: Promise<{ value?: string, from?: string, to?:string, error?: string }>

Examples:

// Possible unit conversion
await ai.unitConversion(100, 'cm', 'meter')
// { value: 1, from: "cm", to: "meter" }

// Impossible unit conversion
await ai.unitConversion(100, 'kg', 'meter')
// { value: "<AI error message>", from: "kg", to: "meter" }

// Error handling
const result = await ai.unitConversion(10, 'cm', 'inch')
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Number:', result.value, 'From:', result.from, 'To:', result.to)
}

patternGenerator(pattern!, from?, to?)

Generate a number/string pattern based on user input.

Parameters:

  • pattern: string - (Required) The pattern to generate.
  • from: number - (Optional) The start of the range.
  • to: number - (Optional) The end of the range.

Returns: Promise<{ sequence?: (number | string)[], error?: string }>

Examples:

// Fibonacci sequence from 1 to 10
await ai.patternGenerator('fibonacci', 1, 10)
// { sequence: [1, 1, 2, 3, 5, 8]}

// Error handling
const result = await ai.patternGenerator('fibonacci', 1, 10)
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Number:', result.sequence)
}

arithmeticOperation(operand1!, operator!, operand2?)

Perform arithmetic operations using AI.

Parameters:

  • operand1: number | string - (Required) The first operand.
  • operator: ArithmeticOperator - (Required) The arithmetic operator. Supported operators: '+', '-', '*', '/', '%', '^', 'log', 'sqrt', 'abs', 'sin', 'cos', 'tan', 'mod', 'floor', 'ceil', 'round', 'min', 'max'.
  • operand2: number | string - (Optional) The second operand. Required for binary operators (+, -, *, /, %, ^, mod, min, max). Not required for unary operators (sqrt, abs, sin, cos, tan, floor, ceil, round, log).

Returns: Promise<{ result?: number, error?: string }>

Examples:

// Addition
await ai.arithmeticOperation(10, '+', 5)
// { result: 15 }

// Square root (unary operator)
await ai.arithmeticOperation(16, 'sqrt')
// { result: 4 }

// Power
await ai.arithmeticOperation(2, '^', 3)
// { result: 8 }

// Trigonometric function
await ai.arithmeticOperation(90, 'sin')
// { result: 1 }

// Error handling
const result = await ai.arithmeticOperation(10, '+', 5)
if (result.error) {
	console.error('Error:', result.error)
} else {
	console.log('Result:', result.result)
}

TypeScript

Full TypeScript support included:

import type {
	ArithmeticOperationResponse,
	ArithmeticOperator,
	DescribeNumberResponse,
	IsPrimeResponse,
	LLMOptions,
	PatternDetectionResponse,
	PatternGenerator,
	RandomFloatArrayResponse,
	RandomFloatResponse,
	RandomIntArrayResponse,
	RandomIntResponse,
	UnitConversionResponse,
} from 'number-ai'

const result: RandomIntResponse = await ai.randomInt(1, 100)
const operation: ArithmeticOperationResponse = await ai.arithmeticOperation(10, '+', 5)

License

MIT

About

AI-powered number operations `npm` package using OpenAI, Groq, and other LLM providers. Generate random numbers, discover patterns, and perform intelligent math operations.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published