Created
May 15, 2025 17:19
-
-
Save mjmenger/d08fda797affe5c1d7130bf3871bc55a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "id": "f8115a3e", | |
| "metadata": {}, | |
| "source": [ | |
| "# Install Required Libraries\n", | |
| "This was used in an Anaconda virtual environment with Python 3.11.7" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "07e3853a", | |
| "metadata": { | |
| "vscode": { | |
| "languageId": "shellscript" | |
| } | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/wandb-0.17.3-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/setproctitle-1.3.3-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/grpcio-1.65.0rc2-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/sentry_sdk-2.7.1-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/python_rapidjson-1.18-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/gevent-24.2.1-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/nupic.client-0.1.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/tritonclient-2.47.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/docker_pycreds-0.4.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/geventhttpclient-2.0.2-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/zope.event-5.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0mRequirement already satisfied: PyPDF2 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (3.0.1)\n", | |
| "Note: you may need to restart the kernel to use updated packages.\n", | |
| "\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/wandb-0.17.3-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/setproctitle-1.3.3-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/grpcio-1.65.0rc2-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/sentry_sdk-2.7.1-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/python_rapidjson-1.18-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/gevent-24.2.1-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/nupic.client-0.1.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/tritonclient-2.47.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/docker_pycreds-0.4.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/geventhttpclient-2.0.2-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/zope.event-5.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0mRequirement already satisfied: openai in /home/mjmenger/anaconda3/lib/python3.11/site-packages (1.54.3)\n", | |
| "Requirement already satisfied: anyio<5,>=3.5.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from openai) (4.2.0)\n", | |
| "Requirement already satisfied: distro<2,>=1.7.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from openai) (1.8.0)\n", | |
| "Requirement already satisfied: httpx<1,>=0.23.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from openai) (0.27.2)\n", | |
| "Requirement already satisfied: jiter<1,>=0.4.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from openai) (0.7.0)\n", | |
| "Requirement already satisfied: pydantic<3,>=1.9.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from openai) (2.9.2)\n", | |
| "Requirement already satisfied: sniffio in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from openai) (1.3.0)\n", | |
| "Requirement already satisfied: tqdm>4 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from openai) (4.67.1)\n", | |
| "Requirement already satisfied: typing-extensions<5,>=4.11 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from openai) (4.12.2)\n", | |
| "Requirement already satisfied: idna>=2.8 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai) (3.4)\n", | |
| "Requirement already satisfied: certifi in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai) (2024.8.30)\n", | |
| "Requirement already satisfied: httpcore==1.* in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai) (1.0.6)\n", | |
| "Requirement already satisfied: h11<0.15,>=0.13 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)\n", | |
| "Requirement already satisfied: annotated-types>=0.6.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n", | |
| "Requirement already satisfied: pydantic-core==2.23.4 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->openai) (2.23.4)\n", | |
| "Note: you may need to restart the kernel to use updated packages.\n", | |
| "\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/wandb-0.17.3-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/setproctitle-1.3.3-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/grpcio-1.65.0rc2-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/sentry_sdk-2.7.1-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/python_rapidjson-1.18-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/gevent-24.2.1-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/nupic.client-0.1.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/tritonclient-2.47.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/docker_pycreds-0.4.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/geventhttpclient-2.0.2-py3.11-linux-x86_64.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /home/mjmenger/anaconda3/lib/python3.11/site-packages/zope.event-5.0-py3.11.egg is deprecated. pip 24.3 will enforce this behaviour change. A possible replacement is to use pip for package installation.. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", | |
| "\u001b[0mRequirement already satisfied: anthropic in /home/mjmenger/anaconda3/lib/python3.11/site-packages (0.46.0)\n", | |
| "Requirement already satisfied: anyio<5,>=3.5.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from anthropic) (4.2.0)\n", | |
| "Requirement already satisfied: distro<2,>=1.7.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from anthropic) (1.8.0)\n", | |
| "Requirement already satisfied: httpx<1,>=0.23.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from anthropic) (0.27.2)\n", | |
| "Requirement already satisfied: jiter<1,>=0.4.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from anthropic) (0.7.0)\n", | |
| "Requirement already satisfied: pydantic<3,>=1.9.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from anthropic) (2.9.2)\n", | |
| "Requirement already satisfied: sniffio in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from anthropic) (1.3.0)\n", | |
| "Requirement already satisfied: typing-extensions<5,>=4.10 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from anthropic) (4.12.2)\n", | |
| "Requirement already satisfied: idna>=2.8 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from anyio<5,>=3.5.0->anthropic) (3.4)\n", | |
| "Requirement already satisfied: certifi in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from httpx<1,>=0.23.0->anthropic) (2024.8.30)\n", | |
| "Requirement already satisfied: httpcore==1.* in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from httpx<1,>=0.23.0->anthropic) (1.0.6)\n", | |
| "Requirement already satisfied: h11<0.15,>=0.13 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->anthropic) (0.14.0)\n", | |
| "Requirement already satisfied: annotated-types>=0.6.0 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->anthropic) (0.7.0)\n", | |
| "Requirement already satisfied: pydantic-core==2.23.4 in /home/mjmenger/anaconda3/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->anthropic) (2.23.4)\n", | |
| "Note: you may need to restart the kernel to use updated packages.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%pip install PyPDF2\n", | |
| "%pip install openai\n", | |
| "%pip install anthropic" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "fc315816", | |
| "metadata": {}, | |
| "source": [ | |
| "# Import Required Libraries\n", | |
| "Import the necessary libraries, including PyPDF2.\n", | |
| "\n", | |
| "also create a `.env` file with the following content\n", | |
| "```text\n", | |
| "OPENAI_API_KEY='your-api-key'\n", | |
| "```" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "id": "db64b192", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Import Required Libraries\n", | |
| "import PyPDF2\n", | |
| "import json\n", | |
| "from dotenv import load_dotenv\n", | |
| "import os\n", | |
| "\n", | |
| "load_dotenv()\n", | |
| "\n", | |
| "# Initialize Variables\n", | |
| "responses = []\n", | |
| "errors = []" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "cf1e71bb", | |
| "metadata": {}, | |
| "source": [ | |
| "# Instantiate the OpenAI client" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "f2c4ce69", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from openai import OpenAI\n", | |
| "\n", | |
| "openai_client = OpenAI()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "0afa88bc", | |
| "metadata": {}, | |
| "source": [ | |
| "# Instantiate the Anthropic client" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "id": "72eb672c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import anthropic\n", | |
| "\n", | |
| "anthropic_client = anthropic.Anthropic()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "abe03c02", | |
| "metadata": {}, | |
| "source": [ | |
| "# Define Function to Query Open AI\n", | |
| "Define a function `query_gpt4` that takes a prompt as input and returns the gpt response." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "id": "af30319e", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def query_gpt4(prompt):\n", | |
| " response = openai_client.chat.completions.create(\n", | |
| " model=\"gpt-3.5-turbo\", # Use the appropriate model for GPT-4\n", | |
| " messages=[\n", | |
| " {\"role\": \"system\", \"content\": \" You are an expert product evaluator. You will receive a description of a product's capabilities and a description of a design requirement. Your task is to assess whether the product is applicable to the design requirement and provide a structured JSON response.\"},\n", | |
| " {\"role\": \"user\", \"content\": prompt}\n", | |
| " ],\n", | |
| " max_tokens=2000\n", | |
| " )\n", | |
| " return response.choices[0].message.content" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "679a725d", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import openai\n", | |
| "\n", | |
| "def query_internal_llm(prompt, api_base_url='https://f5ai.pd.f5net.com/openai/', api_key=None, model=\"gpt-4o\"):\n", | |
| " \"\"\"\n", | |
| " Query an OpenAI-compatible internally hosted LLM.\n", | |
| "\n", | |
| " Args:\n", | |
| " prompt (str): The prompt to send.\n", | |
| " api_base_url (str): The base URL of the internal LLM API (e.g., \"http://localhost:8000/v1\").\n", | |
| " api_key (str, optional): API key if required by the internal LLM.\n", | |
| " model (str): Model name to use.\n", | |
| "\n", | |
| " Returns:\n", | |
| " str: The LLM's response content.\n", | |
| " \"\"\"\n", | |
| "\n", | |
| " # Save current OpenAI settings\n", | |
| " old_api_base = openai.base_url\n", | |
| " old_api_key = openai.api_key\n", | |
| "\n", | |
| " # Set internal LLM endpoint and key\n", | |
| " openai.base_url = api_base_url\n", | |
| " if api_key:\n", | |
| " openai.api_key = api_key\n", | |
| "\n", | |
| " try:\n", | |
| " response = openai.chat.completions.create(\n", | |
| " model=model,\n", | |
| " messages=[\n", | |
| " {\"role\": \"system\", \"content\": \"You are an expert product evaluator. You will receive a description of a product's capabilities and a description of a design requirement. Your task is to assess whether the product is applicable to the design requirement and provide a structured JSON response.\"},\n", | |
| " {\"role\": \"user\", \"content\": prompt}\n", | |
| " ],\n", | |
| " max_tokens=2000\n", | |
| " )\n", | |
| " return response.choices[0].message.content\n", | |
| " finally:\n", | |
| " # Restore OpenAI settings\n", | |
| " openai.base_url = old_api_base\n", | |
| " openai.api_key = old_api_key" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "276ac7a5", | |
| "metadata": {}, | |
| "source": [ | |
| "# Define Function to Query Anthropic" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "2604335d", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def query_anthropic(prompt):\n", | |
| " response = anthropic_client.messages.create(\n", | |
| " model=\"claude-3-5-sonnet-20241022\",\n", | |
| " max_tokens=2000,\n", | |
| " temperature=0,\n", | |
| " system=\" You are an expert product evaluator. You will receive a description of a product's capabilities and a description of a design requirement. Your task is to assess whether the product is applicable to the design requirement and provide a structured JSON response.\",\n", | |
| " messages=[\n", | |
| " {\n", | |
| " \"role\": \"user\",\n", | |
| " \"content\": [\n", | |
| " {\n", | |
| " \"type\": \"text\",\n", | |
| " \"text\": prompt\n", | |
| " }\n", | |
| " ]\n", | |
| " }\n", | |
| " ]\n", | |
| " )\n", | |
| " return response.content[0].text" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "7f250be7", | |
| "metadata": {}, | |
| "source": [ | |
| "## Read Product Category information\n", | |
| "prepare a list of product categories for the assessment and description loop" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "id": "cf19af41", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import json\n", | |
| "import logging\n", | |
| "\n", | |
| "responses = []\n", | |
| "\n", | |
| "# Load the JSON file\n", | |
| "with open('bb2dm-descriptions.json', 'r') as file:\n", | |
| " product_categories = json.load(file)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "2cd33cdf", | |
| "metadata": {}, | |
| "source": [ | |
| "# Define a function for selecting a subset of the product or requirements lists" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "id": "c0b18cfa", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import random\n", | |
| "# Function to select a subset of items\n", | |
| "def select_subset(items, subset_size):\n", | |
| " # Return the entire set if the subset size is larger than the set\n", | |
| " if subset_size > len(items):\n", | |
| " return items\n", | |
| " if subset_size == 1:\n", | |
| " return random.choice(items)\n", | |
| " return random.sample(items, subset_size)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "45e663d8", | |
| "metadata": {}, | |
| "source": [ | |
| "# Select a random subset of the products and design requirements" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "id": "e0403596", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "desired_subset_size = 2\n", | |
| "\n", | |
| "# Select the subsets\n", | |
| "selected_product_categories = select_subset(product_categories, desired_subset_size)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "89e1770c", | |
| "metadata": {}, | |
| "source": [ | |
| "## Create a prompt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "id": "5349be6c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import logging\n", | |
| "\n", | |
| "def build_description_prompt(building_block, deployment_model, products, feature_category):\n", | |
| "\n", | |
| "\n", | |
| " prompt = f\"\"\"\n", | |
| " You are an expert product evaluator. You will receive the name of an AI building block, a deployment model, and a feature category. \n", | |
| " Your task is to describe how the listed products address the feature category in the context of the AI building block when deployed using the given deployment model and provide a structured JSON response.\n", | |
| "\n", | |
| " You will find details about the product's capabilities on the internet in general, and f5.com in particular.\n", | |
| "\n", | |
| " The seven AI building blocks are:\n", | |
| " 1. Inference - The AI solution is capable of making predictions or decisions based on input data. It can analyze patterns, recognize trends, and provide insights to support decision-making processes.\n", | |
| " 2. Inference with RAG - The AI solution can perform inference while also utilizing retrieval-augmented generation (RAG) techniques. This allows the system to access external knowledge sources, enhancing its ability to provide accurate and contextually relevant responses.\n", | |
| " 3. Agentic External Services Integration - The AI solution can integrate with external services, enabling it to leverage additional capabilities and data sources. This integration allows for more comprehensive and effective solutions.\n", | |
| " 4. RAG Corpus Management - The AI solution includes features for managing the corpus used in retrieval-augmented generation (RAG) processes. This ensures that the system can efficiently access and utilize relevant information from external sources.\n", | |
| " 5. Model Fine-Tuning - The AI solution supports the fine-tuning of models, allowing for customization and optimization based on specific use cases or datasets. This enhances the performance and relevance of the AI system.\n", | |
| " 6. Model Training - The AI solution is capable of training models from scratch or on new datasets. This allows for the development of tailored AI solutions that meet specific requirements and objectives.\n", | |
| " 7. Development - The AI solution provides development capabilities, enabling users to create, modify, and enhance AI models and applications. This includes tools for coding, testing, and deploying AI solutions.\n", | |
| " \n", | |
| " The four deployment models are:\n", | |
| " 1. SaaS - The AI solution is provided as a fully managed service by a third-party provider. Customers can access and use the AI capabilities over the internet without worrying about the underlying infrastructure, maintenance, or updates, making it a convenient and scalable option.\n", | |
| " 2. Cloud-Hosted - The AI solution runs on cloud infrastructure provided by cloud service providers such as AWS, Google Cloud, or Azure. It offers flexibility, scalability, and ease of integration with other cloud services, while the customer maintains control over the configuration and management of their AI systems.\n", | |
| " 3. Self-Hosted - The AI solution is deployed on the customer's own infrastructure, such as on-premises servers or private data centers. This provides maximum control and customization options but requires significant resources for setup, maintenance, and management of the hardware and software components.\n", | |
| " 4. Edge-Hosted - The AI solution in an edge environment, outside traditional cloud or data center infrastructure. An example is a machine learning solution operating on a device like a kiosk in a retail storefront. This model reduces latency, enhances privacy, and ensures real-time processing by bringing the computation closer to the data source or end-user.\n", | |
| "\n", | |
| " Your response must be formatted as a JSON object with the following fields:\n", | |
| "\n", | |
| " \"building_block\": the name of the AI building block.\n", | |
| " \"deployment_model\": the deployment model for which the description is made.\n", | |
| " \"feature_category\": The name of the feature category.\n", | |
| " \"description\": the description of how the listed products address the feature category in the context of the AI building block when deployed using the given deployment model.\n", | |
| " \"summary_description\": a summary of the description provided.\n", | |
| " \"terse_description\": a terse summary of the description provided, focusing on the problem statement.\n", | |
| "\n", | |
| " The description should clearly explain:\n", | |
| "\n", | |
| " The purpose of addresssing the feature category in the context of the building block.\n", | |
| " Any nuances and special circumstances that arise when addressing the feature category for the building block using the deployment model.\n", | |
| " The applicability of the product or products to addressing the purpose described.\n", | |
| " A concluding statement reinforcing the description.\n", | |
| "\n", | |
| " Now provide the description for the following scenario:\n", | |
| " Building Block: {building_block}\n", | |
| " Deployment Model: {deployment_model}\n", | |
| " Feature Category: {feature_category}\n", | |
| " Products: {products}\n", | |
| "\n", | |
| " Respond only with the JSON object. Do not include any additional text or explanations.\n", | |
| " \"\"\"\n", | |
| " return prompt" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 18, | |
| "id": "e1b69c8c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def write_responses_to_file(responses, product_name, timestamp):\n", | |
| " product_name_clean = product_name.lower().replace(\" \", \"\").replace(\",\", \"\")\n", | |
| " directory = f\"assessments\"\n", | |
| " os.makedirs(directory, exist_ok=True)\n", | |
| " output_filename = f\"{directory}/responses_{timestamp}_{product_name_clean}.json\"\n", | |
| " with open(output_filename, 'w', encoding='utf-8') as output_file:\n", | |
| " output_file.write(json.dumps(responses))\n", | |
| " logging.info(f\"Responses have been written to {output_filename}\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "ca5e8b20", | |
| "metadata": {}, | |
| "source": [ | |
| "## Do the things" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 19, | |
| "id": "77bcdc7a", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from datetime import datetime\n", | |
| "\n", | |
| "timestamp = datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n", | |
| "\n", | |
| "responses = []\n", | |
| "errors = []\n", | |
| "\n", | |
| "for product_category in selected_product_categories:\n", | |
| " try:\n", | |
| " building_block = product_category['buildingblock']\n", | |
| " deployment_model = product_category['deploymentmodel']\n", | |
| " products = product_category['products']\n", | |
| " feature_category = product_category['category']\n", | |
| " description_prompt = build_description_prompt(building_block, deployment_model, products, feature_category)\n", | |
| " gpt_response = query_internal_llm(description_prompt,os.getenv('F5_OPENAI_API_ENDPOINT'),os.getenv('F5_OPENAI_API_KEY')).strip() \n", | |
| " if gpt_response.startswith(\"```json\") and gpt_response.endswith(\"```\"):\n", | |
| " gpt_response = gpt_response[7:-3].strip() \n", | |
| " responses.append(json.loads(gpt_response))\n", | |
| " except Exception as e:\n", | |
| " error_message = f\"Error processing product category {product_category}: {str(e)}\"\n", | |
| " errors.append(error_message)\n", | |
| " print(error_message)\n", | |
| " continue\n", | |
| "\n", | |
| "# Write the responses to a file\n", | |
| "write_responses_to_file(responses, \"product_category\", timestamp)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "base", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.11.7" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment