improve logging (#66)

This commit is contained in:
Harrison Chase 2022-11-05 15:13:12 -07:00 committed by GitHub
parent 818b06ebbc
commit f772934108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 12 deletions

View File

@ -20,9 +20,9 @@
"source": [ "source": [
"llm = OpenAI(temperature=0)\n", "llm = OpenAI(temperature=0)\n",
"search = SerpAPIChain()\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 = 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", "chains = [\n",
" ChainConfig(\n", " ChainConfig(\n",
" action_name = \"Search\",\n", " action_name = \"Search\",\n",
@ -63,6 +63,9 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "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", "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", "Thought:\u001b[102m I need to find the age of Olivia Wilde's boyfriend\n",
"Action: Search\n", "Action: Search\n",
@ -75,10 +78,23 @@
"Thought:\u001b[102m I need to calculate 28 to the 0.23 power\n", "Thought:\u001b[102m I need to calculate 28 to the 0.23 power\n",
"Action: Calculator\n", "Action: Calculator\n",
"Action Input: 28^0.23\u001b[0m\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", "Observation: \u001b[103mAnswer: 2.1520202182226886\n",
"\u001b[0m\n", "\u001b[0m\n",
"Thought:\u001b[102m I now know the final answer\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", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "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", "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", "Thought:\u001b[102m I need to find an album called 'The Storm Before the Calm'\n",
"Action: Search\n", "Action: Search\n",
@ -114,13 +133,30 @@
"Thought:\u001b[102m I need to check if Alanis is in the FooBar database\n", "Thought:\u001b[102m I need to check if Alanis is in the FooBar database\n",
"Action: FooBar DB\n", "Action: FooBar DB\n",
"Action Input: \"Does Alanis Morissette exist in the FooBar database?\"\u001b[0m\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", "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", "Thought:\u001b[102m I need to find out what albums of Alanis's are in the FooBar database\n",
"Action: FooBar DB\n", "Action: FooBar DB\n",
"Action Input: \"What albums by Alanis Morissette are in the FooBar database?\"\u001b[0m\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", "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", "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"
] ]
}, },
{ {

View File

@ -2,10 +2,15 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Any, Dict, List from typing import Any, Dict, List
from pydantic import BaseModel
class Chain(ABC):
class Chain(BaseModel, ABC):
"""Base interface that all chains should implement.""" """Base interface that all chains should implement."""
verbose: bool = False
"""Whether to print out the code that was executed."""
@property @property
@abstractmethod @abstractmethod
def input_keys(self) -> List[str]: def input_keys(self) -> List[str]:
@ -36,6 +41,10 @@ class Chain(ABC):
def __call__(self, inputs: Dict[str, Any]) -> Dict[str, str]: def __call__(self, inputs: Dict[str, Any]) -> Dict[str, str]:
"""Run the logic of this chain and add to output.""" """Run the logic of this chain and add to output."""
self._validate_inputs(inputs) self._validate_inputs(inputs)
if self.verbose:
print("\n\n\033[1m> Entering new chain...\033[0m")
outputs = self._run(inputs) outputs = self._run(inputs)
if self.verbose:
print("\n\033[1m> Finished chain.\033[0m")
self._validate_outputs(outputs) self._validate_outputs(outputs)
return {**inputs, **outputs} return {**inputs, **outputs}

View File

@ -23,8 +23,6 @@ class LLMMathChain(Chain, BaseModel):
llm: LLM llm: LLM
"""LLM wrapper to use.""" """LLM wrapper to use."""
verbose: bool = False
"""Whether to print out the code that was executed."""
input_key: str = "question" #: :meta private: input_key: str = "question" #: :meta private:
output_key: str = "answer" #: :meta private: output_key: str = "answer" #: :meta private:

View File

@ -72,8 +72,6 @@ class MRKLChain(Chain, BaseModel):
"""Prompt to use as router.""" """Prompt to use as router."""
action_to_chain_map: Dict[str, Callable] action_to_chain_map: Dict[str, Callable]
"""Mapping from action name to chain to execute.""" """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: input_key: str = "question" #: :meta private:
output_key: str = "answer" #: :meta private: output_key: str = "answer" #: :meta private:

View File

@ -49,7 +49,6 @@ class ReActChain(Chain, BaseModel):
"""LLM wrapper to use.""" """LLM wrapper to use."""
docstore: Docstore docstore: Docstore
"""Docstore to use.""" """Docstore to use."""
verbose: bool = False
input_key: str = "question" #: :meta private: input_key: str = "question" #: :meta private:
output_key: str = "answer" #: :meta private: output_key: str = "answer" #: :meta private:

View File

@ -89,7 +89,6 @@ class SelfAskWithSearchChain(Chain, BaseModel):
"""LLM wrapper to use.""" """LLM wrapper to use."""
search_chain: SerpAPIChain search_chain: SerpAPIChain
"""Search chain to use.""" """Search chain to use."""
verbose: bool = False
input_key: str = "question" #: :meta private: input_key: str = "question" #: :meta private:
output_key: str = "answer" #: :meta private: output_key: str = "answer" #: :meta private:

View File

@ -26,7 +26,6 @@ class SQLDatabaseChain(Chain, BaseModel):
"""LLM wrapper to use.""" """LLM wrapper to use."""
database: SQLDatabase database: SQLDatabase
"""SQL Database to connect to.""" """SQL Database to connect to."""
verbose: bool = False
input_key: str = "query" #: :meta private: input_key: str = "query" #: :meta private:
output_key: str = "result" #: :meta private: output_key: str = "result" #: :meta private: