File size: 10,591 Bytes
0af0679 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Start with imports - ask ChatGPT to explain any package that you don't know\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from anthropic import Anthropic\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Load and check your API keys</b>\n",
"</br>\n",
"<b>- - - - - - - - - - - - - - - -</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Always remember to do this!\n",
"load_dotenv(override=True)\n",
"\n",
"# Function to check and display API key status\n",
"def check_api_key(key_name):\n",
" key = os.getenv(key_name)\n",
" \n",
" if key:\n",
" # Always show the first 7 characters of the key\n",
" print(f\"✓ {key_name} API Key exists and begins... ({key[:7]})\")\n",
" return True\n",
" else:\n",
" print(f\"⚠️ {key_name} API Key not set\")\n",
" return False\n",
"\n",
"# Check each API key (the function now returns True or False)\n",
"has_openai = check_api_key('OPENAI_API_KEY')\n",
"has_anthropic = check_api_key('ANTHROPIC_API_KEY')\n",
"has_google = check_api_key('GOOGLE_API_KEY')\n",
"has_deepseek = check_api_key('DEEPSEEK_API_KEY')\n",
"has_groq = check_api_key('GROQ_API_KEY')"
]
},
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "html"
}
},
"source": [
"<b>Input for travel planner</b></br>\n",
"Describe yourself, your travel companions, and the destination you plan to visit.\n",
"</br>\n",
"<b>- - - - - - - - - - - - - - - -</b>"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Provide a description of you or your family. Age, interests, etc.\n",
"person_description = \"family with a 3 year-old\"\n",
"# Provide the name of the specific destination or attraction and country\n",
"destination = \"Belgium, Brussels\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>- - - - - - - - - - - - - - - -</b>"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"Given the following description of a person or family:\n",
"{person_description}\n",
"\n",
"And the requested travel destination or attraction:\n",
"{destination}\n",
"\n",
"Provide a concise response including:\n",
"\n",
"1. Fit rating (1-10) specifically for this person or family.\n",
"2. One compelling positive reason why this destination suits them.\n",
"3. One notable drawback they should consider before visiting.\n",
"4. One important additional aspect to consider related to this location.\n",
"5. Suggest a few additional places that might also be of interest to them that are very close to the destination.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def run_prompt_on_available_models(prompt):\n",
" \"\"\"\n",
" Run a prompt on all available AI models based on API keys.\n",
" Continues processing even if some models fail.\n",
" \"\"\"\n",
" results = {}\n",
" api_response = [{\"role\": \"user\", \"content\": prompt}]\n",
" \n",
" # OpenAI\n",
" if check_api_key('OPENAI_API_KEY'):\n",
" try:\n",
" model_name = \"gpt-4o-mini\"\n",
" openai_client = OpenAI()\n",
" response = openai_client.chat.completions.create(model=model_name, messages=api_response)\n",
" results[model_name] = response.choices[0].message.content\n",
" print(f\"✓ Got response from {model_name}\")\n",
" except Exception as e:\n",
" print(f\"⚠️ Error with {model_name}: {str(e)}\")\n",
" # Continue with other models\n",
" \n",
" # Anthropic\n",
" if check_api_key('ANTHROPIC_API_KEY'):\n",
" try:\n",
" model_name = \"claude-3-7-sonnet-latest\"\n",
" # Create new client each time\n",
" claude = Anthropic()\n",
" \n",
" # Use messages directly \n",
" response = claude.messages.create(\n",
" model=model_name,\n",
" messages=[{\"role\": \"user\", \"content\": prompt}],\n",
" max_tokens=1000\n",
" )\n",
" results[model_name] = response.content[0].text\n",
" print(f\"✓ Got response from {model_name}\")\n",
" except Exception as e:\n",
" print(f\"⚠️ Error with {model_name}: {str(e)}\")\n",
" # Continue with other models\n",
" \n",
" # Google\n",
" if check_api_key('GOOGLE_API_KEY'):\n",
" try:\n",
" model_name = \"gemini-2.0-flash\"\n",
" google_api_key = os.getenv('GOOGLE_API_KEY')\n",
" gemini = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
" response = gemini.chat.completions.create(model=model_name, messages=api_response)\n",
" results[model_name] = response.choices[0].message.content\n",
" print(f\"✓ Got response from {model_name}\")\n",
" except Exception as e:\n",
" print(f\"⚠️ Error with {model_name}: {str(e)}\")\n",
" # Continue with other models\n",
" \n",
" # DeepSeek\n",
" if check_api_key('DEEPSEEK_API_KEY'):\n",
" try:\n",
" model_name = \"deepseek-chat\"\n",
" deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
" deepseek = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com/v1\")\n",
" response = deepseek.chat.completions.create(model=model_name, messages=api_response)\n",
" results[model_name] = response.choices[0].message.content\n",
" print(f\"✓ Got response from {model_name}\")\n",
" except Exception as e:\n",
" print(f\"⚠️ Error with {model_name}: {str(e)}\")\n",
" # Continue with other models\n",
" \n",
" # Groq\n",
" if check_api_key('GROQ_API_KEY'):\n",
" try:\n",
" model_name = \"llama-3.3-70b-versatile\"\n",
" groq_api_key = os.getenv('GROQ_API_KEY')\n",
" groq = OpenAI(api_key=groq_api_key, base_url=\"https://api.groq.com/openai/v1\")\n",
" response = groq.chat.completions.create(model=model_name, messages=api_response)\n",
" results[model_name] = response.choices[0].message.content\n",
" print(f\"✓ Got response from {model_name}\")\n",
" except Exception as e:\n",
" print(f\"⚠️ Error with {model_name}: {str(e)}\")\n",
" # Continue with other models\n",
" \n",
" # Check if we got any responses\n",
" if not results:\n",
" print(\"⚠️ No models were able to provide a response\")\n",
" \n",
" return results\n",
"\n",
"# Get responses from all available models\n",
"model_responses = run_prompt_on_available_models(prompt)\n",
"\n",
"# Display the results\n",
"for model, answer in model_responses.items():\n",
" display(Markdown(f\"## Response from {model}\\n\\n{answer}\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Sythesize answers from all models into one</b>\n",
"</br>\n",
"<b>- - - - - - - - - - - - - - - -</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a synthesis prompt\n",
"synthesis_prompt = f\"\"\"\n",
"Here are the responses from different models:\n",
"\"\"\"\n",
"\n",
"# Add each model's response to the synthesis prompt without mentioning model names\n",
"for index, (model, response) in enumerate(model_responses.items()):\n",
" synthesis_prompt += f\"\\n--- Response {index+1} ---\\n{response}\\n\"\n",
"\n",
"synthesis_prompt += \"\"\"\n",
"Please synthesize these responses into one comprehensive answer that:\n",
"1. Captures the best insights from each response\n",
"2. Resolves any contradictions between responses\n",
"3. Presents a clear and coherent final answer\n",
"4. Maintains the same format as the original responses (numbered list format)\n",
"5.Compiles all additional places mentioned by all models \n",
"\n",
"Your synthesized response:\n",
"\"\"\"\n",
"\n",
"# Create the synthesis\n",
"if check_api_key('OPENAI_API_KEY'):\n",
" try:\n",
" openai_client = OpenAI()\n",
" synthesis_response = openai_client.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=[{\"role\": \"user\", \"content\": synthesis_prompt}]\n",
" )\n",
" synthesized_answer = synthesis_response.choices[0].message.content\n",
" print(\"✓ Successfully synthesized responses with gpt-4o-mini\")\n",
" \n",
" # Display the synthesized answer\n",
" display(Markdown(\"## Synthesized Answer\\n\\n\" + synthesized_answer))\n",
" except Exception as e:\n",
" print(f\"⚠️ Error synthesizing responses with gpt-4o-mini: {str(e)}\")\n",
"else:\n",
" print(\"⚠️ OpenAI API key not available, cannot synthesize responses\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|