Datasets:

Modalities:
Image
Languages:
English
Size:
< 1K
Libraries:
Datasets
License:
File size: 6,916 Bytes
c872938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1d983d23",
   "metadata": {},
   "source": [
    "# This is a tutorial on how to use IOAI's provided LLM proxy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5033564",
   "metadata": {},
   "source": [
    "## Step 1: initialize your OpenAI client\n",
    "\n",
    "Please use your provided api key here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "699ae2b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import AsyncClient\n",
    "\n",
    "BASE_URL = \"https://ioai-llm-proxy.up.railway.app/prox/v1\"\n",
    "API_KEY = \"<YOUR_IOAI_API_KEY>\"\n",
    "\n",
    "openai_client = AsyncClient(\n",
    "    base_url=BASE_URL,\n",
    "    api_key=API_KEY,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f2b8e77",
   "metadata": {},
   "source": [
    "## Step 2: Generate output\n",
    "\n",
    "Note that we have an allowlist on model ids:\n",
    "\n",
    "- `openai/gpt-4.1`\n",
    "- `openai/gpt-4.1-mini`\n",
    "- `openai/gpt-4.1-nano`\n",
    "- `openai/gpt-4o`\n",
    "- `openai/gpt-4o-mini`\n",
    "- `gpt-4o-mini`\n",
    "- `gpt-4o`\n",
    "- `gpt-4.1`\n",
    "- `gpt-4.1-mini`\n",
    "- `gpt-4.1-nano`\n",
    "- `google/gemini-2.5-pro`\n",
    "- `google/gemini-2.5-flash`\n",
    "- `moonshotai/kimi-k2`\n",
    "- `qwen/qwen3-235b-a22b-07-25`\n",
    "- `anthropic/claude-sonnet-4`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96895459",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = await openai_client.chat.completions.create(\n",
    "    model=\"openai/gpt-4o-mini\",\n",
    "    messages=[{\"role\": \"user\", \"content\": \"Hello, world!\"}],\n",
    ")\n",
    "\n",
    "print(response)\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c0b2e3e",
   "metadata": {},
   "source": [
    "## Advanced: Structured Outputs\n",
    "\n",
    "You can use structured outputs to generate objects that fit a specific structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f67373b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydantic import BaseModel\n",
    "from rich import print as rprint\n",
    "\n",
    "class CalendarItem(BaseModel):\n",
    "    title: str\n",
    "    month: int\n",
    "    date: int\n",
    "    year: int\n",
    "    description: str\n",
    "\n",
    "result = await openai_client.beta.chat.completions.parse(\n",
    "    model=\"openai/gpt-4o-mini\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": \"You are a helpful assistant that generates calendar items.\"},\n",
    "        {\"role\": \"user\", \"content\": \"IOAI opening ceremony at Aug. 1, 2025\"}\n",
    "    ],\n",
    "    response_format=CalendarItem,\n",
    ")\n",
    "\n",
    "print(result)\n",
    "rprint(result.choices[0].message.parsed)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1d6dafa",
   "metadata": {},
   "source": [
    "## Batch requests with retry\n",
    "\n",
    "To generate large amounts of LLM completions efficiently and rhobustly, we can leverage:\n",
    "\n",
    "- Concurrent requests capped by an async Semaphore -- so that we can make multiple requests at the same time without overwhelming our bandwidth\n",
    "- Exponential backoff based retry for each request -- so that we gracefully retry when unexpected network / provider errors happen\n",
    "\n",
    "The below is an example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f591a91f",
   "metadata": {},
   "outputs": [],
   "source": [
    "class RankingExtraction(BaseModel):\n",
    "    ranking: int\n",
    "    contest: str\n",
    "\n",
    "texts = [f\"We ranked {i}th in the IOAI 2025\" for i in range(1, 101)]\n",
    "len(texts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80f7dafa",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "from tenacity import retry, stop_after_attempt, wait_exponential\n",
    "from asyncio import Semaphore\n",
    "from tqdm.notebook import tqdm\n",
    "from tqdm.asyncio import tqdm as tqdm_asyncio\n",
    "\n",
    "@retry(\n",
    "    stop=stop_after_attempt(3),\n",
    "    wait=wait_exponential(multiplier=1, min=4, max=10),\n",
    ")\n",
    "async def process_one(txt: str) -> RankingExtraction:\n",
    "    result = await openai_client.beta.chat.completions.parse(\n",
    "        model=\"openai/gpt-4o-mini\",\n",
    "        messages=[{\"role\": \"user\", \"content\": txt}],\n",
    "        response_format=RankingExtraction,\n",
    "    )\n",
    "    return result.choices[0].message.parsed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13380165",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(texts[0])\n",
    "print(await process_one(texts[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be960117",
   "metadata": {},
   "outputs": [],
   "source": [
    "async def process_all(texts: List[str]):\n",
    "    semaphore = Semaphore(50) # we limit to making 50 requests concurrently\n",
    "    async def _process_with_sema(txt: str):\n",
    "        async with semaphore:\n",
    "            return await process_one(txt)\n",
    "    return await tqdm_asyncio.gather(\n",
    "        *[_process_with_sema(txt) for txt in texts],\n",
    "        desc=\"Processing\",\n",
    "        total=len(texts),\n",
    "    )\n",
    "\n",
    "results = await process_all(texts)\n",
    "\n",
    "results[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80bee95e",
   "metadata": {},
   "source": [
    "# Check your credits\n",
    "\n",
    "You get $10 of credits, so use it economically! You can run the following cell to check the amount of credits you have used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81d2e662",
   "metadata": {},
   "outputs": [],
   "source": [
    "from httpx import get\n",
    "\n",
    "result = get(f\"https://ioai-llm-proxy.up.railway.app/credits/{API_KEY}\")\n",
    "\n",
    "resp = result.json()\n",
    "\n",
    "print(f\"\"\"\n",
    "Credits limit: ${resp['limit']}\n",
    "Used: ${resp['usage']}\n",
    "Credits remaining: ${resp['limit'] - resp['usage']}\n",
    "\"\"\")"
   ]
  }
 ],
 "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}