|
5 | 5 | for enhanced reasoning in large language models. |
6 | 6 | """ |
7 | 7 |
|
| 8 | +import os |
| 9 | +import sys |
| 10 | +import importlib.util |
8 | 11 | import logging |
9 | 12 | from typing import Tuple, Dict, Any |
10 | | -from optillm.plugins.deepthink.self_discover import SelfDiscover |
11 | | -from optillm.plugins.deepthink.uncertainty_cot import UncertaintyRoutedCoT |
12 | 13 |
|
13 | 14 | # Plugin identifier for optillm |
14 | 15 | SLUG = "deepthink" |
@@ -40,18 +41,38 @@ def run( |
40 | 41 | """ |
41 | 42 | logger.info("Starting Deep Think reasoning process") |
42 | 43 |
|
43 | | - # Extract configuration parameters |
44 | | - config = _parse_config(request_config or {}) |
| 44 | + # Get the directory where this plugin is located |
| 45 | + plugin_dir = os.path.dirname(os.path.abspath(__file__)) |
| 46 | + deepthink_dir = os.path.join(plugin_dir, 'deepthink') |
| 47 | + |
| 48 | + # Add the deepthink directory to the Python path temporarily |
| 49 | + if deepthink_dir not in sys.path: |
| 50 | + sys.path.insert(0, deepthink_dir) |
45 | 51 |
|
46 | 52 | try: |
| 53 | + # Load the modules dynamically |
| 54 | + self_discover_file = os.path.join(deepthink_dir, 'self_discover.py') |
| 55 | + uncertainty_cot_file = os.path.join(deepthink_dir, 'uncertainty_cot.py') |
| 56 | + |
| 57 | + spec1 = importlib.util.spec_from_file_location("self_discover", self_discover_file) |
| 58 | + self_discover_module = importlib.util.module_from_spec(spec1) |
| 59 | + spec1.loader.exec_module(self_discover_module) |
| 60 | + |
| 61 | + spec2 = importlib.util.spec_from_file_location("uncertainty_cot", uncertainty_cot_file) |
| 62 | + uncertainty_cot_module = importlib.util.module_from_spec(spec2) |
| 63 | + spec2.loader.exec_module(uncertainty_cot_module) |
| 64 | + |
| 65 | + # Extract configuration parameters |
| 66 | + config = _parse_config(request_config or {}) |
| 67 | + |
47 | 68 | # Initialize components |
48 | | - self_discover = SelfDiscover( |
| 69 | + self_discover = self_discover_module.SelfDiscover( |
49 | 70 | client=client, |
50 | 71 | model=model, |
51 | 72 | max_tokens=config["max_tokens"] |
52 | 73 | ) |
53 | 74 |
|
54 | | - uncertainty_cot = UncertaintyRoutedCoT( |
| 75 | + uncertainty_cot = uncertainty_cot_module.UncertaintyRoutedCoT( |
55 | 76 | client=client, |
56 | 77 | model=model, |
57 | 78 | max_tokens=config["max_tokens"] |
@@ -108,31 +129,10 @@ def run( |
108 | 129 |
|
109 | 130 | return final_response, total_tokens |
110 | 131 |
|
111 | | - except Exception as e: |
112 | | - logger.error(f"Error in Deep Think plugin: {str(e)}") |
113 | | - logger.debug(f"Exception traceback:", exc_info=True) |
114 | | - |
115 | | - # Fallback to simple generation |
116 | | - try: |
117 | | - logger.info("Attempting fallback to simple generation") |
118 | | - response = client.chat.completions.create( |
119 | | - model=model, |
120 | | - messages=[ |
121 | | - {"role": "system", "content": system_prompt}, |
122 | | - {"role": "user", "content": initial_query} |
123 | | - ], |
124 | | - max_tokens=config["max_tokens"], |
125 | | - temperature=config["temperature"], |
126 | | - top_p=config["top_p"] |
127 | | - ) |
128 | | - |
129 | | - logger.info("Fallback generation successful") |
130 | | - return response.choices[0].message.content.strip(), response.usage.completion_tokens |
131 | | - |
132 | | - except Exception as fallback_error: |
133 | | - logger.error(f"Fallback generation also failed: {str(fallback_error)}") |
134 | | - logger.debug(f"Fallback exception traceback:", exc_info=True) |
135 | | - return f"Error in Deep Think plugin: {str(e)}", 0 |
| 132 | + finally: |
| 133 | + # Remove from path after use |
| 134 | + if deepthink_dir in sys.path: |
| 135 | + sys.path.remove(deepthink_dir) |
136 | 136 |
|
137 | 137 | def _parse_config(request_config: Dict[str, Any]) -> Dict[str, Any]: |
138 | 138 | """Parse and validate configuration parameters.""" |
|
0 commit comments