{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMWsO+T66P1RZEeDMmjXjcm",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"source": [
"# Install Packages and Setup Variables"
],
"metadata": {
"id": "DMXyyXD0xix9"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "o4Q0N2omkAoZ"
},
"outputs": [],
"source": [
"!pip install -q openai==1.6.0 cohere==4.39 tiktoken==0.5.2"
]
},
{
"cell_type": "code",
"source": [
"import os\n",
"\n",
"# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n",
"os.environ[\"OPENAI_API_KEY\"] = \"\""
],
"metadata": {
"id": "xxK7EAAvr2aT"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Load the API client"
],
"metadata": {
"id": "68RbStS-xpbL"
}
},
{
"cell_type": "code",
"source": [
"from openai import OpenAI\n",
"\n",
"# Defining the \"client\" object that enables\n",
"# us to connect to OpenAI API endpoints.\n",
"client = OpenAI()"
],
"metadata": {
"id": "La8hdWqJkFkh"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Query the API"
],
"metadata": {
"id": "CC-sa_uv6J2C"
}
},
{
"cell_type": "code",
"source": [
"# Define two questions: 1) Related to AI, 2) Unrelated topic.\n",
"# These questions will be used to evaluate model's performance.\n",
"QUESTION_AI = \"List a number of famous artificial intelligence frameworks?\"\n",
"QUESTION_NOT_AI = \"What is the name of the highest mountain in the world and its height?\""
],
"metadata": {
"id": "7JRrn0uIsBfg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Defining a function to answer a question using \"gpt-3.5-turbo-16k\" model.\n",
"def ask_ai_tutor(question):\n",
" try:\n",
" # Formulating the system prompt and condition the model to answer only AI-related questions.\n",
" system_prompt = (\n",
" \"You are an AI tutor specialized in answering artificial intelligence-related questions. \"\n",
" \"Only answer AI-related question, else say that you cannot answer this question.\"\n",
" )\n",
"\n",
" # Create a user prompt with the user's question\n",
" prompt = f\"Please provide an informative and accurate answer to the following question.\\nQuestion: {question}\\nAnswer:\"\n",
"\n",
" # Call the OpenAI API\n",
" response = client.chat.completions.create(\n",
" model='gpt-3.5-turbo-16k',\n",
" temperature=0.0,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": prompt}\n",
" ]\n",
" )\n",
"\n",
" # Return the AI's response\n",
" return response.choices[0].message.content.strip()\n",
"\n",
" except Exception as e:\n",
" return f\"An error occurred: {e}\""
],
"metadata": {
"id": "CcP26IauuBuV"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Ask the AI-related question.\n",
"res = ask_ai_tutor( QUESTION_AI )\n",
"print( res )"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "W_dbwURpufR7",
"outputId": "196c4c2f-9b7f-4302-a6e5-3ba2b8cbb37e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Sure! There are several famous artificial intelligence frameworks that are widely used in the field. Some of the popular ones include:\n",
"\n",
"1. TensorFlow: Developed by Google, TensorFlow is an open-source framework that is widely used for machine learning and deep learning tasks. It provides a comprehensive ecosystem of tools, libraries, and resources for building and deploying AI models.\n",
"\n",
"2. PyTorch: Developed by Facebook's AI Research lab, PyTorch is another popular open-source framework for deep learning. It is known for its dynamic computational graph, which allows for more flexibility and ease of use compared to other frameworks.\n",
"\n",
"3. Keras: Keras is a high-level neural networks API written in Python. It is built on top of TensorFlow and provides a user-friendly interface for building and training deep learning models. Keras is known for its simplicity and ease of use, making it a popular choice for beginners.\n",
"\n",
"4. Caffe: Caffe is a deep learning framework developed by Berkeley AI Research (BAIR). It is known for its speed and efficiency, particularly for convolutional neural networks (CNNs). Caffe has been widely used in computer vision tasks and has a large community of users and contributors.\n",
"\n",
"5. Theano: Theano is a Python library that allows for efficient mathematical computations, particularly for deep learning tasks. It provides a high-level interface for defining and optimizing mathematical expressions, making it a popular choice for researchers and developers.\n",
"\n",
"These are just a few examples of famous AI frameworks, and there are many others available depending on specific needs and preferences.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Ask the unrelated question.\n",
"res = ask_ai_tutor( QUESTION_NOT_AI )\n",
"print( res )"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "37YuVJQquhpN",
"outputId": "17c4b543-65fe-4cfb-9020-2d44bfef2eac"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"I'm sorry, but I cannot answer that question as it is not related to artificial intelligence.\n"
]
}
]
}
]
}