-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Use Taskprocessing TextToText provider as LLM #60
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 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
36bf26b
feat: Use Taskprocessing TextToText provider as LLM
marcelklehr 9d787ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d0a1ba0
fix: Parse json if query is json encoded
marcelklehr 200edbd
fix: Address review comments
marcelklehr d8ca18a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7247abb
fix: Address review comments
marcelklehr f789f3e
fix: Address lint issues
marcelklehr 265fde6
fix: Make sure app is enabled when using nc_ texttotext as llm
marcelklehr 7ab883c
fix: Integration test
marcelklehr 4d717f3
fix: Integration test
marcelklehr 4b9d5e7
fix: Fail after 10 minutes of waiting
marcelklehr 3882270
fix: Fail after 30 minutes of waiting
marcelklehr 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
Next
Next commit
feat: Use Taskprocessing TextToText provider as LLM
Signed-off-by: Marcel Klehr <[email protected]>
- Loading branch information
commit 36bf26bcf5e351bdec0f2322f2d889a55525b0ab
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import json | ||
| import time | ||
| from typing import Any, Dict, List, Optional | ||
|
|
||
| from nc_py_api import Nextcloud | ||
| from langchain_core.callbacks.manager import CallbackManagerForLLMRun | ||
| from langchain_core.language_models.llms import LLM | ||
|
|
||
| def get_model_for(model_type: str, model_config: dict): | ||
| if model_config is None: | ||
| return None | ||
|
|
||
| if model_type == 'llm': | ||
| return CustomLLM() | ||
|
|
||
| return None | ||
|
|
||
| class CustomLLM(LLM): | ||
| """A custom chat model that queries Nextcloud's TextToText provider | ||
| """ | ||
|
|
||
| def _call( | ||
| self, | ||
| prompt: str, | ||
| stop: Optional[List[str]] = None, | ||
| run_manager: Optional[CallbackManagerForLLMRun] = None, | ||
| **kwargs: Any, | ||
| ) -> str: | ||
| """Run the LLM on the given input. | ||
|
|
||
| Override this method to implement the LLM logic. | ||
|
|
||
| Args: | ||
| prompt: The prompt to generate from. | ||
| stop: Stop words to use when generating. Model output is cut off at the | ||
| first occurrence of any of the stop substrings. | ||
| If stop tokens are not supported consider raising NotImplementedError. | ||
| run_manager: Callback manager for the run. | ||
| **kwargs: Arbitrary additional keyword arguments. These are usually passed | ||
| to the model provider API call. | ||
|
|
||
| Returns: | ||
| The model output as a string. Actual completions SHOULD NOT include the prompt. | ||
| """ | ||
| nc = Nextcloud() | ||
|
|
||
| print(json.dumps(prompt)) | ||
|
|
||
| response = nc.ocs("POST", "/ocs/v1.php/taskprocessing/schedule", json={ | ||
| "type": "core:text2text", | ||
| "appId": "context_chat_backend", | ||
| "input": { | ||
| "input": prompt | ||
| } | ||
| }) | ||
|
|
||
| task_id = response["task"]["id"] | ||
|
|
||
| while response['task']['status'] != 'STATUS_SUCCESSFUL' and response['task']['status'] != 'STATUS_FAILED': | ||
marcelklehr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| time.sleep(5) | ||
| response = nc.ocs("GET", f"/ocs/v1.php/taskprocessing/task/{task_id}") | ||
marcelklehr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| print(json.dumps(response)) | ||
|
|
||
| if response['task']['status'] == 'STATUS_FAILED': | ||
| raise RuntimeError('Nextcloud TaskProcessing Task failed') | ||
|
|
||
| return response['task']['output']['output'] | ||
|
|
||
| @property | ||
| def _identifying_params(self) -> Dict[str, Any]: | ||
| """Return a dictionary of identifying parameters.""" | ||
| return { | ||
| # The model name allows users to specify custom token counting | ||
| # rules in LLM monitoring applications (e.g., in LangSmith users | ||
| # can provide per token pricing for their model and monitor | ||
| # costs for the given LLM.) | ||
| "model_name": "NextcloudTextToTextProvider", | ||
| } | ||
|
|
||
| @property | ||
| def _llm_type(self) -> str: | ||
| """Get the type of language model used by this chat model. Used for logging purposes only.""" | ||
| return "nc_texttotetx" | ||
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
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.