From f7729341083714ee9b29c984f0b643d760c2d469 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Sat, 5 Nov 2022 15:13:12 -0700 Subject: [PATCH] improve logging (#66) --- examples/mrkl.ipynb | 44 +++++++++++++++++-- langchain/chains/base.py | 11 ++++- langchain/chains/llm_math/base.py | 2 - langchain/chains/mrkl/base.py | 2 - langchain/chains/react/base.py | 1 - langchain/chains/self_ask_with_search/base.py | 1 - langchain/chains/sql_database/base.py | 1 - 7 files changed, 50 insertions(+), 12 deletions(-) diff --git a/examples/mrkl.ipynb b/examples/mrkl.ipynb index 3e4b40e2ab7..e1a50364bad 100644 --- a/examples/mrkl.ipynb +++ b/examples/mrkl.ipynb @@ -20,9 +20,9 @@ "source": [ "llm = OpenAI(temperature=0)\n", "search = SerpAPIChain()\n", - "llm_math_chain = LLMMathChain(llm=llm)\n", + "llm_math_chain = LLMMathChain(llm=llm, verbose=True)\n", "db = SQLDatabase.from_uri(\"sqlite:///../notebooks/Chinook.db\")\n", - "db_chain = SQLDatabaseChain(llm=llm, database=db)\n", + "db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)\n", "chains = [\n", " ChainConfig(\n", " action_name = \"Search\",\n", @@ -63,6 +63,9 @@ "name": "stdout", "output_type": "stream", "text": [ + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", "What is the age of Olivia Wilde's boyfriend raised to the 0.23 power?\n", "Thought:\u001b[102m I need to find the age of Olivia Wilde's boyfriend\n", "Action: Search\n", @@ -75,10 +78,23 @@ "Thought:\u001b[102m I need to calculate 28 to the 0.23 power\n", "Action: Calculator\n", "Action Input: 28^0.23\u001b[0m\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "28^0.23\u001b[102m\n", + "\n", + "```python\n", + "print(28**0.23)\n", + "```\n", + "\u001b[0m\n", + "Answer: \u001b[103m2.1520202182226886\n", + "\u001b[0m\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", "Observation: \u001b[103mAnswer: 2.1520202182226886\n", "\u001b[0m\n", "Thought:\u001b[102m I now know the final answer\n", - "Final Answer: 2.1520202182226886\u001b[0m" + "Final Answer: 2.1520202182226886\u001b[0m\n", + "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { @@ -106,6 +122,9 @@ "name": "stdout", "output_type": "stream", "text": [ + "\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", "Who recently released an album called 'The Storm Before the Calm' and are they in the FooBar database? If so, what albums of theirs are in the FooBar database?\n", "Thought:\u001b[102m I need to find an album called 'The Storm Before the Calm'\n", "Action: Search\n", @@ -114,13 +133,30 @@ "Thought:\u001b[102m I need to check if Alanis is in the FooBar database\n", "Action: FooBar DB\n", "Action Input: \"Does Alanis Morissette exist in the FooBar database?\"\u001b[0m\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "Does Alanis Morissette exist in the FooBar database?\n", + "SQLQuery:\u001b[102m SELECT * FROM Artist WHERE Name = 'Alanis Morissette'\u001b[0m\n", + "SQLResult: \u001b[103m[(4, 'Alanis Morissette')]\u001b[0m\n", + "Answer:\u001b[102m Yes\u001b[0m\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", "Observation: \u001b[101m Yes\u001b[0m\n", "Thought:\u001b[102m I need to find out what albums of Alanis's are in the FooBar database\n", "Action: FooBar DB\n", "Action Input: \"What albums by Alanis Morissette are in the FooBar database?\"\u001b[0m\n", + "\n", + "\u001b[1m> Entering new chain...\u001b[0m\n", + "What albums by Alanis Morissette are in the FooBar database?\n", + "SQLQuery:\u001b[102m SELECT Title FROM Album WHERE ArtistId = (SELECT ArtistId FROM Artist WHERE Name = 'Alanis Morissette')\u001b[0m\n", + "SQLResult: \u001b[103m[('Jagged Little Pill',)]\u001b[0m\n", + "Answer:\u001b[102m The album \"Jagged Little Pill\" by Alanis Morissette is in the FooBar database.\u001b[0m\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", "Observation: \u001b[101m The album \"Jagged Little Pill\" by Alanis Morissette is in the FooBar database.\u001b[0m\n", "Thought:\u001b[102m I now know the final answer\n", - "Final Answer: The album \"Jagged Little Pill\" by Alanis Morissette is the only album by Alanis Morissette in the FooBar database.\u001b[0m" + "Final Answer: The album \"Jagged Little Pill\" by Alanis Morissette is the only album by Alanis Morissette in the FooBar database.\u001b[0m\n", + "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { diff --git a/langchain/chains/base.py b/langchain/chains/base.py index 4d200bd0ce3..dfc48c59b95 100644 --- a/langchain/chains/base.py +++ b/langchain/chains/base.py @@ -2,10 +2,15 @@ from abc import ABC, abstractmethod from typing import Any, Dict, List +from pydantic import BaseModel -class Chain(ABC): + +class Chain(BaseModel, ABC): """Base interface that all chains should implement.""" + verbose: bool = False + """Whether to print out the code that was executed.""" + @property @abstractmethod def input_keys(self) -> List[str]: @@ -36,6 +41,10 @@ class Chain(ABC): def __call__(self, inputs: Dict[str, Any]) -> Dict[str, str]: """Run the logic of this chain and add to output.""" self._validate_inputs(inputs) + if self.verbose: + print("\n\n\033[1m> Entering new chain...\033[0m") outputs = self._run(inputs) + if self.verbose: + print("\n\033[1m> Finished chain.\033[0m") self._validate_outputs(outputs) return {**inputs, **outputs} diff --git a/langchain/chains/llm_math/base.py b/langchain/chains/llm_math/base.py index 49aaf1bbc81..6da17545347 100644 --- a/langchain/chains/llm_math/base.py +++ b/langchain/chains/llm_math/base.py @@ -23,8 +23,6 @@ class LLMMathChain(Chain, BaseModel): llm: LLM """LLM wrapper to use.""" - verbose: bool = False - """Whether to print out the code that was executed.""" input_key: str = "question" #: :meta private: output_key: str = "answer" #: :meta private: diff --git a/langchain/chains/mrkl/base.py b/langchain/chains/mrkl/base.py index 80b4e5c0e59..4ad5d564b6b 100644 --- a/langchain/chains/mrkl/base.py +++ b/langchain/chains/mrkl/base.py @@ -72,8 +72,6 @@ class MRKLChain(Chain, BaseModel): """Prompt to use as router.""" action_to_chain_map: Dict[str, Callable] """Mapping from action name to chain to execute.""" - verbose: bool = False - """Whether to print out the code that was executed.""" input_key: str = "question" #: :meta private: output_key: str = "answer" #: :meta private: diff --git a/langchain/chains/react/base.py b/langchain/chains/react/base.py index 30594d19436..2cd4dd25891 100644 --- a/langchain/chains/react/base.py +++ b/langchain/chains/react/base.py @@ -49,7 +49,6 @@ class ReActChain(Chain, BaseModel): """LLM wrapper to use.""" docstore: Docstore """Docstore to use.""" - verbose: bool = False input_key: str = "question" #: :meta private: output_key: str = "answer" #: :meta private: diff --git a/langchain/chains/self_ask_with_search/base.py b/langchain/chains/self_ask_with_search/base.py index e9d965a006c..26df99ee893 100644 --- a/langchain/chains/self_ask_with_search/base.py +++ b/langchain/chains/self_ask_with_search/base.py @@ -89,7 +89,6 @@ class SelfAskWithSearchChain(Chain, BaseModel): """LLM wrapper to use.""" search_chain: SerpAPIChain """Search chain to use.""" - verbose: bool = False input_key: str = "question" #: :meta private: output_key: str = "answer" #: :meta private: diff --git a/langchain/chains/sql_database/base.py b/langchain/chains/sql_database/base.py index e30c42fd35f..47db1265c37 100644 --- a/langchain/chains/sql_database/base.py +++ b/langchain/chains/sql_database/base.py @@ -26,7 +26,6 @@ class SQLDatabaseChain(Chain, BaseModel): """LLM wrapper to use.""" database: SQLDatabase """SQL Database to connect to.""" - verbose: bool = False input_key: str = "query" #: :meta private: output_key: str = "result" #: :meta private: