feat: 🎸 API support

Add support for ChatGPT API (for pro only)
This commit is contained in:
Grey_D
2023-03-19 19:13:33 +08:00
parent 0d928517a1
commit a3e4d5696c
13 changed files with 1268 additions and 39 deletions

View File

@@ -1,35 +1,22 @@
from chatgpt_wrapper import ChatGPT
from llm_handle.parser import extract_cmd
from task_handle.cmd_execution import execute_cmd
import os
import loguru
from config.chatgpt_config import ChatGPTConfig
from utils.chatgpt import ChatGPT
def __main__():
bot = ChatGPT()
response = bot.ask(
"Can you give me a sample command in Mac terminal for checking the user names? Please give me the code directly."
)
sample_response = """
Certainly! To list all user names on a Mac using the terminal, you can use the `dscl` command with the `list` option for the `/Users` node. Here's the command:
```
dscl . list /Users | grep -v '^_'
```
logger = loguru.logger
This will output a list of all user accounts on the system, excluding any system accounts that start with an underscore.
"""
# print("The response is:", response)
command = extract_cmd(str(response))
print("The command is:", command)
# execute the command in the terminal
output = execute_cmd(command)
print("The output is:\n", output)
# Ideally, the output should be:
"""
daemon
uname
nobody
root"""
# delete the session in the end
bot.delete_conversation()
if __name__ == "__main__":
chatGPTAgent = ChatGPT(ChatGPTConfig())
# the title of this conversation will be new-chat. We can delete it later.
text, conversation_id = chatGPTAgent.send_new_message("Hello, world!")
print(text, conversation_id)
# get history id
history = chatGPTAgent.get_conversation_history()
print(history)
for uuid in history:
print(uuid)
if history[uuid].lower() == "new chat":
result = chatGPTAgent.delete_conversation(uuid)
print(result)
history = chatGPTAgent.get_conversation_history()
print(history)