Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InferenceClient for HF Nvidia NIM API #2482

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions src/huggingface_hub/inference/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,51 +814,51 @@ def chat_completion(
# `self.xxx` takes precedence over the method argument only in `chat_completion`
# since `chat_completion(..., model=xxx)` is also a payload parameter for the
# server, we need to handle it differently
model = self.base_url or self.model or model or self.get_recommended_model("text-generation")
is_url = model.startswith(("http://", "https://"))
model_id_or_url = self.base_url or self.model or model or self.get_recommended_model("text-generation")
is_url = model_id_or_url.startswith(("http://", "https://"))

# First, resolve the model chat completions URL
if model == self.base_url:
if model_id_or_url == self.base_url:
# base_url passed => add server route
model_url = model.rstrip("/")
model_url = model_id_or_url.rstrip("/")
if not model_url.endswith("/v1"):
model_url += "/v1"
model_url += "/chat/completions"
elif is_url:
# model is a URL => use it directly
model_url = model
model_url = model_id_or_url
else:
# model is a model ID => resolve it + add server route
model_url = self._resolve_url(model).rstrip("/") + "/v1/chat/completions"
model_url = self._resolve_url(model_id_or_url).rstrip("/") + "/v1/chat/completions"

# `model` is sent in the payload. Not used by the server but can be useful for debugging/routing.
# If it's a ID on the Hub => use it. Otherwise, we use a random string.
model_id = model if not is_url and model.count("/") == 1 else "tgi"

data = self.post(
model=model_url,
json=dict(
model=model_id,
messages=messages,
frequency_penalty=frequency_penalty,
logit_bias=logit_bias,
logprobs=logprobs,
max_tokens=max_tokens,
n=n,
presence_penalty=presence_penalty,
response_format=response_format,
seed=seed,
stop=stop,
temperature=temperature,
tool_choice=tool_choice,
tool_prompt=tool_prompt,
tools=tools,
top_logprobs=top_logprobs,
top_p=top_p,
stream=stream,
),
model_id = model or self.model or "tgi"
if model_id.startswith(("http://", "https://")):
model_id = "tgi" # dummy value

payload = dict(
model=model_id,
messages=messages,
frequency_penalty=frequency_penalty,
logit_bias=logit_bias,
logprobs=logprobs,
max_tokens=max_tokens,
n=n,
presence_penalty=presence_penalty,
response_format=response_format,
seed=seed,
stop=stop,
temperature=temperature,
tool_choice=tool_choice,
tool_prompt=tool_prompt,
tools=tools,
top_logprobs=top_logprobs,
top_p=top_p,
stream=stream,
)
payload = {key: value for key, value in payload.items() if value is not None}
data = self.post(model=model_url, json=payload, stream=stream)

if stream:
return _stream_chat_completion_response(data) # type: ignore[arg-type]
Expand Down
60 changes: 30 additions & 30 deletions src/huggingface_hub/inference/_generated/_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,51 +824,51 @@ async def chat_completion(
# `self.xxx` takes precedence over the method argument only in `chat_completion`
# since `chat_completion(..., model=xxx)` is also a payload parameter for the
# server, we need to handle it differently
model = self.base_url or self.model or model or self.get_recommended_model("text-generation")
is_url = model.startswith(("http://", "https://"))
model_id_or_url = self.base_url or self.model or model or self.get_recommended_model("text-generation")
is_url = model_id_or_url.startswith(("http://", "https://"))

# First, resolve the model chat completions URL
if model == self.base_url:
if model_id_or_url == self.base_url:
# base_url passed => add server route
model_url = model.rstrip("/")
model_url = model_id_or_url.rstrip("/")
if not model_url.endswith("/v1"):
model_url += "/v1"
model_url += "/chat/completions"
elif is_url:
# model is a URL => use it directly
model_url = model
model_url = model_id_or_url
else:
# model is a model ID => resolve it + add server route
model_url = self._resolve_url(model).rstrip("/") + "/v1/chat/completions"
model_url = self._resolve_url(model_id_or_url).rstrip("/") + "/v1/chat/completions"

# `model` is sent in the payload. Not used by the server but can be useful for debugging/routing.
# If it's a ID on the Hub => use it. Otherwise, we use a random string.
model_id = model if not is_url and model.count("/") == 1 else "tgi"

data = await self.post(
model=model_url,
json=dict(
model=model_id,
messages=messages,
frequency_penalty=frequency_penalty,
logit_bias=logit_bias,
logprobs=logprobs,
max_tokens=max_tokens,
n=n,
presence_penalty=presence_penalty,
response_format=response_format,
seed=seed,
stop=stop,
temperature=temperature,
tool_choice=tool_choice,
tool_prompt=tool_prompt,
tools=tools,
top_logprobs=top_logprobs,
top_p=top_p,
stream=stream,
),
model_id = model or self.model or "tgi"
if model_id.startswith(("http://", "https://")):
model_id = "tgi" # dummy value

payload = dict(
model=model_id,
messages=messages,
frequency_penalty=frequency_penalty,
logit_bias=logit_bias,
logprobs=logprobs,
max_tokens=max_tokens,
n=n,
presence_penalty=presence_penalty,
response_format=response_format,
seed=seed,
stop=stop,
temperature=temperature,
tool_choice=tool_choice,
tool_prompt=tool_prompt,
tools=tools,
top_logprobs=top_logprobs,
top_p=top_p,
stream=stream,
)
payload = {key: value for key, value in payload.items() if value is not None}
data = await self.post(model=model_url, json=payload, stream=stream)

if stream:
return _async_stream_chat_completion_response(data) # type: ignore[arg-type]
Expand Down
Loading