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:
Nayjest 2023-07-07 10:40:49 +03:00 committed by GitHub
parent 87f75cb322
commit 5809c3d29d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 24 additions and 31 deletions

View File

@ -1,5 +1,5 @@
"""Toolkits for agents."""
from abc import abstractmethod
from abc import ABC, abstractmethod
from typing import List
from pydantic import BaseModel
@ -7,8 +7,8 @@ from pydantic import BaseModel
from langchain.tools import BaseTool
class BaseToolkit(BaseModel):
"""Class responsible for defining a collection of related tools."""
class BaseToolkit(BaseModel, ABC):
"""Class representing a collection of related tools."""
@abstractmethod
def get_tools(self) -> List[BaseTool]:

View File

@ -1,4 +1,4 @@
"""Agent for working with csvs."""
"""Agent for working with csv files."""
from typing import Any, List, Optional, Union
from langchain.agents.agent import AgentExecutor

View File

@ -1 +1 @@
"""Gmail toolkit."""
"""Office365 toolkit."""

View File

@ -39,7 +39,7 @@ class RequestsToolkit(BaseToolkit):
class OpenAPIToolkit(BaseToolkit):
"""Toolkit for interacting with a OpenAPI api."""
"""Toolkit for interacting with an OpenAPI API."""
json_agent: AgentExecutor
requests_wrapper: TextRequestsWrapper

View File

@ -17,7 +17,7 @@ from langchain.utilities.powerbi import PowerBIDataset
def create_pbi_agent(
llm: BaseLanguageModel,
toolkit: Optional[PowerBIToolkit],
toolkit: Optional[PowerBIToolkit] = None,
powerbi: Optional[PowerBIDataset] = None,
callback_manager: Optional[BaseCallbackManager] = None,
prefix: str = POWERBI_PREFIX,

View File

@ -18,7 +18,7 @@ from langchain.utilities.powerbi import PowerBIDataset
def create_pbi_chat_agent(
llm: BaseChatModel,
toolkit: Optional[PowerBIToolkit],
toolkit: Optional[PowerBIToolkit] = None,
powerbi: Optional[PowerBIDataset] = None,
callback_manager: Optional[BaseCallbackManager] = None,
output_parser: Optional[AgentOutputParser] = None,
@ -32,9 +32,9 @@ def create_pbi_chat_agent(
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any],
) -> 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 powerbi is None:

View File

@ -30,9 +30,8 @@ class ChatOutputParser(AgentOutputParser):
except Exception:
if not includes_answer:
raise OutputParserException(f"Could not parse LLM output: {text}")
return AgentFinish(
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
)
output = text.split(FINAL_ANSWER_ACTION)[-1].strip()
return AgentFinish({"output": output}, text)
@property
def _type(self) -> str:

View File

@ -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 pydantic import Field
@ -59,7 +59,7 @@ class SelfAskWithSearchAgent(Agent):
class SelfAskWithSearchChain(AgentExecutor):
"""Chain that does self ask with search.
"""Chain that does self-ask with search.
Example:
.. code-block:: python

View File

@ -39,7 +39,7 @@ class FileCallbackHandler(BaseCallbackHandler):
self, action: AgentAction, color: Optional[str] = None, **kwargs: Any
) -> Any:
"""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(
self,
@ -52,24 +52,18 @@ class FileCallbackHandler(BaseCallbackHandler):
"""If not the final action, print out observation."""
if observation_prefix is not None:
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:
print_text(f"\n{llm_prefix}", file=self.file)
def on_text(
self,
text: str,
color: Optional[str] = None,
end: str = "",
**kwargs: Any,
self, text: str, color: Optional[str] = None, end: str = "", **kwargs: Any
) -> None:
"""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(
self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any
) -> None:
"""Run on agent end."""
print_text(
finish.log, color=color if self.color else color, end="\n", file=self.file
)
print_text(finish.log, color=color or self.color, end="\n", file=self.file)

View File

@ -63,7 +63,7 @@ class StdOutCallbackHandler(BaseCallbackHandler):
self, action: AgentAction, color: Optional[str] = None, **kwargs: Any
) -> Any:
"""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(
self,
@ -76,7 +76,7 @@ class StdOutCallbackHandler(BaseCallbackHandler):
"""If not the final action, print out observation."""
if observation_prefix is not None:
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:
print_text(f"\n{llm_prefix}")
@ -94,10 +94,10 @@ class StdOutCallbackHandler(BaseCallbackHandler):
**kwargs: Any,
) -> None:
"""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(
self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any
) -> None:
"""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")

View File

@ -37,7 +37,7 @@ class FinalStreamingStdOutCallbackHandler(StreamingStdOutCallbackHandler):
"""Instantiate FinalStreamingStdOutCallbackHandler.
Args:
answer_prefix_tokens: Token sequence that prefixes the anwer.
answer_prefix_tokens: Token sequence that prefixes the answer.
Default is ["Final", "Answer", ":"]
strip_tokens: Ignore white spaces and new lines when comparing
answer_prefix_tokens to last tokens? (to determine if answer has been