Added support for output=<filename>
This commit is contained in:
1 parent
9cbd557af7
commit
7334ed5d4d
1 file changed
+27
-15
+27
-15
@@ -52,13 +52,12 @@ def show_help():
|
|||||||
Available Commands:
|
Available Commands:
|
||||||
|
|
||||||
- 'new' : Start a new conversation (clear the chat history).
|
- 'new' : Start a new conversation (clear the chat history).
|
||||||
- 'exit' : End the conversation and exit the script.
|
- 'exit', 'quit', 'bye' : End the conversation and exit the script.
|
||||||
- 'quit' : Same as 'exit', ends the conversation.
|
|
||||||
- 'bye' : Same as 'exit', ends the conversation.
|
|
||||||
- 'help' : Show this help message with a list of commands.
|
- 'help' : Show this help message with a list of commands.
|
||||||
- 'model' : Show the current model or available models.
|
- 'model' : Show the current model or available models.
|
||||||
- 'model=<model_name>' : Switch to the specified model (e.g., 'model=gpt-4').
|
- 'model=<model_name>' : Switch to the specified model (e.g., 'model=gpt-4').
|
||||||
- 'output' : Save the last ChatGPT response to a file (e.g., chatgpt-output.py).
|
- 'output' : Save the last ChatGPT response to a file (auto-named).
|
||||||
|
- 'output=<filename>' : Save the last ChatGPT response to the given filename.
|
||||||
"""
|
"""
|
||||||
print(help_text)
|
print(help_text)
|
||||||
|
|
||||||
@@ -91,7 +90,7 @@ def chat_with_gpt():
|
|||||||
break
|
break
|
||||||
|
|
||||||
if user_input.lower().startswith("model="):
|
if user_input.lower().startswith("model="):
|
||||||
model_name = user_input.split("=")[1].strip()
|
model_name = user_input.split("=", 1)[1].strip()
|
||||||
if model_name in available_models:
|
if model_name in available_models:
|
||||||
current_model = model_name
|
current_model = model_name
|
||||||
print(f"ChatGPT: Switched to the '{current_model}' model.")
|
print(f"ChatGPT: Switched to the '{current_model}' model.")
|
||||||
@@ -104,26 +103,38 @@ def chat_with_gpt():
|
|||||||
print(f"Available models: {', '.join(available_models)}.")
|
print(f"Available models: {', '.join(available_models)}.")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if user_input.lower() == "output":
|
if user_input.lower().startswith("output"):
|
||||||
if not last_response:
|
if not last_response:
|
||||||
print("ChatGPT: No response available to save.")
|
print("ChatGPT: No response available to save.")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if "```python" in last_response:
|
# Determine extension based on code block
|
||||||
ext = "py"
|
def get_extension(content):
|
||||||
elif "```html" in last_response:
|
if "```python" in content:
|
||||||
ext = "html"
|
return "py"
|
||||||
elif "```json" in last_response:
|
elif "```html" in content:
|
||||||
ext = "json"
|
return "html"
|
||||||
elif "```bash" in last_response:
|
elif "```json" in content:
|
||||||
ext = "sh"
|
return "json"
|
||||||
|
elif "```bash" in content:
|
||||||
|
return "sh"
|
||||||
else:
|
else:
|
||||||
ext = "txt"
|
return "txt"
|
||||||
|
|
||||||
|
# Parse optional filename
|
||||||
|
if "=" in user_input:
|
||||||
|
filename = user_input.split("=", 1)[1].strip()
|
||||||
|
if "." not in filename:
|
||||||
|
ext = get_extension(last_response)
|
||||||
|
filename = f"{filename}.{ext}"
|
||||||
|
else:
|
||||||
|
ext = get_extension(last_response)
|
||||||
filename = f"chatgpt-output.{ext}"
|
filename = f"chatgpt-output.{ext}"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
content = last_response
|
content = last_response
|
||||||
if "```" in content:
|
if "```" in content:
|
||||||
|
# Extract only the content within the triple backticks
|
||||||
content = content.split("```")[1]
|
content = content.split("```")[1]
|
||||||
if "\n" in content:
|
if "\n" in content:
|
||||||
content = "\n".join(content.split("\n")[1:])
|
content = "\n".join(content.split("\n")[1:])
|
||||||
@@ -155,3 +166,4 @@ def chat_with_gpt():
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
openai.api_key = get_api_key()
|
openai.api_key = get_api_key()
|
||||||
chat_with_gpt()
|
chat_with_gpt()
|
||||||
|
|
||||||
Reference in new issue
Block a user