Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-coupled Tools for Marketplace #17

Open
wants to merge 69 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
e82d3f3
adding google search toolkit
AdityaSharma13064 Sep 7, 2023
ceeb683
deleting google toolkit
AdityaSharma13064 Sep 7, 2023
61be8e7
adding google search toolkit
AdityaSharma13064 Sep 7, 2023
67d5aa0
adding requirements.txt
AdityaSharma13064 Sep 7, 2023
66d2ff3
remove extra file
AdityaSharma13064 Sep 7, 2023
3bc61e3
fixing
AdityaSharma13064 Sep 7, 2023
81868d8
adding config.yaml
AdityaSharma13064 Sep 7, 2023
016c0b2
adding apollo toolkit
AdityaSharma13064 Sep 11, 2023
af79d87
removing apollo
AdityaSharma13064 Sep 11, 2023
28d7ef5
adding apollo toolkit
AdityaSharma13064 Sep 11, 2023
64ecbe3
adding google serp search toolkit
AdityaSharma13064 Sep 11, 2023
248acef
adding github toolkit
AdityaSharma13064 Sep 12, 2023
e8e50e1
adding searx toolkit
AdityaSharma13064 Sep 13, 2023
ddd9619
adding slack toolkit
AdityaSharma13064 Sep 13, 2023
516e3b2
adding more tools
AdityaSharma13064 Sep 13, 2023
1aeb17b
adding test for google serp and google search toolkit
AdityaSharma13064 Sep 13, 2023
56e0182
adding unit tests
AdityaSharma13064 Sep 14, 2023
c879b68
adding slack unit test
AdityaSharma13064 Sep 14, 2023
66e2088
removing bugs in github file
Sep 15, 2023
990ae6c
removing bugs
Sep 15, 2023
69c77cc
addiing file path of jira toolkit
Sep 16, 2023
6caaa91
fix
Sep 16, 2023
e07bbc8
fixes
Sep 16, 2023
79bd347
fixing-2
Sep 16, 2023
e640d15
adding webscaper toolkit
Sep 17, 2023
5e959ab
adding helper for webscaper
Sep 18, 2023
92262e0
fixing
Sep 18, 2023
4e0a6bb
fixing jira
Sep 18, 2023
f42dd3a
fixing jira
Sep 18, 2023
7a5a48e
adding fixes
Sep 18, 2023
09d3c16
fixing webscaper
Sep 18, 2023
a72c28d
adding helper for toolkits
Sep 18, 2023
ea93559
adding assets for file toolkit
Sep 19, 2023
ed519ea
fixing base_file
Sep 20, 2023
23a8f23
adding apollo search kit
Sep 20, 2023
05517b8
adding resource helper
Sep 20, 2023
8dc3742
bug fix
Sep 20, 2023
2531e7a
bugfix
Sep 20, 2023
93ce083
bug fixing
Sep 20, 2023
69af4bd
adding bug fix
Sep 20, 2023
4407219
adding fix
Sep 20, 2023
8b7b09e
adding fix
Sep 20, 2023
918ddac
adding fix
Sep 20, 2023
b6576ad
adding fix
Sep 20, 2023
70d3424
adding fix
Sep 20, 2023
ee70ef2
adding fix
Sep 20, 2023
40a286a
adding fix
Sep 20, 2023
0e088f6
adding fix
Sep 20, 2023
8fbc95d
adding fix
Sep 20, 2023
f9bc3b1
adding fix
Sep 20, 2023
e64b1d1
adding apollo search kit
Sep 21, 2023
2b3464f
adding apollo search kit
Sep 21, 2023
5647733
adding assets
Sep 22, 2023
4632f6e
adding temp file
Sep 23, 2023
ff09432
adding email toolkit
Sep 25, 2023
0407d6a
adding logs
Sep 25, 2023
b1b320b
bug resolve
Sep 25, 2023
5bbe373
adding complete path
Sep 25, 2023
4b7b84e
adding changes
Sep 26, 2023
cf8c2c9
adding changes
Sep 26, 2023
c4625a1
adding instagram toolkit
Sep 26, 2023
2e9c560
adding changes
Sep 26, 2023
16056cd
adding more tools
Sep 28, 2023
7add4eb
adding requirements for instragram
Sep 28, 2023
090c6ca
removing basetool
Sep 28, 2023
b7c9d8d
adding coding toolkit changes
Sep 28, 2023
59b31ff
adding correct imports
Sep 28, 2023
2ca6334
adding final changes
Sep 28, 2023
e7b3c50
adding assets folder
Sep 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding more tools
  • Loading branch information
