Skip to content

Commit

Permalink
Merge pull request #16 from haseeb-heaven/feat/file-prompt-user
Browse files Browse the repository at this point in the history
Feat/file prompt user
  • Loading branch information
haseeb-heaven committed Mar 19, 2024
2 parents d044d4f + 3168ff3 commit 51d3f76
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -208,6 +208,11 @@ python interpreter.py -m 'code-llama' -md 'code' -s
python interpreter.py -m 'gemini-pro' -md 'code' -s -l 'language-name'
```

- Switching to File mode for prompt input </br>
```python
python interpreter.py -m 'gemini-pro' -md 'code' --file
```

- Using Upgrade interpreter </br>
```python
python interpreter.py --upgrade
Expand All @@ -231,6 +236,7 @@ Here are the available commands:
- 🚪 `/exit` - Exit the interpreter.
- 📜 `/log` - Toggle different modes of logging.
-`/upgrade` - Upgrade the interpreter.
- 📁 `/prompt` - Switch the prompt mode.
- 💻 `/shell` - Access the shell.

## ⚙️ **Settings**
Expand Down Expand Up @@ -307,6 +313,7 @@ If you're interested in contributing to **Code-Interpreter**, we'd love to have

🔥 **v2.1** - Added AnhtorpicAI Claude-3 models powerful _Opus,Sonnet,Haiku_ models.
- **v2.1.1** - Added **Groq-AI** Model _Gemma-7B_ with **700 Tokens/Sec**.
- **v2.1.2** - Added **Prompt input Mode** now you can input prompt from file as well.

## 📜 **License**

Expand Down
3 changes: 2 additions & 1 deletion interpreter.py
Expand Up @@ -24,7 +24,7 @@
from libs.utility_manager import UtilityManager

# The main version of the interpreter.
INTERPRETER_VERSION = "2.1.1"
INTERPRETER_VERSION = "2.1.2"

def main():
parser = argparse.ArgumentParser(description='Code - Interpreter')
Expand All @@ -37,6 +37,7 @@ def main():
parser.add_argument('--display_code', '-dc', action='store_true', default=False, help='Display the code in output')
parser.add_argument('--history', '-hi', action='store_true', default=False, help='Use history as memory')
parser.add_argument('--upgrade', '-up', action='store_true', default=False, help='Upgrade the interpreter')
parser.add_argument('--file', '-f', type=str, nargs='?', const='prompt.txt', default=None, help='Sets the file to read the input prompt from')
args = parser.parse_args()

# Check if only the application name is passed
Expand Down
77 changes: 71 additions & 6 deletions libs/interpreter_lib.py
Expand Up @@ -57,6 +57,16 @@ def initialize(self):
self.system_message = ""
self.INTERPRETER_MODE = 'code'

if self.args.file is None:
self.INTERPRETER_PROMPT_FILE = False
self.INTERPRETER_PROMPT_INPUT = True
else:
self.INTERPRETER_PROMPT_FILE = True
self.INTERPRETER_PROMPT_INPUT = False
# If the user didn't provide a file name, use the default one
if self.args.file == '':
self.args.file = 'prompt.txt'

# Set the history optional(Argparse)
if hasattr(self.args, 'history'):
self.INTERPRETER_HISTORY = self.args.history
Expand Down Expand Up @@ -452,16 +462,59 @@ def interpreter_main(self,version):
self.logger.info(f"Mode: {self.INTERPRETER_MODE} Start separator: {start_sep}, End separator: {end_sep}, Skip first line: {skip_first_line}")

# Display system and Assistant information.
display_code(f"OS: '{os_name}', Language: '{self.INTERPRETER_LANGUAGE}', Mode: '{self.INTERPRETER_MODE}' Model: '{self.INTERPRETER_MODEL}'")
display_markdown_message("Welcome to the **Interpreter**. I'm here to **assist** you with your everyday tasks. "
"\nPlease enter your task and I'll do my best to help you out.")
input_prompt_mode = "File" if self.INTERPRETER_PROMPT_FILE else "Input"
display_code(f"OS: '{os_name}', Language: '{self.INTERPRETER_LANGUAGE}', Mode: '{self.INTERPRETER_MODE}', Prompt: '{input_prompt_mode}', Model: '{self.INTERPRETER_MODEL}'")

# Display the welcome message.
display_markdown_message("Welcome to the **Interpreter**, I'm here to **assist** you with your everyday tasks. "
"\nEnter your task and I'll do my best to help you out.")

