From f554b299aca700389fb96087db91cb6da0b76ffe Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 2 Dec 2023 19:54:40 -0800 Subject: [PATCH] [FEAT][Agent update] [DEMO][Grupa Software GPT] --- playground/demos/grupa/app.py | 78 +++++++++++++++++++++ playground/demos/grupa/run__software_gpt.sh | 13 ++++ playground/demos/grupa/test_bash.bash | 13 ++++ pyproject.toml | 2 +- sequential_workflow_example.py | 7 +- swarms/structs/agent.py | 32 ++++++++- swarms/utils/parse_code.py | 34 ++++++--- 7 files changed, 166 insertions(+), 13 deletions(-) create mode 100644 playground/demos/grupa/app.py create mode 100755 playground/demos/grupa/run__software_gpt.sh create mode 100755 playground/demos/grupa/test_bash.bash diff --git a/playground/demos/grupa/app.py b/playground/demos/grupa/app.py new file mode 100644 index 00000000..5f09999b --- /dev/null +++ b/playground/demos/grupa/app.py @@ -0,0 +1,78 @@ +import os + +from dotenv import load_dotenv +from fastapi import FastAPI +from pydantic import BaseModel + +from swarms.models import OpenAIChat +from swarms.prompts.code_interpreter import CODE_INTERPRETER +from swarms.structs import Agent + + +class AgentInput(BaseModel): + feature: str + codebase: str + + +app = FastAPI() + +load_dotenv() + +# Load the environment variables +api_key = os.getenv("OPENAI_API_KEY") + +# Initialize the language agent +llm = OpenAIChat( + model_name="gpt-4", + openai_api_key=api_key, + temperature=0.5, + max_tokens=2000, +) + +# Product Manager Agent init +product_manager_agent = Agent( + llm=llm, max_loops=1, sop=CODE_INTERPRETER, autosave=True +) + +# Initialize the agent with the language agent +feature_implementer_frontend = Agent( + llm=llm, max_loops=1, sop=CODE_INTERPRETER, autosave=True +) + +# Create another agent for a different task +feature_implementer_backend = Agent( + llm=llm, max_loops=1, sop=CODE_INTERPRETER, autosave=True +) + + +# ##################### FastAPI ##################### + + +def feature_codebase_product_agentprompt( + feature: str, codebase: str +) -> str: + prompt = ( + "Create an algorithmic pseudocode for an all-new feature:" + f" {feature} based on this codebase: {codebase}" + ) + return prompt + + +# @app.post("/agent/") +# async def run_feature_implementer_frontend(item: AgentInput): +# agent1_out = feature_implementer_frontend.run( +# f"Create the backend code for {item.feature} in markdown" +# " based off of this algorithmic pseudocode:" +# f" {product_manager_agent.run(feature_codebase_product_agentprompt(item.feature, item.codebase))} write" +# f" the logic based on the following codebase: {item.codebase}" +# ) +# return {"output": agent1_out} + +def software_gpt(feature: str, codebase: str) -> str: + agent1_out = feature_implementer_frontend.run( + f"Create the backend code for {feature} in markdown" + " based off of this algorithmic pseudocode:" + f" {product_manager_agent.run(feature_codebase_product_agentprompt(feature, codebase))} write" + f" the logic based on the following codebase: {codebase}" + ) + print(agent1_out) \ No newline at end of file diff --git a/playground/demos/grupa/run__software_gpt.sh b/playground/demos/grupa/run__software_gpt.sh new file mode 100755 index 00000000..7ae5b190 --- /dev/null +++ b/playground/demos/grupa/run__software_gpt.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Define the base URL +base_url="http://localhost:8000" + +# Define the JSON payload +payload='{"feature": "login system", "codebase": "existing codebase here"}' + +# Send POST request +echo "Sending request to /agent/ endpoint..." +response=$(curl -s -X POST "$base_url/agent/" -H "Content-Type: application/json" -d "$payload") + +echo "Response: $response" \ No newline at end of file diff --git a/playground/demos/grupa/test_bash.bash b/playground/demos/grupa/test_bash.bash new file mode 100755 index 00000000..7ae5b190 --- /dev/null +++ b/playground/demos/grupa/test_bash.bash @@ -0,0 +1,13 @@ +#!/bin/bash + +# Define the base URL +base_url="http://localhost:8000" + +# Define the JSON payload +payload='{"feature": "login system", "codebase": "existing codebase here"}' + +# Send POST request +echo "Sending request to /agent/ endpoint..." +response=$(curl -s -X POST "$base_url/agent/" -H "Content-Type: application/json" -d "$payload") + +echo "Response: $response" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 61fa2834..b630536c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "2.5.4" +version = "2.5.6" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/sequential_workflow_example.py b/sequential_workflow_example.py index a17a6ad6..1742f49c 100644 --- a/sequential_workflow_example.py +++ b/sequential_workflow_example.py @@ -14,12 +14,15 @@ api_key = os.getenv("OPENAI_API_KEY") llm = OpenAIChat( openai_api_key=api_key, temperature=0.5, - max_tokens=3000, + max_tokens=2000, ) # Initialize the agent with the language agent -agent1 = Agent(llm=llm, max_loops=1) +agent1 = Agent( + llm=llm, + max_loops=1, +) # Create another agent for a different task agent2 = Agent(llm=llm, max_loops=1) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index b7339878..30faef6e 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -252,6 +252,14 @@ class Agent: if preset_stopping_token: self.stopping_token = "" + # If memory then add the json to the memory vector database + if memory: + # Add all of the state to the memory + self.add_message_to_memory_db( + {"message": self.state_to_str()}, + {"agent_id": self.id}, + ) + def provide_feedback(self, feedback: str) -> None: """Allow users to provide feedback on the responses.""" self.feedback.append(feedback) @@ -1125,7 +1133,7 @@ class Agent: "agent_description": self.agent_description, "system_prompt": self.system_prompt, "sop": self.sop, - "memory": self.short_memory, + "short_memory": self.short_memory, "loop_interval": self.loop_interval, "retry_attempts": self.retry_attempts, "retry_interval": self.retry_interval, @@ -1143,6 +1151,28 @@ class Agent: saved = colored(f"Saved agent state to: {file_path}", "green") print(saved) + def state_to_str(self): + """Transform the JSON into a string""" + state = { + "agent_id": str(self.id), + "agent_name": self.agent_name, + "agent_description": self.agent_description, + "system_prompt": self.system_prompt, + "sop": self.sop, + "short_memory": self.short_memory, + "loop_interval": self.loop_interval, + "retry_attempts": self.retry_attempts, + "retry_interval": self.retry_interval, + "interactive": self.interactive, + "dashboard": self.dashboard, + "dynamic_temperature": self.dynamic_temperature_enabled, + "autosave": self.autosave, + "saved_state_path": self.saved_state_path, + "max_loops": self.max_loops, + } + out = str(state) + return out + def load_state(self, file_path: str): """ Loads the state of the agent from a json file and restores the configuration and memory. diff --git a/swarms/utils/parse_code.py b/swarms/utils/parse_code.py index 747bd0b6..2e0fa438 100644 --- a/swarms/utils/parse_code.py +++ b/swarms/utils/parse_code.py @@ -1,15 +1,31 @@ import re +# def extract_code_in_backticks_in_string(s: str) -> str: +# """ +# Extracts code blocks from a markdown string. -def extract_code_in_backticks_in_string(message: str) -> str: +# Args: +# s (str): The markdown string to extract code from. + +# Returns: +# list: A list of tuples. Each tuple contains the language of the code block (if specified) and the code itself. +# """ +# pattern = r"```([\w\+\#\-\.\s]*)\n(.*?)```" +# matches = re.findall(pattern, s, re.DOTALL) +# out = [(match[0], match[1].strip()) for match in matches] +# print(out) + + +def extract_code_in_backticks_in_string(s: str) -> str: """ - To extract code from a string in markdown and return a string + Extracts code blocks from a markdown string. + + Args: + s (str): The markdown string to extract code from. + Returns: + str: A string containing all the code blocks. """ - pattern = ( # Non-greedy match between six backticks - r"`` ``(.*?)`` " - ) - match = re.search( - pattern, message, re.DOTALL - ) # re.DOTALL to match newline chars - return match.group(1).strip() if match else None + pattern = r"```([\w\+\#\-\.\s]*)(.*?)```" + matches = re.findall(pattern, s, re.DOTALL) + return "\n".join(match[1].strip() for match in matches)