How to build your own AI personal assistant in 10 lines of code - Python

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:

  • Python 3.7.1 or higher installed on your system
  • An OpenAI API key

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:

  • Keep it simple and clear: The directive should be clear, intentional, concise and declarative.
  • Set the tone: The directive should establish the chat assistant’s personality and tone. This can be done through the choice of words, phrasing, and even punctuation. For example, a chat assistant that is meant to be friendly and approachable might use exclamation points and emojis, while one that is meant to be more serious and professional might use more formal language and punctuation.
  • Test and iterate: Don’t be afraid to experiment with different directive prompts and see how users respond. You may need to tweak the language or tone of the prompt to get the best results.

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.


Greg Smith

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.

回复
Aniket Yadav

Student at sa

7 个月

how i can install python 3.7.1

回复

要查看或添加评论,请登录

Yiman H.的更多文章

社区洞察

其他会员也浏览了