# Main System and Assistant loop.
running = True
while running:
try:
# Main input prompt - System and Assistant.
task = input("> ")
task = None

if self.INTERPRETER_PROMPT_INPUT:
self.logger.info(f"Reading prompt from input.")
# Main input prompt - System and Assistant.
task = input("> ")
elif self.INTERPRETER_PROMPT_FILE:
prompt_file_name = self.args.file

# Setting the prompt file path.
if not prompt_file_name:
prompt_file_name = 'prompt.txt'

prompt_file_path = os.path.join(os.getcwd(),'system',prompt_file_name)
display_markdown_message(f"\nEnter your task in the file **'{prompt_file_path}'**")

# File mode command section.
prompt_confirmation = input(f"Execute the prompt (Y/N/P/C) (P = Prompt Mode,C = Command Mode)?: ")
if prompt_confirmation.lower() == 'y':
self.logger.info(f"Executing prompt from file.")

self.logger.info(f"Executing prompt from file {prompt_file_path}")
task = self.utility_manager.read_file(prompt_file_path)
elif prompt_confirmation.lower() == 'n':
self.logger.info(f"Waiting for user confirmation to execute prompt from file.")
print("Waiting for user confirmation to execute prompt from file.")
self.utility_manager.clear_screen()
continue
elif prompt_confirmation.lower() == 'p':
self.INTERPRETER_PROMPT_INPUT = True
self.INTERPRETER_PROMPT_FILE = False
self.logger.info(f"Changing input mode to prompt from file.")
self.utility_manager.clear_screen()
continue
elif prompt_confirmation.lower() == 'c':
self.logger.info(f"Changing input mode to command from file.")
task = input("> ")
else:
# Invalid input mode (0x000022)
self.logger.error("Invalid input mode.")
self.utility_manager.clear_screen()
continue

# EXIT - Command section.
if task.lower() == '/exit':
Expand All @@ -482,6 +535,18 @@ def interpreter_main(self,version):
self.utility_manager.display_version(self.interpreter_version)
continue

# PROMPT - Command section.
elif task.lower() == '/prompt':
if self.INTERPRETER_PROMPT_INPUT:
self.INTERPRETER_PROMPT_INPUT = False
self.INTERPRETER_PROMPT_FILE = True
self.logger.info(f"Input mode changed to File.")
else:
self.INTERPRETER_PROMPT_INPUT = True
self.INTERPRETER_PROMPT_FILE = False
self.logger.info(f"Input mode changed to Prompt.")
continue

# HISTORY - Command section.
elif task.lower() == '/history':
self.INTERPRETER_HISTORY = not self.INTERPRETER_HISTORY
Expand Down Expand Up @@ -643,7 +708,7 @@ def interpreter_main(self,version):
if model:
model_config_file = f"configs/{model}.config"
if not os.path.isfile(model_config_file):
display_markdown_message(f"Model {model} does not exists. Please check the model name.")
display_markdown_message(f"Model {model} does not exists. Please check the model name using '/list' command.")
continue
else:
self.INTERPRETER_MODEL = model
Expand Down
17 changes: 17 additions & 0 deletions libs/utility_manager.py
Expand Up @@ -213,6 +213,7 @@ def display_help(self):
/list - List the available models.\n\
/version - Display the version of the interpreter.\n\
/log - Switch between Verbose and Silent mode.\n\
/prompt - Switch input prompt mode between file and prompt.\n\
/upgrade - Upgrade the interpreter.\n\
/shell - Access the shell.\n")

Expand All @@ -221,6 +222,22 @@ def display_version(self,version):

def clear_screen(self):
os.system('cls' if os.name == 'nt' else 'clear')

def read_file(self, file_path):
try:
with open(file_path, "r") as file:
return file.read()
except Exception as exception:
self.logger.error(f"Error in reading file: {str(exception)}")
raise

def write_file(self, file_path, content):
try:
with open(file_path, "w") as file:
file.write(content)
except Exception as exception:
self.logger.error(f"Error in writing file: {str(exception)}")
raise

# method to download file from Web and save it

Expand Down
1 change: 1 addition & 0 deletions system/prompt.txt
@@ -0,0 +1 @@
Enter your prompt in plain text here for Code Interpreter to execute.

0 comments on commit 51d3f76

Please sign in to comment.