mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-28 01:19:31 +00:00
Pack of small fixes and refactorings that don't affect functionality (#6990)
Description: Pack of small fixes and refactorings that don't affect functionality, just making code prettier & fixing some misspelling (hand-filtered improvements proposed by SeniorAi.online, prototype of code improving tool based on gpt4), agents and callbacks folders was covered. Dependencies: Nothing changed Twitter: https://twitter.com/nayjest Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
parent
87f75cb322
commit
5809c3d29d
@ -1,5 +1,5 @@
|
|||||||
"""Toolkits for agents."""
|
"""Toolkits for agents."""
|
||||||
from abc import abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@ -7,8 +7,8 @@ from pydantic import BaseModel
|
|||||||
from langchain.tools import BaseTool
|
from langchain.tools import BaseTool
|
||||||
|
|
||||||
|
|
||||||
class BaseToolkit(BaseModel):
|
class BaseToolkit(BaseModel, ABC):
|
||||||
"""Class responsible for defining a collection of related tools."""
|
"""Class representing a collection of related tools."""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_tools(self) -> List[BaseTool]:
|
def get_tools(self) -> List[BaseTool]:
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
"""Agent for working with csvs."""
|
"""Agent for working with csv files."""
|
||||||
from typing import Any, List, Optional, Union
|
from typing import Any, List, Optional, Union
|
||||||
|
|
||||||
from langchain.agents.agent import AgentExecutor
|
from langchain.agents.agent import AgentExecutor
|
||||||
|
@ -1 +1 @@
|
|||||||
"""Gmail toolkit."""
|
"""Office365 toolkit."""
|
||||||
|
@ -39,7 +39,7 @@ class RequestsToolkit(BaseToolkit):
|
|||||||
|
|
||||||
|
|
||||||
class OpenAPIToolkit(BaseToolkit):
|
class OpenAPIToolkit(BaseToolkit):
|
||||||
"""Toolkit for interacting with a OpenAPI api."""
|
"""Toolkit for interacting with an OpenAPI API."""
|
||||||
|
|
||||||
json_agent: AgentExecutor
|
json_agent: AgentExecutor
|
||||||
requests_wrapper: TextRequestsWrapper
|
requests_wrapper: TextRequestsWrapper
|
||||||
|
@ -17,7 +17,7 @@ from langchain.utilities.powerbi import PowerBIDataset
|
|||||||
|
|
||||||
def create_pbi_agent(
|
def create_pbi_agent(
|
||||||
llm: BaseLanguageModel,
|
llm: BaseLanguageModel,
|
||||||
toolkit: Optional[PowerBIToolkit],
|
toolkit: Optional[PowerBIToolkit] = None,
|
||||||
powerbi: Optional[PowerBIDataset] = None,
|
powerbi: Optional[PowerBIDataset] = None,
|
||||||
callback_manager: Optional[BaseCallbackManager] = None,
|
callback_manager: Optional[BaseCallbackManager] = None,
|
||||||
prefix: str = POWERBI_PREFIX,
|
prefix: str = POWERBI_PREFIX,
|
||||||
|
@ -18,7 +18,7 @@ from langchain.utilities.powerbi import PowerBIDataset
|
|||||||
|
|
||||||
def create_pbi_chat_agent(
|
def create_pbi_chat_agent(
|
||||||
llm: BaseChatModel,
|
llm: BaseChatModel,
|
||||||
toolkit: Optional[PowerBIToolkit],
|
toolkit: Optional[PowerBIToolkit] = None,
|
||||||
powerbi: Optional[PowerBIDataset] = None,
|
powerbi: Optional[PowerBIDataset] = None,
|
||||||
callback_manager: Optional[BaseCallbackManager] = None,
|
callback_manager: Optional[BaseCallbackManager] = None,
|
||||||
output_parser: Optional[AgentOutputParser] = None,
|
output_parser: Optional[AgentOutputParser] = None,
|
||||||
@ -32,9 +32,9 @@ def create_pbi_chat_agent(
|
|||||||
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
|
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
|
||||||
**kwargs: Dict[str, Any],
|
**kwargs: Dict[str, Any],
|
||||||
) -> AgentExecutor:
|
) -> AgentExecutor:
|
||||||
"""Construct a pbi agent from an Chat LLM and tools.
|
"""Construct a Power BI agent from a Chat LLM and tools.
|
||||||
|
|
||||||
If you supply only a toolkit and no powerbi dataset, the same LLM is used for both.
|
If you supply only a toolkit and no Power BI dataset, the same LLM is used for both.
|
||||||
"""
|
"""
|
||||||
if toolkit is None:
|
if toolkit is None:
|
||||||
if powerbi is None:
|
if powerbi is None:
|
||||||
|
@ -30,9 +30,8 @@ class ChatOutputParser(AgentOutputParser):
|
|||||||
except Exception:
|
except Exception:
|
||||||
if not includes_answer:
|
if not includes_answer:
|
||||||
raise OutputParserException(f"Could not parse LLM output: {text}")
|
raise OutputParserException(f"Could not parse LLM output: {text}")
|
||||||
return AgentFinish(
|
output = text.split(FINAL_ANSWER_ACTION)[-1].strip()
|
||||||
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
return AgentFinish({"output": output}, text)
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _type(self) -> str:
|
def _type(self) -> str:
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
"""Chain that does self ask with search."""
|
"""Chain that does self-ask with search."""
|
||||||
from typing import Any, Sequence, Union
|
from typing import Any, Sequence, Union
|
||||||
|
|
||||||
from pydantic import Field
|
from pydantic import Field
|
||||||
@ -59,7 +59,7 @@ class SelfAskWithSearchAgent(Agent):
|
|||||||
|
|
||||||
|
|
||||||
class SelfAskWithSearchChain(AgentExecutor):
|
class SelfAskWithSearchChain(AgentExecutor):
|
||||||
"""Chain that does self ask with search.
|
"""Chain that does self-ask with search.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
@ -39,7 +39,7 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|||||||
self, action: AgentAction, color: Optional[str] = None, **kwargs: Any
|
self, action: AgentAction, color: Optional[str] = None, **kwargs: Any
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""Run on agent action."""
|
"""Run on agent action."""
|
||||||
print_text(action.log, color=color if color else self.color, file=self.file)
|
print_text(action.log, color=color or self.color, file=self.file)
|
||||||
|
|
||||||
def on_tool_end(
|
def on_tool_end(
|
||||||
self,
|
self,
|
||||||
@ -52,24 +52,18 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|||||||
"""If not the final action, print out observation."""
|
"""If not the final action, print out observation."""
|
||||||
if observation_prefix is not None:
|
if observation_prefix is not None:
|
||||||
print_text(f"\n{observation_prefix}", file=self.file)
|
print_text(f"\n{observation_prefix}", file=self.file)
|
||||||
print_text(output, color=color if color else self.color, file=self.file)
|
print_text(output, color=color or self.color, file=self.file)
|
||||||
if llm_prefix is not None:
|
if llm_prefix is not None:
|
||||||
print_text(f"\n{llm_prefix}", file=self.file)
|
print_text(f"\n{llm_prefix}", file=self.file)
|
||||||
|
|
||||||
def on_text(
|
def on_text(
|
||||||
self,
|
self, text: str, color: Optional[str] = None, end: str = "", **kwargs: Any
|
||||||
text: str,
|
|
||||||
color: Optional[str] = None,
|
|
||||||
end: str = "",
|
|
||||||
**kwargs: Any,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Run when agent ends."""
|
"""Run when agent ends."""
|
||||||
print_text(text, color=color if color else self.color, end=end, file=self.file)
|
print_text(text, color=color or self.color, end=end, file=self.file)
|
||||||
|
|
||||||
def on_agent_finish(
|
def on_agent_finish(
|
||||||
self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any
|
self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Run on agent end."""
|
"""Run on agent end."""
|
||||||
print_text(
|
print_text(finish.log, color=color or self.color, end="\n", file=self.file)
|
||||||
finish.log, color=color if self.color else color, end="\n", file=self.file
|
|
||||||
)
|
|
||||||
|
@ -63,7 +63,7 @@ class StdOutCallbackHandler(BaseCallbackHandler):
|
|||||||
self, action: AgentAction, color: Optional[str] = None, **kwargs: Any
|
self, action: AgentAction, color: Optional[str] = None, **kwargs: Any
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""Run on agent action."""
|
"""Run on agent action."""
|
||||||
print_text(action.log, color=color if color else self.color)
|
print_text(action.log, color=color or self.color)
|
||||||
|
|
||||||
def on_tool_end(
|
def on_tool_end(
|
||||||
self,
|
self,
|
||||||
@ -76,7 +76,7 @@ class StdOutCallbackHandler(BaseCallbackHandler):
|
|||||||
"""If not the final action, print out observation."""
|
"""If not the final action, print out observation."""
|
||||||
if observation_prefix is not None:
|
if observation_prefix is not None:
|
||||||
print_text(f"\n{observation_prefix}")
|
print_text(f"\n{observation_prefix}")
|
||||||
print_text(output, color=color if color else self.color)
|
print_text(output, color=color or self.color)
|
||||||
if llm_prefix is not None:
|
if llm_prefix is not None:
|
||||||
print_text(f"\n{llm_prefix}")
|
print_text(f"\n{llm_prefix}")
|
||||||
|
|
||||||
@ -94,10 +94,10 @@ class StdOutCallbackHandler(BaseCallbackHandler):
|
|||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Run when agent ends."""
|
"""Run when agent ends."""
|
||||||
print_text(text, color=color if color else self.color, end=end)
|
print_text(text, color=color or self.color, end=end)
|
||||||
|
|
||||||
def on_agent_finish(
|
def on_agent_finish(
|
||||||
self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any
|
self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Run on agent end."""
|
"""Run on agent end."""
|
||||||
print_text(finish.log, color=color if self.color else color, end="\n")
|
print_text(finish.log, color=color or self.color, end="\n")
|
||||||
|
@ -37,7 +37,7 @@ class FinalStreamingStdOutCallbackHandler(StreamingStdOutCallbackHandler):
|
|||||||
"""Instantiate FinalStreamingStdOutCallbackHandler.
|
"""Instantiate FinalStreamingStdOutCallbackHandler.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
answer_prefix_tokens: Token sequence that prefixes the anwer.
|
answer_prefix_tokens: Token sequence that prefixes the answer.
|
||||||
Default is ["Final", "Answer", ":"]
|
Default is ["Final", "Answer", ":"]
|
||||||
strip_tokens: Ignore white spaces and new lines when comparing
|
strip_tokens: Ignore white spaces and new lines when comparing
|
||||||
answer_prefix_tokens to last tokens? (to determine if answer has been
|
answer_prefix_tokens to last tokens? (to determine if answer has been
|
||||||
|
Loading…
Reference in New Issue
Block a user