Spaces:
Sleeping
Sleeping
File size: 2,488 Bytes
1e91476 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
from typing import Any, Dict, Iterator, List, Mapping, Optional
from models.business_logic_utils.business_logic import process_app_request
from langchain_core.callbacks.manager import CallbackManagerForLLMRun
from langchain_core.language_models.llms import LLM
from langchain_core.outputs import GenerationChunk
class CustomDatabricksLLM(LLM):
endpoint_url: str
bearer_token: str
issue: str
language: str
temperature: float
texter_name: str = ""
"""The number of characters from the last message of the prompt to be echoed."""
def generate_databricks_request(self, prompt):
return {
"inputs": {
"conversation_id": [""],
"prompt": [prompt],
"issue": [self.issue],
"language": [self.language],
"temperature": [self.temperature],
"max_tokens": [128],
"texter_name": [self.texter_name]
}
}
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
request = self.generate_databricks_request(prompt)
output = process_app_request(request, self.endpoint_url, self.bearer_token)
return output['predictions'][0]['generated_text']
def _stream(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> Iterator[GenerationChunk]:
output = self._call(prompt, stop, run_manager, **kwargs)
for char in output:
chunk = GenerationChunk(text=char)
if run_manager:
run_manager.on_llm_new_token(chunk.text, chunk=chunk)
yield chunk
@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": "CustomChatModel",
}
@property
def _llm_type(self) -> str:
"""Get the type of language model used by this chat model. Used for logging purposes only."""
return "custom" |