|
5 | 5 | for enhanced reasoning in large language models. |
6 | 6 | """ |
7 | 7 |
|
8 | | -import os |
9 | | -import sys |
10 | | -import importlib.util |
11 | 8 | import logging |
12 | 9 | from typing import Tuple, Dict, Any |
| 10 | +from optillm.plugins.deepthink import SelfDiscover, UncertaintyRoutedCoT |
13 | 11 |
|
14 | 12 | # Plugin identifier for optillm |
15 | 13 | SLUG = "deepthink" |
@@ -41,98 +39,72 @@ def run( |
41 | 39 | """ |
42 | 40 | logger.info("Starting Deep Think reasoning process") |
43 | 41 |
|
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') |
| 42 | + # Extract configuration parameters |
| 43 | + config = _parse_config(request_config or {}) |
47 | 44 |
|
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 | + # Initialize components |
| 46 | + self_discover = SelfDiscover( |
| 47 | + client=client, |
| 48 | + model=model, |
| 49 | + max_tokens=config["max_tokens"] |
| 50 | + ) |
51 | 51 |
|
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 | | - |
68 | | - # Initialize components |
69 | | - self_discover = self_discover_module.SelfDiscover( |
70 | | - client=client, |
71 | | - model=model, |
72 | | - max_tokens=config["max_tokens"] |
73 | | - ) |
74 | | - |
75 | | - uncertainty_cot = uncertainty_cot_module.UncertaintyRoutedCoT( |
76 | | - client=client, |
77 | | - model=model, |
78 | | - max_tokens=config["max_tokens"] |
79 | | - ) |
80 | | - |
81 | | - total_tokens = 0 |
82 | | - |
83 | | - # Stage 1: SELF-DISCOVER reasoning structure (if enabled) |
84 | | - reasoning_structure = None |
85 | | - if config["enable_self_discover"]: |
86 | | - logger.info("Discovering task-specific reasoning structure") |
87 | | - |
88 | | - discovery_result = self_discover.discover_reasoning_structure( |
89 | | - task_description=_extract_task_description(initial_query, system_prompt), |
90 | | - task_examples=None # Could be enhanced to extract examples |
91 | | - ) |
92 | | - |
93 | | - reasoning_structure = discovery_result["reasoning_structure"] |
94 | | - total_tokens += discovery_result["completion_tokens"] |
95 | | - |
96 | | - logger.info(f"Discovered reasoning structure with {len(reasoning_structure)} components") |
97 | | - |
98 | | - # Prepare enhanced prompt |
99 | | - enhanced_prompt = _create_enhanced_prompt( |
100 | | - system_prompt=system_prompt, |
101 | | - initial_query=initial_query, |
102 | | - reasoning_structure=reasoning_structure, |
103 | | - config=config |
104 | | - ) |
105 | | - |
106 | | - # Stage 2: Uncertainty-routed generation |
107 | | - logger.info("Generating response with uncertainty routing") |
| 52 | + uncertainty_cot = UncertaintyRoutedCoT( |
| 53 | + client=client, |
| 54 | + model=model, |
| 55 | + max_tokens=config["max_tokens"] |
| 56 | + ) |
| 57 | + |
| 58 | + total_tokens = 0 |
| 59 | + |
| 60 | + # Stage 1: SELF-DISCOVER reasoning structure (if enabled) |
| 61 | + reasoning_structure = None |
| 62 | + if config["enable_self_discover"]: |
| 63 | + logger.info("Discovering task-specific reasoning structure") |
108 | 64 |
|
109 | | - generation_result = uncertainty_cot.generate_with_uncertainty_routing( |
110 | | - prompt=enhanced_prompt, |
111 | | - num_samples=config["deepthink_samples"], |
112 | | - confidence_threshold=config["confidence_threshold"], |
113 | | - temperature=config["temperature"], |
114 | | - top_p=config["top_p"] |
| 65 | + discovery_result = self_discover.discover_reasoning_structure( |
| 66 | + task_description=_extract_task_description(initial_query, system_prompt), |
| 67 | + task_examples=None # Could be enhanced to extract examples |
115 | 68 | ) |
116 | 69 |
|
117 | | - total_tokens += generation_result["completion_tokens"] |
| 70 | + reasoning_structure = discovery_result["reasoning_structure"] |
| 71 | + total_tokens += discovery_result["completion_tokens"] |
118 | 72 |
|
119 | | - # Log routing decision |
120 | | - logger.info(f"Routing decision: {generation_result['routing_decision']} " |
121 | | - f"(confidence: {generation_result['confidence_score']:.3f})") |
122 | | - |
123 | | - final_response = generation_result["final_response"] |
124 | | - |
125 | | - # Clean up the response if needed |
126 | | - final_response = _clean_response(final_response) |
127 | | - |
128 | | - logger.info(f"Deep Think completed successfully. Total tokens: {total_tokens}") |
129 | | - |
130 | | - return final_response, total_tokens |
131 | | - |
132 | | - finally: |
133 | | - # Remove from path after use |
134 | | - if deepthink_dir in sys.path: |
135 | | - sys.path.remove(deepthink_dir) |
| 73 | + logger.info(f"Discovered reasoning structure with {len(reasoning_structure)} components") |
| 74 | + |
| 75 | + # Prepare enhanced prompt |
| 76 | + enhanced_prompt = _create_enhanced_prompt( |
| 77 | + system_prompt=system_prompt, |
| 78 | + initial_query=initial_query, |
| 79 | + reasoning_structure=reasoning_structure, |
| 80 | + config=config |
| 81 | + ) |
| 82 | + |
| 83 | + # Stage 2: Uncertainty-routed generation |
| 84 | + logger.info("Generating response with uncertainty routing") |
| 85 | + |
| 86 | + generation_result = uncertainty_cot.generate_with_uncertainty_routing( |
| 87 | + prompt=enhanced_prompt, |
| 88 | + num_samples=config["deepthink_samples"], |
| 89 | + confidence_threshold=config["confidence_threshold"], |
| 90 | + temperature=config["temperature"], |
| 91 | + top_p=config["top_p"] |
| 92 | + ) |
| 93 | + |
| 94 | + total_tokens += generation_result["completion_tokens"] |
| 95 | + |
| 96 | + # Log routing decision |
| 97 | + logger.info(f"Routing decision: {generation_result['routing_decision']} " |
| 98 | + f"(confidence: {generation_result['confidence_score']:.3f})") |
| 99 | + |
| 100 | + final_response = generation_result["final_response"] |
| 101 | + |
| 102 | + # Clean up the response if needed |
| 103 | + final_response = _clean_response(final_response) |
| 104 | + |
| 105 | + logger.info(f"Deep Think completed successfully. Total tokens: {total_tokens}") |
| 106 | + |
| 107 | + return final_response, total_tokens |
136 | 108 |
|
137 | 109 | def _parse_config(request_config: Dict[str, Any]) -> Dict[str, Any]: |
138 | 110 | """Parse and validate configuration parameters.""" |
|
0 commit comments