Skip to content

Commit 78cd111

Browse files
committed
feat: Use Taskprocessing TextToText provider as LLM
Signed-off-by: Marcel Klehr <[email protected]>
1 parent b24ef9c commit 78cd111

File tree

8 files changed

+95
-4
lines changed

8 files changed

+95
-4
lines changed

config.cpu.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ embedding:
4040
device: cpu
4141

4242
llm:
43+
nc_texttotext:
44+
4345
llama:
4446
model_path: dolphin-2.2.1-mistral-7b.Q5_K_M.gguf
4547
n_batch: 512

config.gpu.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ embedding:
4040
device: cuda
4141

4242
llm:
43+
nc_texttotext:
44+
4345
llama:
4446
model_path: dolphin-2.2.1-mistral-7b.Q5_K_M.gguf
4547
n_batch: 512
@@ -69,4 +71,4 @@ llm:
6971
pipeline_kwargs:
7072
config:
7173
max_length: 200
72-
template: ""
74+
template: ""

context_chat_backend/chain/query_proc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_pruned_query(llm: LLM, config: TConfig, query: str, template: str, text_
2525
or llm_config.get('config', {}).get('max_new_tokens') \
2626
or max(
2727
llm_config.get('pipeline_kwargs', {}).get('config', {}).get('max_new_tokens', 0),
28-
llm_config.get('pipeline_kwargs', {}).get('config', {}).get('max_length')
28+
llm_config.get('pipeline_kwargs', {}).get('config', {}).get('max_length', 0)
2929
) \
3030
or 4096
3131

context_chat_backend/download.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ def background_init(app: FastAPI):
202202
for model_type in ('embedding', 'llm'):
203203
model_name = _get_model_name_or_path(config, model_type)
204204
if model_name is None:
205-
raise Exception(f'Error: Model name/path not found for {model_type}')
205+
update_progress(app, progress := progress + 50)
206+
continue
206207

207208
if not _download_model(model_name):
208209
raise Exception(f'Error: Model download failed for {model_name}')

context_chat_backend/models/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from langchain.schema.embeddings import Embeddings
66

77
_embedding_models = ['llama', 'hugging_face', 'instructor']
8-
_llm_models = ['llama', 'hugging_face', 'ctransformer']
8+
_llm_models = ['nc_texttotext', 'llama', 'hugging_face', 'ctransformer']
99

1010
models = {
1111
'embedding': _embedding_models,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import json
2+
import time
3+
from typing import Any, Dict, List, Optional
4+
5+
from nc_py_api import Nextcloud
6+
from langchain_core.callbacks.manager import CallbackManagerForLLMRun
7+
from langchain_core.language_models.llms import LLM
8+
9+
def get_model_for(model_type: str, model_config: dict):
10+
if model_config is None:
11+
return None
12+
13+
if model_type == 'llm':
14+
return CustomLLM()
15+
16+
return None
17+
18+
class CustomLLM(LLM):
19+
"""A custom chat model that queries Nextcloud's TextToText provider
20+
"""
21+
22+
def _call(
23+
self,
24+
prompt: str,
25+
stop: Optional[List[str]] = None,
26+
run_manager: Optional[CallbackManagerForLLMRun] = None,
27+
**kwargs: Any,
28+
) -> str:
29+
"""Run the LLM on the given input.
30+
31+
Override this method to implement the LLM logic.
32+
33+
Args:
34+
prompt: The prompt to generate from.
35+
stop: Stop words to use when generating. Model output is cut off at the
36+
first occurrence of any of the stop substrings.
37+
If stop tokens are not supported consider raising NotImplementedError.
38+
run_manager: Callback manager for the run.
39+
**kwargs: Arbitrary additional keyword arguments. These are usually passed
40+
to the model provider API call.
41+
42+
Returns:
43+
The model output as a string. Actual completions SHOULD NOT include the prompt.
44+
"""
45+
nc = Nextcloud()
46+
47+
print(json.dumps(prompt))
48+
49+
response = nc.ocs("POST", "/ocs/v1.php/taskprocessing/schedule", json={
50+
"type": "core:text2text",
51+
"appId": "context_chat_backend",
52+
"input": {
53+
"input": prompt
54+
}
55+
})
56+
57+
task_id = response["task"]["id"]
58+
59+
while response['task']['status'] != 'STATUS_SUCCESSFUL' and response['task']['status'] != 'STATUS_FAILED':
60+
time.sleep(5)
61+
response = nc.ocs("GET", f"/ocs/v1.php/taskprocessing/task/{task_id}")
62+
print(json.dumps(response))
63+
64+
if response['task']['status'] == 'STATUS_FAILED':
65+
raise RuntimeError('Nextcloud TaskProcessing Task failed')
66+
67+
return response['task']['output']['output']
68+
69+
@property
70+
def _identifying_params(self) -> Dict[str, Any]:
71+
"""Return a dictionary of identifying parameters."""
72+
return {
73+
# The model name allows users to specify custom token counting
74+
# rules in LLM monitoring applications (e.g., in LangSmith users
75+
# can provide per token pricing for their model and monitor
76+
# costs for the given LLM.)
77+
"model_name": "NextcloudTextToTextProvider",
78+
}
79+
80+
@property
81+
def _llm_type(self) -> str:
82+
"""Get the type of language model used by this chat model. Used for logging purposes only."""
83+
return "nc_texttotetx"

requirements.in.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ unstructured @ git+https://github.com/kyteinsky/unstructured@d3a404cfb541dae8e16
2929
unstructured-client
3030
weaviate-client
3131
xlrd
32+
nc_py_api

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ mpmath==1.3.0
8282
msg-parser==1.2.0
8383
multidict==6.0.5
8484
mypy-extensions==1.0.0
85+
nc-py-api==0.14.0
8586
nest-asyncio==1.6.0
8687
networkx==3.3
8788
nltk==3.8.1
@@ -189,5 +190,6 @@ websockets==12.0
189190
wrapt==1.16.0
190191
xlrd==2.0.1
191192
XlsxWriter==3.2.0
193+
xmltodict==0.13.0
192194
yarl==1.9.4
193195
zipp==3.19.2

0 commit comments

Comments
 (0)