Skip to main content
Early Beta — The Web SDK is in early beta. APIs may change between releases.

Overview

The TextGeneration.generate() method with default options provides the simplest way to generate text. For a one-line approach, just pass a prompt and get a result.

Basic Usage

import { TextGeneration } from '@runanywhere/web'

const result = await TextGeneration.generate('What is the capital of France?')
console.log(result.text) // "Paris is the capital of France."

API Reference

await TextGeneration.generate(
  prompt: string,
  options?: LLMGenerationOptions
): Promise<LLMGenerationResult>

Parameters

ParameterTypeDescription
promptstringThe user’s message or question
optionsLLMGenerationOptionsOptional generation settings

Returns

TypeDescription
Promise<LLMGenerationResult>Result with text and performance metrics

Throws

Error CodeDescription
NotInitializedSDK not initialized
ModelNotLoadedNo LLM model loaded
GenerationFailedGeneration failed

Examples

Simple Q&A

const capital = await TextGeneration.generate('What is the capital of Japan?')
// capital.text: "Tokyo is the capital of Japan."

const math = await TextGeneration.generate('Calculate 15% of 200')
// math.text: "15% of 200 is 30."

With Options

const result = await TextGeneration.generate('Write a haiku about coding', {
  maxTokens: 50,
  temperature: 0.8,
})

console.log(result.text)
console.log(`${result.tokensPerSecond.toFixed(1)} tok/s | ${result.latencyMs}ms`)

Error Handling

import { TextGeneration, SDKError, SDKErrorCode } from '@runanywhere/web'

try {
  const result = await TextGeneration.generate('Hello')
} catch (err) {
  if (err instanceof SDKError) {
    switch (err.code) {
      case SDKErrorCode.NotInitialized:
        console.error('Initialize the SDK first')
        break
      case SDKErrorCode.ModelNotLoaded:
        console.error('Load a model first')
        break
      default:
        console.error(`SDK error [${err.code}]: ${err.message}`)
    }
  }
}

Simple vs Full Generation

FeatureSimple (defaults)Full (generate() with options)
Return typeLLMGenerationResultLLMGenerationResult
MetricsYesYes
OptionsDefaultsCustomizable
System promptNoneYes
Use caseQuick prototypingProduction apps
For quick prototyping, use generate() with just a prompt. Switch to adding options when you need custom temperature, system prompts, or token limits.