mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-28 01:19:31 +00:00
Harrison/get rid of prints (#490)
deprecate all prints in favor of callback_manager.on_text (open to better naming)
This commit is contained in:
parent
b902bddb8a
commit
175a248506
@ -218,7 +218,7 @@ class AgentExecutor(Chain, BaseModel):
|
|||||||
# If the tool chosen is the finishing tool, then we end and return.
|
# If the tool chosen is the finishing tool, then we end and return.
|
||||||
if isinstance(output, AgentFinish):
|
if isinstance(output, AgentFinish):
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
self.callback_manager.on_agent_end(output.log, color="green")
|
self.callback_manager.on_text(output.log, color="green")
|
||||||
final_output = output.return_values
|
final_output = output.return_values
|
||||||
if self.return_intermediate_steps:
|
if self.return_intermediate_steps:
|
||||||
final_output["intermediate_steps"] = intermediate_steps
|
final_output["intermediate_steps"] = intermediate_steps
|
||||||
|
@ -55,7 +55,7 @@ class BaseCallbackHandler(ABC):
|
|||||||
"""Run when tool errors."""
|
"""Run when tool errors."""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def on_agent_end(self, log: str, **kwargs: Any) -> None:
|
def on_text(self, text: str, **kwargs: Any) -> None:
|
||||||
"""Run when agent ends."""
|
"""Run when agent ends."""
|
||||||
|
|
||||||
|
|
||||||
@ -132,10 +132,10 @@ class CallbackManager(BaseCallbackManager):
|
|||||||
for handler in self.handlers:
|
for handler in self.handlers:
|
||||||
handler.on_tool_error(error)
|
handler.on_tool_error(error)
|
||||||
|
|
||||||
def on_agent_end(self, log: str, **kwargs: Any) -> None:
|
def on_text(self, text: str, **kwargs: Any) -> None:
|
||||||
"""Run when agent ends."""
|
"""Run when agent ends."""
|
||||||
for handler in self.handlers:
|
for handler in self.handlers:
|
||||||
handler.on_agent_end(log, **kwargs)
|
handler.on_text(text, **kwargs)
|
||||||
|
|
||||||
def add_handler(self, handler: BaseCallbackHandler) -> None:
|
def add_handler(self, handler: BaseCallbackHandler) -> None:
|
||||||
"""Add a handler to the callback manager."""
|
"""Add a handler to the callback manager."""
|
||||||
|
@ -88,10 +88,10 @@ class SharedCallbackManager(Singleton, BaseCallbackManager):
|
|||||||
with self._lock:
|
with self._lock:
|
||||||
self._callback_manager.on_tool_error(error)
|
self._callback_manager.on_tool_error(error)
|
||||||
|
|
||||||
def on_agent_end(self, log: str, **kwargs: Any) -> None:
|
def on_text(self, text: str, **kwargs: Any) -> None:
|
||||||
"""Run when agent ends."""
|
"""Run when agent ends."""
|
||||||
with self._lock:
|
with self._lock:
|
||||||
self._callback_manager.on_agent_end(log, **kwargs)
|
self._callback_manager.on_text(text, **kwargs)
|
||||||
|
|
||||||
def add_handler(self, callback: BaseCallbackHandler) -> None:
|
def add_handler(self, callback: BaseCallbackHandler) -> None:
|
||||||
"""Add a callback to the callback manager."""
|
"""Add a callback to the callback manager."""
|
||||||
|
@ -67,8 +67,12 @@ class StdOutCallbackHandler(BaseCallbackHandler):
|
|||||||
"""Do nothing."""
|
"""Do nothing."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def on_agent_end(
|
def on_text(
|
||||||
self, log: str, color: Optional[str] = None, **kwargs: Any
|
self,
|
||||||
|
text: str,
|
||||||
|
color: Optional[str] = None,
|
||||||
|
end: str = "",
|
||||||
|
**kwargs: Optional[str],
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Run when agent ends."""
|
"""Run when agent ends."""
|
||||||
print_text(log, color=color)
|
print_text(text, color=color, end=end)
|
||||||
|
@ -8,7 +8,6 @@ from pydantic import BaseModel, root_validator
|
|||||||
from langchain.chains.api.prompt import API_RESPONSE_PROMPT, API_URL_PROMPT
|
from langchain.chains.api.prompt import API_RESPONSE_PROMPT, API_URL_PROMPT
|
||||||
from langchain.chains.base import Chain
|
from langchain.chains.base import Chain
|
||||||
from langchain.chains.llm import LLMChain
|
from langchain.chains.llm import LLMChain
|
||||||
from langchain.input import print_text
|
|
||||||
from langchain.llms.base import BaseLLM
|
from langchain.llms.base import BaseLLM
|
||||||
from langchain.requests import RequestsWrapper
|
from langchain.requests import RequestsWrapper
|
||||||
|
|
||||||
@ -67,10 +66,10 @@ class APIChain(Chain, BaseModel):
|
|||||||
question=question, api_docs=self.api_docs
|
question=question, api_docs=self.api_docs
|
||||||
)
|
)
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(api_url, color="green", end="\n")
|
self.callback_manager.on_text(api_url, color="green", end="\n")
|
||||||
api_response = self.requests_wrapper.run(api_url)
|
api_response = self.requests_wrapper.run(api_url)
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(api_response, color="yellow", end="\n")
|
self.callback_manager.on_text(api_response, color="yellow", end="\n")
|
||||||
answer = self.api_answer_chain.predict(
|
answer = self.api_answer_chain.predict(
|
||||||
question=question,
|
question=question,
|
||||||
api_docs=self.api_docs,
|
api_docs=self.api_docs,
|
||||||
|
@ -6,7 +6,6 @@ from pydantic import BaseModel, Extra
|
|||||||
from langchain.chains.base import Chain
|
from langchain.chains.base import Chain
|
||||||
from langchain.chains.llm import LLMChain
|
from langchain.chains.llm import LLMChain
|
||||||
from langchain.chains.llm_bash.prompt import PROMPT
|
from langchain.chains.llm_bash.prompt import PROMPT
|
||||||
from langchain.input import print_text
|
|
||||||
from langchain.llms.base import BaseLLM
|
from langchain.llms.base import BaseLLM
|
||||||
from langchain.utilities.bash import BashProcess
|
from langchain.utilities.bash import BashProcess
|
||||||
|
|
||||||
@ -52,11 +51,11 @@ class LLMBashChain(Chain, BaseModel):
|
|||||||
llm_executor = LLMChain(prompt=PROMPT, llm=self.llm)
|
llm_executor = LLMChain(prompt=PROMPT, llm=self.llm)
|
||||||
bash_executor = BashProcess()
|
bash_executor = BashProcess()
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(inputs[self.input_key])
|
self.callback_manager.on_text(inputs[self.input_key])
|
||||||
|
|
||||||
t = llm_executor.predict(question=inputs[self.input_key])
|
t = llm_executor.predict(question=inputs[self.input_key])
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(t, color="green")
|
self.callback_manager.on_text(t, color="green")
|
||||||
|
|
||||||
t = t.strip()
|
t = t.strip()
|
||||||
if t.startswith("```bash"):
|
if t.startswith("```bash"):
|
||||||
@ -69,8 +68,8 @@ class LLMBashChain(Chain, BaseModel):
|
|||||||
output = bash_executor.run(command_list)
|
output = bash_executor.run(command_list)
|
||||||
|
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text("\nAnswer: ")
|
self.callback_manager.on_text("\nAnswer: ")
|
||||||
print_text(output, color="yellow")
|
self.callback_manager.on_text(output, color="yellow")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"unknown format from LLM: {t}")
|
raise ValueError(f"unknown format from LLM: {t}")
|
||||||
|
@ -6,7 +6,6 @@ from pydantic import BaseModel, Extra
|
|||||||
from langchain.chains.base import Chain
|
from langchain.chains.base import Chain
|
||||||
from langchain.chains.llm import LLMChain
|
from langchain.chains.llm import LLMChain
|
||||||
from langchain.chains.llm_math.prompt import PROMPT
|
from langchain.chains.llm_math.prompt import PROMPT
|
||||||
from langchain.input import print_text
|
|
||||||
from langchain.llms.base import BaseLLM
|
from langchain.llms.base import BaseLLM
|
||||||
from langchain.python import PythonREPL
|
from langchain.python import PythonREPL
|
||||||
|
|
||||||
@ -52,17 +51,17 @@ class LLMMathChain(Chain, BaseModel):
|
|||||||
llm_executor = LLMChain(prompt=PROMPT, llm=self.llm)
|
llm_executor = LLMChain(prompt=PROMPT, llm=self.llm)
|
||||||
python_executor = PythonREPL()
|
python_executor = PythonREPL()
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(inputs[self.input_key])
|
self.callback_manager.on_text(inputs[self.input_key])
|
||||||
t = llm_executor.predict(question=inputs[self.input_key], stop=["```output"])
|
t = llm_executor.predict(question=inputs[self.input_key], stop=["```output"])
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(t, color="green")
|
self.callback_manager.on_text(t, color="green")
|
||||||
t = t.strip()
|
t = t.strip()
|
||||||
if t.startswith("```python"):
|
if t.startswith("```python"):
|
||||||
code = t[9:-4]
|
code = t[9:-4]
|
||||||
output = python_executor.run(code)
|
output = python_executor.run(code)
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text("\nAnswer: ")
|
self.callback_manager.on_text("\nAnswer: ")
|
||||||
print_text(output, color="yellow")
|
self.callback_manager.on_text(output, color="yellow")
|
||||||
answer = "Answer: " + output
|
answer = "Answer: " + output
|
||||||
elif t.startswith("Answer:"):
|
elif t.startswith("Answer:"):
|
||||||
answer = t
|
answer = t
|
||||||
|
@ -12,7 +12,6 @@ from langchain.chains.base import Chain
|
|||||||
from langchain.chains.llm import LLMChain
|
from langchain.chains.llm import LLMChain
|
||||||
from langchain.chains.pal.colored_object_prompt import COLORED_OBJECT_PROMPT
|
from langchain.chains.pal.colored_object_prompt import COLORED_OBJECT_PROMPT
|
||||||
from langchain.chains.pal.math_prompt import MATH_PROMPT
|
from langchain.chains.pal.math_prompt import MATH_PROMPT
|
||||||
from langchain.input import print_text
|
|
||||||
from langchain.llms.base import BaseLLM
|
from langchain.llms.base import BaseLLM
|
||||||
from langchain.prompts.base import BasePromptTemplate
|
from langchain.prompts.base import BasePromptTemplate
|
||||||
from langchain.python import PythonREPL
|
from langchain.python import PythonREPL
|
||||||
@ -53,7 +52,7 @@ class PALChain(Chain, BaseModel):
|
|||||||
llm_chain = LLMChain(llm=self.llm, prompt=self.prompt)
|
llm_chain = LLMChain(llm=self.llm, prompt=self.prompt)
|
||||||
code = llm_chain.predict(stop=[self.stop], **inputs)
|
code = llm_chain.predict(stop=[self.stop], **inputs)
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(code, color="green", end="\n")
|
self.callback_manager.on_text(code, color="green", end="\n")
|
||||||
repl = PythonREPL()
|
repl = PythonREPL()
|
||||||
res = repl.run(code + f"\n{self.get_answer_expr}")
|
res = repl.run(code + f"\n{self.get_answer_expr}")
|
||||||
return {self.output_key: res.strip()}
|
return {self.output_key: res.strip()}
|
||||||
|
@ -5,7 +5,7 @@ from typing import Dict, List
|
|||||||
from pydantic import BaseModel, Extra, root_validator
|
from pydantic import BaseModel, Extra, root_validator
|
||||||
|
|
||||||
from langchain.chains.base import Chain
|
from langchain.chains.base import Chain
|
||||||
from langchain.input import get_color_mapping, print_text
|
from langchain.input import get_color_mapping
|
||||||
|
|
||||||
|
|
||||||
class SequentialChain(Chain, BaseModel):
|
class SequentialChain(Chain, BaseModel):
|
||||||
@ -133,5 +133,7 @@ class SimpleSequentialChain(Chain, BaseModel):
|
|||||||
if self.strip_outputs:
|
if self.strip_outputs:
|
||||||
_input = _input.strip()
|
_input = _input.strip()
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(_input, color=color_mapping[str(i)], end="\n")
|
self.callback_manager.on_text(
|
||||||
|
_input, color=color_mapping[str(i)], end="\n"
|
||||||
|
)
|
||||||
return {self.output_key: _input}
|
return {self.output_key: _input}
|
||||||
|
@ -6,7 +6,6 @@ from pydantic import BaseModel, Extra
|
|||||||
from langchain.chains.base import Chain
|
from langchain.chains.base import Chain
|
||||||
from langchain.chains.llm import LLMChain
|
from langchain.chains.llm import LLMChain
|
||||||
from langchain.chains.sql_database.prompt import PROMPT
|
from langchain.chains.sql_database.prompt import PROMPT
|
||||||
from langchain.input import print_text
|
|
||||||
from langchain.llms.base import BaseLLM
|
from langchain.llms.base import BaseLLM
|
||||||
from langchain.sql_database import SQLDatabase
|
from langchain.sql_database import SQLDatabase
|
||||||
|
|
||||||
@ -55,7 +54,7 @@ class SQLDatabaseChain(Chain, BaseModel):
|
|||||||
llm_chain = LLMChain(llm=self.llm, prompt=PROMPT)
|
llm_chain = LLMChain(llm=self.llm, prompt=PROMPT)
|
||||||
input_text = f"{inputs[self.input_key]} \nSQLQuery:"
|
input_text = f"{inputs[self.input_key]} \nSQLQuery:"
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(input_text)
|
self.callback_manager.on_text(input_text)
|
||||||
llm_inputs = {
|
llm_inputs = {
|
||||||
"input": input_text,
|
"input": input_text,
|
||||||
"dialect": self.database.dialect,
|
"dialect": self.database.dialect,
|
||||||
@ -64,15 +63,15 @@ class SQLDatabaseChain(Chain, BaseModel):
|
|||||||
}
|
}
|
||||||
sql_cmd = llm_chain.predict(**llm_inputs)
|
sql_cmd = llm_chain.predict(**llm_inputs)
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(sql_cmd, color="green")
|
self.callback_manager.on_text(sql_cmd, color="green")
|
||||||
result = self.database.run(sql_cmd)
|
result = self.database.run(sql_cmd)
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text("\nSQLResult: ")
|
self.callback_manager.on_text("\nSQLResult: ")
|
||||||
print_text(result, color="yellow")
|
self.callback_manager.on_text(result, color="yellow")
|
||||||
print_text("\nAnswer:")
|
self.callback_manager.on_text("\nAnswer:")
|
||||||
input_text += f"{sql_cmd}\nSQLResult: {result}\nAnswer:"
|
input_text += f"{sql_cmd}\nSQLResult: {result}\nAnswer:"
|
||||||
llm_inputs["input"] = input_text
|
llm_inputs["input"] = input_text
|
||||||
final_result = llm_chain.predict(**llm_inputs)
|
final_result = llm_chain.predict(**llm_inputs)
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_text(final_result, color="green")
|
self.callback_manager.on_text(final_result, color="green")
|
||||||
return {self.output_key: final_result}
|
return {self.output_key: final_result}
|
||||||
|
@ -59,6 +59,6 @@ class FakeCallbackHandler(BaseCallbackHandler):
|
|||||||
"""Run when tool errors."""
|
"""Run when tool errors."""
|
||||||
self.errors += 1
|
self.errors += 1
|
||||||
|
|
||||||
def on_agent_end(self, log: str, **kwargs: Any) -> None:
|
def on_text(self, text: str, **kwargs: Any) -> None:
|
||||||
"""Run when agent is ending."""
|
"""Run when agent is ending."""
|
||||||
self.ends += 1
|
self.ends += 1
|
||||||
|
Loading…
Reference in New Issue
Block a user