
    iV                     b   d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZ  ej        e          Zg dZdZd	Zd
ZdZdZ edd          Zdede	e         defdZeddfdedede de!de!de"eee#f         fdZ$edfdedede de!def
dZ%	 	 djdede
e	e                  de
e         defdZ&de#fd Z'deeef         fd!Z(ed"k    r	  e)d#            e)d$            e            Z*e*s- e)d%            e)d&            e)d'            e+d           n e)d(            e)d)            e(            Z, e)d*            e)d+ e-e,d                    d,d-.                    e,d                                e)d.e,d                      e)d/e,d0                      e)d1e,d2                      e)d3e,d4                      e)d5e,d6                     ej/        r( e)d7ej0                     e)d8ej0         d9           n e)d:            e)d;            e)d<            e)d=            e)d>            e)d?            e)d@            e)dA            e)dB            e)dC            e)dD            e)dE            e)dF            e)dG            e)dH            e)dI            e)dJ            e)dK            e)dL            e)dM            e)dN            e)dOe dPe dQ            e)dR            e)dS            e)dT            e)dU            e)dV            e)dW            e)dX            e)dY            e)dZ           dd[l1m2Z2 d\d]d^dd_d`daidgdbdcZ3 e2j4        d\dde3de e'dfgdgdhi           dS )kaQ  
Mixture-of-Agents Tool Module

This module implements the Mixture-of-Agents (MoA) methodology that leverages
the collective strengths of multiple LLMs through a layered architecture to
achieve state-of-the-art performance on complex reasoning tasks.

Based on the research paper: "Mixture-of-Agents Enhances Large Language Model Capabilities"
by Junlin Wang et al. (arXiv:2406.04692v1)

Key Features:
- Multi-layer LLM collaboration for enhanced reasoning
- Parallel processing of reference models for efficiency
- Intelligent aggregation and synthesis of diverse responses
- Specialized for extremely difficult problems requiring intense reasoning
- Optimized for coding, mathematics, and complex analytical tasks

Available Tool:
- mixture_of_agents_tool: Process complex queries using multiple frontier models

Architecture:
1. Reference models generate diverse initial responses in parallel
2. Aggregator model synthesizes responses into a high-quality output
3. Multiple layers can be used for iterative refinement (future enhancement)

Models Used (via OpenRouter):
- Reference Models: claude-opus-4.6, gemini-3-pro-preview, gpt-5.4-pro, deepseek-v3.2
- Aggregator Model: claude-opus-4.6 (highest capability for synthesis)

Configuration:
    To customize the MoA setup, modify the configuration constants at the top of this file:
    - REFERENCE_MODELS: List of models for generating diverse initial responses
    - AGGREGATOR_MODEL: Model used to synthesize the final response
    - REFERENCE_TEMPERATURE/AGGREGATOR_TEMPERATURE: Sampling temperatures
    - MIN_SUCCESSFUL_REFERENCES: Minimum successful models needed to proceed

Usage:
    from mixture_of_agents_tool import mixture_of_agents_tool
    import asyncio
    
    # Process a complex query
    result = await mixture_of_agents_tool(
        user_prompt="Solve this complex mathematical proof..."
    )
    N)DictAnyListOptional)get_async_clientcheck_api_key)extract_content_or_reasoning)DebugSession)anthropic/claude-opus-4.6zgoogle/gemini-2.5-prozopenai/gpt-5.4-prozdeepseek/deepseek-v3.2r   g333333?g?   a^  You have been provided with a set of responses from various open-source models to the latest user query. Your task is to synthesize these responses into a single, high-quality response. It is crucial to critically evaluate the information provided in these responses, recognizing that some of it may be biased or incorrect. Your response should not simply replicate the given answers but should offer a refined, accurate, and comprehensive reply to the instruction. Ensure your response is well-structured, coherent, and adheres to the highest standards of accuracy and reliability.

