1+ #!/usr/bin/env python3
2+ """
3+ Test script to validate the Ollama infinite loop fix.
4+
5+ This script tests that:
6+ 1. Ollama provider detection works correctly
7+ 2. Tool results summary generation works as expected
8+ 3. Loop termination logic prevents infinite loops
9+ 4. Backward compatibility is maintained for other providers
10+ """
11+
12+ import sys
13+ import os
14+
15+ # Add the source directory to the path
16+ sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), 'src' , 'praisonai-agents' ))
17+
18+ def test_module_imports ():
19+ """Test that we can import the required modules."""
20+ try :
21+ from praisonaiagents .llm .llm import LLM
22+ print ("✅ Successfully imported LLM module" )
23+ return True
24+ except ImportError as e :
25+ print (f"❌ Failed to import LLM module: { e } " )
26+ return False
27+
28+ def test_ollama_provider_detection ():
29+ """Test Ollama provider detection logic."""
30+ try :
31+ from praisonaiagents .llm .llm import LLM
32+
33+ # Test Ollama provider detection
34+ ollama_llm = LLM (model = "ollama/qwen3" )
35+ is_ollama = ollama_llm ._is_ollama_provider ()
36+
37+ if is_ollama :
38+ print ("✅ Ollama provider detection works correctly" )
39+ else :
40+ print ("❌ Ollama provider detection failed" )
41+ return False
42+
43+ # Test non-Ollama provider
44+ openai_llm = LLM (model = "gpt-4o-mini" )
45+ is_not_ollama = not openai_llm ._is_ollama_provider ()
46+
47+ if is_not_ollama :
48+ print ("✅ Non-Ollama provider detection works correctly" )
49+ else :
50+ print ("❌ Non-Ollama provider incorrectly detected as Ollama" )
51+ return False
52+
53+ return True
54+
55+ except Exception as e :
56+ print (f"❌ Provider detection test failed: { e } " )
57+ return False
58+
59+ def test_tool_summary_generation ():
60+ """Test that tool results summary generation works correctly."""
61+ try :
62+ # Mock tool results like what would be generated
63+ tool_results = [
64+ "The stock price of Google is 100" ,
65+ 200
66+ ]
67+
68+ # Simulate the summary generation logic
69+ tool_summary = "Based on the tool execution results:\n "
70+ for i , result in enumerate (tool_results ):
71+ if isinstance (result , dict ) and 'result' in result :
72+ tool_summary += f"- { result .get ('function_name' , 'Tool' )} : { result ['result' ]} \n "
73+ else :
74+ tool_summary += f"- Tool { i + 1 } : { result } \n "
75+
76+ expected_summary = "Based on the tool execution results:\n - Tool 1: The stock price of Google is 100\n - Tool 2: 200"
77+
78+ if tool_summary .strip () == expected_summary :
79+ print ("✅ Tool summary generation works correctly" )
80+ print (f"Generated summary: { repr (tool_summary .strip ())} " )
81+ return True
82+ else :
83+ print ("❌ Tool summary generation failed" )
84+ print (f"Expected: { repr (expected_summary )} " )
85+ print (f"Got: { repr (tool_summary .strip ())} " )
86+ return False
87+
88+ except Exception as e :
89+ print (f"❌ Tool summary generation test failed: { e } " )
90+ return False
91+
92+ def test_backward_compatibility ():
93+ """Test that the fix doesn't break other LLM providers."""
94+ try :
95+ from praisonaiagents .llm .llm import LLM
96+
97+ # Test that non-Ollama providers aren't affected
98+ models_to_test = [
99+ "gpt-4o-mini" ,
100+ "claude-3-sonnet" ,
101+ "gemini/gemini-2.5-pro"
102+ ]
103+
104+ for model in models_to_test :
105+ try :
106+ llm = LLM (model = model )
107+ is_ollama = llm ._is_ollama_provider ()
108+ if not is_ollama :
109+ print (f"✅ Model { model } correctly identified as non-Ollama" )
110+ else :
111+ print (f"❌ Model { model } incorrectly identified as Ollama" )
112+ return False
113+ except Exception as e :
114+ print (f"⚠️ Could not test model { model } : { e } " )
115+
116+ print ("✅ Backward compatibility verified" )
117+ return True
118+
119+ except Exception as e :
120+ print (f"❌ Backward compatibility test failed: { e } " )
121+ return False
122+
123+ def main ():
124+ """Run all tests."""
125+ print ("🚀 Testing Ollama infinite loop fix..." )
126+ print ("=" * 50 )
127+
128+ tests = [
129+ ("Module Imports" , test_module_imports ),
130+ ("Ollama Provider Detection" , test_ollama_provider_detection ),
131+ ("Tool Summary Generation" , test_tool_summary_generation ),
132+ ("Backward Compatibility" , test_backward_compatibility ),
133+ ]
134+
135+ passed = 0
136+ total = len (tests )
137+
138+ for test_name , test_func in tests :
139+ print (f"\n 🧪 Running: { test_name } " )
140+ try :
141+ if test_func ():
142+ passed += 1
143+ print (f"✅ { test_name } : PASSED" )
144+ else :
145+ print (f"❌ { test_name } : FAILED" )
146+ except Exception as e :
147+ print (f"❌ { test_name } : ERROR - { e } " )
148+
149+ print ("\n " + "=" * 50 )
150+ print (f"📊 Test Results: { passed } /{ total } tests passed" )
151+
152+ if passed == total :
153+ print ("🎉 All tests passed! The Ollama fix is working correctly." )
154+ return True
155+ else :
156+ print ("⚠️ Some tests failed. Please review the implementation." )
157+ return False
158+
159+ if __name__ == "__main__" :
160+ success = main ()
161+ sys .exit (0 if success else 1 )
0 commit comments