---
# System prepended metadata

title: Simple Terminal ChatGPT BOT
tags: [python, 程式設計, bash, chatGPT, linux]

---

Simple Terminal ChatGPT BOT
===
###### tags: `程式設計` `python` `chatGPT` `bash` `linux`
:::info
適用於Linux環境下的Terminal ChatGPT
:::
### Source Code
```python=
import os
#import openai
from revChatGPT.V3 import Chatbot
OPENAI_API_KEY = "{YOUR KEY}"
ENGINE = "gpt-3.5-turbo"    #推薦使用gpt-3.5-turbo
chatbot = Chatbot(api_key=OPENAI_API_KEY, engine=ENGINE)
#-------------------------------------------------------#
os.system('clear')

def chat(message) -> str:
    responseMessage = chatbot.ask(message)
    return responseMessage

while True:
    prompt= input(f"\033[36m[{os.getlogin()}@localhost]\n\033[93m>\033[0m ")
    if prompt.lower() == 'exit':
        print('\033[36m[ChatGPT]\n\033[93m>\033[0m Goodbye!')
        break
    response = chat(prompt)
    print(f"\033[36m[ChatGPT]\n\033[93m>\033[0m {response}")
```
**申請API**: [platform.openai.com](https://platform.openai.com/account/)