An implementation of GPT-4 that recognizes which commands it must run to fulfill an instruction, using a graph. Create new commands easily by describing them using natural language and coding the functions corresponding to the commands.
Project description
CommandsGPT
An implementation of GPT-4 to recognize instructions. It recognizes which commands it must run to fulfill the user's instruction, using a graph where each node is a command and the data generated by each command can be passed to other commands.
Create new commands easily by describing them using natural language and coding the functions corresponding to the commands.
Installation
- Install the
commandsgpt
module.
pip install commandsgpt
If you're using a virtual environment:
pipenv install commandsgpt
- You also have to install the OpenAI package:
pip install openai
or
pipenv install openai
- Set an environment variable OPENAI_API_KEY to store your OpenAI key.
Usage
Create a commands
dictionary that will store the commands described in natural language.
commands = {
"REQUEST_USER_INPUT": {
"description": "Asks the user to input data through the interface.",
"arguments": {
"message": {"description": "Message displayed to the user related to the data that will be requested (example: 'Enter your age').", "type": "string"},
},
"generates_data": {
"input": {"description": "Data entered by the user", "type": "string"},
},
},
...
}
Now, create the functions that will be called when the commands are executed.
- The name of the function is irrelevant.
- The first parameter must be the Config object; the second one must be the Graph object. Normally, you won't work directly with these objects, so you don't have to worry about them. Just use them as the first two parameters.
- The following parameters must match those described in the commands dictionary (name and data type).
- The return value must be a dictionary. Its keys, values and data types must match the return values described in the commands dictionary.
Example of a command function
from commands_gpt.config import Config
from commands_gpt.commands.graphs import Graph
def request_user_input_command(config: Config, graph: Graph, message: str) -> dict[str, Any]:
input_ = input(f"{message}\n*: ")
results = {
"input": input_,
}
return results
Create a command_name_to_func
dictionary that will take the name of a command and return the corresponding function.
Example of command_name_to_func dictionary
command_name_to_func = {
"REQUEST_USER_INPUT": request_user_input_command,
...
}
Add the essential commands to your commands dictionaries.
- These are the default commands that implement core logic to the model's thinking, like an IF command.
- If you already defined your own core logic commands (IF command, THINK command, CALCULATE command, etc.), then you are free not to use them.
from commands_gpt.commands.commands_funcs import add_essential_commands
add_essential_commands(commands, command_name_to_func)
Your config
object:
# keyword arguments are optional
config = Config("gpt-4-0314", verbosity=1, explain_graph=True, save_graph_as_file=False)
Create an instruction:
instruction = input("Enter your instruction: ")
Create a recognizer:
recognizer = ComplexRecognizer(config, commands, command_name_to_func)
Pass your instruction to the recognizer model:
commands_data_str = recognizer.recognize(instruction)
Create the graph of commands and execute it:
graph = Graph(recognizer, commands_data_str)
graph.execute_commands(config)
Example
Create two files: custom_commands.py
and main.py
.
custom_commands.py
from typing import Any
from pathlib import Path
from commands_gpt.config import Config
from commands_gpt.commands.graphs import Graph
commands = {
"WRITE_TO_USER": {
"description": "Writes something to the interface to communicate with the user.",
"arguments": {
"content": {"description": "Content to write.", "type": "string"},
},
"generates_data": {},
},
"REQUEST_USER_INPUT": {
"description": "Asks the user to input data through the interface.",
"arguments": {
"message": {"description": "Message displayed to the user related to the data that will be requested (example: 'Enter your age').", "type": "string"},
},
"generates_data": {
"input": {"description": "Data entered by the user", "type": "string"},
},
},
"WRITE_FILE": {
"description": "Write a file.",
"arguments": {
"content": {"description": "Content that will be written.", "type": "string"},
"file_path": {"description": "Complete path of the file that will be written.", "type": "string"},
},
"generates_data": {},
},
}
# Commands functions
# The name of the function is irrelevant
# The first argument must be the Config object, followed by the Graph object
# The arguments must match the arguments from the commands dictionary
# The return value must be a dictionary which keys must match the "generates_data" keys
# The data types must match the ones declared in the commands dictionary
def write_to_user_command(config: Config, graph: Graph, content: str) -> dict[str, Any]:
print(f">>> {content}")
return {}
def request_user_input_command(config: Config, graph: Graph, message: str) -> dict[str, Any]:
input_ = input(f"{message}\n*: ")
results = {
"input": input_,
}
return results
def write_file_command(config: Config, graph: Graph, content: str, file_path: str) -> dict[str, Any]:
file_dir = Path(file_path).parent
assert file_dir.exists(), f"Container directory '{file_dir}' does not exist."
with open(file_path, "w+", encoding="utf-8") as f:
f.write(content)
f.close()
return {}
# add your functions here
command_name_to_func = {
"WRITE_TO_USER": write_to_user_command,
"REQUEST_USER_INPUT": request_user_input_command,
"WRITE_FILE": write_file_command,
}
main.py
from commands_gpt.recognizers import ComplexRecognizer
from commands_gpt.commands.graphs import Graph
from commands_gpt.config import Config
from custom_commands import commands, command_name_to_func
from commands_gpt.commands.commands_funcs import add_essential_commands
add_essential_commands(commands, command_name_to_func)
chat_model = "gpt-4-0314"
config = Config(chat_model, verbosity=2, explain_graph=False, save_graph_as_file=False)
instruction = input("Enter your prompt: ")
recognizer = ComplexRecognizer(config, commands, command_name_to_func)
commands_data_str = recognizer.recognize(instruction)
graph = Graph(recognizer, commands_data_str)
graph.execute_commands(config)
Copyright (c) 2023, Martín Alexis Martínez Andrade. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file commandsgpt-1.5.2.tar.gz
.
File metadata
- Download URL: commandsgpt-1.5.2.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff4a68ad03e6b6bb1998b3f4e50e30cb9a2f5886569c4fe8d465af2a072ff62d |
|
MD5 | a62a9f89b8169ff864f8a87b50eee90a |
|
BLAKE2b-256 | 2537073826f13af58709d7c1a3e49448cb3dbca2e2c8d87ab666a99f41176f02 |
File details
Details for the file commandsgpt-1.5.2-py3-none-any.whl
.
File metadata
- Download URL: commandsgpt-1.5.2-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1d45f1c67f82d2190aba6ce35359ef35b210bdac82c474b985d881fef92d152 |
|
MD5 | 1a4ac4d75a11a52ef49050aa1daf96f7 |
|
BLAKE2b-256 | e6ba64c9df5086ad3b3176d8fb0b76603a8d5691998b899518661d77c848417f |