2023-04-19 04:41:03 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import List, Optional
|
|
|
|
|
|
|
|
from langchain.chains.llm import LLMChain
|
|
|
|
from langchain.chat_models.base import BaseChatModel
|
2023-06-16 00:49:03 +00:00
|
|
|
from langchain.memory import ChatMessageHistory
|
2023-04-19 04:41:03 +00:00
|
|
|
from langchain.schema import (
|
2023-06-16 00:49:03 +00:00
|
|
|
BaseChatMessageHistory,
|
2023-04-19 04:41:03 +00:00
|
|
|
Document,
|
|
|
|
)
|
2023-07-01 17:39:19 +00:00
|
|
|
from langchain.schema.messages import AIMessage, HumanMessage, SystemMessage
|
2023-09-25 19:44:23 +00:00
|
|
|
from langchain.schema.vectorstore import VectorStoreRetriever
|
2023-04-19 04:41:03 +00:00
|
|
|
from langchain.tools.base import BaseTool
|
|
|
|
from langchain.tools.human.tool import HumanInputRun
|
2023-07-22 01:44:32 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
Use a submodule for pydantic v1 compat (#9371)
<!-- Thank you for contributing to LangChain!
Replace this entire comment with:
- Description: a description of the change,
- Issue: the issue # it fixes (if applicable),
- Dependencies: any dependencies required for this change,
- Tag maintainer: for a quicker response, tag the relevant maintainer
(see below),
- Twitter handle: we announce bigger features on Twitter. If your PR
gets announced and you'd like a mention, we'll gladly shout you out!
Please make sure your PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` to check this
locally.
See contribution guidelines for more information on how to write/run
tests, lint, etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. These live is docs/extras
directory.
If no one reviews your PR within a few days, please @-mention one of
@baskaryan, @eyurtsev, @hwchase17, @rlancemartin.
-->
2023-08-17 15:35:49 +00:00
|
|
|
from langchain_experimental.pydantic_v1 import ValidationError
|
2023-04-19 04:41:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AutoGPT:
|
|
|
|
"""Agent class for interacting with Auto-GPT."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
ai_name: str,
|
|
|
|
memory: VectorStoreRetriever,
|
|
|
|
chain: LLMChain,
|
|
|
|
output_parser: BaseAutoGPTOutputParser,
|
|
|
|
tools: List[BaseTool],
|
|
|
|
feedback_tool: Optional[HumanInputRun] = None,
|
2023-06-16 00:49:03 +00:00
|
|
|
chat_history_memory: Optional[BaseChatMessageHistory] = None,
|
2023-04-19 04:41:03 +00:00
|
|
|
):
|
|
|
|
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
|
2023-06-16 00:49:03 +00:00
|
|
|
self.chat_history_memory = chat_history_memory or ChatMessageHistory()
|
2023-04-19 04:41:03 +00:00
|
|
|
|
|
|
|
@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,
|
2023-06-16 00:49:03 +00:00
|
|
|
chat_history_memory: Optional[BaseChatMessageHistory] = None,
|
2023-04-19 04:41:03 +00:00
|
|
|
) -> AutoGPT:
|
|
|
|
prompt = AutoGPTPrompt(
|
|
|
|
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,
|
2023-06-16 00:49:03 +00:00
|
|
|
chat_history_memory=chat_history_memory,
|
2023-04-19 04:41:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2023-06-16 00:49:03 +00:00
|
|
|
messages=self.chat_history_memory.messages,
|
2023-04-19 04:41:03 +00:00
|
|
|
memory=self.memory,
|
|
|
|
user_input=user_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Print Assistant thoughts
|
|
|
|
print(assistant_reply)
|
2023-06-16 00:49:03 +00:00
|
|
|
self.chat_history_memory.add_message(HumanMessage(content=user_input))
|
|
|
|
self.chat_history_memory.add_message(AIMessage(content=assistant_reply))
|
2023-04-19 04:41:03 +00:00
|
|
|
|
|
|
|
# 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:
|
2023-04-24 03:02:37 +00:00
|
|
|
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}"
|
|
|
|
)
|
2023-04-19 04:41:03 +00:00
|
|
|
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"\n{self.feedback_tool.run('Input: ')}"
|
|
|
|
if feedback in {"q", "stop"}:
|
|
|
|
print("EXITING")
|
|
|
|
return "EXITING"
|
|
|
|
memory_to_add += feedback
|
|
|
|
|
|
|
|
self.memory.add_documents([Document(page_content=memory_to_add)])
|
2023-06-16 00:49:03 +00:00
|
|
|
self.chat_history_memory.add_message(SystemMessage(content=result))
|