|
13 | 13 | "\n", |
14 | 14 | "As the volume of scientific literature continues to grow rapidly, manually extracting and organizing this information becomes increasingly difficult and time-consuming. Therefore, automating data extraction from the literature using AI can help researchers to rapidly identify relevant findings, convert unstructured text into structured datasets, and integrate knowledge across thousands of publications. \n", |
15 | 15 | "\n", |
16 | | - ":::{note}\n", |
17 | | - "To run the code snippets, you can click the **Live Code** button OR the 🚀 icon at the top of the page to launch the page as an interactive Google Colab notebook.\n", |
| 16 | + "::{admonition} 🚀 Getting Started\n", |
| 17 | + ":class: tip\n", |
18 | 18 | "\n", |
19 | | - "Clicking **Live Code** will launch a Binder environment (~1-2 min). \n", |
20 | | - "Your code will run directly in the page once it's ready.\n", |
21 | | - ":::\n", |
| 19 | + "This tutorial can be launched using the rocket button at the top of the page.\n", |
22 | 20 | "\n", |
23 | | - "## 2.2.1 Getting started with LLMs\n", |
| 21 | + "### Option 1 — Google Colab (**recommended**)\n", |
| 22 | + "Opens the notebook in Google Colab with the fastest and most reliable experience.\n", |
| 23 | + "\n", |
| 24 | + "Before running the tutorial, add your API keys using **either**:\n", |
| 25 | + "\n", |
| 26 | + "- a `.env` file, or\n", |
| 27 | + "- **Colab Secrets** (`🔑 Secrets` tab in the left sidebar)\n", |
| 28 | + "\n", |
| 29 | + "Example `.env`:\n", |
| 30 | + "```bash\n", |
| 31 | + "OPENAI_API_KEY=your_key_here\n", |
| 32 | + "ANTHROPIC_API_KEY=your_key_here\n", |
| 33 | + "```\n", |
| 34 | + "\n", |
| 35 | + "### Option 2 — MyBinder\n", |
| 36 | + "Launches a temporary cloud Jupyter environment directly in your browser.\n", |
| 37 | + "\n", |
| 38 | + "⚠️ Binder environments can take a few minutes to build and start.\n", |
| 39 | + "\n", |
| 40 | + "After the notebook loads, create a `.env` file in the notebook directory containing your API keys:\n", |
| 41 | + "\n", |
| 42 | + "```bash\n", |
| 43 | + "OPENAI_API_KEY=your_key_here\n", |
| 44 | + "ANTHROPIC_API_KEY=your_key_here\n", |
| 45 | + "```\n", |
| 46 | + "\n", |
| 47 | + "### Notes\n", |
| 48 | + "- You only need API keys for the providers used in a given notebook.\n", |
| 49 | + "- Never commit or publicly share your API keys.\n", |
| 50 | + "- If a cell fails due to missing credentials, verify that your keys were loaded correctly before rerunning the cell.\n", |
| 51 | + ":::\n" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "id": "79a2787b", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "## 2.2.1 Accessing LLMs through APIs\n", |
24 | 60 | "\n", |
25 | 61 | "You might have used LLMs through chat interfaces such as ChatGPT or Claude before. But to access them through a python code as what we have here, we need to use an API.\n", |
26 | 62 | "\n", |
|
98 | 134 | "source": [ |
99 | 135 | "import os\n", |
100 | 136 | "\n", |
101 | | - "def get_api_key():\n", |
102 | | - " \"\"\"Load API key from Colab secrets, environment variable, or user input.\"\"\"\n", |
| 137 | + "LLM_API_KEYS = {\n", |
| 138 | + " \"openai\": \"OPENAI_API_KEY\",\n", |
| 139 | + " \"anthropic\": \"ANTHROPIC_API_KEY\",\n", |
| 140 | + "}\n", |
| 141 | + "\n", |
| 142 | + "def get_api_key(llm: str = \"openai\") -> str:\n", |
| 143 | + " \"\"\"\n", |
| 144 | + " Load API key for the specified LLM from Colab secrets,\n", |
| 145 | + " environment variable, or user input.\n", |
| 146 | + " \n", |
| 147 | + " Args:\n", |
| 148 | + " llm: LLM provider name. eg: 'openai', 'anthropic'\n", |
103 | 149 | " \n", |
| 150 | + " Returns:\n", |
| 151 | + " API key string\n", |
| 152 | + " \n", |
| 153 | + " Example:\n", |
| 154 | + " api_key = get_api_key(\"anthropic\")\n", |
| 155 | + " \"\"\"\n", |
| 156 | + "\n", |
| 157 | + " llm = llm.lower()\n", |
| 158 | + " if llm not in LLM_API_KEYS:\n", |
| 159 | + " raise ValueError(\n", |
| 160 | + " f\"Unknown LLM '{llm}'. Choose from: {list(LLM_API_KEYS.keys())}\"\n", |
| 161 | + " )\n", |
| 162 | + "\n", |
| 163 | + " env_var = LLM_API_KEYS[llm]\n", |
| 164 | + "\n", |
104 | 165 | " # 1. Try Colab secrets\n", |
105 | 166 | " try:\n", |
106 | 167 | " from google.colab import userdata\n", |
107 | | - " return userdata.get('OPENAI_API_KEY')\n", |
| 168 | + " key = userdata.get(env_var)\n", |
| 169 | + " if key:\n", |
| 170 | + " return key\n", |
108 | 171 | " except ImportError:\n", |
109 | 172 | " pass\n", |
110 | 173 | "\n", |
111 | 174 | " # 2. Try environment variable / .env file\n", |
112 | 175 | " try:\n", |
113 | 176 | " from dotenv import load_dotenv\n", |
114 | 177 | " load_dotenv()\n", |
115 | | - " api_key = os.environ.get('OPENAI_API_KEY')\n", |
116 | | - " if api_key:\n", |
117 | | - " return api_key\n", |
| 178 | + " key = os.environ.get(env_var)\n", |
| 179 | + " if key:\n", |
| 180 | + " return key\n", |
118 | 181 | " except ImportError:\n", |
119 | 182 | " pass\n", |
120 | 183 | "\n", |
121 | | - " # 3. For live code in Binder/Thebe — ask user to input it\n", |
122 | | - " try:\n", |
123 | | - " import ipywidgets as widgets\n", |
124 | | - " from IPython.display import display\n", |
125 | | - "\n", |
126 | | - " key_input = widgets.Password(\n", |
127 | | - " placeholder='Paste your OpenAI API key here',\n", |
128 | | - " description='API Key:',\n", |
129 | | - " layout=widgets.Layout(width='400px')\n", |
130 | | - " )\n", |
131 | | - " submit = widgets.Button(description='Submit', button_style='primary')\n", |
132 | | - " output = widgets.Output()\n", |
133 | | - "\n", |
134 | | - " result = {'key': None}\n", |
135 | | - "\n", |
136 | | - " def on_submit(b):\n", |
137 | | - " result['key'] = key_input.value\n", |
138 | | - " os.environ['OPENAI_API_KEY'] = key_input.value\n", |
139 | | - " with output:\n", |
140 | | - " print(\"✅ API key set successfully!\")\n", |
141 | | - "\n", |
142 | | - " submit.on_click(on_submit)\n", |
143 | | - " display(widgets.VBox([key_input, submit, output]))\n", |
144 | | - "\n", |
145 | | - " # wait for user to submit\n", |
146 | | - " return result\n", |
147 | | - "\n", |
148 | | - " except ImportError:\n", |
149 | | - " raise ValueError(\n", |
150 | | - " \"API key not found. Please set OPENAI_API_KEY:\\n\"\n", |
151 | | - " \" export OPENAI_API_KEY='your-key-here'\"\n", |
152 | | - " )" |
| 184 | + " raise ValueError(\n", |
| 185 | + " f\"API key not found. Please set {env_var}:\\n\"\n", |
| 186 | + " f\" export {env_var}='your-key-here'\\n\"\n", |
| 187 | + " f\" or add it to a .env file\"\n", |
| 188 | + " )" |
153 | 189 | ] |
154 | 190 | }, |
155 | 191 | { |
|
177 | 213 | }, |
178 | 214 | "outputs": [], |
179 | 215 | "source": [ |
180 | | - "\n", |
181 | 216 | "from openai import OpenAI\n", |
182 | 217 | "\n", |
183 | 218 | "# Access the OpenAI API key\n", |
184 | | - "openai_api_key = get_api_key()\n", |
| 219 | + "openai_api_key = get_api_key(\"openai\")\n", |
185 | 220 | "# Tell the OpenAI client to use your API key\n", |
186 | 221 | "client = OpenAI(api_key=openai_api_key)\n", |
187 | 222 | "# LLM model to generate answer. Replace model name if deprecated\n", |
188 | | - "LLM_MODEL = \"gpt-5.5\"\n", |
| 223 | + "LLM_MODEL = \"gpt-4.1-nano\"\n", |
189 | 224 | "# Add your question here\n", |
190 | 225 | "QUESTION = \"What is the difference between Machine Learning and Deep Learning?\" \n", |
191 | 226 | "\n", |
|
0 commit comments