How to Run ChatGPT on Raspberry Pi or PC
Don’t miss the frenetic excitement surrounding ChatGPT (Chat Generative-Pre-Trained Transformer). We create images, write chords, songs and poems, and try to solve our problems, big and small. It may not beat Star Trek’s Kobayashi Maru simulation, but you can easily write Python and Bash code.Writing is also possible with one push G-code for best 3d printer we haven’t tried it yet Clarity Ender 2 Pro.
Normally we interact with ChatGPT via a browser, but this how-to uses a special Python library that makes a humble connection. raspberry pi It connects you to powerful AI and gives you the tools to answer almost any question. This project is not exclusive to Raspberry Pi. Also available for Windows, macOS and Linux PCs. If you can run Python on top of that, this project might work too.
If you have a Raspberry Pi, you can use almost any model for this project. This is because you are simply making the request over your internet connection. However, for the smoothest overall performance, raspberry pi 4 Or Raspberry Pi 3B+.
Setting ChatGPT API Key for Raspberry Pi
Before using ChatGPT with Raspberry Pi and Python, you first need to set up an API key. This key allows you to connect your code to your OpenAI account and use AI to answer your queries, write chords, write poetry, or create your next hit song.
1. Log in to your OpenAI account.
2. Click the menu to View your API key.
3. [Create new secret key]Click to generate an API key. Copy this key and paste it somewhere safe. It will not appear again. Never share API keys. They are specific to your account and any costs incurred will come from your account.
Install ChatGPT Python API on Raspberry Pi
Now that you have your API key in hand, you can configure your Raspberry Pi and specifically Python to use the API via the openAI Python library.
1. Open a terminal and update the Raspberry Pi’s software. There are two commands for this. First run an update to make sure your Pi’s list of software repositories is up to date. If not, download the latest details. The “&&” means that if the first command (update) is successful, the second command to upgrade the software will be started. The “-y” flag is used to accept the installation without user input.
sudo apt update && sudo apt upgrade -y
2. Install the openai Python library using the pip package manager.
pip3 install openai
3. Open the .bashrc file hidden in your home directory. This file is where you need to set the paths where the Raspberry Pi OS and Python can find executables and configuration files.
nano ~/.bashrc
Four. Scroll to the bottom of the file using your keyboard and add the following line:
export PATH="$HOME/.local/bin:$PATH"
Five. Press CTRL + X, Y, Enter to save the file.
6. Reload the .bashrc configuration to complete the configuration. Then close the terminal.
source ~/.bashrc
Create a ChatGPT chatbot for Raspberry Pi
The goal of chatbots is to respond to questions posed by users. This project code works as long as the response is in text format. When the user is finished, they can type a word to exit or press CTRL+C to stop the code. We tested it with facts and trivia questions and asked them to write Python code, Bash, and a little G-code for a 3D printer.
1. launch sonythe built-in Python editor. This can be found in the Raspberry Pi menu[プログラミング]>>[Thonny]is in
2. Import the openai library. This will bring your Python code online to ChatGPT.
import openai
3. Create an object model_engine and save your preferred model there. davinci-003 is the most capable, but (in order of ability) you can also use “text-curie-001”, “text-babbage-001”, “text-ada-001”. The ADA model has the lowest token cost.
model_engine = "text-davinci-003"
Four. Create an object open.api_key to store your API key. Paste your API key between the quotes.
openai.api_key = "YOUR API KEY HERE”
Five. Create a function GPT() that takes a query (question) from the user as an argument. This means you can reuse the function for any question.
def GPT(query):
6. Create a response object that passes query details to ChatGPT. Ask questions using the model and query of your choice. We set the maximum spend of tokens to 1024, but in practice we will spend much less so that we can tweak this. “Temperature” controls how creative the response can be. The higher the value (say 0.9), the more creative the model. 0.5 is a good mix of creativity and fact.
response = openai.Completion.create(
engine=model_engine,
prompt=query,
max_tokens=1024,
temperature=0.5,
)
7. Returns data from ChatGPT and strips the response text and number of tokens used. The data returned is in dictionary/JSON format, so the key must be used to specifically target the correct data. These keys return associated values.
return str.strip(response['choices'][0]['text']), response['usage']['total_tokens']
8. Create a tuple and use it to store a list of strings that can be used to end the chat. Tuples are immutable. This means that tuples can be created and destroyed, but never updated by running code. They make the perfect “set and forget” configuration.
exit_words = ("q","Q","quit","QUIT","EXIT")
9. Follow try with while True: to tell Python to try and run your code.
try:
while True:
Ten. Print user instructionsin this case how to end the chat.
print("Type q, Q, quit, QUIT or EXIT and press Enter to end the chat session")
11. Capture the user query with a custom prompt and store it in an object called query.
query = input("What is your question?> ")
12. Use a conditional test to see if any of the exit_words exist by themselves in the query. You can use these words in your query, but if they are the only words, the chat will end.
if query in exit_words:
13. Set as follows: If exit_words are present, The code prints “ENDING CHAT” When Then use break to stop the code.
print("ENDING CHAT")
break
14. Create an else condition. This condition is always executed if no exit_words are found.
else:
15. Run a ChatGPT query When Save the output in two objects: res (response) and usage (tokens used).
(res, usage) = GPT(query)
16. Print the ChatGPT response to the Python shell.
print(res)
17. Continue printing 20 = to create a barrier between ChatGPT text and print the number of tokens used.
print("="*20)
print("You have used %s tokens" % usage)
print("="*20)
18. Create an exception handler that activates when the user presses CTRL+C. Prints an exit message to the Python shell before the code exits.
except KeyboardInterrupt:
print("\nExiting ChatGPT")
19. Save the code as ChatGPT-Chatbot.py When [実行]Click to get started. Ask the chatbot a question and when finished type one of the exit_words or press CTRL+C to exit.
complete code listing
import openai
model_engine = "text-davinci-003"
openai.api_key = "YOUR API KEY HERE"
def GPT(query):
response = openai.Completion.create(
engine=model_engine,
prompt=query,
max_tokens=1024,
temperature=0.5,
)
return str.strip(response['choices'][0]['text']), response['usage']['total_tokens']
exit_words = ("q","Q","quit","QUIT","EXIT")
try:
while True:
print("Type q, Q, quit, QUIT or EXIT and press Enter to end the chat session")
query = input("What is your question?> ")
if query in exit_words:
print("ENDING CHAT")
break
else:
(res, usage) = GPT(query)
print(res)
print("="*20)
print("You have used %s tokens" % usage)
print("="*20)
except KeyboardInterrupt:
print("\nExiting ChatGPT")