Responses from models:	moa_toolsMOA_TOOLS_DEBUG)env_varsystem_prompt	responsesreturnc                 h    d                     d t          |          D                       }|  d| S )aG  
    Construct the final system prompt for the aggregator including all model responses.
    
    Args:
        system_prompt (str): Base system prompt for aggregation
        responses (List[str]): List of responses from reference models
        
    Returns:
        str: Complete system prompt with enumerated responses
    
c                 (    g | ]\  }}|d z    d| S )r   z.  ).0iresponses      A/home/ubuntu/.hermes/hermes-agent/tools/mixture_of_agents_tool.py
<listcomp>z0_construct_aggregator_prompt.<locals>.<listcomp>d   s.    \\\8!A#3333\\\    z

)join	enumerate)r   r   response_texts      r   _construct_aggregator_promptr    Y   sA     II\\yQZG[G[\\\]]M00000r   i }     modeluser_prompttemperature
max_tokensmax_retriesc                   K   t          |          D ]t}	 t                              d| |dz   |           | d|dg|ddddid	}|                                                     d
          s||d<    t                      j        j        j        di | d{V }t          |          }|sXt          
                    d| |dz   |           ||dz
  k     r/t          j        t          d|dz   z  d                     d{V  t                              d| t          |                     | |dfc S # t          $ rF}	t!          |	          }
d|
                                v r!t          
                    d| |dz   |
           nmd|
                                v sd|
                                v r!t          
                    d| |dz   |
           n t          
                    d| |dz   |
           ||dz
  k     rLt          d|dz   z  d          }t                              d|           t          j        |           d{V  n3|  d| d|
 }t                              d|d           | |dfcY d}	~	c S Y d}	~	nd}	~	ww xY wdS )a  
    Run a single reference model with retry logic and graceful failure handling.
    
    Args:
        model (str): Model identifier to use
        user_prompt (str): The user's query
        temperature (float): Sampling temperature for response generation
        max_tokens (int): Maximum tokens in response
        max_retries (int): Maximum number of retry attempts
        
    Returns:
        tuple[str, str, bool]: (model_name, response_content_or_error, success_flag)
    zQuerying %s (attempt %s/%s)r   userrolecontent	reasoningTxhighenabledeffortr"   messagesr%   
