Skip to content

Instantly share code, notes, and snippets.

@mpottinger
Created March 4, 2023 19:06
Show Gist options
  • Select an option

  • Save mpottinger/984a2628bd5a73e8bca2700893487296 to your computer and use it in GitHub Desktop.

Select an option

Save mpottinger/984a2628bd5a73e8bca2700893487296 to your computer and use it in GitHub Desktop.
import os
import sys
import openai
# for openai
openai.api_key = "<your openai api key>"
tokens_used = 0
def get_reply(messages):
global tokens_used
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
reply = response["choices"][0]["message"]["content"]
tokens = response['usage']['total_tokens']
# The ChatGPT API costs $0.002 per 1,000 tokens. So we need to keep track of how many tokens we use.
tokens_used += tokens
messages.append({"role": "assistant", "content": reply})
return reply
messages = []
system_message = "You are ChatGPT Shell. An AI which assists with shell commands. The user can decribe what they want to do and the ChatGPT shell will provide the command(s) to do it."
messages.append({"role":"system","content":system_message})
message = "Hello! I am in need of assistance with shell commands. Can you help me?"
messages.append({"role":"user","content": message})
assistant_message = "I am " + system_message + ". I can help you with shell commands. What would you like to do? First provide some information about the system you are working on so I can have some context. The more info the better."
messages.append({"role":"assistant","content":assistant_message})
# get the operating system
platform = sys.platform
message = "OS: " + platform
messages.append({"role":"user","content": message})
assistant_message = "Thank you for the information. I will now be " + system_message + ". Tell me what you'd like to do and I will provide the command(s) to do it."
messages.append({"role":"assistant","content":assistant_message})
message = "ls -l"
messages.append({"role":"user","content": message})
assistant_message = "*COMMAND*"
messages.append({"role":"assistant","content":assistant_message})
message = "I want to list all the files in the current directory."
messages.append({"role":"user","content": message})
assistant_message = "```ls -l```"
messages.append({"role":"assistant","content":assistant_message})
message = "I want to know the current working directory."
messages.append({"role":"user","content": message})
assistant_message = "```pwd```"
print("ChatGPT Shell")
print("Describe what you want to do and the ChatGPT shell will provide the command(s) to do it.")
print("Type 'exit' to exit, type $ before a command to run it.")
print("running on: " + platform)
while True:
cost = tokens_used / 1000 * 0.002
print("tokens: " + str(tokens_used) + " Total used this session: " + str(tokens_used) + " Cost: $" + str(cost))
message = input("ChatGPT//: ").strip()
if message == "":
continue
if message == "exit":
quit()
elif message.startswith("$"):
print("running: " + message)
# remove leading $ and/or spaces
message = message.replace("$", "")
message = message.strip()
# run the command
os.system(message)
continue
else:
messages.append({"role":"user","content": message})
reply = get_reply(messages)
# change formatting of code blocks change ``` to lines like --------------------------------
reply = reply.replace("```", "\n--------------------------------\n")
# colorize the code blocks to make them stand out
# break the reply into lines
reply_lines = reply.splitlines()
# loop through the lines
is_code_block = False
for i in range(len(reply_lines)):
# if the line is a code block line
if reply_lines[i].startswith("----"):
if is_code_block:
# add another newline to the end of the code block
reply_lines[i] = reply_lines[i] + "\n"
# toggle the is_code_block flag
is_code_block = not is_code_block
# if the line is a code block line
if is_code_block:
# set the color to green if it's not a divider line
if not reply_lines[i].startswith("----"):
reply_lines[i] = "\033[92m" + reply_lines[i] + "\033[0m"
# remove blank lines
reply_lines = [line for line in reply_lines if line.strip()]
# join the lines back together
reply = "\n".join(reply_lines)
print(reply)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment