Kotta commited on
Commit ·
5445763
1
Parent(s): b038ad0
feature(#23): Implement autogpt with langchain
Browse files
Brain/src/rising_plugin/llm/autogpt_llm.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""autogpt plugin with langchain"""
|
| 2 |
+
|
| 3 |
+
from langchain.experimental import AutoGPT
|
| 4 |
+
from langchain.chat_models import ChatOpenAI
|
| 5 |
+
from langchain.experimental.autonomous_agents.autogpt.prompt_generator import (
|
| 6 |
+
FINISH_NAME,
|
| 7 |
+
)
|
| 8 |
+
from langchain.utilities import SerpAPIWrapper
|
| 9 |
+
from langchain.agents import Tool
|
| 10 |
+
from langchain.tools.file_management.write import WriteFileTool
|
| 11 |
+
from langchain.tools.file_management.read import ReadFileTool
|
| 12 |
+
from langchain.vectorstores import FAISS
|
| 13 |
+
from langchain.docstore import InMemoryDocstore
|
| 14 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 15 |
+
from langchain.schema import (
|
| 16 |
+
AIMessage,
|
| 17 |
+
Document,
|
| 18 |
+
HumanMessage,
|
| 19 |
+
SystemMessage,
|
| 20 |
+
)
|
| 21 |
+
from typing import List
|
| 22 |
+
|
| 23 |
+
from marshmallow import ValidationError
|
| 24 |
+
|
| 25 |
+
import faiss
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class AutoGPTLLM:
|
| 29 |
+
"""autogpt run method to get the expected result"""
|
| 30 |
+
|
| 31 |
+
def run(self, agent: AutoGPT, goals: List[str]) -> str:
|
| 32 |
+
user_input = (
|
| 33 |
+
"Determine which next command to use, "
|
| 34 |
+
"and respond using the format specified above:"
|
| 35 |
+
)
|
| 36 |
+
# Interaction Loop
|
| 37 |
+
loop_count = 0
|
| 38 |
+
while True:
|
| 39 |
+
# Discontinue if continuous limit is reached
|
| 40 |
+
loop_count += 1
|
| 41 |
+
|
| 42 |
+
# Send message to AI, get response
|
| 43 |
+
assistant_reply = agent.chain.run(
|
| 44 |
+
goals=goals,
|
| 45 |
+
messages=agent.full_message_history,
|
| 46 |
+
memory=agent.memory,
|
| 47 |
+
user_input=user_input,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Print Assistant thoughts
|
| 51 |
+
print(assistant_reply)
|
| 52 |
+
agent.full_message_history.append(HumanMessage(content=user_input))
|
| 53 |
+
agent.full_message_history.append(AIMessage(content=assistant_reply))
|
| 54 |
+
|
| 55 |
+
# Get command name and arguments
|
| 56 |
+
action = agent.output_parser.parse(assistant_reply)
|
| 57 |
+
tools = {t.name: t for t in agent.tools}
|
| 58 |
+
if action.name == FINISH_NAME:
|
| 59 |
+
return action.args["response"]
|
| 60 |
+
if action.name in tools:
|
| 61 |
+
tool = tools[action.name]
|
| 62 |
+
try:
|
| 63 |
+
observation = tool.run(action.args)
|
| 64 |
+
except ValidationError as e:
|
| 65 |
+
observation = (
|
| 66 |
+
f"Validation Error in args: {str(e)}, args: {action.args}"
|
| 67 |
+
)
|
| 68 |
+
except Exception as e:
|
| 69 |
+
observation = (
|
| 70 |
+
f"Error: {str(e)}, {type(e).__name__}, args: {action.args}"
|
| 71 |
+
)
|
| 72 |
+
result = f"Command {tool.name} returned: {observation}"
|
| 73 |
+
elif action.name == "ERROR":
|
| 74 |
+
result = f"Error: {action.args}. "
|
| 75 |
+
else:
|
| 76 |
+
result = (
|
| 77 |
+
f"Unknown command '{action.name}'. "
|
| 78 |
+
f"Please refer to the 'COMMANDS' list for available "
|
| 79 |
+
f"commands and only respond in the specified JSON format."
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
memory_to_add = (
|
| 83 |
+
f"Assistant Reply: {assistant_reply} " f"\nResult: {result} "
|
| 84 |
+
)
|
| 85 |
+
if agent.feedback_tool is not None:
|
| 86 |
+
feedback = f"\n{agent.feedback_tool.run('Input: ')}"
|
| 87 |
+
if feedback in {"q", "stop"}:
|
| 88 |
+
print("EXITING")
|
| 89 |
+
return "EXITING"
|
| 90 |
+
memory_to_add += feedback
|
| 91 |
+
|
| 92 |
+
agent.memory.add_documents([Document(page_content=memory_to_add)])
|
| 93 |
+
agent.full_message_history.append(SystemMessage(content=result))
|
| 94 |
+
|
| 95 |
+
"""function to manage auto-task achievement
|
| 96 |
+
ex: query = write a weather report for SF today
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
def ask_task(self, query: str):
|
| 100 |
+
search = SerpAPIWrapper()
|
| 101 |
+
tools = [
|
| 102 |
+
Tool(
|
| 103 |
+
name="search",
|
| 104 |
+
func=search.run,
|
| 105 |
+
description="useful for when you need to answer questions about current events. You should ask targeted questions",
|
| 106 |
+
),
|
| 107 |
+
WriteFileTool(),
|
| 108 |
+
ReadFileTool(),
|
| 109 |
+
]
|
| 110 |
+
|
| 111 |
+
# Define your embedding model
|
| 112 |
+
embeddings_model = OpenAIEmbeddings()
|
| 113 |
+
# Initialize the vectorstore as empty
|
| 114 |
+
|
| 115 |
+
embedding_size = 1536
|
| 116 |
+
index = faiss.IndexFlatL2(embedding_size)
|
| 117 |
+
vectorstore = FAISS(
|
| 118 |
+
embeddings_model.embed_query, index, InMemoryDocstore({}), {}
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
agent = AutoGPT.from_llm_and_tools(
|
| 122 |
+
ai_name="Tom",
|
| 123 |
+
ai_role="Assistant",
|
| 124 |
+
tools=tools,
|
| 125 |
+
llm=ChatOpenAI(temperature=0),
|
| 126 |
+
memory=vectorstore.as_retriever(),
|
| 127 |
+
)
|
| 128 |
+
# Set verbose to be true
|
| 129 |
+
agent.chain.verbose = True
|
| 130 |
+
self.run(agent, [query])
|