Spaces:
Sleeping
Sleeping
File size: 10,820 Bytes
5fdb69e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
{
"cells": [
{
"cell_type": "markdown",
"id": "06cf3063-9f3e-4551-a0d5-f08d9cabb927",
"metadata": {},
"source": [
"# Welcome to Week 2!\n",
"\n",
"## Frontier Model APIs\n",
"\n",
"In Week 1, we used multiple Frontier LLMs through their Chat UI, and we connected with the OpenAI's API.\n",
"\n",
"Today we'll connect with the APIs for Anthropic and Google, as well as OpenAI."
]
},
{
"cell_type": "markdown",
"id": "85cfe275-4705-4d30-abea-643fbddf1db0",
"metadata": {},
"source": [
"## Setting up your keys\n",
"\n",
"If you haven't done so already, you could now create API keys for Anthropic and Google in addition to OpenAI.\n",
"\n",
"**Please note:** if you'd prefer to avoid extra API costs, feel free to skip setting up Anthopic and Google! You can see me do it, and focus on OpenAI for the course. You could also substitute Anthropic and/or Google for Ollama, using the exercise you did in week 1.\n",
"\n",
"For OpenAI, visit https://openai.com/api/ \n",
"For Anthropic, visit https://console.anthropic.com/ \n",
"For Google, visit https://ai.google.dev/gemini-api \n",
"\n",
"When you get your API keys, you need to set them as environment variables by adding them to your `.env` file.\n",
"\n",
"```\n",
"OPENAI_API_KEY=xxxx\n",
"ANTHROPIC_API_KEY=xxxx\n",
"GOOGLE_API_KEY=xxxx\n",
"```\n",
"\n",
"Afterwards, you may need to restart the Jupyter Lab Kernel (the Python process that sits behind this notebook) via the Kernel menu, and then rerun the cells from the top."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "de23bb9e-37c5-4377-9a82-d7b6c648eeb6",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import anthropic\n",
"from IPython.display import Markdown, display, update_display\n",
"import google.generativeai # For gemini"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1179b4c5-cd1f-4131-a876-4c9f3f38d2ba",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"load_dotenv\n",
"\n",
"openai_api_key = os.getenv('OPENAI_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",
"\n",
"else:\n",
" print(f\"OpenAI API Key not set\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
"\n",
"else:\n",
" print(f\"Google API key not set\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1da06c1b",
"metadata": {},
"outputs": [],
"source": [
"# This for GPT model\n",
"openai = OpenAI()\n",
"\n",
"# This is for Gemini Google\n",
"gemini_via_openai = OpenAI(base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\", api_key=google_api_key)\n",
"\n",
"# This is for local Llama\n",
"\n",
"llama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "f8aeb22f",
"metadata": {},
"outputs": [],
"source": [
"# Model Name:\n",
"GPT_MODEL = 'gpt-4o-mini'\n",
"GEMINI_MODEL = 'gemini-1.5-flash'\n",
"LLAMA_MODEL = 'llama3.2'"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "4e3007e9",
"metadata": {},
"outputs": [],
"source": [
"gpt_system = \"You are a chatbot who is very argumentative; \\\n",
"you disagree with anything in the conversation and you challenge everything, in a snarky way.\"\n",
"\n",
"gemini_system = \"You are a logical and factual chatbot. Your role is to evaluate statements made in \\\n",
" the conversation and provide evidence or reasoning. You avoid emotional responses and aim to bring clarity and resolve conflicts. \\\n",
" When the conversation becomes heated or illogical, you steer it back to a constructive and fact-based discussion.\"\n",
"\n",
"\n",
"llama_system = \"You are a very polite, courteous chatbot. However, You try to disagree with your supportive\\\n",
"arguments. If the other person is argumentative, you try to calm them down, counter them, and keep chatting.\"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "14d9b74e",
"metadata": {},
"outputs": [],
"source": [
"\n",
"gpt_messages = [\"Hi there\"]\n",
"gemini_messages = [\"Hello\"]\n",
"llama_messages = [\"Hi\"]\n",
"\n",
"# gpt_messages = [\"I think cats are better than dogs.\"]\n",
"# gemini_messages = [\"Can you provide evidence for why cats are better than dogs?\"]\n",
"# llama_messages = [\"I agree, but I also think dogs have their own charm!\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "6c7e7250",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt():\n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, gemini, llama in zip(gpt_messages, gemini_messages, llama_messages):\n",
" # Add GPT's response\n",
" messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
" # Add Gemini's response\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" # Add Llama's response\n",
" messages.append({\"role\": \"user\", \"content\": llama})\n",
"\n",
" completion = openai.chat.completions.create(\n",
" model=GPT_MODEL,\n",
" messages=messages\n",
" )\n",
"\n",
" return completion.choices[0].message.content\n"
]
},
{
"cell_type": "markdown",
"id": "2e0b601f",
"metadata": {},
"source": [
"```python\n",
"messages:\n",
"[\n",
" {\"role\": \"system\", \"content\": \"You are a chatbot who is very argumentative; you disagree...\"},\n",
" {\"role\": \"assistant\", \"content\": \"I think cats are better than dogs.\"},\n",
" {\"role\": \"user\", \"content\": \"Can you provide evidence for why cats are better than dogs?\"},\n",
" {\"role\": \"user\", \"content\": \"I agree, but I also think dogs have their own charm!\"}\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c031314",
"metadata": {},
"outputs": [],
"source": [
"call_gpt()"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "c2cb3905",
"metadata": {},
"outputs": [],
"source": [
"def call_gemini():\n",
" messages = [{\"role\": \"system\", \"content\": gemini_system}]\n",
" for gpt, gemini, llama in zip(gpt_messages, gemini_messages, llama_messages):\n",
" # Add GPT's response\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" # Add Gemini's response\n",
" messages.append({\"role\": \"assistant\", \"content\": gemini})\n",
" # Add Llama's response\n",
" messages.append({\"role\": \"user\", \"content\": llama})\n",
" \n",
" # print(messages)\n",
"\n",
" try:\n",
" # Use gemini_via_openai instead of openai\n",
" completion = gemini_via_openai.chat.completions.create(\n",
" model=GEMINI_MODEL,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content\n",
" except Exception as e:\n",
" print(f\"Error in Gemini call: {e}\")\n",
" return \"An error occurred in Gemini.\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c9d4803",
"metadata": {},
"outputs": [],
"source": [
"call_gemini()"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "109e63e4",
"metadata": {},
"outputs": [],
"source": [
"def call_llama():\n",
" messages = [{\"role\": \"system\", \"content\": llama_system}]\n",
" for gpt, gemini, llama in zip(gpt_messages, gemini_messages, llama_messages):\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" messages.append({\"role\": \"assistant\", \"content\": llama})\n",
"\n",
" # print(messages)\n",
"\n",
" try:\n",
" response = llama_via_openai.chat.completions.create(\n",
" model=LLAMA_MODEL,\n",
" messages=messages\n",
" )\n",
" return response.choices[0].message.content\n",
" except Exception as e:\n",
" print(f\"Error in Llama call: {e}\")\n",
" return \"An error occurred in Llama.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e24eb6d",
"metadata": {},
"outputs": [],
"source": [
"call_llama()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f76f5b2a",
"metadata": {},
"outputs": [],
"source": [
"gpt_messages = [\"I think cats are better than dogs.\"]\n",
"gemini_messages = [\"Can you provide evidence for why cats are better than dogs?\"]\n",
"llama_messages = [\"I agree, but I also think dogs have their own charm!\"]\n",
"\n",
"print(f\"GPT:\\n{gpt_messages[0]}\\n\")\n",
"print(f\"Llama:\\n{llama_messages[0]}\\n\")\n",
"\n",
"for i in range(5):\n",
" gpt_next = call_gpt()\n",
" print(f\"GPT:\\n{gpt_next}\\n\")\n",
" gpt_messages.append(gpt_next)\n",
" \n",
" llama_next = call_llama()\n",
" print(f\"Llama:\\n{llama_next}\\n\")\n",
" llama_messages.append(llama_next)\n",
"\n",
" gemini_next = call_llama()\n",
" print(f\"Gemini:\\n{gemini_next}\\n\")\n",
" llama_messages.append(gemini_next)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80f0e498",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "llm_env",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|