Skip to content

Commit

Permalink
Merge pull request #354 from MervinPraison/develop
Browse files Browse the repository at this point in the history
Update TypeScript Documentation and Examples with Simplified Agent Wo…
  • Loading branch information
MervinPraison authored Jan 31, 2025
2 parents f37d8fe + 6d6303d commit e45b844
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 148 deletions.
41 changes: 28 additions & 13 deletions docs/js/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,46 @@ graph LR
```
</Step>
<Step title="Create File">
Create `app.js` file
Create `app.ts` file

## Code Example

<CodeGroup>
```javascript Single Agent
const { Agent } = require('praisonai');
const agent = new Agent({ instructions: 'You are a helpful AI assistant' });
agent.start('Write a movie script about a robot in Mars');
import { Agent } from 'praisonai';

const agent = new Agent({
instructions: `You are a creative writer who writes short stories with emojis.`,
name: "StoryWriter"
});

agent.start("Write a story about a time traveler")
```

```javascript Multi Agents
const { Agent, PraisonAIAgents } = require('praisonai');
import { Agent, PraisonAIAgents } from 'praisonai';

const researchAgent = new Agent({ instructions: 'Research about AI' });
const summariseAgent = new Agent({ instructions: 'Summarise research agent\'s findings' });
const storyAgent = new Agent({
instructions: "Generate a very short story (2-3 sentences) about artificial intelligence with emojis.",
name: "StoryAgent"
});

const agents = new PraisonAIAgents({ agents: [researchAgent, summariseAgent] });
agents.start();
const summaryAgent = new Agent({
instructions: "Summarize the provided AI story in one sentence with emojis.",
name: "SummaryAgent"
});

const agents = new PraisonAIAgents({
agents: [storyAgent, summaryAgent]
});

agents.start()
```
</CodeGroup>
</Step>
<Step title="Run Script">
```bash
node app.js
npx ts-node app.ts
```
</Step>
</Steps>
Expand All @@ -103,7 +118,7 @@ agents.start();
Create and run a single agent to perform a specific task:

```typescript
import { Agent } from '../../src/agent/simple';
import { Agent } from 'praisonai';

