-
Notifications
You must be signed in to change notification settings - Fork 5.2k
CAMEL-24527: camel-huggingface - apply the configured token to every task, not only chat #25899
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ce/src/test/java/org/apache/camel/component/huggingface/tasks/AuthTokenInjectionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.camel.component.huggingface.tasks; | ||
|
|
||
| import org.apache.camel.component.huggingface.HuggingFaceConfiguration; | ||
| import org.apache.camel.component.huggingface.HuggingFaceEndpoint; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * The configured Hugging Face token must be applied for every task, not only chat, so that gated or private models can | ||
| * be loaded. The token is injected centrally as the HF_TOKEN environment variable of the generated handler. | ||
| */ | ||
| class AuthTokenInjectionTest { | ||
|
|
||
| private TextGenerationPredictor predictorWithToken(String token) { | ||
| HuggingFaceConfiguration config = new HuggingFaceConfiguration(); | ||
| config.setAuthToken(token); | ||
| return new TextGenerationPredictor(new HuggingFaceEndpoint(null, null, config)); | ||
| } | ||
|
|
||
| @Test | ||
| void authTokenIsExposedAsHfTokenEnvForEveryTask() { | ||
| String result = predictorWithToken("hf_secret123").withAuthToken("PIPELINE"); | ||
| assertTrue(result.contains("os.environ['HF_TOKEN'] = 'hf_secret123'"), | ||
| "generated script should export the token as HF_TOKEN"); | ||
| assertTrue(result.endsWith("PIPELINE"), "the original task script must be preserved"); | ||
| } | ||
|
|
||
| @Test | ||
| void noAuthTokenLeavesTheScriptUnchanged() { | ||
| assertEquals("PIPELINE", predictorWithToken(null).withAuthToken("PIPELINE")); | ||
| assertEquals("PIPELINE", predictorWithToken("").withAuthToken("PIPELINE")); | ||
| } | ||
|
|
||
| @Test | ||
| void authTokenWithASingleQuoteIsEscaped() { | ||
| String result = predictorWithToken("ab'cd").withAuthToken("PIPELINE"); | ||
| assertTrue(result.contains("os.environ['HF_TOKEN'] = 'ab\\'cd'"), | ||
| "a single quote in the token must be escaped so it cannot break the Python string literal"); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
...face/src/test/java/org/apache/camel/component/huggingface/tasks/ChatScriptFormatTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.camel.component.huggingface.tasks; | ||
|
|
||
| import org.apache.camel.component.huggingface.HuggingFaceConfiguration; | ||
| import org.apache.camel.component.huggingface.HuggingFaceEndpoint; | ||
| import org.apache.camel.impl.DefaultCamelContext; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Guards the chat.py template's format-argument alignment after the per-task token clause was removed (the token is now | ||
| * applied centrally as HF_TOKEN). A misaligned placeholder would make getPythonScript throw. | ||
| */ | ||
| class ChatScriptFormatTest { | ||
|
|
||
| private DefaultCamelContext context; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| context = new DefaultCamelContext(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| context.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| void chatScriptFormatsAndCarriesNoTokenClause() { | ||
| HuggingFaceConfiguration config = new HuggingFaceConfiguration(); | ||
| config.setModelId("gpt2"); | ||
| HuggingFaceEndpoint endpoint = new HuggingFaceEndpoint(null, null, config); | ||
| endpoint.setCamelContext(context); | ||
|
|
||
| String script = new ChatPredictor(endpoint).getPythonScript(); | ||
|
|
||
| assertTrue(script.contains("pipeline(task='text-generation'"), "the chat pipeline call must be rendered"); | ||
| assertTrue(script.contains("model='gpt2'"), "the configured model must be interpolated"); | ||
| assertFalse(script.contains("token="), "the per-task token clause must be gone (token is applied via HF_TOKEN)"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.