{ "cells": [ { "cell_type": "code", "execution_count": 27, "id": "c44c5494-950d-4d2f-8d4f-b87b57c5b330", "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "import os\n", "import requests\n", "from bs4 import BeautifulSoup\n", "from typing import List\n", "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "import google.generativeai\n", "import anthropic" ] }, { "cell_type": "code", "execution_count": 28, "id": "d1715421-cead-400b-99af-986388a97aff", "metadata": {}, "outputs": [], "source": [ "import gradio as gr # oh yeah!" ] }, { "cell_type": "code", "execution_count": 29, "id": "337d5dfc-0181-4e3b-8ab9-e78e0c3f657b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenAI API Key exists and begins sk-proj-\n", "Anthropic API Key exists and begins sk-ant-\n" ] } ], "source": [ "# Load environment variables in a file called .env\n", "# Print the key prefixes to help with any debugging\n", "\n", "load_dotenv(override=True)\n", "openai_api_key = os.getenv('OPENAI_API_KEY')\n", "anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n", "google_api_key = os.getenv('GOOGLE_API_KEY')\n", "\n", "if openai_api_key:\n", " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n", "else:\n", " print(\"OpenAI API Key not set\")\n", " \n", "if anthropic_api_key:\n", " print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n", "else:\n", " print(\"Anthropic API Key not set\")" ] }, { "cell_type": "code", "execution_count": 30, "id": "22586021-1795-4929-8079-63f5bb4edd4c", "metadata": {}, "outputs": [], "source": [ "# Connect to OpenAI, Anthropic and Google; comment out the Claude or Google lines if you're not using them\n", "\n", "openai = OpenAI()\n", "claude = anthropic.Anthropic()" ] }, { "cell_type": "code", "execution_count": 31, "id": "b16e6021-6dc4-4397-985a-6679d6c8ffd5", "metadata": {}, "outputs": [], "source": [ "# A generic system message - no more snarky adversarial AIs!\n", "system_message = \"You are a helpful assistant\"" ] }, { "cell_type": "code", "execution_count": 32, "id": "02ef9b69-ef31-427d-86d0-b8c799e1c1b1", "metadata": {}, "outputs": [], "source": [ "\n", "def stream_gpt(prompt, model_version):\n", " messages = [\n", " {\"role\": \"system\", \"content\": system_message},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ]\n", " stream = openai.chat.completions.create(\n", " model=model_version,\n", " messages=messages,\n", " stream=True\n", " )\n", " result = \"\"\n", " for chunk in stream:\n", " result += chunk.choices[0].delta.content or \"\"\n", " yield result" ] }, { "cell_type": "code", "execution_count": 33, "id": "41e98d2d-e7d3-4753-8908-185b208b4044", "metadata": {}, "outputs": [], "source": [ "def stream_claude(prompt, model_version):\n", " result = claude.messages.stream(\n", " model=model_version,\n", " max_tokens=1000,\n", " temperature=0.7,\n", " system=system_message,\n", " messages=[\n", " {\"role\": \"user\", \"content\": prompt},\n", " ],\n", " )\n", " response = \"\"\n", " with result as stream:\n", " for text in stream.text_stream:\n", " response += text or \"\"\n", " yield response" ] }, { "cell_type": "code", "execution_count": 34, "id": "5786802b-5ed8-4098-9d80-9bdcf4f7685b", "metadata": {}, "outputs": [], "source": [ "# function using both dropdown values\n", "def stream_model(message, model_family, model_version):\n", " if model_family == 'GPT':\n", " result = stream_gpt(message, model_version)\n", " elif model_family == 'Claude':\n", " result = stream_claude ( message, model_version)\n", " yield from result" ] }, { "cell_type": "code", "execution_count": 35, "id": "0d30be74-149c-41f8-9eef-1628eb31d74d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7891\n", "* To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "