kotaemon/knowledgehub/llms/completions/base.py
Nguyen Trung Duc (john) c6dd01e820 [AUR-338, AUR-406, AUR-407] Export pipeline to config for PromptUI. Construct PromptUI dynamically based on config. (#16)
From pipeline > config > UI. Provide example project for promptui

- Pipeline to config: `kotaemon.contribs.promptui.config.export_pipeline_to_config`. The config follows schema specified in this document: https://cinnamon-ai.atlassian.net/wiki/spaces/ATM/pages/2748711193/Technical+Detail. Note: this implementation exclude the logs, which will be handled in AUR-408.
- Config to UI: `kotaemon.contribs.promptui.build_from_yaml`
- Example project is located at `examples/promptui/`
2023-09-21 14:27:23 +07:00

71 lines
2.0 KiB
Python

from typing import List, Type
from langchain.schema.language_model import BaseLanguageModel
from theflow.base import Param
from ...base import BaseComponent
from ..base import LLMInterface
class LLM(BaseComponent):
pass
class LangchainLLM(LLM):
_lc_class: Type[BaseLanguageModel]
def __init__(self, **params):
if self._lc_class is None:
raise AttributeError(
"Should set _lc_class attribute to the LLM class from Langchain "
"if using LLM from Langchain"
)
self._kwargs: dict = {}
for param in list(params.keys()):
if param in self._lc_class.__fields__:
self._kwargs[param] = params.pop(param)
super().__init__(**params)
@Param.decorate(no_cache=True)
def agent(self):
return self._lc_class(**self._kwargs)
def run_raw(self, text: str) -> LLMInterface:
pred = self.agent.generate([text])
return LLMInterface(
text=[each.text for each in pred.generations[0]],
completion_tokens=pred.llm_output["token_usage"]["completion_tokens"],
total_tokens=pred.llm_output["token_usage"]["total_tokens"],
prompt_tokens=pred.llm_output["token_usage"]["prompt_tokens"],
logits=[],
)
def run_batch_raw(self, text: List[str]) -> List[LLMInterface]:
outputs = []
for each_text in text:
outputs.append(self.run_raw(each_text))
return outputs
def run_document(self, text: str) -> LLMInterface:
return self.run_raw(text)
def run_batch_document(self, text: List[str]) -> List[LLMInterface]:
return self.run_batch_raw(text)
def is_document(self, text) -> bool:
return False
def is_batch(self, text) -> bool:
return False if isinstance(text, str) else True
def __setattr__(self, name, value):
if name in self._lc_class.__fields__:
setattr(self.agent, name, value)
else:
super().__setattr__(name, value)
class LLMChat(BaseComponent):
pass