-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add all required Langchain LLM wrappers
- Loading branch information
Showing
5 changed files
with
83 additions
and
10 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
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
from llm.langchain.iris_langchain_completion_model import IrisLangchainCompletionModel | ||
from llm.langchain.iris_langchain_chat_model import IrisLangchainChatModel | ||
from llm.langchain.iris_langchain_embedding import IrisLangchainEmbeddingModel |
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
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,37 @@ | ||
from typing import List, Optional, Any | ||
|
||
from langchain_core.callbacks import CallbackManagerForLLMRun | ||
from langchain_core.language_models.llms import BaseLLM | ||
from langchain_core.outputs import LLMResult | ||
from langchain_core.outputs.generation import Generation | ||
|
||
from llm import RequestHandlerInterface, CompletionArguments | ||
|
||
|
||
class IrisLangchainCompletionModel(BaseLLM): | ||
"""Custom langchain chat model for our own request handler""" | ||
|
||
request_handler: RequestHandlerInterface | ||
|
||
def __init__(self, request_handler: RequestHandlerInterface, **kwargs: Any) -> None: | ||
super().__init__(request_handler=request_handler, **kwargs) | ||
|
||
def _generate( | ||
self, | ||
prompts: List[str], | ||
stop: Optional[List[str]] = None, | ||
run_manager: Optional[CallbackManagerForLLMRun] = None, | ||
**kwargs: Any | ||
) -> LLMResult: | ||
generations = [] | ||
args = CompletionArguments(stop=stop) | ||
for prompt in prompts: | ||
completion = self.request_handler.completion( | ||
prompt=prompt, arguments=args, **kwargs | ||
) | ||
generations.append([Generation(text=completion)]) | ||
return LLMResult(generations=generations) | ||
|
||
@property | ||
def _llm_type(self) -> str: | ||
return "Iris" |
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,20 @@ | ||
from typing import List, Any | ||
|
||
from langchain_core.embeddings import Embeddings | ||
|
||
from llm import RequestHandlerInterface | ||
|
||
|
||
class IrisLangchainEmbeddingModel(Embeddings): | ||
"""Custom langchain embedding for our own request handler""" | ||
|
||
request_handler: RequestHandlerInterface | ||
|
||
def __init__(self, request_handler: RequestHandlerInterface, **kwargs: Any) -> None: | ||
super().__init__(request_handler=request_handler, **kwargs) | ||
|
||
def embed_documents(self, texts: List[str]) -> List[List[float]]: | ||
return [self.embed_query(text) for text in texts] | ||
|
||
def embed_query(self, text: str) -> List[float]: | ||
return self.request_handler.create_embedding(text) |