Modules@robinpath/ai
ai

@robinpath/ai

0.1.2Node.jsPublic

LLM integration for OpenAI, Anthropic, and compatible APIs

AI

LLM integration: chat, complete, summarize, extract, classify, translate, sentiment analysis, and embeddings

Package: @robinpath/ai | Category: Ai | Type: Integration

Authentication

ai.configure "openai" {"provider": "openai", "apiKey": $key}

Call this once at the start of your script before using any other function. Credentials persist for the duration of the script execution.

Use Cases

Use the ai module when you need to:

  • Send a chat message and get a response -- Use ai.chat to perform this operation
  • Get a simple text completion (returns just the text) -- Use ai.complete to perform this operation
  • Summarize text using AI -- Use ai.summarize to perform this operation
  • Extract structured data from text using AI -- Use ai.extract to perform this operation
  • Classify text into one of given categories -- Use ai.classify to perform this operation

Quick Reference

FunctionDescriptionReturns
configureConfigure an AI provider (OpenAI, Anthropic, or custom){name, provider, model}
chatSend a chat message and get a response{content, role, model, usage}
completeGet a simple text completion (returns just the text)Generated text
summarizeSummarize text using AISummary text
extractExtract structured data from text using AIExtracted key-value object
classifyClassify text into one of given categoriesSelected category
translateTranslate text to a target languageTranslated text
sentimentAnalyze the sentiment of text{sentiment, score, confidence}
generateJsonGenerate structured JSON from a promptGenerated JSON object
embeddingGenerate text embeddings (OpenAI only)Embedding vector(s)

Functions

configure

Configure an AI provider (OpenAI, Anthropic, or custom)

Module: ai | Returns: object -- {name, provider, model}

ai.configure "openai" {"provider": "openai", "apiKey": $key}
ParameterTypeRequiredDescription
namestringYesProvider name
optionsobjectYes{provider, apiKey, baseUrl, model, maxTokens}

chat

Send a chat message and get a response

Module: ai | Returns: object -- {content, role, model, usage}

ai.chat "openai" "Explain quantum computing" {"system": "You are a teacher"}
ParameterTypeRequiredDescription
providerstringYesProvider name
messagesanyYesString or array of {role, content}
optionsobjectNo{model, maxTokens, temperature, system}

complete

Get a simple text completion (returns just the text)

Module: ai | Returns: string -- Generated text

ai.complete "openai" "Write a haiku about automation"
ParameterTypeRequiredDescription
providerstringYesProvider name
promptstringYesPrompt text
optionsobjectNo{model, maxTokens, temperature}

summarize

Summarize text using AI

Module: ai | Returns: string -- Summary text

ai.summarize "openai" $longText {"maxLength": 100}
ParameterTypeRequiredDescription
providerstringYesProvider name
textstringYesText to summarize
optionsobjectNo{maxLength}

extract

Extract structured data from text using AI

Module: ai | Returns: object -- Extracted key-value object

ai.extract "openai" "John Smith, age 30, from NYC" ["name", "age", "city"]
ParameterTypeRequiredDescription
providerstringYesProvider name
textstringYesSource text
fieldsarrayYesFields to extract

classify

Classify text into one of given categories

Module: ai | Returns: string -- Selected category

ai.classify "openai" "I love this product!" ["positive", "negative", "neutral"]
ParameterTypeRequiredDescription
providerstringYesProvider name
textstringYesText to classify
categoriesarrayYesPossible categories

translate

Translate text to a target language

Module: ai | Returns: string -- Translated text

ai.translate "openai" "Hello world" "Spanish"
ParameterTypeRequiredDescription
providerstringYesProvider name
textstringYesText to translate
targetLangstringYesTarget language

sentiment

Analyze the sentiment of text

Module: ai | Returns: object -- {sentiment, score, confidence}

ai.sentiment "openai" "This product is amazing!"
ParameterTypeRequiredDescription
providerstringYesProvider name
textstringYesText to analyze

generateJson

Generate structured JSON from a prompt

Module: ai | Returns: object -- Generated JSON object

ai.generateJson "openai" "Generate 3 fake users" {"name": "string", "email": "string"}
ParameterTypeRequiredDescription
providerstringYesProvider name
promptstringYesPrompt describing what to generate
schemaobjectNoOptional JSON schema/structure

embedding

Generate text embeddings (OpenAI only)

Module: ai | Returns: array -- Embedding vector(s)

ai.embedding "openai" "Hello world"
ParameterTypeRequiredDescription
providerstringYesProvider name
inputanyYesString or array of strings
optionsobjectNo{model}

Error Handling

All functions throw on failure. Common errors:

ErrorCause
OpenAI API error: ${JSON.stringify(data.error ?? data)}Check the error message for details
Anthropic API error: ${JSON.stringify(data.error ?? data)}Check the error message for details
Embeddings error: ${JSON.stringify(data.error ?? data)}Check the error message for details
AI provider "..." not configured. Use ai.configure first.Check the error message for details
AI provider "..." not configured.Check the error message for details
@desc "Configure and validate result"
do
  set $result as ai.configure "openai" {"provider": "openai", "apiKey": $key}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step AI workflow

Chain multiple ai operations together.

@desc "Configure, chat, and more"
do
  set $r_configure as ai.configure "openai" {"provider": "openai", "apiKey": $key}
  set $r_chat as ai.chat "openai" "Explain quantum computing" {"system": "You are a teacher"}
  set $r_complete as ai.complete "openai" "Write a haiku about automation"
  print "All operations complete"
enddo

2. Safe configure with validation

Check results before proceeding.

@desc "Configure and validate result"
do
  set $result as ai.configure "openai" {"provider": "openai", "apiKey": $key}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • openai -- OpenAI module for complementary functionality
  • anthropic -- Anthropic module for complementary functionality
  • deepl -- DeepL module for complementary functionality
  • translate -- Translate module for complementary functionality
  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/ai

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size6.4 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

ai