Skip to content

Commit 445f483

Browse files
authored
[magics] Add --api-base and --api-key-name arguments (#1471)
* [magics] Enable passing API base url and key for proxy service access in magicsfor * Update magics.py * updated api-key-name
1 parent 79b2be8 commit 445f483

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

packages/jupyter-ai-magics/jupyter_ai_magics/magics.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import click
1111
import litellm
1212
import traitlets
13+
from typing import Optional
1314
from IPython.core.magic import Magics, line_cell_magic, magics_class
1415
from IPython.display import HTML, JSON, Markdown, Math
1516
from jupyter_ai.model_providers.model_list import CHAT_MODELS
@@ -314,10 +315,29 @@ def run_ai_cell(self, args: CellArgs, prompt: str):
314315
print(error_msg, file=sys.stderr) # Log to stderr
315316
return
316317
try:
318+
# Prepare litellm completion arguments
319+
completion_args = {
320+
"model": model_id,
321+
"messages": messages,
322+
"stream": False
323+
}
324+
325+
# Add api_base if provided
326+
if args.api_base:
327+
completion_args["api_base"] = args.api_base
328+
329+
# Add API key from .env if api_key_name is provided
330+
if args.api_key_name:
331+
# Retrieve the actual API key from the .env file
332+
api_key_name_value = os.getenv(args.api_key_name)
333+
if not api_key_name_value:
334+
error_msg = f"API key '{args.api_key_name}' not found in .env file."
335+
print(error_msg, file=sys.stderr)
336+
return
337+
completion_args["api_key_name"] = api_key_name_value
338+
317339
# Call litellm completion
318-
response = litellm.completion(
319-
model=model_id, messages=messages, stream=False
320-
)
340+
response = litellm.completion(**completion_args)
321341

322342
# Extract output text from response
323343
output = response.choices[0].message.content

packages/jupyter-ai-magics/jupyter_ai_magics/parsers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class CellArgs(BaseModel):
5151
region_name: Optional[str] = None
5252
request_schema: Optional[str] = None
5353
response_path: Optional[str] = None
54+
# Parameters for custom API endpoints
55+
api_base: Optional[str] = None
56+
api_key_name: Optional[str] = None
5457

5558

5659
# Should match CellArgs
@@ -63,6 +66,9 @@ class FixArgs(BaseModel):
6366
region_name: Optional[str] = None
6467
request_schema: Optional[str] = None
6568
response_path: Optional[str] = None
69+
# Parameters for custom API endpoints
70+
api_base: Optional[str] = None
71+
api_key_name: Optional[str] = None
6672

6773

6874
class HelpArgs(BaseModel):
@@ -133,6 +139,16 @@ def verify_json_value(ctx, param, value):
133139
default="markdown",
134140
help=FORMAT_HELP,
135141
)
142+
@click.option(
143+
"--api-base",
144+
required=False,
145+
help="Base URL for the API endpoint.",
146+
)
147+
@click.option(
148+
"--api-key-name",
149+
required=False,
150+
help="Name of the environment variable containing the API key.",
151+
)
136152
@click.option(
137153
REGION_NAME_SHORT_OPTION,
138154
REGION_NAME_LONG_OPTION,
@@ -168,6 +184,11 @@ def cell_magic_parser(context: click.Context, **kwargs):
168184
model IDs (with the provider ID explicitly prefixed, followed by a colon)
169185
are accepted.
170186
187+
Optional parameters:
188+
--api-base: Base URL for the API endpoint
189+
--api-key-name: Name of the environment variable containing the API key
190+
(the actual key should be stored in the .env file)
191+
171192
To view available language models, please run `%ai list`.
172193
"""
173194
if not kwargs["model_id"] and context.default_map:
@@ -191,6 +212,16 @@ def line_magic_parser():
191212
default="markdown",
192213
help=FORMAT_HELP,
193214
)
215+
@click.option(
216+
"--api-base",
217+
required=False,
218+
help="Base URL for the API endpoint.",
219+
)
220+
@click.option(
221+
"--api-key-name",
222+
required=False,
223+
help="Name of the environment variable containing the API key.",
224+
)
194225
@click.option(
195226
REGION_NAME_SHORT_OPTION,
196227
REGION_NAME_LONG_OPTION,

0 commit comments

Comments
 (0)