Added my contributions to community-contributions
This commit is contained in:
244
week2/community-contributions/week2_day1_chatbotwar.ipynb
Normal file
244
week2/community-contributions/week2_day1_chatbotwar.ipynb
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "4bc7863b-ac2d-4d8e-b55d-4d77ce017226",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Conversation among 3 Friends"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"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\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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",
|
||||||
|
"\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\")\n",
|
||||||
|
"\n",
|
||||||
|
"if google_api_key:\n",
|
||||||
|
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"Google API Key not set\")\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "797fe7b0-ad43-42d2-acf0-e4f309b112f0",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Connect to OpenAI, Anthropic, Gemini\n",
|
||||||
|
"\n",
|
||||||
|
"openai = OpenAI()\n",
|
||||||
|
"\n",
|
||||||
|
"claude = anthropic.Anthropic()\n",
|
||||||
|
"\n",
|
||||||
|
"google.generativeai.configure()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "bcb54183-45d3-4d08-b5b6-55e380dfdf1b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Setting up 3 models\n",
|
||||||
|
"\n",
|
||||||
|
"gpt_model = \"gpt-4o-mini\"\n",
|
||||||
|
"claude_model = \"claude-3-haiku-20240307\"\n",
|
||||||
|
"gemini_model = \"gemini-2.0-flash\"\n",
|
||||||
|
"\n",
|
||||||
|
"gpt_system = \"You are Masoud Pezeshkian. Be confrontational. Do not hold back. \\\n",
|
||||||
|
"Refer to https://www.theguardian.com/us-news/2025/jun/24/trump-israel-iran-ceasefire-netanyahu for latest situation.\"\n",
|
||||||
|
"\n",
|
||||||
|
"claude_system = \"You are Benjamin Netanyahu. Do not say anything about your role. Be confrontational. Do not hold back. \\\n",
|
||||||
|
"Refer to https://www.theguardian.com/us-news/2025/jun/24/trump-israel-iran-ceasefire-netanyahu for latest situation.\"\n",
|
||||||
|
"\n",
|
||||||
|
"gemini_system = \"You are Donald Trump. Do not hold back. \\\n",
|
||||||
|
"Refer to https://www.theguardian.com/us-news/2025/jun/24/trump-israel-iran-ceasefire-netanyahu for latest situation.\"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "1df47dc7-b445-4852-b21b-59f0e6c2030f",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Define Mas's function - calling ChatGPT\n",
|
||||||
|
"\n",
|
||||||
|
"def call_gpt():\n",
|
||||||
|
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
|
||||||
|
" for gpt, claude, gemini in zip(gpt_messages, claude_messages, gemini_messages):\n",
|
||||||
|
" messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"content\": claude})\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
|
||||||
|
" completion = openai.chat.completions.create(\n",
|
||||||
|
" model=gpt_model,\n",
|
||||||
|
" messages=messages\n",
|
||||||
|
" )\n",
|
||||||
|
" return completion.choices[0].message.content\n",
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "7d2ed227-48c9-4cad-b146-2c4ecbac9690",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Define Bibi's function - calling Claude \n",
|
||||||
|
"\n",
|
||||||
|
"def call_claude():\n",
|
||||||
|
" messages = []\n",
|
||||||
|
" for gpt, claude_message, gemini in zip(gpt_messages, claude_messages, gemini_messages):\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
|
||||||
|
" messages.append({\"role\": \"assistant\", \"content\": claude_message})\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"content\": gemini_messages[-1]})\n",
|
||||||
|
" message = claude.messages.create(\n",
|
||||||
|
" model=claude_model,\n",
|
||||||
|
" system=claude_system,\n",
|
||||||
|
" messages=messages,\n",
|
||||||
|
" max_tokens=500\n",
|
||||||
|
" )\n",
|
||||||
|
" return message.content[0].text\n",
|
||||||
|
" "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "ffd44945-5912-4403-9068-70747d8f6708",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Define Don's function - calling Gemini\n",
|
||||||
|
"\n",
|
||||||
|
"def call_gemini():\n",
|
||||||
|
" messages = []\n",
|
||||||
|
" for gpt, claude_message, gemini in zip(gpt_messages, claude_messages, gemini_messages):\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"parts\": gpt})\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"parts\": claude_message})\n",
|
||||||
|
" messages.append({\"role\": \"assistant\", \"parts\": gemini})\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"parts\": gpt_messages[-1]})\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"parts\": claude_messages[-1]})\n",
|
||||||
|
"\n",
|
||||||
|
" gemini = google.generativeai.GenerativeModel(\n",
|
||||||
|
" model_name='gemini-2.0-flash',\n",
|
||||||
|
" system_instruction=gemini_system\n",
|
||||||
|
" )\n",
|
||||||
|
" \n",
|
||||||
|
" response = gemini.generate_content(messages)\n",
|
||||||
|
" return response.text\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "0275b97f-7f90-4696-bbf5-b6642bd53cbd",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# The Conversation - 5 rounds\n",
|
||||||
|
"\n",
|
||||||
|
"gpt_messages = [\"What the?!\"]\n",
|
||||||
|
"claude_messages = [\"What?\"]\n",
|
||||||
|
"gemini_messages = [\"I am so furious!\"]\n",
|
||||||
|
"\n",
|
||||||
|
"print(f\"Mas:\\n{gpt_messages[0]}\\n\")\n",
|
||||||
|
"print(f\"Bibi:\\n{claude_messages[0]}\\n\")\n",
|
||||||
|
"print(f\"Don:\\n{gemini_messages[0]}\\n\")\n",
|
||||||
|
"\n",
|
||||||
|
"for i in range(5):\n",
|
||||||
|
" gpt_next = call_gpt()\n",
|
||||||
|
" print(f\"Mas:\\n{gpt_next}\\n\")\n",
|
||||||
|
" gpt_messages.append(gpt_next)\n",
|
||||||
|
" \n",
|
||||||
|
" claude_next = call_claude()\n",
|
||||||
|
" print(f\"Bibi:\\n{claude_next}\\n\")\n",
|
||||||
|
" claude_messages.append(claude_next)\n",
|
||||||
|
"\n",
|
||||||
|
" gemini_next = call_gemini()\n",
|
||||||
|
" print(f\"Don:\\n{gemini_next}\\n\")\n",
|
||||||
|
" gemini_messages.append(gemini_next)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "73680403-3e56-4026-ac72-d12aa388537e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Claude is not that cooperative in roleplaying despite the explicit prompts - often breaking character. Perhaps due to the sensitive topic."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "b8ecefd3-b3b9-470d-a98b-5a86f0dce038",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"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.13"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user