How to build your own AI personal assistant in 10 lines of code - Python
Recently I have developed my own GEN AI Applications MollyJob, and I think it is quite cool for everyone to have their own AI Bot.
Prerequisites
Before starting, you will need to have:
Step 1: Install the OpenAI Python Library
First, we need to install the latest Python client library for the OpenAI API. You can install it using pip, the Python package manager, with the following command:
pip install openai
Step 2: Set Up Your OpenAI API Key
You will need to set up an OpenAI API key. If you don’t already have one, you can get one by following the instructions provided in the OpenAI API documentation 450.
Once you have your API key, replace "YOUR_API_KEY" in the code snippet below with your API key:
import openai
openai.api_key = "YOUR_API_KEY"
Step 3: Write the system message
The system message is a message object with the "role" : "system". The system message helps set the behavior of the assistant. You can also give the assistant a name using the system message.
When creating your own chat assistant, it’s important to choose a good directive prompt. Here are some tips to keep in mind:
Replace the "DIRECTIVE_FOR_gpt-3.5-turbo" placeholder in next step (4) with the system message you wrote.
Step 4: Create the Chat Assistant
Now that we have the OpenAI Python library installed and our API key set up, we can create the chat assistant.
import openai
openai.api_key = "YOUR_API_KEY" # supply your API key however you choose
message = {"role":"user", "content": input("This is the beginning of your chat with AI. [To exit, send \"###\".]\n\nYou:")};
conversation = [{"role": "system", "content": "DIRECTIVE_FOR_gpt-3.5-turbo"}]
while(message["content"]!="###"):
conversation.append(message)
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=conversation)
message["content"] = input(f"Assistant: {completion.choices[0].message.content} \nYou:")
print()
conversation.append(completion.choices[0].message)
领英推荐
Step 5: Run the AI Assistant
To run the AI assistant, save the code snippet from step 4
Extension for voice ChatBot:
In this tutorial, we create a chat assistant using the OpenAI Python library and the GPT-3.5-turbo model.
Packages required:
To build a personal voice assistant it’s necessary to install the following packages in your system using the pip command.
1) Speech recognition — Speech recognition is an important feature used in house automation and in artificial intelligence devices. The main function of this library is it tries to understand whatever the humans speak and converts the speech to text.
2) pyttsx3 — pyttxs3 is a text to speech conversion library in python. This package supports text to speech engines on Mac os x, Windows and on Linux.
3) wikipedia — Wikipedia is a multilingual online encyclopedia used by many people from academic community ranging from freshmen to students to professors who wants to gain information over a particular topic. This package in python extracts data’s required from Wikipedia.
4) ecapture — This module is used to capture images from your camera
5) datetime — This is an inbuilt module in python and it works on date and time
6) os — This module is a standard library in python and it provides the function to interact with operating system
7) time — The time module helps us to display time
8) Web browser — This is an in-built package in python. It extracts data from the web
9) Subprocess — This is a standard library use to process various system commands like to log off or to restart your PC.
10) Json- The json module is used for storing and exchanging data.
11) request- The request module is used to send all types of HTTP request. Its accepts URL as parameters and gives access to the given URL’S.
12) wolfram alpha — Wolfram Alpha is an API which can compute expert-level answers using Wolfram’s algorithms, knowledge base and AI technology. It is made possible by the Wolfram Language.
This Blog is originally from Open AI community by Sukhman Preet Singh Jawa. Hope it works for you.
Sr Tech at MasterCard
4 个月Having an issue with getting the API key. Looks like this version is using gpt-3.5-turbo, but when trying to run with a personal API key, it's stating that you are at limit. And the price for gpt-3.5-turbo is okay, but I didn't think it was a prerequisite. I currently have a free OpenAI account, is a paid one required for your method? openai.error.RateLimitError: You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.
Student at sa
7 个月how i can install python 3.7.1