-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Text2Text Assistant as LLM for LangChain
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,10 @@ | ||
import pytest | ||
|
||
from text2text.langchain.text2text_assistant import Text2TextAssistant | ||
|
||
@pytest.mark.requires("langchain") | ||
def test_llm_inference() -> None: | ||
input_text = 'Say "hello, world" back to me' | ||
llm = Text2TextAssistant() | ||
result = llm(input_text) | ||
assert "hello" in result.lower() |
This file contains 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,28 @@ | ||
from typing import Any, List, Mapping, Optional | ||
|
||
import text2text as t2t | ||
from langchain.callbacks.manager import CallbackManagerForLLMRun | ||
from langchain.llms.base import LLM | ||
|
||
class Text2TextAssistant(LLM): | ||
model: t2t.Assistant = t2t.Assistant() | ||
|
||
@property | ||
def _llm_type(self) -> str: | ||
return "Text2Text" | ||
|
||
def _call( | ||
self, | ||
prompt: str, | ||
stop: Optional[List[str]] = None, | ||
run_manager: Optional[CallbackManagerForLLMRun] = None, | ||
**kwargs | ||
) -> str: | ||
if stop is not None: | ||
raise ValueError("stop kwargs are not permitted.") | ||
return self.model.transform([prompt], **kwargs)[0] | ||
|
||
@property | ||
def _identifying_params(self) -> Mapping[str, Any]: | ||
"""Get the identifying parameters.""" | ||
return {"type": self._llm_type} |