Aditya Sharma authored and Aditya Sharma committed Sep 28, 2023
commit 16056cde6b8238a07b70fcadb9e5e7e29d075cbb
29 changes: 29 additions & 0 deletions code/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<p align=center>
<a href="https://superagi.co"><img src=https://superagi.co/wp-content/uploads/2023/05/SuperAGI_icon.png></a>
</p>

# SuperAGI Coding Tool

The robust SuperAGI Coding Tool lets help with their coding tasks like writing, reviewing, refactoring code, fixing bugs, and understanding programming concepts.

## 💡 Features
1. **Write Code:** With SuperAGI's Coding Tool, writing new code is a streamlined and effortless process, making your programming tasks much simpler.

2. **Review Code:** SuperAGI's Coding Tool allows comprehensive code reviews, ensuring your code maintains quality standards and adheres to best practices.

3. **Refactor Code:** Refactoring your code is a breeze with SuperAGI's Coding Tool, allowing you to improve your code structure without changing its functionality.

4. **Debugging:** The Coding Tool is equipped to identify and fix bugs efficiently, ensuring your code performs as intended.

5. **Concept Explanation:** This feature provides clear explanations for various programming concepts, enhancing your understanding and making complex coding problems easier to solve.

## ⚙️ Installation

### 🛠 **Setting Up of SuperAGI**
Set up the SuperAGI by following the instructions given (https://github.com/TransformerOptimus/SuperAGI/blob/main/README.MD)

You'll be able to use the Coding Tool on the fly once you have setup SuperAGI.

## Running SuperAGI Coding Tool

You can simply ask your agent to read or go through your coding files in the Resource Manager, and it'll be able to do any coding feature as mentioned above.
Empty file added code/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions code/coding_toolkit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from abc import ABC
from typing import List

from superagi.tools.base_tool import BaseToolkit, BaseTool, ToolConfiguration
from superagi.tools.code.improve_code import ImproveCodeTool
from superagi.tools.code.write_code import CodingTool
from superagi.tools.code.write_spec import WriteSpecTool
from superagi.tools.code.write_test import WriteTestTool


class CodingToolkit(BaseToolkit, ABC):
name: str = "CodingToolkit"
description: str = "Coding Tool kit contains all tools related to coding tasks"

def get_tools(self) -> List[BaseTool]:
return [CodingTool(), WriteSpecTool(), WriteTestTool(), ImproveCodeTool()]

def get_env_keys(self) -> List[ToolConfiguration]:
return []
99 changes: 99 additions & 0 deletions code/improve_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import re
from typing import Type, Optional, List

from pydantic import BaseModel, Field

from superagi.agent.agent_prompt_builder import AgentPromptBuilder
from superagi.helper.prompt_reader import PromptReader
from superagi.helper.token_counter import TokenCounter
from superagi.lib.logger import logger
from superagi.llms.base_llm import BaseLlm
from superagi.resource_manager.file_manager import FileManager
from superagi.tools.base_tool import BaseTool
from superagi.tools.tool_response_query_manager import ToolResponseQueryManager


class ImproveCodeSchema(BaseModel):
pass


class ImproveCodeTool(BaseTool):
"""
Used to improve the already generated code by reading the code from the files

Attributes:
llm: LLM used for code generation.
name : The name of the tool.
description : The description of the tool.
resource_manager: Manages the file resources.
"""
llm: Optional[BaseLlm] = None
agent_id: int = None
name = "ImproveCodeTool"
description = (
"This tool improves the generated code."
)
args_schema: Type[ImproveCodeSchema] = ImproveCodeSchema
resource_manager: Optional[FileManager] = None
tool_response_manager: Optional[ToolResponseQueryManager] = None
goals: List[str] = []

class Config:
arbitrary_types_allowed = True

def _execute(self) -> str:
"""
Execute the improve code tool.

Returns:
Improved code or error message.
"""
# Get all file names that the CodingTool has written
file_names = self.resource_manager.get_files()
logger.info(file_names)
# Loop through each file
for file_name in file_names:
if '.txt' not in file_name and '.sh' not in file_name and '.json' not in file_name:
# Read the file content
content = self.resource_manager.read_file(file_name)

# Generate a prompt from improve_code.txt
prompt = PromptReader.read_tools_prompt(__file__, "improve_code.txt")

# Combine the hint from the file, goals, and content
prompt = prompt.replace("{goals}", AgentPromptBuilder.add_list_items_to_string(self.goals))
prompt = prompt.replace("{content}", content)

# Add the file content to the chat completion prompt
prompt = prompt + "\nOriginal Code:\n```\n" + content + "\n```"



# Use LLM to generate improved code
result = self.llm.chat_completion([{'role': 'system', 'content': prompt}])

# Extract the response first
response = result.get('response')
if not response:
logger.info("RESPONSE NOT AVAILABLE")

# Now extract the choices from response
choices = response.get('choices')
if not choices:
logger.info("CHOICES NOT AVAILABLE")

# Now you can safely extract the message content
improved_content = choices[0]["message"]["content"]
# improved_content = result["messages"][0]["content"]
parsed_content = re.findall("```(?:\w*\n)?(.*?)```", improved_content, re.DOTALL)
parsed_content_code = "\n".join(parsed_content)

# Rewrite the file with the improved content
save_result = self.resource_manager.write_file(file_name, parsed_content_code)

if save_result.startswith("Error"):
return save_result
else:
continue

return f"All codes improved and saved successfully in: " + " ".join(file_names)
11 changes: 11 additions & 0 deletions code/prompts/generate_logic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
You typically always place distinct classes in separate files.
Always create a run.sh file which act as the entrypoint of the program, create it intellligently after analyzing the file types
For Python, always generate a suitable requirements.txt file.
For NodeJS, consistently produce an appropriate package.json file.
Always include a brief comment that describes the purpose of the function definition.
Attempt to provide comments that explain complicated logic.
Consistently adhere to best practices for the specified languages, ensuring code is defined as a package or project.

Preferred Python toolbelt:
- pytest
- dataclasses
26 changes: 26 additions & 0 deletions code/prompts/improve_code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
You are a super smart developer. You have been tasked with fixing and filling the function and classes where only the description of code is written without the actual code . There might be placeholders in the code you have to fill in.
You provide fully functioning, well formatted code with few comments, that works and has no bugs.
If the code is already correct and doesn't need change, just return the same code
However, make sure that you only return the improved code, without any additional content.


Please structure the improved code as follows:

```
CODE
```

Please return the full new code in same format as the original code
Don't write any explanation or description in your response other than the actual code

Your high-level goal is:
{goals}

The content of the file you need to improve is:
{content}

Only return the code and not any other line

To start, first analyze the existing code. Check for any function with missing logic inside it and fill the function.
Make sure, that not a single function is empty or contains just comments, there should be function logic inside it
Return fully completed functions by filling the placeholders
32 changes: 32 additions & 0 deletions code/prompts/write_code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
You are a super smart developer who practices good Development for writing code according to a specification.
Please note that the code should be fully functional. There should be no placeholder in functions or classes in any file.

Your high-level goal is:
{goals}

Coding task description:
{code_description}

{spec}

You will get instructions for code to write.
You need to write a detailed answer. Make sure all parts of the architecture are turned into code.
Think carefully about each step and make good choices to get it right. First, list the main classes,
functions, methods you'll use and a quick comment on their purpose.

Then you will output the content of each file including ALL code.
Each file must strictly follow a markdown code block format, where the following tokens must be replaced such that
FILENAME is the lowercase file name including the file extension,
[LANG] is the markup code block language for the code's language, and [CODE] is the code:
FILENAME
```[LANG]
[CODE]
```

You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on.

Follow a language and framework appropriate best practice file naming convention.
Make sure that files contain all imports, types etc. Make sure that code in different files are compatible with each other.
Ensure to implement all code, if you are unsure, write a plausible implementation.
Include module dependency or package manager dependency definition file.
Before you finish, double check that all parts of the architecture is present in the files.
12 changes: 12 additions & 0 deletions code/prompts/write_spec.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
You are a super smart developer who has been asked to make a specification for a program.

Your high-level goal is:
{goals}

Please keep in mind the following when creating the specification:
1. Be super explicit about what the program should do, which features it should have, and give details about anything that might be unclear.
2. Lay out the names of the core classes, functions, methods that will be necessary, as well as a quick comment on their purpose.
3. List all non-standard dependencies that will have to be used.

Write a specification for the following task:
{task}
21 changes: 21 additions & 0 deletions code/prompts/write_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
You are a super smart developer who practices Test Driven Development for writing tests according to a specification.

Your high-level goal is:
{goals}

Test Description:
{test_description}

{spec}

Test should follow the following format:
FILENAME is the lowercase file name including the file extension,
[LANG] is the markup code block language for the code's language, and [UNIT_TEST_CODE] is the code:

FILENAME
```[LANG]
[UNIT_TEST_CODE]
```

The tests should be as simple as possible, but still cover all the functionality described in the specification.

Empty file added code/requirements.txt
Empty file.
112 changes: 112 additions & 0 deletions code/write_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import re
from typing import Type, Optional, List

from pydantic import BaseModel, Field

from superagi.agent.agent_prompt_builder import AgentPromptBuilder
from superagi.helper.prompt_reader import PromptReader
from superagi.helper.token_counter import TokenCounter
from superagi.lib.logger import logger
from superagi.llms.base_llm import BaseLlm
from superagi.resource_manager.file_manager import FileManager
from superagi.tools.base_tool import BaseTool
from superagi.tools.tool_response_query_manager import ToolResponseQueryManager
from superagi.models.agent import Agent

class CodingSchema(BaseModel):
code_description: str = Field(
...,
description="Description of the coding task",
)


class CodingTool(BaseTool):
"""
Used to generate code.

Attributes:
llm: LLM used for code generation.
name : The name of tool.
description : The description of tool.
args_schema : The args schema.
goals : The goals.
resource_manager: Manages the file resources
"""
llm: Optional[BaseLlm] = None
agent_id: int = None
name = "CodingTool"
description = (
"You will get instructions for code to write. You will write a very long answer. "
"Make sure that every detail of the architecture is, in the end, implemented as code. "
"Think step by step and reason yourself to the right decisions to make sure we get it right. "
"You will first lay out the names of the core classes, functions, methods that will be necessary, "
"as well as a quick comment on their purpose. Then you will output the content of each file including each function and class and ALL code."
)
args_schema: Type[CodingSchema] = CodingSchema
goals: List[str] = []
resource_manager: Optional[FileManager] = None
tool_response_manager: Optional[ToolResponseQueryManager] = None

class Config:
arbitrary_types_allowed = True

def _execute(self, code_description: str) -> str:
"""
Execute the write_code tool.

Args:
code_description : The coding task description.
code_file_name: The name of the file where the generated codes will be saved.

Returns:
Generated code with where the code is being saved or error message.
"""
prompt = PromptReader.read_tools_prompt(__file__, "write_code.txt") + "\nUseful to know:\n" + PromptReader.read_tools_prompt(__file__, "generate_logic.txt")
prompt = prompt.replace("{goals}", AgentPromptBuilder.add_list_items_to_string(self.goals))
prompt = prompt.replace("{code_description}", code_description)
spec_response = self.tool_response_manager.get_last_response("WriteSpecTool")
if spec_response != "":
prompt = prompt.replace("{spec}", "Use this specs for generating the code:\n" + spec_response)
logger.info(prompt)
messages = [{"role": "system", "content": prompt}]

organisation = Agent.find_org_by_agent_id(session=self.toolkit_config.session, agent_id=self.agent_id)
total_tokens = TokenCounter.count_message_tokens(messages, self.llm.get_model())
token_limit = TokenCounter(session=self.toolkit_config.session, organisation_id=organisation.id).token_limit(self.llm.get_model())

result = self.llm.chat_completion(messages, max_tokens=(token_limit - total_tokens - 100))

# Get all filenames and corresponding code blocks
regex = r"(\S+?)\n```\S*\n(.+?)```"
matches = re.finditer(regex, result["content"], re.DOTALL)

file_names = []
# Save each file

for match in matches:
# Get the filename
file_name = re.sub(r'[<>"|?*]', "", match.group(1))
if not file_name[0].isalnum():
file_name = file_name[1:-1]

# Get the code
code = match.group(2)

# Ensure file_name is not empty
if not file_name.strip():
continue

file_names.append(file_name)
save_result = self.resource_manager.write_file(file_name, code)
if save_result.startswith("Error"):
return save_result

# Get README contents and save
split_result = result["content"].split("```")
if split_result:
readme = split_result[0]
save_readme_result = self.resource_manager.write_file("README.md", readme)
if save_readme_result.startswith("Error"):
return save_readme_result

return result["content"] + "\n Codes generated and saved successfully in " + ", ".join(file_names)
Loading