[FEAT][Agent update] [DEMO][Grupa Software GPT]

pull/245/head
Kye 1 year ago
parent d797909643
commit f554b299ac

@ -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)

@ -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"

@ -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"

@ -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 <kye@apac.ai>"]

@ -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)

@ -252,6 +252,14 @@ class Agent:
if preset_stopping_token:
self.stopping_token = "<DONE>"
# 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.

@ -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)

Loading…
Cancel
Save