MCPcopy
hub / github.com/SqueezeAILab/LLMCompiler / Planner

Class Planner

src/llm_compiler/planner.py:190–282  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

188
189
190class Planner:
191 def __init__(
192 self,
193 llm: BaseChatModel,
194 example_prompt: str,
195 example_prompt_replan: str,
196 tools: Sequence[Union[Tool, StructuredTool]],
197 stop: Optional[list[str]],
198 ):
199 self.llm = llm
200 # different system prompt is needed when replanning
201 # since they have different guidelines, and also examples provided by the user
202 self.system_prompt = generate_llm_compiler_prompt(
203 tools=tools,
204 example_prompt=example_prompt,
205 is_replan=False,
206 )
207 self.system_prompt_replan = generate_llm_compiler_prompt(
208 tools=tools,
209 example_prompt=example_prompt_replan,
210 is_replan=True,
211 )
212 self.tools = tools
213 self.output_parser = LLMCompilerPlanParser(tools=tools)
214 self.stop = stop
215
216 async def run_llm(
217 self,
218 inputs: dict[str, Any],
219 is_replan: bool = False,
220 callbacks: Callbacks = None,
221 ) -> str:
222 """Run the LLM."""
223 if is_replan:
224 system_prompt = self.system_prompt_replan
225 assert "context" in inputs, "If replanning, context must be provided"
226 human_prompt = f"Question: {inputs['input']}\n{inputs['context']}\n"
227 else:
228 system_prompt = self.system_prompt
229 human_prompt = f"Question: {inputs['input']}"
230
231 if isinstance(self.llm, BaseChatModel):
232 messages = [
233 SystemMessage(content=system_prompt),
234 HumanMessage(content=human_prompt),
235 ]
236 llm_response = await self.llm._call_async(
237 messages,
238 callbacks=callbacks,
239 stop=self.stop,
240 )
241 response = llm_response.content
242 elif isinstance(self.llm, BaseLLM):
243 message = system_prompt + "\n\n" + human_prompt
244 response = await self.llm.apredict(
245 message,
246 callbacks=callbacks,
247 stop=self.stop,

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected