mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-09 03:39:37 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
834f710cf6 | ||
|
|
ede71a7fbe | ||
|
|
7c751c88a3 | ||
|
|
b198154b71 | ||
|
|
a5bbb65017 | ||
|
|
4b6c847260 | ||
|
|
2fc459fe05 | ||
|
|
1c3c2941cc | ||
|
|
6e38a88cd3 | ||
|
|
a8fa70c455 | ||
|
|
1b4b4a10b5 | ||
|
|
1b2182968f | ||
|
|
d471d9754a | ||
|
|
1f4e2b1c5a | ||
|
|
9e081e9413 | ||
|
|
bc0449b8de | ||
|
|
c262942ba7 | ||
|
|
40e5c13f47 | ||
|
|
5eea991a60 | ||
|
|
efb927137f | ||
|
|
162223162f | ||
|
|
35b9d46bdf | ||
|
|
544672edea | ||
|
|
72dcc91b3a | ||
|
|
75e728f7d7 | ||
|
|
0213dea12b | ||
|
|
841a7b52c4 | ||
|
|
99506e80cf | ||
|
|
c0d495fd48 | ||
|
|
0e51ed0d6c | ||
|
|
32dce14f96 | ||
|
|
27aaac80c6 | ||
|
|
5a0a3983e4 | ||
|
|
400a4c934e | ||
|
|
e88b0fd47b | ||
|
|
e6c1c0679f | ||
|
|
108aee0a54 | ||
|
|
cb017aad70 | ||
|
|
ea29d3181b | ||
|
|
7f0f265671 | ||
|
|
bc1c508419 | ||
|
|
bd108ba3e7 | ||
|
|
ac5f2649df | ||
|
|
1b64762d54 | ||
|
|
df402bcfdb | ||
|
|
061c8d58b4 | ||
|
|
82c9ff80a7 | ||
|
|
f2c89c2012 | ||
|
|
f3f5f63b22 | ||
|
|
ccf9470f41 |
No files matched your search
@@ -0,0 +1,23 @@
|
||||
name: Auto Issue Comment
|
||||
|
||||
on:
|
||||
issues:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
add-comment:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
issues: write
|
||||
steps:
|
||||
- name: Add automated comment
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
github-token: ${{ secrets.GH_TOKEN }}
|
||||
script: |
|
||||
github.rest.issues.createComment({
|
||||
issue_number: context.issue.number,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: '@claude review this issue and do a detailed analysis and fix this if the existing code doesn\'t have the solution implemented. Making sure it has backward compatibility, no existing features removed. After making those changes, again use multi agents to review the applied changes. Use @web to search if you dont know any information or to find the latest documentation or to find the latest version. Run the code if you think you need to run it to test it. Minimal code change to start with if required any changes.'
|
||||
})
|
||||
@@ -0,0 +1,23 @@
|
||||
name: Auto PR Comment
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
add-comment:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
pull-requests: write
|
||||
steps:
|
||||
- name: Add automated comment
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
github-token: ${{ secrets.GH_TOKEN }}
|
||||
script: |
|
||||
github.rest.issues.createComment({
|
||||
issue_number: context.issue.number,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: '@claude review this pull request and do a detailed analysis and fix this if the existing code doesn\'t have the solution implemented. Making sure it has backward compatibility, no existing features removed. After making those changes, again use multi agents to review the applied changes. Use @web to search if you dont know any information or to find the latest documentation or to find the latest version. Run the code if you think you need to run it to test it. Minimal code change to start with if required any changes.'
|
||||
})
|
||||
@@ -151,7 +151,7 @@ jobs:
|
||||
return 'INVALID_TYPE'
|
||||
|
||||
print('🔧 Direct PraisonAI API Key Check:')
|
||||
env_api_key = os.environ.get(\\"OPENAI_API_KEY\\", \\"NOT_SET\\")
|
||||
env_api_key = os.environ.get(\"OPENAI_API_KEY\", \"NOT_SET\")
|
||||
print(f'Environment OPENAI_API_KEY: {get_key_display_value(env_api_key)}...')
|
||||
|
||||
from praisonai import PraisonAI
|
||||
@@ -163,7 +163,7 @@ jobs:
|
||||
|
||||
from praisonai.inc.models import PraisonAIModel
|
||||
|
||||
print('\\\\n🧪 Testing PraisonAIModel with explicit API key (CrewAI method):')
|
||||
print('\\n🧪 Testing PraisonAIModel with explicit API key (CrewAI method):')
|
||||
model_with_explicit_key = PraisonAIModel(
|
||||
model='openai/gpt-4o-mini',
|
||||
base_url=praisonai.config_list[0].get('base_url'),
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
flask \
|
||||
"praisonai>=2.2.46" \
|
||||
"praisonai>=2.2.49" \
|
||||
"praisonai[api]" \
|
||||
gunicorn \
|
||||
markdown
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.2.46" \
|
||||
"praisonai>=2.2.49" \
|
||||
"praisonai[chat]" \
|
||||
"embedchain[github,youtube]"
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.2.46" \
|
||||
"praisonai>=2.2.49" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[chat]" \
|
||||
"praisonai[realtime]" \
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
|
||||
# Install Python packages (using latest versions)
|
||||
RUN pip install --no-cache-dir \
|
||||
praisonai_tools \
|
||||
"praisonai>=2.2.46" \
|
||||
"praisonai>=2.2.49" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[crewai]"
|
||||
|
||||
|
||||
+2
-2
@@ -121,7 +121,7 @@ healthcheck:
|
||||
## 📦 Package Versions
|
||||
|
||||
All Docker images use consistent, up-to-date versions:
|
||||
- PraisonAI: `>=2.2.46`
|
||||
- PraisonAI: `>=2.2.49`
|
||||
- PraisonAI Agents: `>=0.0.92`
|
||||
- Python: `3.11-slim`
|
||||
|
||||
@@ -218,7 +218,7 @@ docker-compose up -d
|
||||
### Version Pinning
|
||||
To use specific versions, update the Dockerfile:
|
||||
```dockerfile
|
||||
RUN pip install "praisonai==2.2.46" "praisonaiagents==0.0.92"
|
||||
RUN pip install "praisonai==2.2.49" "praisonaiagents==0.0.92"
|
||||
```
|
||||
|
||||
## 🌐 Production Deployment
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
from praisonaiagents import AutoAgents
|
||||
from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper
|
||||
|
||||
def tavily(query: str):
|
||||
api_wrapper = TavilySearchAPIWrapper()
|
||||
results = api_wrapper.results(query=query, max_results=5)
|
||||
return results
|
||||
|
||||
# Create AutoAgents instance
|
||||
agents = AutoAgents(
|
||||
instructions="Search for information about AI Agents",
|
||||
tools=[tavily],
|
||||
process="sequential",
|
||||
verbose=True,
|
||||
max_agents=3 # Maximum number of agents to create
|
||||
)
|
||||
|
||||
# Start the agents
|
||||
result = agents.start()
|
||||
print(result)
|
||||
|
||||
##or
|
||||
|
||||
from praisonaiagents import AutoAgents
|
||||
from praisonaiagents.tools import (
|
||||
evaluate, solve_equation, convert_units,
|
||||
calculate_statistics, calculate_financial
|
||||
)
|
||||
|
||||
|
||||
def get_stock_price(company_name: str) -> str:
|
||||
"""
|
||||
Get the stock price of a company
|
||||
|
||||
Args:
|
||||
company_name (str): The name of the company
|
||||
|
||||
Returns:
|
||||
str: The stock price of the company
|
||||
"""
|
||||
|
||||
if company_name.lower() == "apple" or company_name.lower() == "aapl":
|
||||
return f"The stock price of {company_name} is 100"
|
||||
elif company_name.lower() == "google" or company_name.lower() == "googl":
|
||||
return f"The stock price of {company_name} is 200"
|
||||
else:
|
||||
return f"The stock price of {company_name} is 50"
|
||||
|
||||
# Create AutoAgents instance
|
||||
agents = AutoAgents(
|
||||
instructions="Get the stock price of google and compare it to the stock price of apple",
|
||||
tools=[evaluate, get_stock_price],
|
||||
process="sequential",
|
||||
llm="gemini/gemini-2.5-flash-lite-preview-06-17",
|
||||
self_reflect=False,
|
||||
verbose=False,
|
||||
max_agents=3 # Maximum number of agents to create
|
||||
)
|
||||
|
||||
# Start the agents
|
||||
result = agents.start()
|
||||
print(result)
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test script to verify the Gemini JSON parsing fix."""
|
||||
|
||||
from praisonaiagents import Agent
|
||||
|
||||
# Test with minimal configuration to isolate the issue
|
||||
llm_config = {
|
||||
"model": "gemini/gemini-1.5-flash-latest",
|
||||
"temperature": 0.7,
|
||||
"max_tokens": 500,
|
||||
}
|
||||
|
||||
# Create agent with self-reflection enabled
|
||||
agent = Agent(
|
||||
instructions="You are a helpful assistant. Be concise and clear.",
|
||||
llm=llm_config,
|
||||
verbose=True,
|
||||
self_reflect=True,
|
||||
max_reflect=2,
|
||||
min_reflect=1
|
||||
)
|
||||
|
||||
# Test with a simple prompt
|
||||
print("Testing Gemini with self-reflection...")
|
||||
try:
|
||||
response = agent.start("What is 2+2? Explain briefly.")
|
||||
print(f"\nFinal response: {response}")
|
||||
print("\nTest completed successfully!")
|
||||
except Exception as e:
|
||||
print(f"\nError occurred: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
@@ -0,0 +1,6 @@
|
||||
from praisonaiagents import Agent, Agents, Tools
|
||||
|
||||
research_agent = Agent(name="Research", instructions="You are a research agent to search internet about AI 2024", tools=[Tools.internet_search])
|
||||
summarise_agent = Agent(name="Summarise", instructions="You are a summarize agent to summarise in points")
|
||||
agents = Agents(agents=[research_agent, summarise_agent])
|
||||
agents.launch(path="/agents", port=3030)
|
||||
@@ -1233,7 +1233,8 @@ Your Goal: {self.goal}
|
||||
# Add to chat history and return raw response
|
||||
self.chat_history.append({"role": "user", "content": original_prompt})
|
||||
self.chat_history.append({"role": "assistant", "content": response_text})
|
||||
if self.verbose:
|
||||
# Only display interaction if not using custom LLM (to avoid double output) and verbose is True
|
||||
if self.verbose and not self._using_custom_llm:
|
||||
display_interaction(original_prompt, response_text, markdown=self.markdown,
|
||||
generation_time=time.time() - start_time, console=self.console)
|
||||
return response_text
|
||||
@@ -1243,7 +1244,9 @@ Your Goal: {self.goal}
|
||||
self.chat_history.append({"role": "assistant", "content": response_text})
|
||||
if self.verbose:
|
||||
logging.debug(f"Agent {self.name} final response: {response_text}")
|
||||
display_interaction(original_prompt, response_text, markdown=self.markdown, generation_time=time.time() - start_time, console=self.console)
|
||||
# Only display interaction if not using custom LLM (to avoid double output) and verbose is True
|
||||
if self.verbose and not self._using_custom_llm:
|
||||
display_interaction(original_prompt, response_text, markdown=self.markdown, generation_time=time.time() - start_time, console=self.console)
|
||||
# Return only reasoning content if reasoning_steps is True
|
||||
if reasoning_steps and hasattr(response.choices[0].message, 'reasoning_content'):
|
||||
# Apply guardrail to reasoning content
|
||||
@@ -1271,25 +1274,40 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
messages.append({"role": "user", "content": reflection_prompt})
|
||||
|
||||
try:
|
||||
# Check if OpenAI client is available
|
||||
if self._openai_client is None:
|
||||
# For custom LLMs, self-reflection with structured output is not supported
|
||||
if self.verbose:
|
||||
display_self_reflection(f"Agent {self.name}: Self-reflection with structured output is not supported for custom LLM providers. Skipping reflection.", console=self.console)
|
||||
# Return the original response without reflection
|
||||
self.chat_history.append({"role": "user", "content": prompt})
|
||||
self.chat_history.append({"role": "assistant", "content": response_text})
|
||||
display_interaction(prompt, response_text, markdown=self.markdown, generation_time=time.time() - start_time, console=self.console)
|
||||
return response_text
|
||||
|
||||
reflection_response = self._openai_client.sync_client.beta.chat.completions.parse(
|
||||
model=self.reflect_llm if self.reflect_llm else self.llm,
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
response_format=ReflectionOutput
|
||||
)
|
||||
# Check if we're using a custom LLM (like Gemini)
|
||||
if self._using_custom_llm or self._openai_client is None:
|
||||
# For custom LLMs, we need to handle reflection differently
|
||||
# Use non-streaming to get complete JSON response
|
||||
reflection_response = self._chat_completion(messages, temperature=temperature, tools=None, stream=False, reasoning_steps=False)
|
||||
|
||||
if not reflection_response or not reflection_response.choices:
|
||||
raise Exception("No response from reflection request")
|
||||
|
||||
reflection_text = reflection_response.choices[0].message.content.strip()
|
||||
|
||||
# Clean the JSON output
|
||||
cleaned_json = self.clean_json_output(reflection_text)
|
||||
|
||||
# Parse the JSON manually
|
||||
reflection_data = json.loads(cleaned_json)
|
||||
|
||||
# Create a reflection output object manually
|
||||
class CustomReflectionOutput:
|
||||
def __init__(self, data):
|
||||
self.reflection = data.get('reflection', '')
|
||||
self.satisfactory = data.get('satisfactory', 'no').lower()
|
||||
|
||||
reflection_output = CustomReflectionOutput(reflection_data)
|
||||
else:
|
||||
# Use OpenAI's structured output for OpenAI models
|
||||
reflection_response = self._openai_client.sync_client.beta.chat.completions.parse(
|
||||
model=self.reflect_llm if self.reflect_llm else self.llm,
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
response_format=ReflectionOutput
|
||||
)
|
||||
|
||||
reflection_output = reflection_response.choices[0].message.parsed
|
||||
reflection_output = reflection_response.choices[0].message.parsed
|
||||
|
||||
if self.verbose:
|
||||
display_self_reflection(f"Agent {self.name} self reflection (using {self.reflect_llm if self.reflect_llm else self.llm}): reflection='{reflection_output.reflection}' satisfactory='{reflection_output.satisfactory}'", console=self.console)
|
||||
@@ -1302,7 +1320,9 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
display_self_reflection("Agent marked the response as satisfactory after meeting minimum reflections", console=self.console)
|
||||
self.chat_history.append({"role": "user", "content": prompt})
|
||||
self.chat_history.append({"role": "assistant", "content": response_text})
|
||||
display_interaction(prompt, response_text, markdown=self.markdown, generation_time=time.time() - start_time, console=self.console)
|
||||
# Only display interaction if not using custom LLM (to avoid double output) and verbose is True
|
||||
if self.verbose and not self._using_custom_llm:
|
||||
display_interaction(prompt, response_text, markdown=self.markdown, generation_time=time.time() - start_time, console=self.console)
|
||||
# Apply guardrail validation after satisfactory reflection
|
||||
try:
|
||||
validated_response = self._apply_guardrail_with_retry(response_text, prompt, temperature, tools)
|
||||
@@ -1317,7 +1337,9 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
display_self_reflection("Maximum reflection count reached, returning current response", console=self.console)
|
||||
self.chat_history.append({"role": "user", "content": prompt})
|
||||
self.chat_history.append({"role": "assistant", "content": response_text})
|
||||
display_interaction(prompt, response_text, markdown=self.markdown, generation_time=time.time() - start_time, console=self.console)
|
||||
# Only display interaction if not using custom LLM (to avoid double output) and verbose is True
|
||||
if self.verbose and not self._using_custom_llm:
|
||||
display_interaction(prompt, response_text, markdown=self.markdown, generation_time=time.time() - start_time, console=self.console)
|
||||
# Apply guardrail validation after max reflections
|
||||
try:
|
||||
validated_response = self._apply_guardrail_with_retry(response_text, prompt, temperature, tools)
|
||||
@@ -1328,7 +1350,9 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
|
||||
logging.debug(f"{self.name} reflection count {reflection_count + 1}, continuing reflection process")
|
||||
messages.append({"role": "user", "content": "Now regenerate your response using the reflection you made"})
|
||||
response = self._chat_completion(messages, temperature=temperature, tools=None, stream=self.stream)
|
||||
# For custom LLMs during reflection, always use non-streaming to ensure complete responses
|
||||
use_stream = self.stream if not self._using_custom_llm else False
|
||||
response = self._chat_completion(messages, temperature=temperature, tools=None, stream=use_stream)
|
||||
response_text = response.choices[0].message.content.strip()
|
||||
reflection_count += 1
|
||||
continue # Continue the loop for more reflections
|
||||
|
||||
@@ -13,7 +13,8 @@ import logging
|
||||
import os
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from ..main import display_instruction, display_tool_call, display_interaction
|
||||
from ..llm import get_openai_client
|
||||
from ..llm import get_openai_client, LLM, OpenAIClient
|
||||
import json
|
||||
|
||||
# Define Pydantic models for structured output
|
||||
class TaskConfig(BaseModel):
|
||||
@@ -108,6 +109,8 @@ class AutoAgents(PraisonAIAgents):
|
||||
self.max_execution_time = max_execution_time
|
||||
self.max_iter = max_iter
|
||||
self.reflect_llm = reflect_llm
|
||||
self.base_url = base_url
|
||||
self.api_key = api_key
|
||||
|
||||
# Display initial instruction
|
||||
if self.verbose:
|
||||
@@ -211,9 +214,37 @@ Tools: {', '.join(agent_tools)}"""
|
||||
|
||||
return assigned_tools
|
||||
|
||||
def _validate_config(self, config: AutoAgentsConfig) -> tuple[bool, str]:
|
||||
"""
|
||||
Validate that the configuration has proper TaskConfig objects.
|
||||
|
||||
Returns:
|
||||
Tuple of (is_valid, error_message)
|
||||
"""
|
||||
for agent_idx, agent in enumerate(config.agents):
|
||||
if not hasattr(agent, 'tasks') or not agent.tasks:
|
||||
return False, f"Agent '{agent.name}' has no tasks defined"
|
||||
|
||||
for task_idx, task in enumerate(agent.tasks):
|
||||
# Check if task is a proper TaskConfig instance
|
||||
if not isinstance(task, TaskConfig):
|
||||
return False, f"Task at index {task_idx} for agent '{agent.name}' is not a proper TaskConfig object"
|
||||
|
||||
# Check required fields
|
||||
if not task.name:
|
||||
return False, f"Task at index {task_idx} for agent '{agent.name}' has no name"
|
||||
if not task.description:
|
||||
return False, f"Task at index {task_idx} for agent '{agent.name}' has no description"
|
||||
if not task.expected_output:
|
||||
return False, f"Task at index {task_idx} for agent '{agent.name}' has no expected_output"
|
||||
if task.tools is None:
|
||||
return False, f"Task at index {task_idx} for agent '{agent.name}' has no tools field"
|
||||
|
||||
return True, ""
|
||||
|
||||
def _generate_config(self) -> AutoAgentsConfig:
|
||||
"""Generate the configuration for agents and tasks"""
|
||||
prompt = f"""
|
||||
"""Generate the configuration for agents and tasks with retry logic"""
|
||||
base_prompt = f"""
|
||||
Generate a configuration for AI agents to accomplish this task: "{self.instructions}"
|
||||
|
||||
The configuration should include:
|
||||
@@ -234,40 +265,161 @@ Requirements:
|
||||
4. The process type should match the task requirements
|
||||
5. Generate maximum {self.max_agents} agents to handle this task efficiently
|
||||
|
||||
Return the configuration in a structured JSON format matching the AutoAgentsConfig schema.
|
||||
Return the configuration in a structured JSON format matching this exact schema:
|
||||
{{
|
||||
"main_instruction": "Overall goal description",
|
||||
"process_type": "sequential|workflow|hierarchical",
|
||||
"agents": [
|
||||
{{
|
||||
"name": "Agent Name",
|
||||
"role": "Agent Role",
|
||||
"goal": "Agent Goal",
|
||||
"backstory": "Agent Backstory",
|
||||
"tools": ["tool1", "tool2"],
|
||||
"tasks": [
|
||||
{{
|
||||
"name": "Task Name",
|
||||
"description": "Detailed task description",
|
||||
"expected_output": "What the task should produce",
|
||||
"tools": ["tool1", "tool2"]
|
||||
}}
|
||||
]
|
||||
}}
|
||||
]
|
||||
}}
|
||||
|
||||
IMPORTANT: Each task MUST be an object with name, description, expected_output, and tools fields, NOT a simple string.
|
||||
"""
|
||||
|
||||
try:
|
||||
# Get OpenAI client
|
||||
max_retries = 3
|
||||
last_response = None
|
||||
last_error = None
|
||||
|
||||
for attempt in range(max_retries):
|
||||
# Initialize variables for this attempt
|
||||
use_openai_structured = False
|
||||
client = None
|
||||
|
||||
# Prepare prompt for this attempt
|
||||
if attempt > 0 and last_response and last_error:
|
||||
# On retry, include the previous response and error
|
||||
prompt = f"""{base_prompt}
|
||||
|
||||
PREVIOUS ATTEMPT FAILED!
|
||||
Your previous response was:
|
||||
```json
|
||||
{last_response}
|
||||
```
|
||||
|
||||
Error: {last_error}
|
||||
|
||||
REMEMBER: Tasks MUST be objects with the following structure:
|
||||
{{
|
||||
"name": "Task Name",
|
||||
"description": "Task Description",
|
||||
"expected_output": "Expected Output",
|
||||
"tools": ["tool1", "tool2"]
|
||||
}}
|
||||
|
||||
DO NOT use strings for tasks. Each task MUST be a complete object with all four fields."""
|
||||
else:
|
||||
prompt = base_prompt
|
||||
|
||||
try:
|
||||
client = get_openai_client()
|
||||
except ValueError as e:
|
||||
# AutoAgents requires OpenAI for structured output generation
|
||||
raise ValueError(
|
||||
"AutoAgents requires OpenAI API for automatic agent generation. "
|
||||
"Please set OPENAI_API_KEY environment variable or use PraisonAIAgents class directly "
|
||||
"with manually configured agents for non-OpenAI providers."
|
||||
) from e
|
||||
# Check if we have OpenAI API and the model supports structured output
|
||||
from ..llm import supports_structured_outputs
|
||||
if self.llm and supports_structured_outputs(self.llm):
|
||||
client = get_openai_client()
|
||||
use_openai_structured = True
|
||||
except:
|
||||
# If OpenAI client is not available, we'll use the LLM class
|
||||
pass
|
||||
|
||||
try:
|
||||
if use_openai_structured and client:
|
||||
# Use OpenAI's structured output for OpenAI models (backward compatibility)
|
||||
config = client.parse_structured_output(
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a helpful assistant designed to generate AI agent configurations."},
|
||||
{"role": "user", "content": prompt}
|
||||
],
|
||||
response_format=AutoAgentsConfig,
|
||||
model=self.llm
|
||||
)
|
||||
# Store the response for potential retry
|
||||
last_response = json.dumps(config.model_dump(), indent=2)
|
||||
else:
|
||||
# Use LLM class for all other providers (Gemini, Anthropic, etc.)
|
||||
llm_instance = LLM(
|
||||
model=self.llm,
|
||||
base_url=self.base_url,
|
||||
api_key=self.api_key
|
||||
)
|
||||
|
||||
response_text = llm_instance.get_response(
|
||||
prompt=prompt,
|
||||
system_prompt="You are a helpful assistant designed to generate AI agent configurations.",
|
||||
output_pydantic=AutoAgentsConfig,
|
||||
temperature=0.7,
|
||||
stream=False,
|
||||
verbose=False
|
||||
)
|
||||
|
||||
# Store the raw response for potential retry
|
||||
last_response = response_text
|
||||
|
||||
# Parse the JSON response
|
||||
try:
|
||||
# First try to parse as is
|
||||
config_dict = json.loads(response_text)
|
||||
config = AutoAgentsConfig(**config_dict)
|
||||
except json.JSONDecodeError:
|
||||
# If that fails, try to extract JSON from the response
|
||||
# Handle cases where the model might wrap JSON in markdown blocks
|
||||
cleaned_response = response_text.strip()
|
||||
if cleaned_response.startswith("```json"):
|
||||
cleaned_response = cleaned_response[7:]
|
||||
if cleaned_response.startswith("```"):
|
||||
cleaned_response = cleaned_response[3:]
|
||||
if cleaned_response.endswith("```"):
|
||||
cleaned_response = cleaned_response[:-3]
|
||||
cleaned_response = cleaned_response.strip()
|
||||
|
||||
config_dict = json.loads(cleaned_response)
|
||||
config = AutoAgentsConfig(**config_dict)
|
||||
|
||||
response = client.beta.chat.completions.parse(
|
||||
model=self.llm,
|
||||
response_format=AutoAgentsConfig,
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a helpful assistant designed to generate AI agent configurations."},
|
||||
{"role": "user", "content": prompt}
|
||||
]
|
||||
)
|
||||
|
||||
# Ensure we have exactly max_agents number of agents
|
||||
if len(response.choices[0].message.parsed.agents) > self.max_agents:
|
||||
response.choices[0].message.parsed.agents = response.choices[0].message.parsed.agents[:self.max_agents]
|
||||
elif len(response.choices[0].message.parsed.agents) < self.max_agents:
|
||||
logging.warning(f"Generated {len(response.choices[0].message.parsed.agents)} agents, expected {self.max_agents}")
|
||||
|
||||
return response.choices[0].message.parsed
|
||||
except Exception as e:
|
||||
logging.error(f"Error generating configuration: {e}")
|
||||
raise
|
||||
# Validate the configuration
|
||||
is_valid, error_msg = self._validate_config(config)
|
||||
if not is_valid:
|
||||
last_error = error_msg
|
||||
if attempt < max_retries - 1:
|
||||
logging.warning(f"Configuration validation failed (attempt {attempt + 1}/{max_retries}): {error_msg}")
|
||||
continue
|
||||
else:
|
||||
raise ValueError(f"Configuration validation failed after {max_retries} attempts: {error_msg}")
|
||||
|
||||
# Ensure we have exactly max_agents number of agents
|
||||
if len(config.agents) > self.max_agents:
|
||||
config.agents = config.agents[:self.max_agents]
|
||||
elif len(config.agents) < self.max_agents:
|
||||
logging.warning(f"Generated {len(config.agents)} agents, expected {self.max_agents}")
|
||||
|
||||
return config
|
||||
|
||||
except ValueError as e:
|
||||
# Re-raise validation errors
|
||||
raise
|
||||
except Exception as e:
|
||||
last_error = str(e)
|
||||
if attempt < max_retries - 1:
|
||||
logging.warning(f"Error generating configuration (attempt {attempt + 1}/{max_retries}): {e}")
|
||||
continue
|
||||
else:
|
||||
logging.error(f"Error generating configuration after {max_retries} attempts: {e}")
|
||||
raise
|
||||
|
||||
# This should never be reached due to the raise statements above
|
||||
raise RuntimeError(f"Failed to generate valid configuration after {max_retries} attempts")
|
||||
|
||||
def _create_agents_and_tasks(self, config: AutoAgentsConfig) -> Tuple[List[Agent], List[Task]]:
|
||||
"""Create agents and tasks from configuration"""
|
||||
@@ -308,7 +460,9 @@ Return the configuration in a structured JSON format matching the AutoAgentsConf
|
||||
max_rpm=self.max_rpm,
|
||||
max_execution_time=self.max_execution_time,
|
||||
max_iter=self.max_iter,
|
||||
reflect_llm=self.reflect_llm
|
||||
reflect_llm=self.reflect_llm,
|
||||
base_url=self.base_url,
|
||||
api_key=self.api_key
|
||||
)
|
||||
agents.append(agent)
|
||||
|
||||
|
||||
@@ -32,6 +32,10 @@ from .openai_client import (
|
||||
ToolCall,
|
||||
process_stream_chunks
|
||||
)
|
||||
from .model_capabilities import (
|
||||
supports_structured_outputs,
|
||||
supports_streaming_with_tools
|
||||
)
|
||||
|
||||
# Ensure telemetry is disabled after import as well
|
||||
try:
|
||||
@@ -52,5 +56,7 @@ __all__ = [
|
||||
"CompletionUsage",
|
||||
"ChatCompletion",
|
||||
"ToolCall",
|
||||
"process_stream_chunks"
|
||||
"process_stream_chunks",
|
||||
"supports_structured_outputs",
|
||||
"supports_streaming_with_tools"
|
||||
]
|
||||
@@ -87,6 +87,65 @@ class LLM:
|
||||
"llama-3.2-90b-text-preview": 6144 # 8,192 actual
|
||||
}
|
||||
|
||||
def _log_llm_config(self, method_name: str, **config):
|
||||
"""Centralized debug logging for LLM configuration and parameters.
|
||||
|
||||
Args:
|
||||
method_name: The name of the method calling this logger (e.g., '__init__', 'get_response')
|
||||
**config: Configuration parameters to log
|
||||
"""
|
||||
# Check for debug logging - either global debug level OR explicit verbose mode
|
||||
verbose = config.get('verbose', self.verbose if hasattr(self, 'verbose') else False)
|
||||
should_log = logging.getLogger().getEffectiveLevel() == logging.DEBUG or (not isinstance(verbose, bool) and verbose >= 10)
|
||||
|
||||
if should_log:
|
||||
# Mask sensitive information
|
||||
safe_config = config.copy()
|
||||
if 'api_key' in safe_config:
|
||||
safe_config['api_key'] = "***" if safe_config['api_key'] is not None else None
|
||||
if 'extra_settings' in safe_config and isinstance(safe_config['extra_settings'], dict):
|
||||
safe_config['extra_settings'] = {k: v for k, v in safe_config['extra_settings'].items() if k not in ["api_key"]}
|
||||
|
||||
# Handle special formatting for certain fields
|
||||
if 'prompt' in safe_config:
|
||||
prompt = safe_config['prompt']
|
||||
# Convert to string first for consistent logging behavior
|
||||
prompt_str = str(prompt) if not isinstance(prompt, str) else prompt
|
||||
if len(prompt_str) > 100:
|
||||
safe_config['prompt'] = prompt_str[:100] + "..."
|
||||
else:
|
||||
safe_config['prompt'] = prompt_str
|
||||
if 'system_prompt' in safe_config:
|
||||
sp = safe_config['system_prompt']
|
||||
if sp and isinstance(sp, str) and len(sp) > 100:
|
||||
safe_config['system_prompt'] = sp[:100] + "..."
|
||||
if 'chat_history' in safe_config:
|
||||
ch = safe_config['chat_history']
|
||||
safe_config['chat_history'] = f"[{len(ch)} messages]" if ch else None
|
||||
if 'tools' in safe_config:
|
||||
tools = safe_config['tools']
|
||||
# Check if tools is iterable before processing
|
||||
if tools and hasattr(tools, '__iter__') and not isinstance(tools, str):
|
||||
safe_config['tools'] = [t.__name__ if hasattr(t, "__name__") else str(t) for t in tools]
|
||||
else:
|
||||
safe_config['tools'] = None
|
||||
if 'output_json' in safe_config:
|
||||
oj = safe_config['output_json']
|
||||
safe_config['output_json'] = str(oj.__class__.__name__) if oj else None
|
||||
if 'output_pydantic' in safe_config:
|
||||
op = safe_config['output_pydantic']
|
||||
safe_config['output_pydantic'] = str(op.__class__.__name__) if op else None
|
||||
|
||||
# Log based on method name - check more specific conditions first
|
||||
if method_name == '__init__':
|
||||
logging.debug(f"LLM instance initialized with: {json.dumps(safe_config, indent=2, default=str)}")
|
||||
elif "parameters" in method_name:
|
||||
logging.debug(f"{method_name}: {json.dumps(safe_config, indent=2, default=str)}")
|
||||
elif "_async" in method_name:
|
||||
logging.debug(f"LLM async instance configuration: {json.dumps(safe_config, indent=2, default=str)}")
|
||||
else:
|
||||
logging.debug(f"{method_name} configuration: {json.dumps(safe_config, indent=2, default=str)}")
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model: str,
|
||||
@@ -181,35 +240,34 @@ class LLM:
|
||||
litellm.modify_params = True
|
||||
self._setup_event_tracking(events)
|
||||
|
||||
# Log all initialization parameters when in debug mode
|
||||
if not isinstance(verbose, bool) and verbose >= 10:
|
||||
debug_info = {
|
||||
"model": self.model,
|
||||
"timeout": self.timeout,
|
||||
"temperature": self.temperature,
|
||||
"top_p": self.top_p,
|
||||
"n": self.n,
|
||||
"max_tokens": self.max_tokens,
|
||||
"presence_penalty": self.presence_penalty,
|
||||
"frequency_penalty": self.frequency_penalty,
|
||||
"logit_bias": self.logit_bias,
|
||||
"response_format": self.response_format,
|
||||
"seed": self.seed,
|
||||
"logprobs": self.logprobs,
|
||||
"top_logprobs": self.top_logprobs,
|
||||
"api_version": self.api_version,
|
||||
"stop_phrases": self.stop_phrases,
|
||||
"api_key": "***" if self.api_key else None, # Mask API key for security
|
||||
"base_url": self.base_url,
|
||||
"verbose": self.verbose,
|
||||
"markdown": self.markdown,
|
||||
"self_reflect": self.self_reflect,
|
||||
"max_reflect": self.max_reflect,
|
||||
"min_reflect": self.min_reflect,
|
||||
"reasoning_steps": self.reasoning_steps,
|
||||
"extra_settings": {k: v for k, v in self.extra_settings.items() if k not in ["api_key"]}
|
||||
}
|
||||
logging.debug(f"LLM instance initialized with: {json.dumps(debug_info, indent=2, default=str)}")
|
||||
# Log all initialization parameters when in debug mode or verbose >= 10
|
||||
self._log_llm_config(
|
||||
'__init__',
|
||||
model=self.model,
|
||||
timeout=self.timeout,
|
||||
temperature=self.temperature,
|
||||
top_p=self.top_p,
|
||||
n=self.n,
|
||||
max_tokens=self.max_tokens,
|
||||
presence_penalty=self.presence_penalty,
|
||||
frequency_penalty=self.frequency_penalty,
|
||||
logit_bias=self.logit_bias,
|
||||
response_format=self.response_format,
|
||||
seed=self.seed,
|
||||
logprobs=self.logprobs,
|
||||
top_logprobs=self.top_logprobs,
|
||||
api_version=self.api_version,
|
||||
stop_phrases=self.stop_phrases,
|
||||
api_key=self.api_key,
|
||||
base_url=self.base_url,
|
||||
verbose=self.verbose,
|
||||
markdown=self.markdown,
|
||||
self_reflect=self.self_reflect,
|
||||
max_reflect=self.max_reflect,
|
||||
min_reflect=self.min_reflect,
|
||||
reasoning_steps=self.reasoning_steps,
|
||||
extra_settings=self.extra_settings
|
||||
)
|
||||
|
||||
def _is_ollama_provider(self) -> bool:
|
||||
"""Detect if this is an Ollama provider regardless of naming convention"""
|
||||
@@ -328,8 +386,11 @@ class LLM:
|
||||
if self._is_ollama_provider():
|
||||
return False
|
||||
|
||||
# OpenAI models support streaming with tools
|
||||
if any(self.model.startswith(prefix) for prefix in ["gpt-", "o1-", "o3-"]):
|
||||
# Import the capability check function
|
||||
from .model_capabilities import supports_streaming_with_tools
|
||||
|
||||
# Check if this model supports streaming with tools
|
||||
if supports_streaming_with_tools(self.model):
|
||||
return True
|
||||
|
||||
# Anthropic Claude models support streaming with tools
|
||||
@@ -530,54 +591,53 @@ class LLM:
|
||||
"""Enhanced get_response with all OpenAI-like features"""
|
||||
logging.info(f"Getting response from {self.model}")
|
||||
# Log all self values when in debug mode
|
||||
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
|
||||
debug_info = {
|
||||
"model": self.model,
|
||||
"timeout": self.timeout,
|
||||
"temperature": self.temperature,
|
||||
"top_p": self.top_p,
|
||||
"n": self.n,
|
||||
"max_tokens": self.max_tokens,
|
||||
"presence_penalty": self.presence_penalty,
|
||||
"frequency_penalty": self.frequency_penalty,
|
||||
"logit_bias": self.logit_bias,
|
||||
"response_format": self.response_format,
|
||||
"seed": self.seed,
|
||||
"logprobs": self.logprobs,
|
||||
"top_logprobs": self.top_logprobs,
|
||||
"api_version": self.api_version,
|
||||
"stop_phrases": self.stop_phrases,
|
||||
"api_key": "***" if self.api_key else None, # Mask API key for security
|
||||
"base_url": self.base_url,
|
||||
"verbose": self.verbose,
|
||||
"markdown": self.markdown,
|
||||
"self_reflect": self.self_reflect,
|
||||
"max_reflect": self.max_reflect,
|
||||
"min_reflect": self.min_reflect,
|
||||
"reasoning_steps": self.reasoning_steps
|
||||
}
|
||||
logging.debug(f"LLM instance configuration: {json.dumps(debug_info, indent=2, default=str)}")
|
||||
|
||||
# Log the parameter values passed to get_response
|
||||
param_info = {
|
||||
"prompt": str(prompt)[:100] + "..." if isinstance(prompt, str) and len(str(prompt)) > 100 else str(prompt),
|
||||
"system_prompt": system_prompt[:100] + "..." if system_prompt and len(system_prompt) > 100 else system_prompt,
|
||||
"chat_history": f"[{len(chat_history)} messages]" if chat_history else None,
|
||||
"temperature": temperature,
|
||||
"tools": [t.__name__ if hasattr(t, "__name__") else str(t) for t in tools] if tools else None,
|
||||
"output_json": str(output_json.__class__.__name__) if output_json else None,
|
||||
"output_pydantic": str(output_pydantic.__class__.__name__) if output_pydantic else None,
|
||||
"verbose": verbose,
|
||||
"markdown": markdown,
|
||||
"self_reflect": self_reflect,
|
||||
"max_reflect": max_reflect,
|
||||
"min_reflect": min_reflect,
|
||||
"agent_name": agent_name,
|
||||
"agent_role": agent_role,
|
||||
"agent_tools": agent_tools,
|
||||
"kwargs": str(kwargs)
|
||||
}
|
||||
logging.debug(f"get_response parameters: {json.dumps(param_info, indent=2, default=str)}")
|
||||
self._log_llm_config(
|
||||
'LLM instance',
|
||||
model=self.model,
|
||||
timeout=self.timeout,
|
||||
temperature=self.temperature,
|
||||
top_p=self.top_p,
|
||||
n=self.n,
|
||||
max_tokens=self.max_tokens,
|
||||
presence_penalty=self.presence_penalty,
|
||||
frequency_penalty=self.frequency_penalty,
|
||||
logit_bias=self.logit_bias,
|
||||
response_format=self.response_format,
|
||||
seed=self.seed,
|
||||
logprobs=self.logprobs,
|
||||
top_logprobs=self.top_logprobs,
|
||||
api_version=self.api_version,
|
||||
stop_phrases=self.stop_phrases,
|
||||
api_key=self.api_key,
|
||||
base_url=self.base_url,
|
||||
verbose=self.verbose,
|
||||
markdown=self.markdown,
|
||||
self_reflect=self.self_reflect,
|
||||
max_reflect=self.max_reflect,
|
||||
min_reflect=self.min_reflect,
|
||||
reasoning_steps=self.reasoning_steps
|
||||
)
|
||||
|
||||
# Log the parameter values passed to get_response
|
||||
self._log_llm_config(
|
||||
'get_response parameters',
|
||||
prompt=prompt,
|
||||
system_prompt=system_prompt,
|
||||
chat_history=chat_history,
|
||||
temperature=temperature,
|
||||
tools=tools,
|
||||
output_json=output_json,
|
||||
output_pydantic=output_pydantic,
|
||||
verbose=verbose,
|
||||
markdown=markdown,
|
||||
self_reflect=self_reflect,
|
||||
max_reflect=max_reflect,
|
||||
min_reflect=min_reflect,
|
||||
agent_name=agent_name,
|
||||
agent_role=agent_role,
|
||||
agent_tools=agent_tools,
|
||||
kwargs=str(kwargs)
|
||||
)
|
||||
try:
|
||||
import litellm
|
||||
# This below **kwargs** is passed to .completion() directly. so reasoning_steps has to be popped. OR find alternate best way of handling this.
|
||||
@@ -689,6 +749,7 @@ class LLM:
|
||||
)
|
||||
if delta.content:
|
||||
live.update(display_generating(response_text, current_time))
|
||||
|
||||
else:
|
||||
# Non-verbose streaming
|
||||
for chunk in litellm.completion(
|
||||
@@ -702,9 +763,12 @@ class LLM:
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta:
|
||||
delta = chunk.choices[0].delta
|
||||
response_text, tool_calls = self._process_stream_delta(
|
||||
delta, response_text, tool_calls, formatted_tools
|
||||
)
|
||||
if delta.content:
|
||||
response_text += delta.content
|
||||
|
||||
# Capture tool calls from streaming chunks if provider supports it
|
||||
if formatted_tools and self._supports_streaming_tools():
|
||||
tool_calls = self._process_tool_calls_from_stream(delta, tool_calls)
|
||||
|
||||
response_text = response_text.strip()
|
||||
|
||||
@@ -745,20 +809,7 @@ class LLM:
|
||||
# Handle tool calls - Sequential tool calling logic
|
||||
if tool_calls and execute_tool_fn:
|
||||
# Convert tool_calls to a serializable format for all providers
|
||||
serializable_tool_calls = []
|
||||
for tc in tool_calls:
|
||||
if isinstance(tc, dict):
|
||||
serializable_tool_calls.append(tc) # Already a dict
|
||||
else:
|
||||
# Convert object to dict
|
||||
serializable_tool_calls.append({
|
||||
"id": tc.id,
|
||||
"type": getattr(tc, 'type', "function"),
|
||||
"function": {
|
||||
"name": tc.function.name,
|
||||
"arguments": tc.function.arguments
|
||||
}
|
||||
})
|
||||
serializable_tool_calls = self._serialize_tool_calls(tool_calls)
|
||||
messages.append({
|
||||
"role": "assistant",
|
||||
"content": response_text,
|
||||
@@ -769,20 +820,8 @@ class LLM:
|
||||
tool_results = [] # Store all tool results
|
||||
for tool_call in tool_calls:
|
||||
# Handle both object and dict access patterns
|
||||
if isinstance(tool_call, dict):
|
||||
is_ollama = self._is_ollama_provider()
|
||||
function_name, arguments, tool_call_id = self._parse_tool_call_arguments(tool_call, is_ollama)
|
||||
else:
|
||||
# Handle object-style tool calls
|
||||
try:
|
||||
function_name = tool_call.function.name
|
||||
arguments = json.loads(tool_call.function.arguments) if tool_call.function.arguments else {}
|
||||
tool_call_id = tool_call.id
|
||||
except (json.JSONDecodeError, AttributeError) as e:
|
||||
logging.error(f"Error parsing object-style tool call: {e}")
|
||||
function_name = "unknown_function"
|
||||
arguments = {}
|
||||
tool_call_id = f"tool_{id(tool_call)}"
|
||||
is_ollama = self._is_ollama_provider()
|
||||
function_name, arguments, tool_call_id = self._extract_tool_call_info(tool_call, is_ollama)
|
||||
|
||||
logging.debug(f"[TOOL_EXEC_DEBUG] About to execute tool {function_name} with args: {arguments}")
|
||||
tool_result = execute_tool_fn(function_name, arguments)
|
||||
@@ -1186,54 +1225,53 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
import litellm
|
||||
logging.info(f"Getting async response from {self.model}")
|
||||
# Log all self values when in debug mode
|
||||
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
|
||||
debug_info = {
|
||||
"model": self.model,
|
||||
"timeout": self.timeout,
|
||||
"temperature": self.temperature,
|
||||
"top_p": self.top_p,
|
||||
"n": self.n,
|
||||
"max_tokens": self.max_tokens,
|
||||
"presence_penalty": self.presence_penalty,
|
||||
"frequency_penalty": self.frequency_penalty,
|
||||
"logit_bias": self.logit_bias,
|
||||
"response_format": self.response_format,
|
||||
"seed": self.seed,
|
||||
"logprobs": self.logprobs,
|
||||
"top_logprobs": self.top_logprobs,
|
||||
"api_version": self.api_version,
|
||||
"stop_phrases": self.stop_phrases,
|
||||
"api_key": "***" if self.api_key else None, # Mask API key for security
|
||||
"base_url": self.base_url,
|
||||
"verbose": self.verbose,
|
||||
"markdown": self.markdown,
|
||||
"self_reflect": self.self_reflect,
|
||||
"max_reflect": self.max_reflect,
|
||||
"min_reflect": self.min_reflect,
|
||||
"reasoning_steps": self.reasoning_steps
|
||||
}
|
||||
logging.debug(f"LLM async instance configuration: {json.dumps(debug_info, indent=2, default=str)}")
|
||||
|
||||
# Log the parameter values passed to get_response_async
|
||||
param_info = {
|
||||
"prompt": str(prompt)[:100] + "..." if isinstance(prompt, str) and len(str(prompt)) > 100 else str(prompt),
|
||||
"system_prompt": system_prompt[:100] + "..." if system_prompt and len(system_prompt) > 100 else system_prompt,
|
||||
"chat_history": f"[{len(chat_history)} messages]" if chat_history else None,
|
||||
"temperature": temperature,
|
||||
"tools": [t.__name__ if hasattr(t, "__name__") else str(t) for t in tools] if tools else None,
|
||||
"output_json": str(output_json.__class__.__name__) if output_json else None,
|
||||
"output_pydantic": str(output_pydantic.__class__.__name__) if output_pydantic else None,
|
||||
"verbose": verbose,
|
||||
"markdown": markdown,
|
||||
"self_reflect": self_reflect,
|
||||
"max_reflect": max_reflect,
|
||||
"min_reflect": min_reflect,
|
||||
"agent_name": agent_name,
|
||||
"agent_role": agent_role,
|
||||
"agent_tools": agent_tools,
|
||||
"kwargs": str(kwargs)
|
||||
}
|
||||
logging.debug(f"get_response_async parameters: {json.dumps(param_info, indent=2, default=str)}")
|
||||
self._log_llm_config(
|
||||
'get_response_async',
|
||||
model=self.model,
|
||||
timeout=self.timeout,
|
||||
temperature=self.temperature,
|
||||
top_p=self.top_p,
|
||||
n=self.n,
|
||||
max_tokens=self.max_tokens,
|
||||
presence_penalty=self.presence_penalty,
|
||||
frequency_penalty=self.frequency_penalty,
|
||||
logit_bias=self.logit_bias,
|
||||
response_format=self.response_format,
|
||||
seed=self.seed,
|
||||
logprobs=self.logprobs,
|
||||
top_logprobs=self.top_logprobs,
|
||||
api_version=self.api_version,
|
||||
stop_phrases=self.stop_phrases,
|
||||
api_key=self.api_key,
|
||||
base_url=self.base_url,
|
||||
verbose=self.verbose,
|
||||
markdown=self.markdown,
|
||||
self_reflect=self.self_reflect,
|
||||
max_reflect=self.max_reflect,
|
||||
min_reflect=self.min_reflect,
|
||||
reasoning_steps=self.reasoning_steps
|
||||
)
|
||||
|
||||
# Log the parameter values passed to get_response_async
|
||||
self._log_llm_config(
|
||||
'get_response_async parameters',
|
||||
prompt=prompt,
|
||||
system_prompt=system_prompt,
|
||||
chat_history=chat_history,
|
||||
temperature=temperature,
|
||||
tools=tools,
|
||||
output_json=output_json,
|
||||
output_pydantic=output_pydantic,
|
||||
verbose=verbose,
|
||||
markdown=markdown,
|
||||
self_reflect=self_reflect,
|
||||
max_reflect=max_reflect,
|
||||
min_reflect=min_reflect,
|
||||
agent_name=agent_name,
|
||||
agent_role=agent_role,
|
||||
agent_tools=agent_tools,
|
||||
kwargs=str(kwargs)
|
||||
)
|
||||
reasoning_steps = kwargs.pop('reasoning_steps', self.reasoning_steps)
|
||||
litellm.set_verbose = False
|
||||
|
||||
@@ -1311,6 +1349,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
if delta.content:
|
||||
print("\033[K", end="\r")
|
||||
print(f"Generating... {time.time() - start_time:.1f}s", end="\r")
|
||||
|
||||
else:
|
||||
# Non-verbose streaming
|
||||
async for chunk in await litellm.acompletion(
|
||||
@@ -1324,9 +1363,12 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta:
|
||||
delta = chunk.choices[0].delta
|
||||
response_text, tool_calls = self._process_stream_delta(
|
||||
delta, response_text, tool_calls, formatted_tools
|
||||
)
|
||||
if delta.content:
|
||||
response_text += delta.content
|
||||
|
||||
# Capture tool calls from streaming chunks if provider supports it
|
||||
if formatted_tools and self._supports_streaming_tools():
|
||||
tool_calls = self._process_tool_calls_from_stream(delta, tool_calls)
|
||||
|
||||
response_text = response_text.strip()
|
||||
|
||||
@@ -1361,20 +1403,7 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
|
||||
if tool_calls:
|
||||
# Convert tool_calls to a serializable format for all providers
|
||||
serializable_tool_calls = []
|
||||
for tc in tool_calls:
|
||||
if isinstance(tc, dict):
|
||||
serializable_tool_calls.append(tc) # Already a dict
|
||||
else:
|
||||
# Convert object to dict
|
||||
serializable_tool_calls.append({
|
||||
"id": tc.id,
|
||||
"type": getattr(tc, 'type', "function"),
|
||||
"function": {
|
||||
"name": tc.function.name,
|
||||
"arguments": tc.function.arguments
|
||||
}
|
||||
})
|
||||
serializable_tool_calls = self._serialize_tool_calls(tool_calls)
|
||||
messages.append({
|
||||
"role": "assistant",
|
||||
"content": response_text,
|
||||
@@ -1384,20 +1413,8 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
tool_results = [] # Store all tool results
|
||||
for tool_call in tool_calls:
|
||||
# Handle both object and dict access patterns
|
||||
if isinstance(tool_call, dict):
|
||||
is_ollama = self._is_ollama_provider()
|
||||
function_name, arguments, tool_call_id = self._parse_tool_call_arguments(tool_call, is_ollama)
|
||||
else:
|
||||
# Handle object-style tool calls
|
||||
try:
|
||||
function_name = tool_call.function.name
|
||||
arguments = json.loads(tool_call.function.arguments) if tool_call.function.arguments else {}
|
||||
tool_call_id = tool_call.id
|
||||
except (json.JSONDecodeError, AttributeError) as e:
|
||||
logging.error(f"Error parsing object-style tool call: {e}")
|
||||
function_name = "unknown_function"
|
||||
arguments = {}
|
||||
tool_call_id = f"tool_{id(tool_call)}"
|
||||
is_ollama = self._is_ollama_provider()
|
||||
function_name, arguments, tool_call_id = self._extract_tool_call_info(tool_call, is_ollama)
|
||||
|
||||
tool_result = await execute_tool_fn(function_name, arguments)
|
||||
tool_results.append(tool_result) # Store the result
|
||||
@@ -1843,6 +1860,90 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
|
||||
return params
|
||||
|
||||
def _prepare_response_logging(self, temperature: float, stream: bool, verbose: bool, markdown: bool, **kwargs) -> Optional[Dict[str, Any]]:
|
||||
"""Prepare debug logging information for response methods"""
|
||||
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
|
||||
debug_info = {
|
||||
"model": self.model,
|
||||
"timeout": self.timeout,
|
||||
"temperature": temperature,
|
||||
"top_p": self.top_p,
|
||||
"n": self.n,
|
||||
"max_tokens": self.max_tokens,
|
||||
"presence_penalty": self.presence_penalty,
|
||||
"frequency_penalty": self.frequency_penalty,
|
||||
"stream": stream,
|
||||
"verbose": verbose,
|
||||
"markdown": markdown,
|
||||
"kwargs": str(kwargs)
|
||||
}
|
||||
return debug_info
|
||||
return None
|
||||
|
||||
def _process_streaming_chunk(self, chunk) -> Optional[str]:
|
||||
"""Extract content from a streaming chunk"""
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
return chunk.choices[0].delta.content
|
||||
return None
|
||||
|
||||
def _process_tool_calls_from_stream(self, delta, tool_calls: List[Dict]) -> List[Dict]:
|
||||
"""Process tool calls from streaming delta chunks.
|
||||
|
||||
This handles the accumulation of tool call data from streaming chunks,
|
||||
building up the complete tool call information incrementally.
|
||||
"""
|
||||
if hasattr(delta, 'tool_calls') and delta.tool_calls:
|
||||
for tc in delta.tool_calls:
|
||||
if tc.index >= len(tool_calls):
|
||||
tool_calls.append({
|
||||
"id": tc.id,
|
||||
"type": "function",
|
||||
"function": {"name": "", "arguments": ""}
|
||||
})
|
||||
if tc.function.name:
|
||||
tool_calls[tc.index]["function"]["name"] = tc.function.name
|
||||
if tc.function.arguments:
|
||||
tool_calls[tc.index]["function"]["arguments"] += tc.function.arguments
|
||||
return tool_calls
|
||||
|
||||
def _serialize_tool_calls(self, tool_calls) -> List[Dict]:
|
||||
"""Convert tool calls to a serializable format for all providers."""
|
||||
serializable_tool_calls = []
|
||||
for tc in tool_calls:
|
||||
if isinstance(tc, dict):
|
||||
serializable_tool_calls.append(tc) # Already a dict
|
||||
else:
|
||||
# Convert object to dict
|
||||
serializable_tool_calls.append({
|
||||
"id": tc.id,
|
||||
"type": getattr(tc, 'type', "function"),
|
||||
"function": {
|
||||
"name": tc.function.name,
|
||||
"arguments": tc.function.arguments
|
||||
}
|
||||
})
|
||||
return serializable_tool_calls
|
||||
|
||||
def _extract_tool_call_info(self, tool_call, is_ollama: bool = False) -> tuple:
|
||||
"""Extract function name, arguments, and tool_call_id from a tool call.
|
||||
|
||||
Handles both dict and object formats for tool calls.
|
||||
"""
|
||||
if isinstance(tool_call, dict):
|
||||
return self._parse_tool_call_arguments(tool_call, is_ollama)
|
||||
else:
|
||||
# Handle object-style tool calls
|
||||
try:
|
||||
function_name = tool_call.function.name
|
||||
arguments = json.loads(tool_call.function.arguments) if tool_call.function.arguments else {}
|
||||
tool_call_id = tool_call.id
|
||||
except (json.JSONDecodeError, AttributeError) as e:
|
||||
logging.error(f"Error parsing object-style tool call: {e}")
|
||||
function_name = "unknown_function"
|
||||
arguments = {}
|
||||
tool_call_id = f"tool_{id(tool_call)}"
|
||||
return function_name, arguments, tool_call_id
|
||||
|
||||
# Response without tool calls
|
||||
def response(
|
||||
self,
|
||||
@@ -1867,22 +1968,21 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
logger.debug("Using synchronous response function")
|
||||
|
||||
# Log all self values when in debug mode
|
||||
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
|
||||
debug_info = {
|
||||
"model": self.model,
|
||||
"timeout": self.timeout,
|
||||
"temperature": temperature,
|
||||
"top_p": self.top_p,
|
||||
"n": self.n,
|
||||
"max_tokens": self.max_tokens,
|
||||
"presence_penalty": self.presence_penalty,
|
||||
"frequency_penalty": self.frequency_penalty,
|
||||
"stream": stream,
|
||||
"verbose": verbose,
|
||||
"markdown": markdown,
|
||||
"kwargs": str(kwargs)
|
||||
}
|
||||
logger.debug(f"Response method configuration: {json.dumps(debug_info, indent=2, default=str)}")
|
||||
self._log_llm_config(
|
||||
'Response method',
|
||||
model=self.model,
|
||||
timeout=self.timeout,
|
||||
temperature=temperature,
|
||||
top_p=self.top_p,
|
||||
n=self.n,
|
||||
max_tokens=self.max_tokens,
|
||||
presence_penalty=self.presence_penalty,
|
||||
frequency_penalty=self.frequency_penalty,
|
||||
stream=stream,
|
||||
verbose=verbose,
|
||||
markdown=markdown,
|
||||
kwargs=str(kwargs)
|
||||
)
|
||||
|
||||
# Build messages list using shared helper (simplified version without JSON output)
|
||||
messages, _ = self._build_messages(
|
||||
@@ -1891,42 +1991,29 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
)
|
||||
|
||||
# Get response from LiteLLM
|
||||
response_text = ""
|
||||
completion_params = self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=stream,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
if stream:
|
||||
response_text = ""
|
||||
if verbose:
|
||||
with Live(display_generating("", start_time), console=console or self.console, refresh_per_second=4) as live:
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=True,
|
||||
**kwargs
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
content = chunk.choices[0].delta.content
|
||||
for chunk in litellm.completion(**completion_params):
|
||||
content = self._process_streaming_chunk(chunk)
|
||||
if content:
|
||||
response_text += content
|
||||
live.update(display_generating(response_text, start_time))
|
||||
else:
|
||||
for chunk in litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=True,
|
||||
**kwargs
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
response_text += chunk.choices[0].delta.content
|
||||
for chunk in litellm.completion(**completion_params):
|
||||
content = self._process_streaming_chunk(chunk)
|
||||
if content:
|
||||
response_text += content
|
||||
else:
|
||||
response = litellm.completion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=False,
|
||||
**kwargs
|
||||
)
|
||||
)
|
||||
response = litellm.completion(**completion_params)
|
||||
response_text = response.choices[0].message.content.strip()
|
||||
|
||||
if verbose:
|
||||
@@ -1967,23 +2054,23 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
|
||||
logger.debug("Using asynchronous response function")
|
||||
|
||||
|
||||
# Log all self values when in debug mode
|
||||
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
|
||||
debug_info = {
|
||||
"model": self.model,
|
||||
"timeout": self.timeout,
|
||||
"temperature": temperature,
|
||||
"top_p": self.top_p,
|
||||
"n": self.n,
|
||||
"max_tokens": self.max_tokens,
|
||||
"presence_penalty": self.presence_penalty,
|
||||
"frequency_penalty": self.frequency_penalty,
|
||||
"stream": stream,
|
||||
"verbose": verbose,
|
||||
"markdown": markdown,
|
||||
"kwargs": str(kwargs)
|
||||
}
|
||||
logger.debug(f"Async response method configuration: {json.dumps(debug_info, indent=2, default=str)}")
|
||||
self._log_llm_config(
|
||||
'Async response method',
|
||||
model=self.model,
|
||||
timeout=self.timeout,
|
||||
temperature=temperature,
|
||||
top_p=self.top_p,
|
||||
n=self.n,
|
||||
max_tokens=self.max_tokens,
|
||||
presence_penalty=self.presence_penalty,
|
||||
frequency_penalty=self.frequency_penalty,
|
||||
stream=stream,
|
||||
verbose=verbose,
|
||||
markdown=markdown,
|
||||
kwargs=str(kwargs)
|
||||
)
|
||||
|
||||
# Build messages list using shared helper (simplified version without JSON output)
|
||||
messages, _ = self._build_messages(
|
||||
@@ -1992,42 +2079,29 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
|
||||
)
|
||||
|
||||
# Get response from LiteLLM
|
||||
response_text = ""
|
||||
completion_params = self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=stream,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
if stream:
|
||||
response_text = ""
|
||||
if verbose:
|
||||
with Live(display_generating("", start_time), console=console or self.console, refresh_per_second=4) as live:
|
||||
async for chunk in await litellm.acompletion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=True,
|
||||
**kwargs
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
content = chunk.choices[0].delta.content
|
||||
async for chunk in await litellm.acompletion(**completion_params):
|
||||
content = self._process_streaming_chunk(chunk)
|
||||
if content:
|
||||
response_text += content
|
||||
live.update(display_generating(response_text, start_time))
|
||||
else:
|
||||
async for chunk in await litellm.acompletion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=True,
|
||||
**kwargs
|
||||
)
|
||||
):
|
||||
if chunk and chunk.choices and chunk.choices[0].delta.content:
|
||||
response_text += chunk.choices[0].delta.content
|
||||
async for chunk in await litellm.acompletion(**completion_params):
|
||||
content = self._process_streaming_chunk(chunk)
|
||||
if content:
|
||||
response_text += content
|
||||
else:
|
||||
response = await litellm.acompletion(
|
||||
**self._build_completion_params(
|
||||
messages=messages,
|
||||
temperature=temperature,
|
||||
stream=False,
|
||||
**kwargs
|
||||
)
|
||||
)
|
||||
response = await litellm.acompletion(**completion_params)
|
||||
response_text = response.choices[0].message.content.strip()
|
||||
|
||||
if verbose:
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
"""
|
||||
Model capabilities configuration for different LLM providers.
|
||||
This module defines which models support specific features like structured outputs.
|
||||
"""
|
||||
|
||||
# Models that support OpenAI-style structured outputs (response_format with Pydantic models)
|
||||
MODELS_SUPPORTING_STRUCTURED_OUTPUTS = {
|
||||
# OpenAI models
|
||||
"gpt-4o",
|
||||
"gpt-4o-mini",
|
||||
"gpt-4-turbo",
|
||||
"gpt-4-turbo-preview",
|
||||
"gpt-4-turbo-2024-04-09",
|
||||
"gpt-4-1106-preview",
|
||||
"gpt-4-0125-preview",
|
||||
"gpt-3.5-turbo",
|
||||
"gpt-3.5-turbo-1106",
|
||||
"gpt-3.5-turbo-0125",
|
||||
|
||||
# New/Future OpenAI models (as mentioned by user)
|
||||
"codex-mini",
|
||||
"o3-pro",
|
||||
"gpt-4.5-preview",
|
||||
"o3-mini",
|
||||
"o1",
|
||||
"o1-preview",
|
||||
"o1-mini",
|
||||
"gpt-4.1",
|
||||
"gpt-4.1-nano",
|
||||
"gpt-4.1-mini",
|
||||
"o4-mini",
|
||||
"o3",
|
||||
}
|
||||
|
||||
# Models that explicitly DON'T support structured outputs
|
||||
MODELS_NOT_SUPPORTING_STRUCTURED_OUTPUTS = {
|
||||
# Audio preview models
|
||||
"gpt-4o-audio-preview",
|
||||
"gpt-4o-mini-audio-preview",
|
||||
|
||||
# Legacy o1 models (don't support system messages either)
|
||||
"o1-preview-2024-09-12",
|
||||
"o1-mini-2024-09-12",
|
||||
}
|
||||
|
||||
|
||||
def supports_structured_outputs(model_name: str) -> bool:
|
||||
"""
|
||||
Check if a model supports OpenAI-style structured outputs.
|
||||
|
||||
Args:
|
||||
model_name: The name of the model to check
|
||||
|
||||
Returns:
|
||||
bool: True if the model supports structured outputs, False otherwise
|
||||
"""
|
||||
if not model_name:
|
||||
return False
|
||||
|
||||
# First check if it's explicitly in the NOT supporting list
|
||||
if model_name in MODELS_NOT_SUPPORTING_STRUCTURED_OUTPUTS:
|
||||
return False
|
||||
|
||||
# Then check if it's in the supporting list
|
||||
if model_name in MODELS_SUPPORTING_STRUCTURED_OUTPUTS:
|
||||
return True
|
||||
|
||||
# For models with version suffixes, check the base model name
|
||||
base_model = model_name.split('-2024-')[0].split('-2025-')[0]
|
||||
if base_model in MODELS_SUPPORTING_STRUCTURED_OUTPUTS:
|
||||
return True
|
||||
|
||||
# Default to False for unknown models
|
||||
return False
|
||||
|
||||
|
||||
def supports_streaming_with_tools(model_name: str) -> bool:
|
||||
"""
|
||||
Check if a model supports streaming when tools are provided.
|
||||
Most models that support structured outputs also support streaming with tools.
|
||||
|
||||
Args:
|
||||
model_name: The name of the model to check
|
||||
|
||||
Returns:
|
||||
bool: True if the model supports streaming with tools, False otherwise
|
||||
"""
|
||||
# For now, use the same logic as structured outputs
|
||||
# In the future, this could be a separate list if needed
|
||||
return supports_structured_outputs(model_name)
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "praisonaiagents"
|
||||
version = "0.0.119"
|
||||
version = "0.0.123"
|
||||
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
|
||||
requires-python = ">=3.10"
|
||||
authors = [
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
from praisonaiagents import Agent, MCP
|
||||
|
||||
search_agent = Agent(
|
||||
instructions="""You are a Tweet.""",
|
||||
llm="openai/gpt-4o-mini",
|
||||
tools=MCP("http://localhost:8080/sse")
|
||||
)
|
||||
search_agent.launch(path="/tweet", port=3030)
|
||||
@@ -0,0 +1,4 @@
|
||||
from praisonaiagents import Agent
|
||||
|
||||
agent = Agent(instructions="Create a Tweet")
|
||||
agent.launch(port=8080, protocol="mcp")
|
||||
Generated
+1
-1
@@ -2375,7 +2375,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.0.119"
|
||||
version = "0.0.123"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "mcp" },
|
||||
|
||||
@@ -3,8 +3,8 @@ class Praisonai < Formula
|
||||
|
||||
desc "AI tools for various AI applications"
|
||||
homepage "https://github.com/MervinPraison/PraisonAI"
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.46.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.46.tar.gz | shasum -a 256`.split.first
|
||||
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.49.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.49.tar.gz | shasum -a 256`.split.first
|
||||
license "MIT"
|
||||
|
||||
depends_on "[email protected]"
|
||||
|
||||
@@ -57,7 +57,7 @@ class CloudDeployer:
|
||||
file.write("FROM python:3.11-slim\n")
|
||||
file.write("WORKDIR /app\n")
|
||||
file.write("COPY . .\n")
|
||||
file.write("RUN pip install flask praisonai==2.2.46 gunicorn markdown\n")
|
||||
file.write("RUN pip install flask praisonai==2.2.49 gunicorn markdown\n")
|
||||
file.write("EXPOSE 8080\n")
|
||||
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "PraisonAI"
|
||||
version = "2.2.46"
|
||||
version = "2.2.49"
|
||||
description = "PraisonAI is an AI Agents Framework with Self Reflection. PraisonAI application combines PraisonAI Agents, AutoGen, and CrewAI into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customisation, and efficient human-agent collaboration."
|
||||
readme = "README.md"
|
||||
license = ""
|
||||
@@ -12,7 +12,7 @@ dependencies = [
|
||||
"rich>=13.7",
|
||||
"markdown>=3.5",
|
||||
"pyparsing>=3.0.0",
|
||||
"praisonaiagents>=0.0.119",
|
||||
"praisonaiagents>=0.0.123",
|
||||
"python-dotenv>=0.19.0",
|
||||
"instructor>=1.3.3",
|
||||
"PyYAML>=6.0",
|
||||
@@ -95,7 +95,7 @@ autogen = ["pyautogen>=0.2.19", "praisonai-tools>=0.0.15", "crewai"]
|
||||
|
||||
[tool.poetry]
|
||||
name = "PraisonAI"
|
||||
version = "2.2.46"
|
||||
version = "2.2.49"
|
||||
description = "PraisonAI is an AI Agents Framework with Self Reflection. PraisonAI application combines PraisonAI Agents, AutoGen, and CrewAI into a low-code solution for building and managing multi-agent LLM systems, focusing on simplicity, customisation, and efficient human-agent collaboration."
|
||||
authors = ["Mervin Praison"]
|
||||
license = ""
|
||||
@@ -113,7 +113,7 @@ python = ">=3.10,<3.13"
|
||||
rich = ">=13.7"
|
||||
markdown = ">=3.5"
|
||||
pyparsing = ">=3.0.0"
|
||||
praisonaiagents = ">=0.0.119"
|
||||
praisonaiagents = ">=0.0.123"
|
||||
python-dotenv = ">=0.19.0"
|
||||
instructor = ">=1.3.3"
|
||||
PyYAML = ">=6.0"
|
||||
|
||||
@@ -0,0 +1,518 @@
|
||||
"""
|
||||
Unit tests for AutoAgents class
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch, MagicMock
|
||||
from praisonaiagents.agents.autoagents import AutoAgents, AutoAgentsConfig, AgentConfig, TaskConfig
|
||||
from praisonaiagents.llm import OpenAIClient, LLM
|
||||
import json
|
||||
import logging
|
||||
|
||||
|
||||
class TestAutoAgents:
|
||||
"""Test suite for AutoAgents functionality"""
|
||||
|
||||
@pytest.fixture
|
||||
def sample_valid_config(self):
|
||||
"""Fixture providing a valid AutoAgentsConfig"""
|
||||
return AutoAgentsConfig(
|
||||
main_instruction="Create a blog post about AI",
|
||||
process_type="sequential",
|
||||
agents=[
|
||||
AgentConfig(
|
||||
name="Research Agent",
|
||||
role="Researcher",
|
||||
goal="Research AI topics",
|
||||
backstory="Expert researcher",
|
||||
tools=["web_search"],
|
||||
tasks=[
|
||||
TaskConfig(
|
||||
name="Research Task",
|
||||
description="Research latest AI trends",
|
||||
expected_output="Research summary",
|
||||
tools=["web_search"]
|
||||
)
|
||||
]
|
||||
),
|
||||
AgentConfig(
|
||||
name="Writer Agent",
|
||||
role="Writer",
|
||||
goal="Write blog post",
|
||||
backstory="Professional writer",
|
||||
tools=["text_editor"],
|
||||
tasks=[
|
||||
TaskConfig(
|
||||
name="Writing Task",
|
||||
description="Write blog post based on research",
|
||||
expected_output="Complete blog post",
|
||||
tools=["text_editor"]
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
@pytest.fixture
|
||||
def sample_invalid_config_string_tasks(self):
|
||||
"""Fixture providing an invalid config with string tasks"""
|
||||
return {
|
||||
"main_instruction": "Create a blog post about AI",
|
||||
"process_type": "sequential",
|
||||
"agents": [
|
||||
{
|
||||
"name": "Research Agent",
|
||||
"role": "Researcher",
|
||||
"goal": "Research AI topics",
|
||||
"backstory": "Expert researcher",
|
||||
"tools": ["web_search"],
|
||||
"tasks": ["Research latest AI trends"] # Invalid: string instead of object
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def sample_invalid_config_missing_fields(self):
|
||||
"""Fixture providing an invalid config with missing task fields"""
|
||||
return AutoAgentsConfig(
|
||||
main_instruction="Create a blog post about AI",
|
||||
process_type="sequential",
|
||||
agents=[
|
||||
AgentConfig(
|
||||
name="Research Agent",
|
||||
role="Researcher",
|
||||
goal="Research AI topics",
|
||||
backstory="Expert researcher",
|
||||
tools=["web_search"],
|
||||
tasks=[
|
||||
TaskConfig(
|
||||
name="Research Task",
|
||||
description="", # Missing description
|
||||
expected_output="Research summary",
|
||||
tools=["web_search"]
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
@pytest.fixture
|
||||
def mock_tools(self):
|
||||
"""Fixture providing mock tools"""
|
||||
web_search = Mock()
|
||||
web_search.__name__ = "web_search"
|
||||
|
||||
text_editor = Mock()
|
||||
text_editor.__name__ = "text_editor"
|
||||
|
||||
return [web_search, text_editor]
|
||||
|
||||
def test_validate_config_success(self, sample_valid_config):
|
||||
"""Test successful validation of a valid configuration"""
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Test instructions",
|
||||
max_agents=3
|
||||
)
|
||||
|
||||
is_valid, error_msg = auto_agents._validate_config(sample_valid_config)
|
||||
|
||||
assert is_valid is True
|
||||
assert error_msg == ""
|
||||
|
||||
def test_validate_config_failure_not_taskconfig(self):
|
||||
"""Test validation failure when task is not a TaskConfig instance"""
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Test instructions",
|
||||
max_agents=3
|
||||
)
|
||||
|
||||
# Create a config with a non-TaskConfig task
|
||||
config = AutoAgentsConfig(
|
||||
main_instruction="Test",
|
||||
process_type="sequential",
|
||||
agents=[
|
||||
AgentConfig(
|
||||
name="Agent",
|
||||
role="Role",
|
||||
goal="Goal",
|
||||
backstory="Story",
|
||||
tools=[],
|
||||
tasks=["This is a string task"] # This will cause validation to fail
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
# Mock the task to bypass Pydantic validation for testing
|
||||
config.agents[0].tasks = ["This is a string task"]
|
||||
|
||||
is_valid, error_msg = auto_agents._validate_config(config)
|
||||
|
||||
assert is_valid is False
|
||||
assert "is not a proper TaskConfig object" in error_msg
|
||||
|
||||
def test_validate_config_failure_missing_name(self):
|
||||
"""Test validation failure when task is missing name"""
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Test instructions",
|
||||
max_agents=3
|
||||
)
|
||||
|
||||
config = AutoAgentsConfig(
|
||||
main_instruction="Test",
|
||||
process_type="sequential",
|
||||
agents=[
|
||||
AgentConfig(
|
||||
name="Agent",
|
||||
role="Role",
|
||||
goal="Goal",
|
||||
backstory="Story",
|
||||
tools=[],
|
||||
tasks=[
|
||||
TaskConfig(
|
||||
name="", # Empty name
|
||||
description="Description",
|
||||
expected_output="Output",
|
||||
tools=[]
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
is_valid, error_msg = auto_agents._validate_config(config)
|
||||
|
||||
assert is_valid is False
|
||||
assert "has no name" in error_msg
|
||||
|
||||
def test_validate_config_failure_missing_description(self, sample_invalid_config_missing_fields):
|
||||
"""Test validation failure when task is missing description"""
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Test instructions",
|
||||
max_agents=3
|
||||
)
|
||||
|
||||
is_valid, error_msg = auto_agents._validate_config(sample_invalid_config_missing_fields)
|
||||
|
||||
assert is_valid is False
|
||||
assert "has no description" in error_msg
|
||||
|
||||
@patch('praisonaiagents.agents.autoagents.get_openai_client')
|
||||
def test_generate_config_openai_success(self, mock_get_client, sample_valid_config, mock_tools):
|
||||
"""Test successful config generation using OpenAI structured output"""
|
||||
# Mock OpenAI client
|
||||
mock_client = Mock(spec=OpenAIClient)
|
||||
mock_client.parse_structured_output.return_value = sample_valid_config
|
||||
mock_get_client.return_value = mock_client
|
||||
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Create a blog post about AI",
|
||||
tools=mock_tools,
|
||||
max_agents=2,
|
||||
llm="gpt-4"
|
||||
)
|
||||
|
||||
# The config should have been generated in __init__
|
||||
assert hasattr(auto_agents, 'agents')
|
||||
assert len(auto_agents.agents) == 2
|
||||
|
||||
# Verify OpenAI client was called
|
||||
mock_client.parse_structured_output.assert_called_once()
|
||||
|
||||
@patch('praisonaiagents.agents.autoagents.LLM')
|
||||
def test_generate_config_llm_success(self, mock_llm_class, sample_valid_config, mock_tools):
|
||||
"""Test successful config generation using generic LLM"""
|
||||
# Mock LLM instance
|
||||
mock_llm = Mock()
|
||||
mock_llm.response.return_value = json.dumps(sample_valid_config.model_dump())
|
||||
mock_llm_class.return_value = mock_llm
|
||||
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Create a blog post about AI",
|
||||
tools=mock_tools,
|
||||
max_agents=2,
|
||||
llm="claude-3" # Non-OpenAI model
|
||||
)
|
||||
|
||||
# The config should have been generated in __init__
|
||||
assert hasattr(auto_agents, 'agents')
|
||||
assert len(auto_agents.agents) == 2
|
||||
|
||||
# Verify LLM was called
|
||||
mock_llm.response.assert_called_once()
|
||||
|
||||
@patch('praisonaiagents.agents.autoagents.LLM')
|
||||
def test_generate_config_with_markdown_response(self, mock_llm_class, sample_valid_config, mock_tools):
|
||||
"""Test config generation when LLM returns markdown-wrapped JSON"""
|
||||
# Mock LLM instance
|
||||
mock_llm = Mock()
|
||||
# Return JSON wrapped in markdown code block
|
||||
wrapped_response = f"```json\n{json.dumps(sample_valid_config.model_dump(), indent=2)}\n```"
|
||||
mock_llm.response.return_value = wrapped_response
|
||||
mock_llm_class.return_value = mock_llm
|
||||
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Create a blog post about AI",
|
||||
tools=mock_tools,
|
||||
max_agents=2,
|
||||
llm="claude-3"
|
||||
)
|
||||
|
||||
# The config should have been generated successfully despite markdown wrapping
|
||||
assert hasattr(auto_agents, 'agents')
|
||||
assert len(auto_agents.agents) == 2
|
||||
|
||||
@patch('praisonaiagents.agents.autoagents.get_openai_client')
|
||||
def test_generate_config_retry_on_validation_failure(self, mock_get_client, sample_valid_config, mock_tools):
|
||||
"""Test retry mechanism when validation fails"""
|
||||
# Mock OpenAI client
|
||||
mock_client = Mock(spec=OpenAIClient)
|
||||
|
||||
# First attempt: return invalid config with string tasks
|
||||
invalid_config = AutoAgentsConfig(
|
||||
main_instruction="Test",
|
||||
process_type="sequential",
|
||||
agents=[
|
||||
AgentConfig(
|
||||
name="Agent",
|
||||
role="Role",
|
||||
goal="Goal",
|
||||
backstory="Story",
|
||||
tools=[],
|
||||
tasks=[] # This will be mocked to contain strings
|
||||
)
|
||||
]
|
||||
)
|
||||
# Mock the tasks to be strings (bypassing Pydantic validation)
|
||||
invalid_config.agents[0].tasks = ["Invalid string task"]
|
||||
|
||||
# Second attempt: return valid config
|
||||
mock_client.parse_structured_output.side_effect = [invalid_config, sample_valid_config]
|
||||
mock_get_client.return_value = mock_client
|
||||
|
||||
with patch('logging.warning') as mock_warning:
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Create a blog post about AI",
|
||||
tools=mock_tools,
|
||||
max_agents=2,
|
||||
llm="gpt-4"
|
||||
)
|
||||
|
||||
# Should succeed after retry
|
||||
assert hasattr(auto_agents, 'agents')
|
||||
assert len(auto_agents.agents) == 2
|
||||
|
||||
# Verify retry occurred
|
||||
assert mock_client.parse_structured_output.call_count == 2
|
||||
mock_warning.assert_called_once()
|
||||
assert "Configuration validation failed" in str(mock_warning.call_args)
|
||||
|
||||
@patch('praisonaiagents.agents.autoagents.get_openai_client')
|
||||
def test_generate_config_max_retries_exceeded(self, mock_get_client, mock_tools):
|
||||
"""Test that max retries are properly enforced"""
|
||||
# Mock OpenAI client
|
||||
mock_client = Mock(spec=OpenAIClient)
|
||||
|
||||
# Always return invalid config
|
||||
invalid_config = AutoAgentsConfig(
|
||||
main_instruction="Test",
|
||||
process_type="sequential",
|
||||
agents=[
|
||||
AgentConfig(
|
||||
name="Agent",
|
||||
role="Role",
|
||||
goal="Goal",
|
||||
backstory="Story",
|
||||
tools=[],
|
||||
tasks=[]
|
||||
)
|
||||
]
|
||||
)
|
||||
# Mock the tasks to be strings
|
||||
invalid_config.agents[0].tasks = ["Invalid string task"]
|
||||
|
||||
mock_client.parse_structured_output.return_value = invalid_config
|
||||
mock_get_client.return_value = mock_client
|
||||
|
||||
with pytest.raises(ValueError, match="Configuration validation failed after 3 attempts"):
|
||||
AutoAgents(
|
||||
instructions="Create a blog post about AI",
|
||||
tools=mock_tools,
|
||||
max_agents=2,
|
||||
llm="gpt-4"
|
||||
)
|
||||
|
||||
# Verify all retries were attempted
|
||||
assert mock_client.parse_structured_output.call_count == 3
|
||||
|
||||
@patch('praisonaiagents.agents.autoagents.get_openai_client')
|
||||
def test_max_agents_truncation(self, mock_get_client, mock_tools):
|
||||
"""Test that agents are truncated when exceeding max_agents"""
|
||||
# Create config with 4 agents
|
||||
config_with_many_agents = AutoAgentsConfig(
|
||||
main_instruction="Test",
|
||||
process_type="sequential",
|
||||
agents=[
|
||||
AgentConfig(
|
||||
name=f"Agent {i}",
|
||||
role="Role",
|
||||
goal="Goal",
|
||||
backstory="Story",
|
||||
tools=[],
|
||||
tasks=[
|
||||
TaskConfig(
|
||||
name=f"Task {i}",
|
||||
description="Description",
|
||||
expected_output="Output",
|
||||
tools=[]
|
||||
)
|
||||
]
|
||||
)
|
||||
for i in range(4)
|
||||
]
|
||||
)
|
||||
|
||||
mock_client = Mock(spec=OpenAIClient)
|
||||
mock_client.parse_structured_output.return_value = config_with_many_agents
|
||||
mock_get_client.return_value = mock_client
|
||||
|
||||
# Create AutoAgents with max_agents=2
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Test",
|
||||
tools=mock_tools,
|
||||
max_agents=2,
|
||||
llm="gpt-4"
|
||||
)
|
||||
|
||||
# Should only have 2 agents
|
||||
assert len(auto_agents.agents) == 2
|
||||
assert auto_agents.agents[0].name == "Agent 0"
|
||||
assert auto_agents.agents[1].name == "Agent 1"
|
||||
|
||||
@patch('praisonaiagents.agents.autoagents.get_openai_client')
|
||||
def test_insufficient_agents_warning(self, mock_get_client, mock_tools):
|
||||
"""Test warning when fewer agents than max_agents are generated"""
|
||||
# Create config with only 1 agent
|
||||
config_with_few_agents = AutoAgentsConfig(
|
||||
main_instruction="Test",
|
||||
process_type="sequential",
|
||||
agents=[
|
||||
AgentConfig(
|
||||
name="Single Agent",
|
||||
role="Role",
|
||||
goal="Goal",
|
||||
backstory="Story",
|
||||
tools=[],
|
||||
tasks=[
|
||||
TaskConfig(
|
||||
name="Task",
|
||||
description="Description",
|
||||
expected_output="Output",
|
||||
tools=[]
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
mock_client = Mock(spec=OpenAIClient)
|
||||
mock_client.parse_structured_output.return_value = config_with_few_agents
|
||||
mock_get_client.return_value = mock_client
|
||||
|
||||
with patch('logging.warning') as mock_warning:
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Test",
|
||||
tools=mock_tools,
|
||||
max_agents=3,
|
||||
llm="gpt-4"
|
||||
)
|
||||
|
||||
# Should have 1 agent
|
||||
assert len(auto_agents.agents) == 1
|
||||
|
||||
# Verify warning was logged
|
||||
mock_warning.assert_called()
|
||||
warning_msg = str(mock_warning.call_args)
|
||||
assert "Generated 1 agents, expected 3" in warning_msg
|
||||
|
||||
def test_max_agents_validation(self):
|
||||
"""Test max_agents parameter validation"""
|
||||
# Test too low
|
||||
with pytest.raises(ValueError, match="max_agents must be at least 1"):
|
||||
AutoAgents(instructions="Test", max_agents=0)
|
||||
|
||||
# Test too high
|
||||
with pytest.raises(ValueError, match="max_agents cannot exceed 10"):
|
||||
AutoAgents(instructions="Test", max_agents=11)
|
||||
|
||||
@patch('praisonaiagents.agents.autoagents.LLM')
|
||||
def test_retry_with_previous_response_in_prompt(self, mock_llm_class, sample_valid_config, mock_tools):
|
||||
"""Test that retry includes previous response and error in prompt"""
|
||||
# Mock LLM instance
|
||||
mock_llm = Mock()
|
||||
|
||||
# First response: invalid JSON that will fail validation
|
||||
first_response = json.dumps({
|
||||
"main_instruction": "Test",
|
||||
"process_type": "sequential",
|
||||
"agents": [{
|
||||
"name": "Agent",
|
||||
"role": "Role",
|
||||
"goal": "Goal",
|
||||
"backstory": "Story",
|
||||
"tools": [],
|
||||
"tasks": ["String task instead of object"]
|
||||
}]
|
||||
})
|
||||
|
||||
# Second response: valid config
|
||||
second_response = json.dumps(sample_valid_config.model_dump())
|
||||
|
||||
mock_llm.response.side_effect = [first_response, second_response]
|
||||
mock_llm_class.return_value = mock_llm
|
||||
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Create a blog post about AI",
|
||||
tools=mock_tools,
|
||||
max_agents=2,
|
||||
llm="claude-3"
|
||||
)
|
||||
|
||||
# Should succeed after retry
|
||||
assert hasattr(auto_agents, 'agents')
|
||||
|
||||
# Check that the second call included the previous response
|
||||
assert mock_llm.response.call_count == 2
|
||||
second_call_prompt = mock_llm.response.call_args_list[1][1]['prompt']
|
||||
assert "PREVIOUS ATTEMPT FAILED!" in second_call_prompt
|
||||
assert first_response in second_call_prompt
|
||||
assert "Error:" in second_call_prompt
|
||||
|
||||
@patch('praisonaiagents.agents.autoagents.OpenAIClient')
|
||||
@patch('praisonaiagents.agents.autoagents.get_openai_client')
|
||||
def test_custom_api_key_and_base_url(self, mock_get_client, mock_openai_class, sample_valid_config, mock_tools):
|
||||
"""Test that custom API key and base URL are used correctly"""
|
||||
# Mock OpenAI client instance
|
||||
mock_client = Mock(spec=OpenAIClient)
|
||||
mock_client.parse_structured_output.return_value = sample_valid_config
|
||||
mock_openai_class.return_value = mock_client
|
||||
|
||||
custom_api_key = "custom-api-key"
|
||||
custom_base_url = "https://custom.api.url"
|
||||
|
||||
auto_agents = AutoAgents(
|
||||
instructions="Test",
|
||||
tools=mock_tools,
|
||||
max_agents=2,
|
||||
llm="gpt-4",
|
||||
api_key=custom_api_key,
|
||||
base_url=custom_base_url
|
||||
)
|
||||
|
||||
# Verify custom client was created with correct parameters
|
||||
mock_openai_class.assert_called_once_with(
|
||||
api_key=custom_api_key,
|
||||
base_url=custom_base_url
|
||||
)
|
||||
|
||||
# Verify get_openai_client was not called
|
||||
mock_get_client.assert_not_called()
|
||||
Generated
+5
-5
@@ -3931,7 +3931,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonai"
|
||||
version = "2.2.46"
|
||||
version = "2.2.49"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "instructor" },
|
||||
@@ -4073,7 +4073,7 @@ requires-dist = [
|
||||
{ name = "plotly", marker = "extra == 'realtime'", specifier = ">=5.24.0" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'autogen'", specifier = ">=0.0.15" },
|
||||
{ name = "praisonai-tools", marker = "extra == 'crewai'", specifier = ">=0.0.15" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.0.119" },
|
||||
{ name = "praisonaiagents", specifier = ">=0.0.123" },
|
||||
{ name = "pyautogen", marker = "extra == 'autogen'", specifier = ">=0.2.19" },
|
||||
{ name = "pydantic", marker = "extra == 'chat'", specifier = "<=2.10.1" },
|
||||
{ name = "pydantic", marker = "extra == 'code'", specifier = "<=2.10.1" },
|
||||
@@ -4130,7 +4130,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.0.119"
|
||||
version = "0.0.123"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "mcp" },
|
||||
@@ -4139,9 +4139,9 @@ dependencies = [
|
||||
{ name = "pydantic" },
|
||||
{ name = "rich" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/5f/ea/56832de856714fa3bb29da1577dcdfa7aa488b15035341329888a4e28c47/praisonaiagents-0.0.119.tar.gz", hash = "sha256:2e16aab996669ff2b0051cf9a688d6482e5560457422506d991336e7fcc357da", size = 171774 }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/dc/d0/1d9b8a60284b206184dc0d877cc1df8d800f79d89f82c5ff88dc60202d4b/praisonaiagents-0.0.123.tar.gz", hash = "sha256:9dd2496d86de0311135f0d075a313c66979ada8104d87fdb36701b4623c47e1b", size = 174756 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/c1/bf/ea47cf5843d128ab24973a7a5260e72503be00707435d1fca79e6acc7a19/praisonaiagents-0.0.119-py3-none-any.whl", hash = "sha256:e4ddb1da0c2fdf38c25e2f918fc59f686ac5d142884c34506fd53550813edd51", size = 192290 },
|
||||
{ url = "https://files.pythonhosted.org/packages/ce/a2/a3620d28a342077d3e0a16f09bbdca37f0183279e8bd0db0e08ee6fdbb87/praisonaiagents-0.0.123-py3-none-any.whl", hash = "sha256:f2b79ce3944d1f1f683e5e64c20c022fc5951f67917f9797a51bf2c0f8162401", size = 195581 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
Reference in new issue
Block a user