From a19ad935b3329ff6c745a8a51343eb3ece48d949 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Sat, 19 Nov 2022 20:39:35 -0800 Subject: [PATCH] Harrison/verbose prompt (#159) Add printing of prompt to LLMChain --- docs/examples/demos/simple_prompts.ipynb | 21 ++++++++++++++++++--- langchain/chains/llm.py | 5 ++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/examples/demos/simple_prompts.ipynb b/docs/examples/demos/simple_prompts.ipynb index 4299261d9c5..208b2e48a97 100644 --- a/docs/examples/demos/simple_prompts.ipynb +++ b/docs/examples/demos/simple_prompts.ipynb @@ -12,17 +12,32 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "51a54c4d", "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "Prompt after formatting:\n", + "\u001b[32;1m\u001b[1;3mQuestion: What NFL team won the Super Bowl in the year Justin Beiber was born?\n", + "\n", + "Answer: Let's think step by step.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, { "data": { "text/plain": [ "' The year Justin Beiber was born was 1994. In 1994, the Dallas Cowboys won the Super Bowl.'" ] }, - "execution_count": 2, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -34,7 +49,7 @@ "\n", "Answer: Let's think step by step.\"\"\"\n", "prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n", - "llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0))\n", + "llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0), verbose=True)\n", "\n", "question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n", "\n", diff --git a/langchain/chains/llm.py b/langchain/chains/llm.py index 5133129ec64..93ac27cbc0e 100644 --- a/langchain/chains/llm.py +++ b/langchain/chains/llm.py @@ -4,6 +4,7 @@ from typing import Any, Dict, List from pydantic import BaseModel, Extra from langchain.chains.base import Chain +from langchain.input import print_text from langchain.llms.base import LLM from langchain.prompts.base import BasePromptTemplate @@ -53,7 +54,9 @@ class LLMChain(Chain, BaseModel): def _call(self, inputs: Dict[str, Any]) -> Dict[str, str]: selected_inputs = {k: inputs[k] for k in self.prompt.input_variables} prompt = self.prompt.format(**selected_inputs) - + if self.verbose: + print("Prompt after formatting:") + print_text(prompt, color="green", end="\n") kwargs = {} if "stop" in inputs: kwargs["stop"] = inputs["stop"]