extra_bodygpt-r$   Nz3%s returned empty content (attempt %s/%s), retrying   <   z%s responded (%s characters)invalidz)%s invalid request error (attempt %s): %sratelimitz$%s rate limit error (attempt %s): %sz!%s unknown error (attempt %s): %szRetrying in %ss...z failed after z attempts: %sexc_infoFr   )rangeloggerinfolower
startswith_get_openrouter_clientchatcompletionscreater	   warningasynciosleepminlen	Exceptionstrerror)r"   r#   r$   r%   r&   attempt
api_paramsr   r+   e	error_str
sleep_time	error_msgs                r   _run_reference_model_saferT   h   s,     ( %% 5/ 5/4	/KK5ugk;WWW &,EEF(#'")" "	
 
J ;;==++F33 8,7
=)M355:FM[[PZ[[[[[[[[H28<<G TV[]dgh]hjuvvv[1_,,!-A'A+,>(C(CDDDDDDDDDKK6s7||LLL'4'''' 	/ 	/ 	/AI IOO----JESZ]^S^`ijjjj9??,,,,9??;L;L0L0LEugXYk[deeeeBE7UV;Xabbbq(( w{!3R88
0*===mJ//////////$WWKWWIWW	T9t<<<i.........	 0////	/E5/ 5/s%   C-D4-D44J?D3J 2J Jc                 F  K   t                               dt                     t          d| dd|dg|ddddid	}t                                                              d
          s||d<    t                      j        j        j        di | d{V }t          |          }|sRt           
                    d            t                      j        j        j        di | d{V }t          |          }t                               dt          |                     |S )a  
    Run the aggregator model to synthesize the final response.
    
    Args:
        system_prompt (str): System prompt with all reference responses
        user_prompt (str): Original user query
        temperature (float): Focused temperature for consistent aggregation
        max_tokens (int): Maximum tokens in final response
        
    Returns:
        str: Synthesized final response
    zRunning aggregator model: %ssystemr)   r(   r,   Tr-   r.   r1   r4   r$   Nz0Aggregator returned empty content, retrying oncez$Aggregation complete (%s characters)r   )r>   r?   AGGREGATOR_MODELr@   rA   rB   rC   rD   rE   r	   rF   rJ   )r   r#   r$   r%   rO   r   r+   s          r   _run_aggregator_modelrX      sZ     $ KK.0@AAA "-8844
 !! 
 J" !!##..v66 0$/
=!E+--2>ESS
SSSSSSSSH*844G  9IJJJI/116BIWWJWWWWWWWW.x88
KK6GEEENr   reference_modelsaggregator_modelc           
      \   K   t           j                                         }t                     dk    r dd         dz   n |pt          |pt          t
          t          t          dddddg ddi d	}	 t          	                    d           t          	                    d	 dd
                    t          j        d          st          d          |pt          }|pt          }t          	                    dt          |                     t          	                    d           t          j         fd|D               d{V }g }g }	|D ]3\  }
}}|r|                    |           |	                    |
           4t          |          }t          |	          }t          	                    d||           |	r.t                              dd                    |	                     |t          k     r+t          d| dt          |           dt           d          ||d<   ||d<   |	|d<   t          	                    d           t%          t&          |          }t)          | t                     d{V }t           j                                         }||z
                                  }t          	                    d|           d|||dd}d|d<   t          |          |d <   ||d!<   |d"         |d"<   t,                              d#|           t,                                           t3          j        |d$d%          S # t6          $ r}d&t9          |           }t                              d'|d(           t           j                                         }||z
                                  }dd)|pt          |pt          d|d*}||d+<   ||d!<   t,                              d#|           t,                                           t3          j        |d$d%          cY d}~S d}~ww xY w),a  
    Process a complex query using the Mixture-of-Agents methodology.
    
    This tool leverages multiple frontier language models to collaboratively solve
    extremely difficult problems requiring intense reasoning. It's particularly
    effective for:
    - Complex mathematical proofs and calculations
    - Advanced coding problems and algorithm design
    - Multi-step analytical reasoning tasks
    - Problems requiring diverse domain expertise
    - Tasks where single models show limitations
    
    The MoA approach uses a fixed 2-layer architecture:
    1. Layer 1: Multiple reference models generate diverse responses in parallel (temp=0.6)
    2. Layer 2: Aggregator model synthesizes the best elements into final response (temp=0.4)
    
    Args:
        user_prompt (str): The complex query or problem to solve
        reference_models (Optional[List[str]]): Custom reference models to use
        aggregator_model (Optional[str]): Custom aggregator model to use
    
    Returns:
        str: JSON string containing the MoA results with the following structure:
             {
                 "success": bool,
                 "response": str,
                 "models_used": {
                     "reference_models": List[str],
                     "aggregator_model": str
                 },
                 "processing_time": float
             }
    
    Raises:
        Exception: If MoA processing fails or API key is not set
       Nz...)r#   rY   rZ   reference_temperatureaggregator_temperaturemin_successful_referencesFr   )	
parametersrM   successreference_responses_countfailed_models_countfailed_modelsfinal_response_lengthprocessing_time_secondsmodels_usedz(Starting Mixture-of-Agents processing...z	Query: %sd   OPENROUTER_API_KEYz/OPENROUTER_API_KEY environment variable not setz5Using %s reference models in 2-layer MoA architecturez*Layer 1: Generating reference responses...c                 <    g | ]}t          |t                    S r   )rT   REFERENCE_TEMPERATURE)r   r"   r#   s     r   r   z*mixture_of_agents_tool.<locals>.<listcomp>9  s6     /
 /
 /
 &e[:OPP/
 /
 /
r   z1Reference model results: %s successful, %s failedzFailed models: %s, z*Insufficient successful reference models (/z). Need at least z successful responses.rb   rc   rd   z'Layer 2: Synthesizing final response...z(MoA processing completed in %.2f secondsT)rY   rZ   )ra   r   rg   ra   re   rf   rg   mixture_of_agents_toolr5   )indentensure_asciizError in MoA processing: r:   r;   zMMoA processing failed. Please try again or use a single model for this query.)ra   r   rg   rM   rM   )datetimenowrJ   REFERENCE_MODELSrW   rk   AGGREGATOR_TEMPERATUREMIN_SUCCESSFUL_REFERENCESr>   r?   osgetenv
ValueErrorrG   gatherappendrF   r   r    AGGREGATOR_SYSTEM_PROMPTrX   total_seconds_debuglog_callsavejsondumpsrK   rL   rM   )r#   rY   rZ   
start_timedebug_call_data
ref_models	agg_modelmodel_resultssuccessful_responsesrd   
model_namer+   ra   successful_countfailed_countaggregator_system_promptfinal_responseend_timeprocessing_timeresultrP   rS   s   `                     r   rn   rn      s     R "&&((J 9<K8H8H38N8N;tt,u44T_ 0 D4D 0 D4D%:&<)B
 
 %& !"#$! O&o@>???KTcT!2333 y-.. 	PNOOO &9)9
$8(8	KSQ[__]]] 	@AAA%n /
 /
 /
 /
#/
 /
 /
         ",9 	1 	1(J 1$++G4444$$Z0000344=))GIY[ghhh 	JNN.		-0H0HIII 777  qJZ  q  q]`ak]l]l  q  q  @Y  q  q  q  r  r  r7G341=-.+8( 	=>>>#?$ $
 $
 
  5$" 
  
 
 
 
 
 
 
 $((**#j0??AA>PPP &$.$- 
 
 &*	"36~3F3F/05D12)/)>& 	0/BBBz&???? @ @ @8A88	T9t444 $((**#j0??AA g$4$H8H$4$H8H  
 
 $- 5D120/BBBz&?????????1@s    3KM 
P+CP& P+&P+c                      t                      S )z
    Check if all requirements for MoA tools are met.
    
    Returns:
        bool: True if requirements are met, False otherwise
    )check_openrouter_api_keyr   r   r   check_moa_requirementsr     s     $%%%r   c                      t           t          t          t          t          t          t                     t          t                     t          z
   dt          t                      ddS )z
    Get the current MoA configuration settings.
    
    Returns:
        Dict[str, Any]: Dictionary containing all configuration parameters
    rm   z models can fail)rY   rZ   r]   r^   r_   total_reference_modelsfailure_tolerance)rs   rW   rk   rt   ru   rJ   r   r   r   get_moa_configurationr     s[     -,!6"8%>"%&6"7"7 #$4 5 58Q Q{{TWXhTiTi{{{  r   __main__u"   🤖 Mixture-of-Agents Tool Modulez2==================================================u3   ❌ OPENROUTER_API_KEY environment variable not setzBPlease set your API key: export OPENROUTER_API_KEY='your-key-here'z&Get API key at: https://openrouter.ai/u   ✅ OpenRouter API key foundu!   🛠️  MoA tools ready for use!u   
⚙️  Current Configuration:u     🤖 Reference models (z): rl   u     🧠 Aggregator model: u"     🌡️  Reference temperature: r]   u#     🌡️  Aggregator temperature: r^   u     🛡️  Failure tolerance: r   u"     📊 Minimum successful models: r_   u'   
🐛 Debug mode ENABLED - Session ID: z7   Debug logs will be saved to: ./logs/moa_tools_debug_z.jsonu>   
🐛 Debug mode disabled (set MOA_TOOLS_DEBUG=true to enable)z
Basic usage:z;  from mixture_of_agents_tool import mixture_of_agents_toolz  import asyncio z  async def main():z,      result = await mixture_of_agents_tool(z@          user_prompt='Solve this complex mathematical proof...'z      )z      print(result)z  asyncio.run(main())z
Best use cases:z0  - Complex mathematical proofs and calculationsz1  - Advanced coding problems and algorithm designz)  - Multi-step analytical reasoning tasksz/  - Problems requiring diverse domain expertisez.  - Tasks where single models show limitationsz
Performance characteristics:z.  - Higher latency due to multiple model callsz4  - Significantly improved quality for complex tasksz&  - Parallel processing for efficiencyz  - Optimized temperatures: z for reference models, z for aggregationz;  - Token-efficient: only returns final aggregated responsez4  - Resilient: continues with partial model failureszC  - Configurable: easy to modify models and settings at top of filez6  - State-of-the-art results on challenging benchmarksz
Debug mode:z  # Enable debug loggingz  export MOA_TOOLS_DEBUG=truez;  # Debug logs capture all MoA processing steps and metricsz3  # Logs saved to: ./logs/moa_tools_debug_UUID.json)registrymixture_of_agentsuI  Route a hard problem through multiple frontier LLMs collaboratively. Makes 5 API calls (4 reference models + 1 aggregator) with maximum reasoning effort — use sparingly for genuinely difficult problems. Best for: complex math, advanced algorithms, multi-step analytical reasoning, problems benefiting from diverse perspectives.objectstringzThe complex query or problem to solve using multiple AI models. Should be a challenging problem that benefits from diverse perspectives and collaborative reasoning.)typedescription)r   
propertiesrequired)namer   r`   moac                 J    t          |                     dd                    S )Nr#   r   )r#   )rn   get)argskws     r   <lambda>r     s!    5$((=Z\B]B]^^^ r   ri   Tu   🧠)r   toolsetschemahandlercheck_fnrequires_envis_asyncemoji)NN)5__doc__r   loggingrv   rG   rq   typingr   r   r   r   tools.openrouter_clientr   rB   r   r   agent.auxiliary_clientr	   tools.debug_helpersr
   	getLogger__name__r>   rs   rW   rk   rt   ru   r{   r}   rL   r    floatinttupleboolrT   rX   rn   r   r   printapi_availableexitconfigrJ   r   active
session_idtools.registryr   
MOA_SCHEMAregisterr   r   r   <module>r      sS  , ,\   				   , , , , , , , , , , , , y y y y y y y y ? ? ? ? ? ? , , , , , ,		8	$	$
    /        
k+<	=	=	=1 1S	 1c 1 1 1 1$ /I/ I/I/I/ I/ 	I/
 I/ 3T>I/ I/ I/ I/^ 0	4 444 4 	4
 	4 4 4 4r -1&*m@ m@m@tCy)m@ smm@ 		m@ m@ m@ m@`& & & & &tCH~    $ z 
E
.///	E(OOO -,..M .CDDDRSSS6777Q,---	E
-... #"$$F	E
,---	E
qcc&1C*D&E&E
q
q$))TZ[mTnJoJo
q
qrrr	E
Bf-?&@
B
BCCC	E
Pv6M/N
P
PQQQ	E
R7O0P
R
RSSS	E
H62E+F
H
HIII	E
Tv6Q/R
T
TUUU } QL9JLLMMM`HY```aaaaOPPP	E
	E
GHHH	E
	E"III	E
   	E
8999	E
LMMM	E)	E
   	E
!"""	E
	E
<===	E
=>>>	E
5666	E
;<<<	E
:;;;	E
*+++	E
:;;;	E
@AAA	E
2333	E
)>

Wm


  A  A  A	E
GHHH	E
@AAA	E
OPPP	E
BCCC	E/	E
$%%%	E
)***	E
GHHH	E
?@@@ $ # # # # #   _   F 
 #O	 	 
  	^^#&'
	 	 	 	 	 	r   