forked from Archives/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
"""Tool for asking human input."""
|
|
|
|
from typing import Callable, Optional
|
|
|
|
from pydantic import Field
|
|
|
|
from langchain.callbacks.manager import (
|
|
AsyncCallbackManagerForToolRun,
|
|
CallbackManagerForToolRun,
|
|
)
|
|
from langchain.tools.base import BaseTool
|
|
|
|
|
|
def _print_func(text: str) -> None:
|
|
print("\n")
|
|
print(text)
|
|
|
|
|
|
class HumanInputRun(BaseTool):
|
|
"""Tool that adds the capability to ask user for input."""
|
|
|
|
name = "human"
|
|
description = (
|
|
"You can ask a human for guidance when you think you "
|
|
"got stuck or you are not sure what to do next. "
|
|
"The input should be a question for the human."
|
|
)
|
|
prompt_func: Callable[[str], None] = Field(default_factory=lambda: _print_func)
|
|
input_func: Callable = Field(default_factory=lambda: input)
|
|
|
|
def _run(
|
|
self,
|
|
query: str,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Use the Human input tool."""
|
|
self.prompt_func(query)
|
|
return self.input_func()
|
|
|
|
async def _arun(
|
|
self,
|
|
query: str,
|
|
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Use the Human tool asynchronously."""
|
|
raise NotImplementedError("Human tool does not support async")
|