mirror of
https://github.com/hwchase17/langchain
synced 2024-11-11 19:11:02 +00:00
a0c2281540
```python """python scripts/update_mypy_ruff.py""" import glob import tomllib from pathlib import Path import toml import subprocess import re ROOT_DIR = Path(__file__).parents[1] def main(): for path in glob.glob(str(ROOT_DIR / "libs/**/pyproject.toml"), recursive=True): print(path) with open(path, "rb") as f: pyproject = tomllib.load(f) try: pyproject["tool"]["poetry"]["group"]["typing"]["dependencies"]["mypy"] = ( "^1.10" ) pyproject["tool"]["poetry"]["group"]["lint"]["dependencies"]["ruff"] = ( "^0.5" ) except KeyError: continue with open(path, "w") as f: toml.dump(pyproject, f) cwd = "/".join(path.split("/")[:-1]) completed = subprocess.run( "poetry lock --no-update; poetry install --with typing; poetry run mypy . --no-color", cwd=cwd, shell=True, capture_output=True, text=True, ) logs = completed.stdout.split("\n") to_ignore = {} for l in logs: if re.match("^(.*)\:(\d+)\: error:.*\[(.*)\]", l): path, line_no, error_type = re.match( "^(.*)\:(\d+)\: error:.*\[(.*)\]", l ).groups() if (path, line_no) in to_ignore: to_ignore[(path, line_no)].append(error_type) else: to_ignore[(path, line_no)] = [error_type] print(len(to_ignore)) for (error_path, line_no), error_types in to_ignore.items(): all_errors = ", ".join(error_types) full_path = f"{cwd}/{error_path}" try: with open(full_path, "r") as f: file_lines = f.readlines() except FileNotFoundError: continue file_lines[int(line_no) - 1] = ( file_lines[int(line_no) - 1][:-1] + f" # type: ignore[{all_errors}]\n" ) with open(full_path, "w") as f: f.write("".join(file_lines)) subprocess.run( "poetry run ruff format .; poetry run ruff --select I --fix .", cwd=cwd, shell=True, capture_output=True, text=True, ) if __name__ == "__main__": main() ```
144 lines
5.2 KiB
Python
144 lines
5.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import List, Optional
|
|
|
|
from langchain.chains.llm import LLMChain
|
|
from langchain.memory import ChatMessageHistory
|
|
from langchain.schema import (
|
|
BaseChatMessageHistory,
|
|
Document,
|
|
)
|
|
from langchain.tools.base import BaseTool
|
|
from langchain_community.tools.human.tool import HumanInputRun
|
|
from langchain_core.language_models import BaseChatModel
|
|
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
|
from langchain_core.vectorstores import VectorStoreRetriever
|
|
|
|
from langchain_experimental.autonomous_agents.autogpt.output_parser import (
|
|
AutoGPTOutputParser,
|
|
BaseAutoGPTOutputParser,
|
|
)
|
|
from langchain_experimental.autonomous_agents.autogpt.prompt import AutoGPTPrompt
|
|
from langchain_experimental.autonomous_agents.autogpt.prompt_generator import (
|
|
FINISH_NAME,
|
|
)
|
|
from langchain_experimental.pydantic_v1 import ValidationError
|
|
|
|
|
|
class AutoGPT:
|
|
"""Agent for interacting with AutoGPT."""
|
|
|
|
def __init__(
|
|
self,
|
|
ai_name: str,
|
|
memory: VectorStoreRetriever,
|
|
chain: LLMChain,
|
|
output_parser: BaseAutoGPTOutputParser,
|
|
tools: List[BaseTool],
|
|
feedback_tool: Optional[HumanInputRun] = None,
|
|
chat_history_memory: Optional[BaseChatMessageHistory] = None,
|
|
):
|
|
self.ai_name = ai_name
|
|
self.memory = memory
|
|
self.next_action_count = 0
|
|
self.chain = chain
|
|
self.output_parser = output_parser
|
|
self.tools = tools
|
|
self.feedback_tool = feedback_tool
|
|
self.chat_history_memory = chat_history_memory or ChatMessageHistory()
|
|
|
|
@classmethod
|
|
def from_llm_and_tools(
|
|
cls,
|
|
ai_name: str,
|
|
ai_role: str,
|
|
memory: VectorStoreRetriever,
|
|
tools: List[BaseTool],
|
|
llm: BaseChatModel,
|
|
human_in_the_loop: bool = False,
|
|
output_parser: Optional[BaseAutoGPTOutputParser] = None,
|
|
chat_history_memory: Optional[BaseChatMessageHistory] = None,
|
|
) -> AutoGPT:
|
|
prompt = AutoGPTPrompt( # type: ignore[call-arg, call-arg, call-arg, call-arg]
|
|
ai_name=ai_name,
|
|
ai_role=ai_role,
|
|
tools=tools,
|
|
input_variables=["memory", "messages", "goals", "user_input"],
|
|
token_counter=llm.get_num_tokens,
|
|
)
|
|
human_feedback_tool = HumanInputRun() if human_in_the_loop else None
|
|
chain = LLMChain(llm=llm, prompt=prompt)
|
|
return cls(
|
|
ai_name,
|
|
memory,
|
|
chain,
|
|
output_parser or AutoGPTOutputParser(),
|
|
tools,
|
|
feedback_tool=human_feedback_tool,
|
|
chat_history_memory=chat_history_memory,
|
|
)
|
|
|
|
def run(self, goals: List[str]) -> str:
|
|
user_input = (
|
|
"Determine which next command to use, "
|
|
"and respond using the format specified above:"
|
|
)
|
|
# Interaction Loop
|
|
loop_count = 0
|
|
while True:
|
|
# Discontinue if continuous limit is reached
|
|
loop_count += 1
|
|
|
|
# Send message to AI, get response
|
|
assistant_reply = self.chain.run(
|
|
goals=goals,
|
|
messages=self.chat_history_memory.messages,
|
|
memory=self.memory,
|
|
user_input=user_input,
|
|
)
|
|
|
|
# Print Assistant thoughts
|
|
print(assistant_reply) # noqa: T201
|
|
self.chat_history_memory.add_message(HumanMessage(content=user_input))
|
|
self.chat_history_memory.add_message(AIMessage(content=assistant_reply))
|
|
|
|
# Get command name and arguments
|
|
action = self.output_parser.parse(assistant_reply)
|
|
tools = {t.name: t for t in self.tools}
|
|
if action.name == FINISH_NAME:
|
|
return action.args["response"]
|
|
if action.name in tools:
|
|
tool = tools[action.name]
|
|
try:
|
|
observation = tool.run(action.args)
|
|
except ValidationError as e:
|
|
observation = (
|
|
f"Validation Error in args: {str(e)}, args: {action.args}"
|
|
)
|
|
except Exception as e:
|
|
observation = (
|
|
f"Error: {str(e)}, {type(e).__name__}, args: {action.args}"
|
|
)
|
|
result = f"Command {tool.name} returned: {observation}"
|
|
elif action.name == "ERROR":
|
|
result = f"Error: {action.args}. "
|
|
else:
|
|
result = (
|
|
f"Unknown command '{action.name}'. "
|
|
f"Please refer to the 'COMMANDS' list for available "
|
|
f"commands and only respond in the specified JSON format."
|
|
)
|
|
|
|
memory_to_add = (
|
|
f"Assistant Reply: {assistant_reply} " f"\nResult: {result} "
|
|
)
|
|
if self.feedback_tool is not None:
|
|
feedback = f"{self.feedback_tool.run('Input: ')}"
|
|
if feedback in {"q", "stop"}:
|
|
print("EXITING") # noqa: T201
|
|
return "EXITING"
|
|
memory_to_add += f"\n{feedback}"
|
|
|
|
self.memory.add_documents([Document(page_content=memory_to_add)])
|
|
self.chat_history_memory.add_message(SystemMessage(content=result))
|