Compare commits

...
3 Commits
Author SHA1 Message Date
Mervin Praison fd2569e642 chore: update PraisonAI and related package versions to 2.2.45 and 0.0.118
- Updated PraisonAI version in Dockerfiles and README to 2.2.45
- Updated version in pyproject.toml and uv.lock for PraisonAI and PraisonAI agents to 0.0.118
- Adjusted deployment script to reflect the new PraisonAI version
2025-07-10 22:08:47 +01:00
Mervin Praison 58efb4b4cd Merge pull request #783 from MervinPraison/claude/issue-754-20250709_155947
refactor: extract Ollama handler to eliminate code duplication
2025-07-10 22:04:05 +01:00
claude[bot]andMervin Praison 75cf937581 refactor: extract Ollama handler to eliminate code duplication
- Created shared _handle_ollama_model method to consolidate logic
- Updated both sync and async methods to use the shared handler
- Eliminated ~185 lines of duplicated code
- Maintained 100% backward compatibility

Co-authored-by: Mervin Praison <[email protected]>
2025-07-09 16:04:38 +00:00
13 changed files with 189 additions and 205 deletions

No files matched your search

+1 -1
View File
@@ -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.44" \
"praisonai>=2.2.45" \
"praisonai[api]" \
gunicorn \
markdown
+1 -1
View File
@@ -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.44" \
"praisonai>=2.2.45" \
"praisonai[chat]" \
"embedchain[github,youtube]"
+1 -1
View File
@@ -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.44" \
"praisonai>=2.2.45" \
"praisonai[ui]" \
"praisonai[chat]" \
"praisonai[realtime]" \
+1 -1
View File
@@ -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.44" \
"praisonai>=2.2.45" \
"praisonai[ui]" \
"praisonai[crewai]"
+2 -2
View File
@@ -121,7 +121,7 @@ healthcheck:
## 📦 Package Versions
All Docker images use consistent, up-to-date versions:
- PraisonAI: `>=2.2.44`
- PraisonAI: `>=2.2.45`
- 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.44" "praisonaiagents==0.0.92"
RUN pip install "praisonai==2.2.45" "praisonaiagents==0.0.92"
```
## 🌐 Production Deployment
+8
View File
@@ -0,0 +1,8 @@
from praisonaiagents import Agent
agent = Agent(
instructions="You are a helpful assistant",
llm="ollama/qwen3"
)
agent.start("Why sky is Blue?")
+161 -185
View File
@@ -813,99 +813,56 @@ class LLM:
# Make one more call to get the final summary response
# Special handling for Ollama models that don't automatically process tool results
ollama_handled = False
if self.model and self.model.startswith("ollama/") and tool_results:
# For Ollama models, we need to explicitly ask the model to process the tool results
# First, check if the response is just a JSON tool call
try:
# If the response_text is a valid JSON that looks like a tool call,
# we need to make a follow-up call to process the results
json_response = json.loads(response_text.strip())
if ('name' in json_response or 'function' in json_response) and not any(word in response_text.lower() for word in ['summary', 'option', 'result', 'found']):
logging.debug("Detected Ollama returning only tool call JSON, making follow-up call to process results")
# Create a prompt that asks the model to process the tool results based on original context
# Extract the original user query from messages
original_query = ""
for msg in reversed(messages): # Look from the end to find the most recent user message
if msg.get("role") == "user":
content = msg.get("content", "")
# Handle list content (multimodal)
if isinstance(content, list):
for item in content:
if isinstance(item, dict) and item.get("type") == "text":
original_query = item.get("text", "")
break
else:
original_query = content
if original_query:
break
# Create a shorter follow-up prompt with all tool results
# If there's only one result, use it directly; otherwise combine them
if len(tool_results) == 1:
results_text = json.dumps(tool_results[0], indent=2)
else:
results_text = json.dumps(tool_results, indent=2)
follow_up_prompt = f"Results:\n{results_text}\nProvide Answer to this Original Question based on the above results: '{original_query}'"
logging.debug(f"[OLLAMA_DEBUG] Original query extracted: {original_query}")
logging.debug(f"[OLLAMA_DEBUG] Follow-up prompt: {follow_up_prompt[:200]}...")
# Make a follow-up call to process the results
follow_up_messages = [
{"role": "user", "content": follow_up_prompt}
]
# Get response with streaming
if verbose:
with Live(display_generating("", start_time), console=console, refresh_per_second=4) as live:
response_text = ""
for chunk in litellm.completion(
**self._build_completion_params(
messages=follow_up_messages,
temperature=temperature,
stream=stream
)
):
if chunk and chunk.choices and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
response_text += content
live.update(display_generating(response_text, start_time))
else:
response_text = ""
for chunk in litellm.completion(
**self._build_completion_params(
messages=follow_up_messages,
temperature=temperature,
stream=stream
)
):
if chunk and chunk.choices and chunk.choices[0].delta.content:
response_text += chunk.choices[0].delta.content
# Set flag to indicate Ollama was handled
ollama_handled = True
final_response_text = response_text.strip()
logging.debug(f"[OLLAMA_DEBUG] Ollama follow-up response: {final_response_text[:200]}...")
# Display the response if we got one
if final_response_text and verbose:
display_interaction(
original_prompt,
final_response_text,
markdown=markdown,
generation_time=time.time() - start_time,
console=console
ollama_params = self._handle_ollama_model(response_text, tool_results, messages, original_prompt)
if ollama_params:
# Get response with streaming
if verbose:
with Live(display_generating("", start_time), console=console, refresh_per_second=4) as live:
response_text = ""
for chunk in litellm.completion(
**self._build_completion_params(
messages=ollama_params["follow_up_messages"],
temperature=temperature,
stream=stream
)
# Return the final response after processing Ollama's follow-up
if final_response_text:
return final_response_text
else:
logging.warning("[OLLAMA_DEBUG] Ollama follow-up returned empty response")
except (json.JSONDecodeError, KeyError):
# Not a JSON response or not a tool call format, continue normally
pass
):
if chunk and chunk.choices and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
response_text += content
live.update(display_generating(response_text, start_time))
else:
response_text = ""
for chunk in litellm.completion(
**self._build_completion_params(
messages=ollama_params["follow_up_messages"],
temperature=temperature,
stream=stream
)
):
if chunk and chunk.choices and chunk.choices[0].delta.content:
response_text += chunk.choices[0].delta.content
# Set flag to indicate Ollama was handled
ollama_handled = True
final_response_text = response_text.strip()
logging.debug(f"[OLLAMA_DEBUG] Ollama follow-up response: {final_response_text[:200]}...")
# Display the response if we got one
if final_response_text and verbose:
display_interaction(
ollama_params["original_prompt"],
final_response_text,
markdown=markdown,
generation_time=time.time() - start_time,
console=console
)
# Return the final response after processing Ollama's follow-up
if final_response_text:
return final_response_text
else:
logging.warning("[OLLAMA_DEBUG] Ollama follow-up returned empty response")
# If reasoning_steps is True and we haven't handled Ollama already, do a single non-streaming call
if reasoning_steps and not ollama_handled:
@@ -1480,99 +1437,56 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
# Special handling for Ollama models that don't automatically process tool results
ollama_handled = False
if self._is_ollama_provider() and tool_results:
# For Ollama models, we need to explicitly ask the model to process the tool results
# First, check if the response is just a JSON tool call
try:
# If the response_text is a valid JSON that looks like a tool call,
# we need to make a follow-up call to process the results
json_response = json.loads(response_text.strip())
if ('name' in json_response or 'function' in json_response) and not any(word in response_text.lower() for word in ['summary', 'option', 'result', 'found']):
logging.debug("Detected Ollama returning only tool call JSON in async mode, making follow-up call to process results")
# Create a prompt that asks the model to process the tool results based on original context
# Extract the original user query from messages
original_query = ""
for msg in reversed(messages): # Look from the end to find the most recent user message
if msg.get("role") == "user":
content = msg.get("content", "")
# Handle list content (multimodal)
if isinstance(content, list):
for item in content:
if isinstance(item, dict) and item.get("type") == "text":
original_query = item.get("text", "")
break
else:
original_query = content
if original_query:
break
# Create a shorter follow-up prompt with all tool results
# If there's only one result, use it directly; otherwise combine them
if len(tool_results) == 1:
results_text = json.dumps(tool_results[0], indent=2)
else:
results_text = json.dumps(tool_results, indent=2)
follow_up_prompt = f"Results:\n{results_text}\nProvide Answer to this Original Question based on the above results: '{original_query}'"
logging.debug(f"[OLLAMA_DEBUG] Original query extracted: {original_query}")
logging.debug(f"[OLLAMA_DEBUG] Follow-up prompt: {follow_up_prompt[:200]}...")
# Make a follow-up call to process the results
follow_up_messages = [
{"role": "user", "content": follow_up_prompt}
]
# Get response with streaming
if verbose:
response_text = ""
async for chunk in await litellm.acompletion(
**self._build_completion_params(
messages=follow_up_messages,
temperature=temperature,
stream=stream
)
):
if chunk and chunk.choices and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
response_text += content
print("\033[K", end="\r")
print(f"Processing results... {time.time() - start_time:.1f}s", end="\r")
else:
response_text = ""
async for chunk in await litellm.acompletion(
**self._build_completion_params(
messages=follow_up_messages,
temperature=temperature,
stream=stream
)
):
if chunk and chunk.choices and chunk.choices[0].delta.content:
response_text += chunk.choices[0].delta.content
# Set flag to indicate Ollama was handled
ollama_handled = True
final_response_text = response_text.strip()
logging.debug(f"[OLLAMA_DEBUG] Ollama follow-up response: {final_response_text[:200]}...")
# Display the response if we got one
if final_response_text and verbose:
display_interaction(
original_prompt,
final_response_text,
markdown=markdown,
generation_time=time.time() - start_time,
console=console
)
# Return the final response after processing Ollama's follow-up
if final_response_text:
return final_response_text
else:
logging.warning("[OLLAMA_DEBUG] Ollama follow-up returned empty response")
except (json.JSONDecodeError, KeyError):
# Not a JSON response or not a tool call format, continue normally
pass
ollama_params = self._handle_ollama_model(response_text, tool_results, messages, original_prompt)
if ollama_params:
# Get response with streaming
if verbose:
response_text = ""
async for chunk in await litellm.acompletion(
**self._build_completion_params(
messages=ollama_params["follow_up_messages"],
temperature=temperature,
stream=stream
)
):
if chunk and chunk.choices and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
response_text += content
print("\033[K", end="\r")
print(f"Processing results... {time.time() - start_time:.1f}s", end="\r")
else:
response_text = ""
async for chunk in await litellm.acompletion(
**self._build_completion_params(
messages=ollama_params["follow_up_messages"],
temperature=temperature,
stream=stream
)
):
if chunk and chunk.choices and chunk.choices[0].delta.content:
response_text += chunk.choices[0].delta.content
# Set flag to indicate Ollama was handled
ollama_handled = True
final_response_text = response_text.strip()
logging.debug(f"[OLLAMA_DEBUG] Ollama follow-up response: {final_response_text[:200]}...")
# Display the response if we got one
if final_response_text and verbose:
display_interaction(
ollama_params["original_prompt"],
final_response_text,
markdown=markdown,
generation_time=time.time() - start_time,
console=console
)
# Return the final response after processing Ollama's follow-up
if final_response_text:
return final_response_text
else:
logging.warning("[OLLAMA_DEBUG] Ollama follow-up returned empty response")
# If no special handling was needed or if it's not an Ollama model
if reasoning_steps and not ollama_handled:
@@ -1839,6 +1753,68 @@ Output MUST be JSON with 'reflection' and 'satisfactory'.
litellm.callbacks = events
def _handle_ollama_model(self, response_text: str, tool_results: List[Any], messages: List[Dict], original_prompt: Union[str, List[Dict]]) -> Optional[Dict[str, Any]]:
"""
Handle special Ollama model requirements when processing tool results.
Args:
response_text: The initial response text from the model
tool_results: List of tool execution results
messages: The conversation messages list
original_prompt: The original user prompt
Returns:
Dict with follow-up parameters if Ollama needs special handling, None otherwise
"""
if not self._is_ollama_provider() or not tool_results:
return None
# Check if the response is just a JSON tool call
try:
json_response = json.loads(response_text.strip())
if not (('name' in json_response or 'function' in json_response) and
not any(word in response_text.lower() for word in ['summary', 'option', 'result', 'found'])):
return None
logging.debug("Detected Ollama returning only tool call JSON, preparing follow-up call to process results")
# Extract the original user query from messages
original_query = ""
for msg in reversed(messages): # Look from the end to find the most recent user message
if msg.get("role") == "user":
content = msg.get("content", "")
# Handle list content (multimodal)
if isinstance(content, list):
for item in content:
if isinstance(item, dict) and item.get("type") == "text":
original_query = item.get("text", "")
break
else:
original_query = content
if original_query:
break
# Create a shorter follow-up prompt with all tool results
# If there's only one result, use it directly; otherwise combine them
if len(tool_results) == 1:
results_text = json.dumps(tool_results[0], indent=2)
else:
results_text = json.dumps(tool_results, indent=2)
follow_up_prompt = f"Results:\n{results_text}\nProvide Answer to this Original Question based on the above results: '{original_query}'"
logging.debug(f"[OLLAMA_DEBUG] Original query extracted: {original_query}")
logging.debug(f"[OLLAMA_DEBUG] Follow-up prompt: {follow_up_prompt[:200]}...")
# Return parameters for follow-up call
return {
"follow_up_messages": [{"role": "user", "content": follow_up_prompt}],
"original_prompt": original_prompt
}
except (json.JSONDecodeError, KeyError):
# Not a JSON response or not a tool call format
return None
def _build_completion_params(self, **override_params) -> Dict[str, Any]:
"""Build parameters for litellm completion calls with all necessary config"""
params = {
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "praisonaiagents"
version = "0.0.117"
version = "0.0.118"
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
requires-python = ">=3.10"
authors = [
+1 -1
View File
@@ -2375,7 +2375,7 @@ wheels = [
[[package]]
name = "praisonaiagents"
version = "0.0.117"
version = "0.0.118"
source = { editable = "." }
dependencies = [
{ name = "mcp" },
+2 -2
View File
@@ -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.44.tar.gz"
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.44.tar.gz | shasum -a 256`.split.first
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.45.tar.gz"
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.2.45.tar.gz | shasum -a 256`.split.first
license "MIT"
depends_on "[email protected]"
+1 -1
View File
@@ -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.44 gunicorn markdown\n")
file.write("RUN pip install flask praisonai==2.2.45 gunicorn markdown\n")
file.write("EXPOSE 8080\n")
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
+4 -4
View File
@@ -1,6 +1,6 @@
[project]
name = "PraisonAI"
version = "2.2.44"
version = "2.2.45"
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.117",
"praisonaiagents>=0.0.118",
"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.44"
version = "2.2.45"
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.117"
praisonaiagents = ">=0.0.118"
python-dotenv = ">=0.19.0"
instructor = ">=1.3.3"
PyYAML = ">=6.0"
+5 -5
View File
@@ -3931,7 +3931,7 @@ wheels = [
[[package]]
name = "praisonai"
version = "2.2.44"
version = "2.2.45"
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.117" },
{ name = "praisonaiagents", specifier = ">=0.0.118" },
{ 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.117"
version = "0.0.118"
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/3e/0e/663cdeecbc32af35b8f7ee662079d75c7d743a7d6507fe9742fe5e3edf9e/praisonaiagents-0.0.117.tar.gz", hash = "sha256:b28605506863689433d7825bda72272b04254b5fe95be2a9899d8e96a89467aa", size = 173294 }
sdist = { url = "https://files.pythonhosted.org/packages/ba/53/0363b4e986b3916a246af2798cecaba08d47d46acc5faf313789d8de07cf/praisonaiagents-0.0.118.tar.gz", hash = "sha256:0e7cb593fc1733e33c6f17d55e4670ca973cfe4233c86747c7d97be41347a3da", size = 172108 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/9d/7b/cd590e238196f9539db7302118a2fad2eec4a319566025ae7c2768bbec6f/praisonaiagents-0.0.117-py3-none-any.whl", hash = "sha256:42014556f65da3a27aa30f6446e4408df7e9cc3f2e9cf428e43e44c3244d4857", size = 193878 },
{ url = "https://files.pythonhosted.org/packages/9a/a1/605677f442a983259be11079b492fff2a1cca78686c8a6385f8adcdea285/praisonaiagents-0.0.118-py3-none-any.whl", hash = "sha256:5e3f27ffc9af64c2db1726dc1174f348b5bb0dea07b110651fcde6f480ca36a1", size = 192626 },
]
[[package]]