Developing Custom Extensions Part 16 - Interfacing with Claude (anthropic) AI

Not actually a video in this case, it is an alternative content framework for Module 15 but using Claude (anthropic) in place of Gemini

This is so similar i will just show the associated files. An additional input allows the number of tokens used to be limited.
API key should be stored in environment variable "ANTHROPIC_API_KEY"
Dont forget, if not installing via NPM you need to add "@anthropic-ai/sdk" from npm repo manually.

{
  type: 'claude_claude',
  module : 'claude',
  action : 'claude',
  groupTitle : 'AI Modules',
  groupIcon : 'fas fa-solid fa-brain comp-loops',
  title : 'Claude (anthropic)',
  icon : 'fas fa-solid fa-brain comp-loops',
     serverModel : ['node'],
    dataPickObject: true,
            dataScheme: [
    {name: 'result', type: 'text'},
     ],
  usedModules : {
    node: {
      '@anthropic-ai/sdk' : '^0.24.3'
    }
  }
  properties : [
    {
      group: 'Input Question',
      variables: [
        { name: 'actionName', 
        optionName: 'name', 
        title: 'Name', 
        type: 'text', 
        required: true, 
        defaultValue: '',
        baseName: "claude1"
        },
        { name: 'myquestion', 
        optionName: 'myquestion', 
        title: 'AI Question', 
        type: 'text', 
        required: true, 
        defaultValue: '',
        serverDataBindings: true
        },
        { name: 'max_tokens', 
          optionName: 'max_tokens', 
          title: 'Maximum tokens', 
          type: 'text', 
          required: true,
          defaultValue: "30", 
          serverDataBindings: true
           },
        { name: 'service', optionName: 'service', title: 'Claude Service', 
  type: 'droplist', 
  values: [
    {title: 'claude-3-5-sonnet-20240620', value: 'claude-3-5-sonnet-20240620' },
  {title: 'claude-3-opus-20240229', value: 'claude-3-opus-20240229' }, 
   {title: 'claude-3-haiku-20240307', value: 'claude-3-haiku-20240307' }
  ], initValue: 'claude-3-5-sonnet-20240620',
	help: 'Choose your AI service'
},
          { name: 'output', 
          optionName: 'output', 
          title: 'Output', 
          type: 'boolean', 
          defaultValue: false 
          }
      ]
    },
    
  ]
}
exports.claude = async function (options, name) {
    // import Anthropic from '@anthropic-ai/sdk';
    const { Anthropic } = require("@anthropic-ai/sdk");
    const myQuestion = this.parseRequired(options.myquestion, '*', "No Question sent")
// default to 30 tokens 
    const maxTokens = this.parseOptional(options.max_tokens, '*', 30)
// default to claude-3-5-sonnet-20240620
    const service = this.parseOptional(options.service, '*', 'claude-3-5-sonnet-20240620')
    const anthropic = new Anthropic({
        apiKey: process.env["ANTHROPIC_API_KEY"]
    });

    const msg = await anthropic.messages.create({
        model: options.service,
        max_tokens: maxTokens,
        messages: [{ role: "user", content: myQuestion }],
    });
    console.log(msg);
    return { 'result': msg.content[0].text }
}

Interface

Files:
claude.zip (1.3 KB)

NPM

1 Like