societyai 0.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/CHANGELOG.md +111 -0
- package/LICENSE +21 -0
- package/README.md +879 -0
- package/dist/builder.d.ts +181 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +667 -0
- package/dist/builder.js.map +1 -0
- package/dist/config.d.ts +43 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +11 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +107 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +319 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +31 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +85 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +219 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +395 -0
- package/dist/events.js.map +1 -0
- package/dist/graph.d.ts +104 -0
- package/dist/graph.d.ts.map +1 -0
- package/dist/graph.js +366 -0
- package/dist/graph.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +113 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +13 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +78 -0
- package/dist/logger.js.map +1 -0
- package/dist/memory.d.ts +146 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +353 -0
- package/dist/memory.js.map +1 -0
- package/dist/metrics.d.ts +143 -0
- package/dist/metrics.d.ts.map +1 -0
- package/dist/metrics.js +271 -0
- package/dist/metrics.js.map +1 -0
- package/dist/middleware.d.ts +147 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +484 -0
- package/dist/middleware.js.map +1 -0
- package/dist/models.d.ts +32 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +211 -0
- package/dist/models.js.map +1 -0
- package/dist/patterns.d.ts +6 -0
- package/dist/patterns.d.ts.map +1 -0
- package/dist/patterns.js +68 -0
- package/dist/patterns.js.map +1 -0
- package/dist/pipeline.d.ts +84 -0
- package/dist/pipeline.d.ts.map +1 -0
- package/dist/pipeline.js +569 -0
- package/dist/pipeline.js.map +1 -0
- package/dist/retry.d.ts +5 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +70 -0
- package/dist/retry.js.map +1 -0
- package/dist/society.d.ts +94 -0
- package/dist/society.d.ts.map +1 -0
- package/dist/society.js +721 -0
- package/dist/society.js.map +1 -0
- package/dist/strategies.d.ts +55 -0
- package/dist/strategies.d.ts.map +1 -0
- package/dist/strategies.js +678 -0
- package/dist/strategies.js.map +1 -0
- package/dist/tools.d.ts +88 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +366 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +213 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/dist/validation.d.ts +64 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +334 -0
- package/dist/validation.js.map +1 -0
- package/dist/worker-pool.d.ts +17 -0
- package/dist/worker-pool.d.ts.map +1 -0
- package/dist/worker-pool.js +80 -0
- package/dist/worker-pool.js.map +1 -0
- package/docs/README.md +468 -0
- package/docs/advanced.md +616 -0
- package/docs/aggregation-strategies.md +926 -0
- package/docs/api-reference.md +771 -0
- package/docs/architecture.md +648 -0
- package/docs/context-system.md +642 -0
- package/docs/event-system.md +1047 -0
- package/docs/examples.md +576 -0
- package/docs/getting-started.md +564 -0
- package/docs/graph-execution.md +389 -0
- package/docs/memory-system.md +497 -0
- package/docs/metrics-observability.md +560 -0
- package/docs/middleware-system.md +1038 -0
- package/docs/migration.md +296 -0
- package/docs/pipeline-patterns.md +761 -0
- package/docs/structured-output.md +612 -0
- package/docs/tool-calling.md +491 -0
- package/docs/workflows.md +740 -0
- package/examples/README.md +234 -0
- package/examples/advanced-patterns.ts +115 -0
- package/examples/complete-integration.ts +327 -0
- package/examples/graph-workflow.ts +161 -0
- package/examples/memory-system.ts +155 -0
- package/examples/metrics-tracking.ts +243 -0
- package/examples/structured-output.ts +231 -0
- package/examples/tool-calling.ts +163 -0
- package/package.json +94 -0
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
# Getting Started with SocietyAI
|
|
2
|
+
|
|
3
|
+
Welcome to SocietyAI! This guide will help you get up and running with creating your first multi-agent AI system.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Installation](#installation)
|
|
8
|
+
- [Prerequisites](#prerequisites)
|
|
9
|
+
- [Your First Society](#your-first-society)
|
|
10
|
+
- [Understanding the Basics](#understanding-the-basics)
|
|
11
|
+
- [Advanced Features](#advanced-features)
|
|
12
|
+
- [Next Steps](#next-steps)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Install SocietyAI via npm:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install societyai
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Or using yarn:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
yarn add societyai
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Or using pnpm:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add societyai
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Prerequisites
|
|
35
|
+
|
|
36
|
+
### TypeScript Configuration
|
|
37
|
+
|
|
38
|
+
SocietyAI is written in TypeScript and provides full type definitions. Ensure your `tsconfig.json` includes:
|
|
39
|
+
|
|
40
|
+
```json
|
|
41
|
+
{
|
|
42
|
+
"compilerOptions": {
|
|
43
|
+
"target": "ES2020",
|
|
44
|
+
"module": "commonjs",
|
|
45
|
+
"lib": ["ES2020"],
|
|
46
|
+
"esModuleInterop": true,
|
|
47
|
+
"strict": true
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### AI Model Access
|
|
53
|
+
|
|
54
|
+
You'll need access to at least one AI model API:
|
|
55
|
+
|
|
56
|
+
- **OpenAI**: Get an API key from [OpenAI Platform](https://platform.openai.com/)
|
|
57
|
+
- **Anthropic**: Get an API key from [Anthropic Console](https://console.anthropic.com/)
|
|
58
|
+
- **Google AI**: Get an API key from [Google AI Studio](https://makersuite.google.com/)
|
|
59
|
+
- **Local Models**: Use Ollama, LM Studio, or similar
|
|
60
|
+
- **Custom API**: Any HTTP API that accepts prompts and returns text
|
|
61
|
+
|
|
62
|
+
## Your First Society
|
|
63
|
+
|
|
64
|
+
Let's create a simple two-agent system where one agent analyzes data and another reviews the analysis.
|
|
65
|
+
|
|
66
|
+
### Step 1: Create Your AI Model
|
|
67
|
+
|
|
68
|
+
First, create a model that connects to your AI service:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { StandardModelBase } from 'societyai';
|
|
72
|
+
import OpenAI from 'openai';
|
|
73
|
+
|
|
74
|
+
class OpenAIModel extends StandardModelBase {
|
|
75
|
+
private client: OpenAI;
|
|
76
|
+
|
|
77
|
+
constructor(apiKey: string) {
|
|
78
|
+
const client = new OpenAI({ apiKey });
|
|
79
|
+
|
|
80
|
+
super({ name: 'gpt-4-turbo' }, async (prompt) => {
|
|
81
|
+
const response = await client.chat.completions.create({
|
|
82
|
+
model: 'gpt-4-turbo',
|
|
83
|
+
messages: [{ role: 'user', content: String(prompt) }],
|
|
84
|
+
});
|
|
85
|
+
return response.choices[0].message.content || '';
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
this.client = client;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Create an instance
|
|
93
|
+
const model = new OpenAIModel(process.env.OPENAI_API_KEY!);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Step 2: Define Agent Roles
|
|
97
|
+
|
|
98
|
+
Create roles that define how agents behave:
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { RoleBuilder } from 'societyai';
|
|
102
|
+
|
|
103
|
+
// Analyst role
|
|
104
|
+
const analystRole = RoleBuilder.create()
|
|
105
|
+
.withId('analyst')
|
|
106
|
+
.withName('Data Analyst')
|
|
107
|
+
.withSystemPrompt(
|
|
108
|
+
'You are a data analyst. Your job is to examine information ' +
|
|
109
|
+
'objectively, identify patterns, and provide clear insights. ' +
|
|
110
|
+
'Focus on facts and data-driven conclusions.'
|
|
111
|
+
)
|
|
112
|
+
.withCapabilities(['data-analysis', 'pattern-recognition', 'statistics'])
|
|
113
|
+
.build();
|
|
114
|
+
|
|
115
|
+
// Reviewer role
|
|
116
|
+
const reviewerRole = RoleBuilder.create()
|
|
117
|
+
.withId('reviewer')
|
|
118
|
+
.withName('Critical Reviewer')
|
|
119
|
+
.withSystemPrompt(
|
|
120
|
+
'You are a critical reviewer. Your job is to challenge assumptions, ' +
|
|
121
|
+
'identify potential issues, and ensure quality. ' +
|
|
122
|
+
'Be thorough and constructive in your feedback.'
|
|
123
|
+
)
|
|
124
|
+
.withCapabilities(['critical-thinking', 'quality-assurance'])
|
|
125
|
+
.build();
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Step 3: Create Agents
|
|
129
|
+
|
|
130
|
+
Build agents by combining roles with models:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import { AgentBuilder } from 'societyai';
|
|
134
|
+
|
|
135
|
+
const agents = [
|
|
136
|
+
AgentBuilder.create().withId('analyst-1').withRole(analystRole).withModel(model).build(),
|
|
137
|
+
|
|
138
|
+
AgentBuilder.create().withId('reviewer-1').withRole(reviewerRole).withModel(model).build(),
|
|
139
|
+
];
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Step 4: Define Workflow Steps
|
|
143
|
+
|
|
144
|
+
Create steps that define what agents do:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import { StepBuilder } from 'societyai';
|
|
148
|
+
|
|
149
|
+
const steps = [
|
|
150
|
+
// Step 1: Analysis
|
|
151
|
+
StepBuilder.create()
|
|
152
|
+
.withId('analysis')
|
|
153
|
+
.withName('Data Analysis')
|
|
154
|
+
.withAgents(['analyst-1'])
|
|
155
|
+
.withExecutionType('sequential')
|
|
156
|
+
.withInstructions(
|
|
157
|
+
'Analyze the provided data thoroughly. ' + 'Identify key patterns, trends, and insights.'
|
|
158
|
+
)
|
|
159
|
+
.build(),
|
|
160
|
+
|
|
161
|
+
// Step 2: Review
|
|
162
|
+
StepBuilder.create()
|
|
163
|
+
.withId('review')
|
|
164
|
+
.withName('Quality Review')
|
|
165
|
+
.withAgents(['reviewer-1'])
|
|
166
|
+
.withExecutionType('sequential')
|
|
167
|
+
.withInstructions(
|
|
168
|
+
'Review the analysis provided. ' +
|
|
169
|
+
'Check for accuracy, completeness, and potential issues. ' +
|
|
170
|
+
'Provide constructive feedback.'
|
|
171
|
+
)
|
|
172
|
+
.build(),
|
|
173
|
+
];
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Step 5: Build the Workflow
|
|
177
|
+
|
|
178
|
+
Combine everything into a workflow:
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { WorkflowConfigBuilder } from 'societyai';
|
|
182
|
+
|
|
183
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
184
|
+
.withId('analysis-workflow')
|
|
185
|
+
.withName('Data Analysis & Review Workflow')
|
|
186
|
+
.withDescription('Two-stage workflow with analysis and review')
|
|
187
|
+
.addAgents(agents)
|
|
188
|
+
.addSteps(steps)
|
|
189
|
+
.build();
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Step 6: Execute
|
|
193
|
+
|
|
194
|
+
Run the workflow:
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { DefaultWorkflowExecutor } from 'societyai';
|
|
198
|
+
|
|
199
|
+
async function main() {
|
|
200
|
+
const executor = new DefaultWorkflowExecutor();
|
|
201
|
+
|
|
202
|
+
const result = await executor.execute(
|
|
203
|
+
workflow,
|
|
204
|
+
'Analyze the sales data from Q4 2024. ' +
|
|
205
|
+
'Sales increased by 15% year-over-year, ' +
|
|
206
|
+
'with the highest growth in the APAC region (+25%).'
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
console.log('Workflow completed!');
|
|
210
|
+
console.log('Duration:', result.duration, 'ms');
|
|
211
|
+
console.log('\nFinal Output:');
|
|
212
|
+
console.log(result.output);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
main().catch(console.error);
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Complete Example
|
|
219
|
+
|
|
220
|
+
Here's the complete code:
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import {
|
|
224
|
+
StandardModelBase,
|
|
225
|
+
RoleBuilder,
|
|
226
|
+
AgentBuilder,
|
|
227
|
+
StepBuilder,
|
|
228
|
+
WorkflowConfigBuilder,
|
|
229
|
+
DefaultWorkflowExecutor,
|
|
230
|
+
} from 'societyai';
|
|
231
|
+
import OpenAI from 'openai';
|
|
232
|
+
|
|
233
|
+
// 1. Create AI Model
|
|
234
|
+
class OpenAIModel extends StandardModelBase {
|
|
235
|
+
private client: OpenAI;
|
|
236
|
+
|
|
237
|
+
constructor(apiKey: string) {
|
|
238
|
+
const client = new OpenAI({ apiKey });
|
|
239
|
+
super({ name: 'gpt-4-turbo' }, async (prompt) => {
|
|
240
|
+
const response = await client.chat.completions.create({
|
|
241
|
+
model: 'gpt-4-turbo',
|
|
242
|
+
messages: [{ role: 'user', content: String(prompt) }],
|
|
243
|
+
});
|
|
244
|
+
return response.choices[0].message.content || '';
|
|
245
|
+
});
|
|
246
|
+
this.client = client;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// 2. Define Roles
|
|
251
|
+
const analystRole = RoleBuilder.create()
|
|
252
|
+
.withId('analyst')
|
|
253
|
+
.withName('Data Analyst')
|
|
254
|
+
.withSystemPrompt('You are a data analyst. Examine information objectively.')
|
|
255
|
+
.build();
|
|
256
|
+
|
|
257
|
+
const reviewerRole = RoleBuilder.create()
|
|
258
|
+
.withId('reviewer')
|
|
259
|
+
.withName('Critical Reviewer')
|
|
260
|
+
.withSystemPrompt('You are a critical reviewer. Challenge assumptions.')
|
|
261
|
+
.build();
|
|
262
|
+
|
|
263
|
+
// 3. Create Agents
|
|
264
|
+
const model = new OpenAIModel(process.env.OPENAI_API_KEY!);
|
|
265
|
+
|
|
266
|
+
const agents = [
|
|
267
|
+
AgentBuilder.create().withId('analyst-1').withRole(analystRole).withModel(model).build(),
|
|
268
|
+
AgentBuilder.create().withId('reviewer-1').withRole(reviewerRole).withModel(model).build(),
|
|
269
|
+
];
|
|
270
|
+
|
|
271
|
+
// 4. Define Steps
|
|
272
|
+
const steps = [
|
|
273
|
+
StepBuilder.create()
|
|
274
|
+
.withId('analysis')
|
|
275
|
+
.withAgents(['analyst-1'])
|
|
276
|
+
.withExecutionType('sequential')
|
|
277
|
+
.withInstructions('Analyze the data thoroughly.')
|
|
278
|
+
.build(),
|
|
279
|
+
StepBuilder.create()
|
|
280
|
+
.withId('review')
|
|
281
|
+
.withAgents(['reviewer-1'])
|
|
282
|
+
.withExecutionType('sequential')
|
|
283
|
+
.withInstructions('Review the analysis.')
|
|
284
|
+
.build(),
|
|
285
|
+
];
|
|
286
|
+
|
|
287
|
+
// 5. Build Workflow
|
|
288
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
289
|
+
.withId('analysis-workflow')
|
|
290
|
+
.withName('Analysis & Review')
|
|
291
|
+
.addAgents(agents)
|
|
292
|
+
.addSteps(steps)
|
|
293
|
+
.build();
|
|
294
|
+
|
|
295
|
+
// 6. Execute
|
|
296
|
+
async function main() {
|
|
297
|
+
const executor = new DefaultWorkflowExecutor();
|
|
298
|
+
const result = await executor.execute(workflow, 'Your input here');
|
|
299
|
+
console.log(result.output);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
main().catch(console.error);
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
## Understanding the Basics
|
|
306
|
+
|
|
307
|
+
### The Five Core Components
|
|
308
|
+
|
|
309
|
+
1. **AIModel**: Interface to your AI service (OpenAI, Anthropic, etc.)
|
|
310
|
+
2. **AgentRole**: Defines behavior, capabilities, and system prompt
|
|
311
|
+
3. **AgentConfig**: Combines a role with a model to create an agent
|
|
312
|
+
4. **WorkflowStep**: Defines what agents do and how (sequential, parallel, etc.)
|
|
313
|
+
5. **WorkflowConfig**: Orchestrates agents and steps into a complete workflow
|
|
314
|
+
|
|
315
|
+
### Execution Flow
|
|
316
|
+
|
|
317
|
+
```
|
|
318
|
+
Input → Workflow Executor
|
|
319
|
+
↓
|
|
320
|
+
Step 1 (Sequential)
|
|
321
|
+
↓
|
|
322
|
+
Agent 1 → AI Model → Result 1
|
|
323
|
+
↓
|
|
324
|
+
Step 2 (Sequential)
|
|
325
|
+
↓
|
|
326
|
+
Agent 2 → AI Model → Result 2
|
|
327
|
+
↓
|
|
328
|
+
Final Output
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### Key Concepts
|
|
332
|
+
|
|
333
|
+
**Sequential Execution**: Agents run one after another. Each agent can access results from previous agents.
|
|
334
|
+
|
|
335
|
+
```typescript
|
|
336
|
+
.withExecutionType('sequential')
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
**Parallel Execution**: Multiple agents run simultaneously for faster processing.
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
.withExecutionType('parallel')
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
**Collaborative Execution**: Agents exchange messages and iterate together.
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
.withExecutionType('collaborative')
|
|
349
|
+
.withMaxIterations(3)
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**Conditional Execution**: Steps execute only when conditions are met.
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
.withExecutionType('conditional')
|
|
356
|
+
.withCondition((previousResults) => someCondition(previousResults))
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
## Next Steps
|
|
360
|
+
|
|
361
|
+
Now that you've created your first society, explore more advanced features:
|
|
362
|
+
|
|
363
|
+
1. **[Architecture Guide](./architecture.md)** - Understand the design and concepts
|
|
364
|
+
2. **[Workflow Patterns](./workflows.md)** - Learn common workflow configurations
|
|
365
|
+
3. **[API Reference](./api-reference.md)** - Explore all available methods
|
|
366
|
+
4. **[Advanced Features](./advanced.md)** - Error handling, retry, observability
|
|
367
|
+
5. **[Examples](./examples.md)** - See real-world usage patterns
|
|
368
|
+
|
|
369
|
+
### Quick Examples
|
|
370
|
+
|
|
371
|
+
**Parallel Analysis**:
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
StepBuilder.create()
|
|
375
|
+
.withAgents(['agent-1', 'agent-2', 'agent-3'])
|
|
376
|
+
.withExecutionType('parallel') // All agents work simultaneously
|
|
377
|
+
.build();
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
**Collaborative Discussion**:
|
|
381
|
+
|
|
382
|
+
```typescript
|
|
383
|
+
StepBuilder.create()
|
|
384
|
+
.withAgents(['agent-1', 'agent-2', 'agent-3'])
|
|
385
|
+
.withExecutionType('collaborative')
|
|
386
|
+
.withMaxIterations(3)
|
|
387
|
+
.withCompletionCondition((results, iteration) => {
|
|
388
|
+
return iteration >= 2 || consensusReached(results);
|
|
389
|
+
})
|
|
390
|
+
.build();
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
**Custom Result Generation**:
|
|
394
|
+
|
|
395
|
+
```typescript
|
|
396
|
+
WorkflowConfigBuilder.create()
|
|
397
|
+
.withFinalResultGenerator(async (stepResults, context) => {
|
|
398
|
+
// Your custom logic to combine results
|
|
399
|
+
return customFormattedOutput;
|
|
400
|
+
})
|
|
401
|
+
.build();
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
## Advanced Features
|
|
405
|
+
|
|
406
|
+
SocietyAI provides powerful features for complex multi-agent systems:
|
|
407
|
+
|
|
408
|
+
### Graph-Based Execution
|
|
409
|
+
|
|
410
|
+
Execute agents in complex DAG or cyclic workflows:
|
|
411
|
+
|
|
412
|
+
```typescript
|
|
413
|
+
import { GraphBuilder, NodeType } from 'societyai';
|
|
414
|
+
|
|
415
|
+
const graph = GraphBuilder.create()
|
|
416
|
+
.addNode('start', NodeType.START)
|
|
417
|
+
.addNode('analyze', NodeType.AGENT, { agentId: 'analyst' })
|
|
418
|
+
.addNode('condition', NodeType.CONDITION, {
|
|
419
|
+
condition: (ctx) => ctx.data.requiresReview,
|
|
420
|
+
})
|
|
421
|
+
.addNode('review', NodeType.AGENT, { agentId: 'reviewer' })
|
|
422
|
+
.addNode('end', NodeType.END)
|
|
423
|
+
.addEdge('start', 'analyze')
|
|
424
|
+
.addEdge('analyze', 'condition')
|
|
425
|
+
.addEdge('condition', 'review', true) // if true
|
|
426
|
+
.addEdge('condition', 'end', false) // if false
|
|
427
|
+
.addEdge('review', 'end')
|
|
428
|
+
.build();
|
|
429
|
+
|
|
430
|
+
const result = await graph.execute('Analyze this data', agents);
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
**Learn more**: [Graph Execution Guide](./graph-execution.md)
|
|
434
|
+
|
|
435
|
+
### Tool Calling
|
|
436
|
+
|
|
437
|
+
Give agents access to external tools:
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
import { ToolBuilder, ToolRegistry } from 'societyai';
|
|
441
|
+
|
|
442
|
+
const weatherTool = ToolBuilder.create()
|
|
443
|
+
.withName('get_weather')
|
|
444
|
+
.withDescription('Get current weather for a location')
|
|
445
|
+
.withParameter('location', 'string', 'City name', true)
|
|
446
|
+
.withExecutor(async (params) => {
|
|
447
|
+
const { location } = params;
|
|
448
|
+
return `Weather in ${location}: Sunny, 72°F`;
|
|
449
|
+
})
|
|
450
|
+
.build();
|
|
451
|
+
|
|
452
|
+
const registry = new ToolRegistry();
|
|
453
|
+
registry.register(weatherTool);
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
**Learn more**: [Tool Calling Guide](./tool-calling.md)
|
|
457
|
+
|
|
458
|
+
### Memory System
|
|
459
|
+
|
|
460
|
+
Multi-level memory for context management:
|
|
461
|
+
|
|
462
|
+
```typescript
|
|
463
|
+
import { MemoryBuilder } from 'societyai';
|
|
464
|
+
|
|
465
|
+
const memory = MemoryBuilder.create()
|
|
466
|
+
.withShortTermMemory({ maxSize: 10, decayRate: 0.1 })
|
|
467
|
+
.withLongTermMemory({ maxSize: 100, similarityThreshold: 0.7 })
|
|
468
|
+
.withEntityMemory({ maxEntities: 50 })
|
|
469
|
+
.build();
|
|
470
|
+
|
|
471
|
+
// Add memories
|
|
472
|
+
await memory.addShortTerm('User prefers detailed explanations', 0.9);
|
|
473
|
+
await memory.addLongTerm('Project uses React and TypeScript');
|
|
474
|
+
await memory.addEntity('ProjectX', 'uses microservices architecture');
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
**Learn more**: [Memory System Guide](./memory-system.md)
|
|
478
|
+
|
|
479
|
+
### Structured Output
|
|
480
|
+
|
|
481
|
+
Validate AI outputs with JSON schemas:
|
|
482
|
+
|
|
483
|
+
```typescript
|
|
484
|
+
import { StructuredOutputValidator } from 'societyai';
|
|
485
|
+
|
|
486
|
+
const validator = new StructuredOutputValidator({
|
|
487
|
+
type: 'object',
|
|
488
|
+
properties: {
|
|
489
|
+
name: { type: 'string' },
|
|
490
|
+
age: { type: 'number', minimum: 0 },
|
|
491
|
+
},
|
|
492
|
+
required: ['name', 'age'],
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
const result = await validator.validateAndRetry(
|
|
496
|
+
async (previousError) => {
|
|
497
|
+
const prompt = previousError
|
|
498
|
+
? `Previous output was invalid: ${previousError}\nPlease fix it.`
|
|
499
|
+
: 'Generate user JSON';
|
|
500
|
+
return await model.process(prompt);
|
|
501
|
+
},
|
|
502
|
+
3 // Max retries
|
|
503
|
+
);
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
**Learn more**: [Structured Output Guide](./structured-output.md)
|
|
507
|
+
|
|
508
|
+
### Metrics & Observability
|
|
509
|
+
|
|
510
|
+
Track performance, costs, and custom metrics:
|
|
511
|
+
|
|
512
|
+
```typescript
|
|
513
|
+
import { MetricsBuilder, CommonCostConfigs } from 'societyai';
|
|
514
|
+
|
|
515
|
+
const tracker = MetricsBuilder.create()
|
|
516
|
+
.withTokenTracking()
|
|
517
|
+
.withCostTracking(CommonCostConfigs['gpt-4'])
|
|
518
|
+
.withCustomMetrics(['api_calls'])
|
|
519
|
+
.build();
|
|
520
|
+
|
|
521
|
+
tracker.start('workflow-1');
|
|
522
|
+
// ... execute workflow ...
|
|
523
|
+
const snapshot = tracker.end('workflow-1');
|
|
524
|
+
|
|
525
|
+
console.log(`Cost: $${snapshot.costs?.totalCost?.toFixed(4)}`);
|
|
526
|
+
console.log(`Tokens: ${snapshot.tokens?.totalTokens}`);
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
**Learn more**: [Metrics & Observability Guide](./metrics-observability.md)
|
|
530
|
+
|
|
531
|
+
## Common Issues
|
|
532
|
+
|
|
533
|
+
### Issue: "Cannot find module 'societyai'"
|
|
534
|
+
|
|
535
|
+
**Solution**: Make sure you've installed the package:
|
|
536
|
+
|
|
537
|
+
```bash
|
|
538
|
+
npm install societyai
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
### Issue: API Key Errors
|
|
542
|
+
|
|
543
|
+
**Solution**: Set your API key as an environment variable:
|
|
544
|
+
|
|
545
|
+
```bash
|
|
546
|
+
export OPENAI_API_KEY="your-key-here"
|
|
547
|
+
# or
|
|
548
|
+
export ANTHROPIC_API_KEY="your-key-here"
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
### Issue: TypeScript Errors
|
|
552
|
+
|
|
553
|
+
**Solution**: Ensure your TypeScript configuration is compatible (see [Prerequisites](#typescript-configuration))
|
|
554
|
+
|
|
555
|
+
## Getting Help
|
|
556
|
+
|
|
557
|
+
- **Documentation**: [Full docs](../README.md)
|
|
558
|
+
- **Examples**: See [Examples Index](./examples.md)
|
|
559
|
+
- **Issues**: [GitHub Issues](https://github.com/benoitpetit/societyai-package/issues)
|
|
560
|
+
- **Discussions**: [GitHub Discussions](https://github.com/benoitpetit/societyai-package/discussions)
|
|
561
|
+
|
|
562
|
+
---
|
|
563
|
+
|
|
564
|
+
**Next**: [Architecture Overview](./architecture.md) →
|