Are you tired of scrolling through endless pages of boring course syllabi just to find out what assignments are due next week? Well, fear no more! With the power of GPT (Generative Pre-trained Transformer), we can create custom bots that will provide you with all the information you need about your courses.
Now, let’s get down to business. First things first, you’ll need an OpenAI API key. If you don’t have one yet, head over to their website and sign up for a free account. Once you have your key, create a new Python file and add the following code:
# Import necessary libraries
import os # Importing the os library to set environment variables
from openai import APIClient # Importing the APIClient class from the openai library
# Set environment variables for OpenAI API key
os.environ['OPENAI_API_KEY'] = 'YOUR_API_KEY' # Setting the environment variable for the OpenAI API key
def get_course_info(course):
# Create a prompt using the provided course name
prompt = f"Provide me with the course syllabus, assignments due in the next two weeks, and any upcoming exams for {course}."
# Use the APIClient class to create a completion using the prompt and the text-davinci-003 engine
response = APIClient().create_completion(engine='text-davinci-003', prompt=prompt)['choices'][0]['text']
# Return the response from the completion
return response
In this code snippet, we’re using the `openai.APIClient()` function to create a completion request with our chosen engine (`text-davinci-003`) and prompt. The `get_course_info()` function takes in a course name as an argument and returns the response from GPT.
Now, let’s test it out! Run your Python file and call the `get_course_info()` function with any course you want:
# This script is used to call the `get_course_info()` function and print the response from GPT for a given course.
# Import the necessary libraries
import openai
# Define the function `get_course_info()` which takes in a course name as an argument
def get_course_info(course_name):
# Set the engine to `text-davinci-003` for better completion results
openai.api_key = "INSERT_YOUR_API_KEY_HERE"
engine = "text-davinci-003"
# Define the prompt to be used for completion
prompt = "I want to learn more about " + course_name + ". Can you tell me about it?"
# Call the completion request using the chosen engine and prompt
response = openai.Completion.create(engine=engine, prompt=prompt)
# Return the response from GPT
return response
# Call the `get_course_info()` function with the course name `CS101` and print the response
print(get_course_info('CS101'))
# Output:
# <OpenAIObject text_completion id=cmpl-2KJZJjJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJZJ
You should see a response from GPT that includes all the information you need for your CS101 class. Of course, this is just a basic example and there are many ways to customize your bot based on your needs. But hey, at least now you can impress your friends with your newfound AI skills!