Skip to content

Commit

Permalink
Merge pull request #352 from MervinPraison/develop
Browse files Browse the repository at this point in the history
Update PraisonAI TypeScript SDK with Enhanced Logging, Examples, and …
  • Loading branch information
MervinPraison authored Jan 31, 2025
2 parents 72e2de9 + 0738b40 commit 8971706
Show file tree
Hide file tree
Showing 15 changed files with 851 additions and 124 deletions.
11 changes: 11 additions & 0 deletions src/praisonai-ts/examples/commonjs/movie-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { Agent } = require('praisonai');

const agent = new Agent({ instructions: 'You are a helpful AI assistant' });

agent.start('Write a movie script about a robot on Mars')
.then(response => {
console.log(response);
})
.catch(error => {
console.error('Error:', error);
});
19 changes: 11 additions & 8 deletions src/praisonai-ts/examples/commonjs/single-agent.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const { Agent } = require('praisonai');

// Create a simple science explainer agent
const agent = new Agent({
instructions: "You are a science expert who explains complex phenomena in simple terms.",
name: "ScienceExplainer",
// Create a simple math tutor agent
const agent = new Agent({
instructions: `You are a friendly math tutor. Help students solve basic math problems.
Keep explanations simple and clear.`,
name: "MathTutor",
verbose: true
});

// Ask a question
agent.start("Why is the sky blue?")
// Ask the agent to solve a math problem
agent.start("What is 15% of 80?")
.then(response => {
console.log('\nExplanation:');
console.log('\nMath Solution:');
console.log(response);
})
.catch(error => console.error('Error:', error));
.catch(error => {
console.error('Error:', error);
});
27 changes: 20 additions & 7 deletions src/praisonai-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "praisonai",
"version": "1.0.11",
"version": "1.0.13",
"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 Expand Up @@ -43,11 +43,12 @@
"author": "Mervin Praison",
"license": "MIT",
"devDependencies": {
"@types/figlet": "^1.7.0",
"@types/jest": "^29.5.14",
"@types/node": "^22.12.0",
"@typescript-eslint/eslint-plugin": "^8.22.0",
"@typescript-eslint/parser": "^8.22.0",
"eslint": "^8.57.0",
"eslint": "^9.19.0",
"jest": "^29.7.0",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.2",
Expand All @@ -56,11 +57,23 @@
"typescript": "^5.7.3"
},
"dependencies": {
"axios": "^1.6.7",
"dotenv": "^16.4.1",
"fast-xml-parser": "^4.3.4",
"openai": "^4.24.7",
"praisonai": "^1.0.10"
"axios": "^1.7.9",
"dotenv": "^16.4.7",
"fast-xml-parser": "^4.5.1",
"node-fetch": "^3.3.2",
"openai": "^4.81.0",
"praisonai": "^1.0.12"
},
"optionalDependencies": {
"boxen": "^7.1.1",
"chalk": "^4.1.2",
"cli-table3": "^0.6.3",
"figlet": "^1.7.0",
"ora": "^5.4.1"
},
"overrides": {
"whatwg-url": "^14.1.0",
"node-fetch": "^3.3.2"
},
"engines": {
"node": ">=14.0.0"
Expand Down
80 changes: 48 additions & 32 deletions src/praisonai-ts/src/agent/simple.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,81 @@
import { OpenAIService } from '../llm/openai';
import { Logger } from '../utils/logger';

export interface SimpleAgentConfig {
instructions: string;
name?: string;
verbose?: boolean;
pretty?: boolean;
llm?: string;
markdown?: boolean;
stream?: boolean;
}

export class Agent {
private instructions: string;
public name: string;
private verbose: boolean;
private pretty: boolean;
private llm: string;
private markdown: boolean;
private stream: boolean;
private llmService: OpenAIService;
private result: string | null = null;

constructor(config: SimpleAgentConfig) {
this.instructions = config.instructions;
this.name = config.name || `Agent_${Math.random().toString(36).substr(2, 9)}`;
this.verbose = config.verbose || false;
this.verbose = config.verbose ?? process.env.PRAISON_VERBOSE !== 'false';
this.pretty = config.pretty ?? process.env.PRAISON_PRETTY === 'true';
this.llm = config.llm || 'gpt-4o-mini';
this.markdown = config.markdown || true;
this.markdown = config.markdown ?? true;
this.stream = config.stream ?? true;
this.llmService = new OpenAIService(this.llm);

// Configure logging
Logger.setVerbose(this.verbose);
Logger.setPretty(this.pretty);
}

private createSystemPrompt(): string {
return `${this.instructions}
Please provide detailed, accurate, and helpful responses.
Format your response in markdown if appropriate.`;
let prompt = this.instructions;
if (this.markdown) {
prompt += '\nPlease format your response in markdown.';
}
return prompt;
}

async start(prompt: string, previousResult?: string): Promise<string> {
if (this.verbose) {
console.log(`Agent ${this.name} starting with prompt: ${prompt}`);
}
await Logger.debug(`Agent ${this.name} starting with prompt: ${prompt}`);

try {
// Replace placeholder with previous result if available
const finalPrompt = previousResult
? prompt.replace('{previous_result}', previousResult)
: prompt;
if (previousResult) {
prompt = prompt.replace('{{previous}}', previousResult);
}

if (this.verbose) {
console.log('Generating response (streaming)...');
let response: string;
if (this.stream) {
let fullResponse = '';
await this.llmService.streamText(
finalPrompt,
prompt,
this.createSystemPrompt(),
0.7,
(token) => process.stdout.write(token)
(token: string) => {
process.stdout.write(token);
fullResponse += token;
}
);
response = fullResponse;
} else {
response = await this.llmService.generateText(
prompt,
this.createSystemPrompt()
);
console.log('\n');
}

// Get the final response
this.result = await this.llmService.generateText(
finalPrompt,
this.createSystemPrompt()
);

return this.result;
return response;
} catch (error) {
console.error(`Error executing prompt: ${error}`);
await Logger.error('Error in agent execution', error);
throw error;
}
}
Expand All @@ -77,34 +90,39 @@ Format your response in markdown if appropriate.`;
}

getResult(): string | null {
return this.result;
return null;
}
}

export interface PraisonAIAgentsConfig {
agents: Agent[];
tasks: string[];
verbose?: boolean;
pretty?: boolean;
process?: 'sequential' | 'parallel';
}

export class PraisonAIAgents {
private agents: Agent[];
private tasks: string[];
private verbose: boolean;
private pretty: boolean;
private process: 'sequential' | 'parallel';

constructor(config: PraisonAIAgentsConfig) {
this.agents = config.agents;
this.tasks = config.tasks;
this.verbose = config.verbose || false;
this.pretty = config.pretty || false;
this.process = config.process || 'sequential';

// Configure logging
Logger.setVerbose(config.verbose ?? process.env.PRAISON_VERBOSE !== 'false');
Logger.setPretty(config.pretty ?? process.env.PRAISON_PRETTY === 'true');
}

async start(): Promise<string[]> {
if (this.verbose) {
console.log('Starting PraisonAI Agents execution...');
}
await Logger.debug('Starting PraisonAI Agents execution...');

let results: string[];

Expand Down Expand Up @@ -139,9 +157,7 @@ export class PraisonAIAgents {
const task = this.tasks[i];
const previousResult = i > 0 ? results[i - 1] : undefined;

if (this.verbose) {
console.log(`Agent ${agent.name} starting with prompt: ${task}`);
}
await Logger.info(`Agent ${agent.name} starting with prompt: ${task}`);

const result = await agent.start(task, previousResult);
results.push(result);
Expand Down
2 changes: 1 addition & 1 deletion src/praisonai-ts/src/agent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class PraisonAIAgents {
}

async start(): Promise<any[]> {
Logger.info('Starting PraisonAI Agents execution...');
Logger.debug('Starting PraisonAI Agents execution...');
Logger.debug('Starting with process mode:', this.process);

let results: any[];
Expand Down
Loading

0 comments on commit 8971706

Please sign in to comment.