// Single agent example - Science Explainer
const agent = new Agent({
Expand All @@ -129,7 +144,7 @@ agent.start("Why is the sky blue?")
Create and run multiple agents working together:

```typescript
import { Agent, PraisonAIAgents } from '../../src/agent/simple';
import { Agent, PraisonAIAgents } from 'praisonai';

// Create story agent
const storyAgent = new Agent({
Expand Down Expand Up @@ -168,7 +183,7 @@ agents.start()
Create agents with specific tasks and dependencies:

```typescript
import { Agent, PraisonAIAgents } from '../../src/agent/simple';
import { Agent, PraisonAIAgents } from 'praisonai';

// Create recipe agent
const recipeAgent = new Agent({
Expand Down
27 changes: 6 additions & 21 deletions src/praisonai-ts/examples/simple/multi-agent.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
import { Agent, PraisonAIAgents } from 'praisonai';

// Create story agent
const storyAgent = new Agent({
instructions: "You are a storyteller. Write a very short story (2-3 sentences) about a given topic.",
name: "StoryAgent",
verbose: true
instructions: "Generate a very short story (2-3 sentences) about artificial intelligence with emojis.",
name: "StoryAgent"
});

// Create summary agent
const summaryAgent = new Agent({
instructions: "You are an editor. Create a one-sentence summary of the given story.",
name: "SummaryAgent",
verbose: true
instructions: "Summarize the provided AI story in one sentence with emojis.",
name: "SummaryAgent"
});

// Create and start agents
const agents = new PraisonAIAgents({
agents: [storyAgent, summaryAgent],
tasks: [
"Write a short story about a cat",
"{previous_result}" // This will be replaced with the story
],
verbose: true
agents: [storyAgent, summaryAgent]
});

agents.start()
.then(results => {
console.log('\nStory:', results[0]);
console.log('\nSummary:', results[1]);
})
.catch(error => console.error('Error:', error));
agents.start()
16 changes: 3 additions & 13 deletions src/praisonai-ts/examples/simple/single-agent.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { Agent } from 'praisonai';

// Single agent example - Science Explainer
const agent = new Agent({
instructions: `You are a science expert who explains complex phenomena in simple terms.
Provide clear, accurate, and easy-to-understand explanations.`,
name: "ScienceExplainer",
verbose: true
instructions: `You are a creative writer who writes short stories with emojis.`,
name: "StoryWriter"
});

agent.start("Why is the sky blue?")
.then(response => {
console.log('\nExplanation:');
console.log(response);
})
.catch(error => {
console.error('Error:', error);
});
agent.start("Write a story about a time traveler")
52 changes: 8 additions & 44 deletions src/praisonai-ts/examples/simple/task-based-agent.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,21 @@
import { Agent, PraisonAIAgents } from 'praisonai';

// Create recipe agent
const recipeAgent = new Agent({
instructions: `You are a professional chef and nutritionist. Create 5 healthy food recipes that are both nutritious and delicious.
Each recipe should include:
1. Recipe name
2. List of ingredients with quantities
3. Step-by-step cooking instructions
4. Nutritional information
5. Health benefits
Format your response in markdown.`,
name: "RecipeAgent",
verbose: true
instructions: `You are a professional chef and nutritionist. Create 1 healthy food recipes that are both nutritious and delicious.`,
name: "RecipeAgent"
});

// Create blog agent
const blogAgent = new Agent({
instructions: `You are a food and health blogger. Write an engaging blog post about the provided recipes.
The blog post should:
1. Have an engaging title
2. Include an introduction about healthy eating
3. Discuss each recipe and its unique health benefits
4. Include tips for meal planning and preparation
5. End with a conclusion encouraging healthy eating habits
Here are the recipes to write about:
{previous_result}
Format your response in markdown.`,
name: "BlogAgent",
verbose: true
instructions: `You are a food and health blogger. Write an engaging blog post about the provided recipes`,
name: "BlogAgent"
});

// Create PraisonAIAgents instance with tasks
const agents = new PraisonAIAgents({
agents: [recipeAgent, blogAgent],
tasks: [
"Create 5 healthy and delicious recipes",
"Write a blog post about the recipes"
],
verbose: true
"Create 1 healthy and delicious recipes in 5 lines with emojis",
"Write a blog post about the recipes in 5 lines with emojis"
]
});

// Start the agents
agents.start()
.then(results => {
console.log('\nFinal Results:');
console.log('\nRecipe Task Results:');
console.log(results[0]);
console.log('\nBlog Task Results:');
console.log(results[1]);
})
.catch(error => {
console.error('Error:', error);
});
agents.start()
2 changes: 1 addition & 1 deletion src/praisonai-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "praisonai",
"version": "1.0.14",
"version": "1.0.15",
"description": "PraisonAI TypeScript AI Agents Framework - Node.js, npm, and Javascript AI Agents Framework",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/praisonai-ts/src/agent/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ export class PraisonAIAgents {
await Logger.debug(`Running agent ${i + 1}: ${agent.name}`);
await Logger.debug(`Task: ${task}`);

const result = await agent.start(task, previousResult);
// For first agent, use task directly
// For subsequent agents, append previous result to their instructions
const prompt = i === 0 ? task : `${task}\n\nHere is the input: ${previousResult}`;
const result = await agent.start(prompt, previousResult);
results.push(result);
previousResult = result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/praisonai-ts/src/llm/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function getOpenAIClient(): Promise<OpenAI> {
openAIInstance = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
await Logger.success('OpenAI client initialized');
await Logger.debug('OpenAI client initialized');
}
return openAIInstance;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ export class OpenAIService {
onToken(token);
}

await Logger.success('Stream completed successfully');
await Logger.debug('Stream completed successfully');
} catch (error) {
await Logger.error('Error in text stream', error);
throw error;
Expand Down
17 changes: 17 additions & 0 deletions src/praisonai-ts/tests/simple/multi-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Agent, PraisonAIAgents } from '../../src/agent/proxy';

const storyAgent = new Agent({
instructions: "Generate a very short story (2-3 sentences) about artificial intelligence developing emotions and creativity with emojis.",
name: "StoryAgent"
});

const summaryAgent = new Agent({
instructions: "Summarize the provided AI story in one sentence. Do not ask for input - the story will be automatically provided to you with emojis.",
name: "SummaryAgent"
});

const agents = new PraisonAIAgents({
agents: [storyAgent, summaryAgent]
});

agents.start()
22 changes: 3 additions & 19 deletions src/praisonai-ts/tests/simple/multi-agents-detailed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,15 @@ import { Agent, PraisonAIAgents } from '../../src/agent/simple';

// Create research agent
const researchAgent = new Agent({
instructions: `You are an AI research expert. Conduct comprehensive research about artificial intelligence,
focusing on:
1. Current state of AI technology
2. Major breakthroughs and developments
3. Key applications and use cases
4. Future trends and predictions
5. Ethical considerations
Format your response in markdown with clear sections and bullet points.`,
instructions: `You are an AI research expert. Conduct comprehensive research about artificial intelligence`,
name: "ResearchAgent",
verbose: true,
pretty: true
});

// Create summarize agent
const summarizeAgent = new Agent({
instructions: `You are a professional technical writer. Create a concise executive summary of the research findings about AI.
The summary should:
1. Highlight key points and insights
2. Be clear and accessible to non-technical readers
3. Include actionable takeaways
4. Be no more than 500 words
Here is the research to summarize:
{previous_result}`,
instructions: `You are a professional technical writer. Create a concise executive summary of the research findings about AI`,
name: "SummarizeAgent",
verbose: true,
pretty: true
Expand All @@ -35,7 +19,7 @@ Here is the research to summarize:
// Create PraisonAIAgents instance
const agents = new PraisonAIAgents({
agents: [researchAgent, summarizeAgent],
tasks: ["Research current state and future of AI", "Create executive summary"],
tasks: ["Research current state and future of AI with emojis", "Create executive summary with emojis"],
process: 'sequential', // Run agents one after another
verbose: true,
pretty: true
Expand Down
14 changes: 3 additions & 11 deletions src/praisonai-ts/tests/simple/single-agent.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { Agent } from '../../src/agent/proxy';

// Single agent example - Short Story Writer
const agent = new Agent({
instructions: `You are a creative writer who writes short stories.
Keep stories brief (max 100 words) and engaging.`,
name: "StoryWriter",
verbose: true,
pretty: true, // Enable pretty logging
stream: true // Enable streaming output
Keep stories brief (max 50 words) and engaging with emojis.`,
name: "StoryWriter"
});

// Write a very short story
agent.start("Write a 100-word story about a time traveler")
.catch(error => {
console.error('Error:', error);
});
agent.start("Write a story about a time traveler")
27 changes: 4 additions & 23 deletions src/praisonai-ts/tests/simple/task-based-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,14 @@ import { Agent, PraisonAIAgents } from '../../src/agent/simple';

// Create recipe agent
const recipeAgent = new Agent({
instructions: `You are a professional chef and nutritionist. Create 5 healthy food recipes that are both nutritious and delicious.
Each recipe should include:
1. Recipe name
2. List of ingredients with quantities
3. Step-by-step cooking instructions
4. Nutritional information
5. Health benefits
Format your response in markdown.`,
instructions: `You are a professional chef and nutritionist. Create 1 healthy food recipes that are both nutritious and delicious.`,
name: "RecipeAgent",
verbose: true
});

// Create blog agent
const blogAgent = new Agent({
instructions: `You are a food and health blogger. Write an engaging blog post about the provided recipes.
The blog post should:
1. Have an engaging title
2. Include an introduction about healthy eating
3. Discuss each recipe and its unique health benefits
4. Include tips for meal planning and preparation
5. End with a conclusion encouraging healthy eating habits
Here are the recipes to write about:
{previous_result}
Format your response in markdown.`,
instructions: `You are a food and health blogger. Write an engaging blog post about the provided recipes`,
name: "BlogAgent",
verbose: true
});
Expand All @@ -37,8 +18,8 @@ Format your response in markdown.`,
const agents = new PraisonAIAgents({
agents: [recipeAgent, blogAgent],
tasks: [
"Create 1 healthy and delicious recipes",
"Write a blog post about the recipes"
"Create 1 healthy and delicious recipes in 5 lines with emojis",
"Write a blog post about the recipes in 5 lines with emojis"
],
verbose: true
});
Expand Down

0 comments on commit e45b844

Please sign in to comment.