llmjs2 1.7.1 โ 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +116 -18
- package/dist/index.d.mts +146 -0
- package/dist/index.d.ts +146 -0
- package/dist/index.js +1242 -0
- package/dist/index.mjs +1211 -0
- package/package.json +32 -58
- package/chain/AGENT_STEP_README.md +0 -102
- package/chain/README.md +0 -257
- package/chain/WORKFLOW_README.md +0 -85
- package/chain/agent-step-example.js +0 -232
- package/chain/docs/AGENT.md +0 -126
- package/chain/docs/GRAPH.md +0 -490
- package/chain/examples.js +0 -314
- package/chain/index.js +0 -31
- package/chain/lib/agent.js +0 -338
- package/chain/lib/flow/agent-step.js +0 -119
- package/chain/lib/flow/edge.js +0 -24
- package/chain/lib/flow/flow.js +0 -76
- package/chain/lib/flow/graph.js +0 -331
- package/chain/lib/flow/index.js +0 -7
- package/chain/lib/flow/step.js +0 -63
- package/chain/lib/memory/in-memory.js +0 -117
- package/chain/lib/memory/index.js +0 -36
- package/chain/lib/memory/lance-memory.js +0 -225
- package/chain/lib/memory/sqlite-memory.js +0 -309
- package/chain/simple-agent-step-example.js +0 -168
- package/chain/workflow-example-usage.js +0 -70
- package/chain/workflow-example.json +0 -59
- package/core/README.md +0 -485
- package/core/cli.js +0 -275
- package/core/config.yaml +0 -149
- package/core/docs/BASIC_USAGE.md +0 -62
- package/core/docs/CLI.md +0 -104
- package/core/docs/GET_STARTED.md +0 -129
- package/core/docs/GUARDRAILS_GUIDE.md +0 -734
- package/core/docs/README.md +0 -47
- package/core/docs/ROUTER_GUIDE.md +0 -199
- package/core/docs/SERVER_MODE.md +0 -358
- package/core/index.js +0 -115
- package/core/logger.js +0 -115
- package/core/providers/ollama.js +0 -128
- package/core/providers/openai.js +0 -112
- package/core/providers/openrouter.js +0 -206
- package/core/router.js +0 -252
- package/core/server.js +0 -203
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AgentStep Example Program
|
|
3
|
-
*
|
|
4
|
-
* This example demonstrates how to use AgentStep in llmjs-chain workflows.
|
|
5
|
-
* It creates a multi-agent workflow for content creation and analysis.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const { Agent, AgentStep, Graph } = require('./index');
|
|
9
|
-
|
|
10
|
-
async function runAgentStepExample() {
|
|
11
|
-
console.log('๐ Starting AgentStep Workflow Example\n');
|
|
12
|
-
|
|
13
|
-
// ============================================================================
|
|
14
|
-
// 1. Create Specialized AI Agents
|
|
15
|
-
// ============================================================================
|
|
16
|
-
|
|
17
|
-
console.log('๐ Creating specialized AI agents...');
|
|
18
|
-
|
|
19
|
-
// Content Creator Agent - generates creative content
|
|
20
|
-
const contentCreator = new Agent({
|
|
21
|
-
instruction: `You are a creative content writer. Generate engaging, well-structured content based on the given topic.
|
|
22
|
-
Focus on being informative yet entertaining. Keep responses concise but comprehensive.`,
|
|
23
|
-
tools: [
|
|
24
|
-
{
|
|
25
|
-
name: 'get_word_count',
|
|
26
|
-
description: 'Count words in a text',
|
|
27
|
-
parameters: { text: { type: 'string', required: true } },
|
|
28
|
-
execute: async ({ text }) => `Word count: ${text.split(/\s+/).length}`
|
|
29
|
-
}
|
|
30
|
-
]
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// Content Analyzer Agent - analyzes and provides feedback
|
|
34
|
-
const contentAnalyzer = new Agent({
|
|
35
|
-
instruction: `You are a content quality analyst. Analyze the provided content for:
|
|
36
|
-
- Engagement and readability
|
|
37
|
-
- Structure and flow
|
|
38
|
-
- Key strengths and areas for improvement
|
|
39
|
-
- SEO considerations
|
|
40
|
-
Provide specific, actionable feedback.`,
|
|
41
|
-
tools: [
|
|
42
|
-
{
|
|
43
|
-
name: 'analyze_sentiment',
|
|
44
|
-
description: 'Analyze sentiment of text',
|
|
45
|
-
parameters: { text: { type: 'string', required: true } },
|
|
46
|
-
execute: async ({ text }) => {
|
|
47
|
-
// Simple sentiment analysis simulation
|
|
48
|
-
const positiveWords = ['good', 'great', 'excellent', 'amazing', 'wonderful'];
|
|
49
|
-
const negativeWords = ['bad', 'poor', 'terrible', 'awful', 'horrible'];
|
|
50
|
-
|
|
51
|
-
const words = text.toLowerCase().split(/\s+/);
|
|
52
|
-
const positiveCount = words.filter(w => positiveWords.includes(w)).length;
|
|
53
|
-
const negativeCount = words.filter(w => negativeWords.includes(w)).length;
|
|
54
|
-
|
|
55
|
-
if (positiveCount > negativeCount) return 'Positive sentiment';
|
|
56
|
-
if (negativeCount > positiveCount) return 'Negative sentiment';
|
|
57
|
-
return 'Neutral sentiment';
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
]
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// Content Editor Agent - refines and improves content
|
|
64
|
-
const contentEditor = new Agent({
|
|
65
|
-
instruction: `You are an expert content editor. Review and improve the provided content based on:
|
|
66
|
-
- Clarity and conciseness
|
|
67
|
-
- Grammar and style
|
|
68
|
-
- Engagement improvements
|
|
69
|
-
- Call-to-action additions
|
|
70
|
-
Provide the improved version along with change explanations.`,
|
|
71
|
-
tools: [
|
|
72
|
-
{
|
|
73
|
-
name: 'check_grammar',
|
|
74
|
-
description: 'Check grammar in text',
|
|
75
|
-
parameters: { text: { type: 'string', required: true } },
|
|
76
|
-
execute: async ({ text }) => 'Grammar check completed - no major issues found'
|
|
77
|
-
}
|
|
78
|
-
]
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// ============================================================================
|
|
82
|
-
// 2. Create AgentSteps with Input/Output Mapping
|
|
83
|
-
// ============================================================================
|
|
84
|
-
|
|
85
|
-
console.log('๐ง Creating AgentSteps with specialized mappings...');
|
|
86
|
-
|
|
87
|
-
// Step 1: Content Creation
|
|
88
|
-
const createContentStep = new AgentStep({
|
|
89
|
-
name: 'create-content',
|
|
90
|
-
agent: contentCreator,
|
|
91
|
-
inputMapper: (context) => {
|
|
92
|
-
const topic = context.topic || 'Artificial Intelligence';
|
|
93
|
-
const audience = context.audience || 'general public';
|
|
94
|
-
const wordCount = context.targetWordCount || 300;
|
|
95
|
-
|
|
96
|
-
return `Create an engaging article about "${topic}" for a ${audience} audience.
|
|
97
|
-
Target word count: approximately ${wordCount} words.
|
|
98
|
-
Make it informative, entertaining, and well-structured with a clear introduction, body, and conclusion.`;
|
|
99
|
-
},
|
|
100
|
-
outputMapper: (response, context) => ({
|
|
101
|
-
originalContent: response,
|
|
102
|
-
createdAt: new Date().toISOString(),
|
|
103
|
-
topic: context.topic,
|
|
104
|
-
stage: 'created'
|
|
105
|
-
})
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
// Step 2: Content Analysis
|
|
109
|
-
const analyzeContentStep = new AgentStep({
|
|
110
|
-
name: 'analyze-content',
|
|
111
|
-
agent: contentAnalyzer,
|
|
112
|
-
inputMapper: (context) => {
|
|
113
|
-
const content = context['create-content'].originalContent;
|
|
114
|
-
return `Please analyze this content for quality, engagement, and improvement opportunities:
|
|
115
|
-
|
|
116
|
-
${content}
|
|
117
|
-
|
|
118
|
-
Provide specific feedback on strengths, weaknesses, and suggestions for enhancement.`;
|
|
119
|
-
},
|
|
120
|
-
outputMapper: (response, context) => ({
|
|
121
|
-
analysis: response,
|
|
122
|
-
analyzedAt: new Date().toISOString(),
|
|
123
|
-
stage: 'analyzed'
|
|
124
|
-
})
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
// Step 3: Content Editing
|
|
128
|
-
const editContentStep = new AgentStep({
|
|
129
|
-
name: 'edit-content',
|
|
130
|
-
agent: contentEditor,
|
|
131
|
-
inputMapper: (context) => {
|
|
132
|
-
const originalContent = context['create-content'].originalContent;
|
|
133
|
-
const analysis = context['analyze-content'].analysis;
|
|
134
|
-
|
|
135
|
-
return `Please edit and improve this content based on the analysis provided:
|
|
136
|
-
|
|
137
|
-
ORIGINAL CONTENT:
|
|
138
|
-
${originalContent}
|
|
139
|
-
|
|
140
|
-
ANALYSIS/FEEDBACK:
|
|
141
|
-
${analysis}
|
|
142
|
-
|
|
143
|
-
Provide an improved version that addresses the feedback, along with explanations of your changes.`;
|
|
144
|
-
},
|
|
145
|
-
outputMapper: (response, context) => ({
|
|
146
|
-
finalContent: response,
|
|
147
|
-
editedAt: new Date().toISOString(),
|
|
148
|
-
stage: 'final'
|
|
149
|
-
})
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
// ============================================================================
|
|
153
|
-
// 3. Build and Execute the Multi-Agent Workflow
|
|
154
|
-
// ============================================================================
|
|
155
|
-
|
|
156
|
-
console.log('๐๏ธ Building multi-agent workflow...');
|
|
157
|
-
|
|
158
|
-
const contentWorkflow = new Graph({
|
|
159
|
-
name: 'content-creation-pipeline'
|
|
160
|
-
})
|
|
161
|
-
.step(createContentStep, analyzeContentStep, editContentStep)
|
|
162
|
-
.edge(createContentStep, analyzeContentStep)
|
|
163
|
-
.edge(analyzeContentStep, editContentStep)
|
|
164
|
-
.compile();
|
|
165
|
-
|
|
166
|
-
console.log('โถ๏ธ Executing content creation workflow...\n');
|
|
167
|
-
|
|
168
|
-
// Execute with different topics to show flexibility
|
|
169
|
-
const topics = [
|
|
170
|
-
{
|
|
171
|
-
topic: 'The Future of Renewable Energy',
|
|
172
|
-
audience: 'tech-savvy professionals',
|
|
173
|
-
targetWordCount: 400
|
|
174
|
-
},
|
|
175
|
-
{
|
|
176
|
-
topic: 'Healthy Eating Habits',
|
|
177
|
-
audience: 'busy parents',
|
|
178
|
-
targetWordCount: 350
|
|
179
|
-
}
|
|
180
|
-
];
|
|
181
|
-
|
|
182
|
-
for (let i = 0; i < topics.length; i++) {
|
|
183
|
-
const topicConfig = topics[i];
|
|
184
|
-
console.log(`๐ Processing Topic ${i + 1}: "${topicConfig.topic}"`);
|
|
185
|
-
console.log('โ'.repeat(60));
|
|
186
|
-
|
|
187
|
-
try {
|
|
188
|
-
const result = await contentWorkflow.run(topicConfig);
|
|
189
|
-
|
|
190
|
-
// Display results
|
|
191
|
-
console.log('\n๐ ORIGINAL CONTENT:');
|
|
192
|
-
console.log(result['create-content'].originalContent.substring(0, 200) + '...');
|
|
193
|
-
|
|
194
|
-
console.log('\n๐ ANALYSIS:');
|
|
195
|
-
console.log(result['analyze-content'].analysis.substring(0, 150) + '...');
|
|
196
|
-
|
|
197
|
-
console.log('\nโจ FINAL EDITED CONTENT:');
|
|
198
|
-
console.log(result['edit-content'].finalContent.substring(0, 200) + '...');
|
|
199
|
-
|
|
200
|
-
console.log('\nโ
Workflow completed successfully!');
|
|
201
|
-
console.log('โ'.repeat(60));
|
|
202
|
-
console.log();
|
|
203
|
-
|
|
204
|
-
} catch (error) {
|
|
205
|
-
console.error(`โ Error processing topic ${i + 1}:`, error.message);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// ============================================================================
|
|
210
|
-
// 4. Demonstrate Error Handling
|
|
211
|
-
// ============================================================================
|
|
212
|
-
|
|
213
|
-
console.log('๐งช Testing error handling...');
|
|
214
|
-
|
|
215
|
-
try {
|
|
216
|
-
// This should fail due to missing topic
|
|
217
|
-
await contentWorkflow.run({});
|
|
218
|
-
} catch (error) {
|
|
219
|
-
console.log('โ
Error handling works:', error.message);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
console.log('\n๐ AgentStep example completed successfully!');
|
|
223
|
-
console.log('\n๐ก Key Features Demonstrated:');
|
|
224
|
-
console.log(' โข Multiple specialized AI agents in one workflow');
|
|
225
|
-
console.log(' โข Custom input/output mapping for each agent');
|
|
226
|
-
console.log(' โข Sequential processing with context passing');
|
|
227
|
-
console.log(' โข Real-world content creation pipeline');
|
|
228
|
-
console.log(' โข Error handling and validation');
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// Run the example
|
|
232
|
-
runAgentStepExample().catch(console.error);
|
package/chain/docs/AGENT.md
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
# Agent Usage Guide
|
|
2
|
-
|
|
3
|
-
The Agent class provides AI interactions with tool support and conversation memory.
|
|
4
|
-
|
|
5
|
-
## Table of Contents
|
|
6
|
-
- [Basic Usage](#basic-agent-usage)
|
|
7
|
-
- [Tool Definition](#tool-definition)
|
|
8
|
-
- [Memory Integration](#memory-integration)
|
|
9
|
-
- [Advanced Configuration](#advanced-agent-configuration)
|
|
10
|
-
|
|
11
|
-
## Basic Agent Usage
|
|
12
|
-
|
|
13
|
-
```javascript
|
|
14
|
-
const { Agent } = require('llmjs-chain');
|
|
15
|
-
|
|
16
|
-
// Create a simple agent
|
|
17
|
-
const agent = new Agent({
|
|
18
|
-
instruction: 'You are a helpful AI assistant.',
|
|
19
|
-
model: 'openai/gpt-4' // Optional - auto-detected if not provided
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
// Generate a response
|
|
23
|
-
async function chat() {
|
|
24
|
-
const response = await agent.generate('Hello, how are you?');
|
|
25
|
-
console.log(response);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
chat();
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Tool Definition
|
|
32
|
-
|
|
33
|
-
Agents can use tools to perform actions. Tools can be defined in a simplified format:
|
|
34
|
-
|
|
35
|
-
```javascript
|
|
36
|
-
const agent = new Agent({
|
|
37
|
-
instruction: 'You are a helpful assistant with access to tools.',
|
|
38
|
-
tools: [
|
|
39
|
-
{
|
|
40
|
-
name: 'calculate',
|
|
41
|
-
description: 'Perform mathematical calculations',
|
|
42
|
-
parameters: {
|
|
43
|
-
expression: { type: 'string', description: 'Math expression to evaluate', required: true }
|
|
44
|
-
},
|
|
45
|
-
execute: async ({ expression }) => {
|
|
46
|
-
const result = eval(expression);
|
|
47
|
-
return JSON.stringify({ result, expression });
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
name: 'get_weather',
|
|
52
|
-
description: 'Get current weather for a location',
|
|
53
|
-
parameters: {
|
|
54
|
-
location: { type: 'string', description: 'City name', required: true }
|
|
55
|
-
},
|
|
56
|
-
execute: async ({ location }) => {
|
|
57
|
-
// Implement weather API call
|
|
58
|
-
return JSON.stringify({ location, temperature: 22, condition: 'sunny' });
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
]
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// The agent will automatically use tools when needed
|
|
65
|
-
agent.generate('What is 15 + 27?').then(console.log);
|
|
66
|
-
agent.generate('What is the weather in Tokyo?').then(console.log);
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
## Memory Integration
|
|
70
|
-
|
|
71
|
-
Agents can maintain conversation history using the InMemory class:
|
|
72
|
-
|
|
73
|
-
```javascript
|
|
74
|
-
const { Agent, InMemory } = require('llmjs-chain');
|
|
75
|
-
|
|
76
|
-
const memory = new InMemory();
|
|
77
|
-
const agent = new Agent({
|
|
78
|
-
instruction: 'You are a helpful assistant with memory.',
|
|
79
|
-
memory: memory
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// Start a conversation session
|
|
83
|
-
const response1 = await agent.generate({
|
|
84
|
-
userPrompt: 'My name is Alice.',
|
|
85
|
-
memory: {
|
|
86
|
-
resourceId: 'user_alice',
|
|
87
|
-
threadId: 'conversation_1',
|
|
88
|
-
session: true // Include session history
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
const response2 = await agent.generate({
|
|
93
|
-
userPrompt: 'What is my name?',
|
|
94
|
-
memory: {
|
|
95
|
-
resourceId: 'user_alice',
|
|
96
|
-
threadId: 'conversation_1',
|
|
97
|
-
session: true
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
// Agent will remember your name from the previous message
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
## Advanced Agent Configuration
|
|
104
|
-
|
|
105
|
-
```javascript
|
|
106
|
-
const agent = new Agent({
|
|
107
|
-
model: 'ollama/llama2', // Specific model
|
|
108
|
-
apikey: 'your-api-key', // Override auto-detected API key
|
|
109
|
-
instruction: `You are an expert software developer.
|
|
110
|
-
Always provide detailed explanations and code examples.`,
|
|
111
|
-
tools: [/* tool definitions */],
|
|
112
|
-
memory: new InMemory()
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
// Advanced generation with memory options
|
|
116
|
-
const response = await agent.generate({
|
|
117
|
-
userPrompt: 'Help me debug this JavaScript code...',
|
|
118
|
-
apikey: 'override-key', // Override API key for this call
|
|
119
|
-
memory: {
|
|
120
|
-
resourceId: 'debug_session',
|
|
121
|
-
threadId: 'js_debug_001',
|
|
122
|
-
session: true, // Include conversation history
|
|
123
|
-
relevance: true // Also search for relevant past conversations
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
```
|