sentiment package to give Claude the ability to perform quantitative sentiment analysis on text passages.
The Complete Example
Using npm Packages as Tools
The key insight here is that any npm package can be wrapped as a tool. Thesentiment package provides AFINN-based sentiment analysis, but the LLM doesn’t know how to use it directly. By wrapping it in a tool, we give the agent the ability to call this deterministic algorithm whenever it needs precise sentiment measurements.
TextPassage object (validated against the schema) and returns the raw sentiment analysis result. The agent can then interpret these numbers in context.
Typed Tool Inputs with Schemas
Tools can accept structured input by providing a schema as the third argument. Here we define aTextPassage interface with the @JSONSchema decorator:
TextPassage.Schema to the .tool() method, Thinkwell:
- Exposes the schema to the LLM so it knows how to format tool calls
- Validates incoming tool calls against the schema
- Provides full TypeScript typing for the handler function’s
passageparameter
Including Document Content with .quote()
The .quote() method adds content to the prompt in a clearly delimited format. This is ideal for including documents, user input, or any content that should be treated as data rather than instructions.
Complex Schema Patterns
This example showcases several schema features working together:Nested Types
TheDocumentAnalysis schema contains an array of DocumentSection objects:
String Literal Unions (Enums)
TypeScript string literal unions become enum constraints in the generated schema:Descriptive JSDoc Comments
Every property should have a JSDoc comment explaining its purpose:Why Wrap Packages as Tools?
LLMs are powerful reasoners but they can struggle with precise calculations or algorithms that require exact execution. By wrapping deterministic packages as tools, you get the best of both worlds:- The LLM handles understanding context, breaking down the document into sections, and synthesizing a recommendation
- The npm package handles the precise, algorithmic work of calculating sentiment scores
