MCPcopy Create free account
hub / github.com/algorithmicsuperintelligence/optillm / run

Function run

optillm/plugins/proxy_plugin.py:97–245  ·  view source on GitHub ↗

Main proxy plugin entry point. Supports three usage modes: 1. Standalone proxy: model="proxy-gpt-4" 2. Wrapping approach: extra_body={"optillm_approach": "proxy", "proxy_wrap": "moa"} 3. Combined approach: model="bon&proxy-gpt-4" Args: system_prompt: System

(system_prompt: str, initial_query: str, client, model: str, 
        request_config: dict = None)

Source from the content-addressed store, hash-verified

95 return [{"role": "user", "content": combined_message}]
96
97def run(system_prompt: str, initial_query: str, client, model: str,
98 request_config: dict = None) -> Tuple[str, int]:
99 """
100 Main proxy plugin entry point.
101
102 Supports three usage modes:
103 1. Standalone proxy: model="proxy-gpt-4"
104 2. Wrapping approach: extra_body={"optillm_approach": "proxy", "proxy_wrap": "moa"}
105 3. Combined approach: model="bon&proxy-gpt-4"
106
107 Args:
108 system_prompt: System message for the LLM
109 initial_query: User's query
110 client: Original OpenAI client (used as fallback)
111 model: Model identifier
112 request_config: Additional request configuration
113
114 Returns:
115 Tuple of (response_text, token_count)
116 """
117 try:
118 # Load configuration
119 config = ProxyConfig.load()
120
121 if not config.get('providers'):
122 logger.warning("No providers configured, falling back to original client")
123 # Strip stream parameter to force complete response
124 api_config = dict(request_config or {})
125 api_config.pop('stream', None)
126
127 response = client.chat.completions.create(
128 model=model,
129 messages=[
130 {"role": "system", "content": system_prompt},
131 {"role": "user", "content": initial_query}
132 ],
133 **api_config
134 )
135 # Return full response dict to preserve all usage information
136 response_dict = response.model_dump() if hasattr(response, 'model_dump') else response
137 return response_dict, 0
138
139 # Create or reuse proxy client to maintain state (important for round-robin)
140 config_key = str(config) # Simple config-based cache key
141 if config_key not in _proxy_client_cache:
142 logger.debug("Creating new proxy client instance")
143 _proxy_client_cache[config_key] = ProxyClient(
144 config=config,
145 fallback_client=client
146 )
147 else:
148 logger.debug("Reusing existing proxy client instance")
149
150 proxy_client = _proxy_client_cache[config_key]
151
152 # Check for wrapped approach in extra_body (recommended method)
153 wrapped_approach = None
154 if request_config:

Callers

nothing calls this directly

Calls 8

handleMethod · 0.95
ProxyClientClass · 0.90
ApproachHandlerClass · 0.90
loadMethod · 0.80
createMethod · 0.45
model_dumpMethod · 0.45

Tested by

no test coverage detected