From c7eb1ada6532020cb12eb4c71c61365ab681847a Mon Sep 17 00:00:00 2001 From: infra Date: Mon, 3 Jun 2024 15:38:17 -0700 Subject: [PATCH 1/3] add a with_configurable_fields method to implement --- app/custom_chat_model.py | 29 +++++++++++++++++++---------- app/custom_model.py | 22 ++++++++++++---------- app/server.py | 5 ++++- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/app/custom_chat_model.py b/app/custom_chat_model.py index 027fa0d..0248c09 100644 --- a/app/custom_chat_model.py +++ b/app/custom_chat_model.py @@ -6,6 +6,7 @@ from langchain_core.callbacks import ( from langchain_core.language_models import BaseChatModel from langchain_core.messages import AIMessageChunk, BaseMessage, AIMessage from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult +from langchain_core.runnables import ConfigurableField, Runnable class CustomChatModel(BaseChatModel): @@ -18,11 +19,11 @@ class CustomChatModel(BaseChatModel): n: int = 5 def _generate( - self, - messages: List[BaseMessage], - stop: Optional[List[str]] = None, - run_manager: Optional[CallbackManagerForLLMRun] = None, - **kwargs: Any, + self, + messages: List[BaseMessage], + stop: Optional[List[str]] = None, + run_manager: Optional[CallbackManagerForLLMRun] = None, + **kwargs: Any, ) -> ChatResult: """Override the _generate method to implement the chat model logic. @@ -56,11 +57,11 @@ class CustomChatModel(BaseChatModel): return ChatResult(generations=[generation]) def _stream( - self, - messages: List[BaseMessage], - stop: Optional[List[str]] = None, - run_manager: Optional[CallbackManagerForLLMRun] = None, - **kwargs: Any, + self, + messages: List[BaseMessage], + stop: Optional[List[str]] = None, + run_manager: Optional[CallbackManagerForLLMRun] = None, + **kwargs: Any, ) -> Iterator[ChatGenerationChunk]: """Stream the output of the model. @@ -111,3 +112,11 @@ class CustomChatModel(BaseChatModel): # costs for the given LLM.) "model_name": "CustomChatModel", } + + def with_configurable_fields(self) -> Runnable: + """Expose fields you want to be configurable in the playground. We will automatically add these""" + return self.configurable_fields(n=ConfigurableField( + id="n", + name="Num Characters", + description="Number of characters to return from the input prompt.", + )) diff --git a/app/custom_model.py b/app/custom_model.py index 76d5590..f0cfa49 100644 --- a/app/custom_model.py +++ b/app/custom_model.py @@ -3,6 +3,7 @@ from typing import Any, Dict, Iterator, List, Optional from langchain_core.callbacks.manager import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk +from langchain_core.runnables import ConfigurableField class CustomLLM(LLM): @@ -15,11 +16,11 @@ class CustomLLM(LLM): n: int = 5 def _call( - self, - prompt: str, - stop: Optional[List[str]] = None, - run_manager: Optional[CallbackManagerForLLMRun] = None, - **kwargs: Any, + self, + prompt: str, + stop: Optional[List[str]] = None, + run_manager: Optional[CallbackManagerForLLMRun] = None, + **kwargs: Any, ) -> str: """Run the LLM on the given input. @@ -40,11 +41,11 @@ class CustomLLM(LLM): return prompt[: self.n] def _stream( - self, - prompt: str, - stop: Optional[List[str]] = None, - run_manager: Optional[CallbackManagerForLLMRun] = None, - **kwargs: Any, + self, + prompt: str, + stop: Optional[List[str]] = None, + run_manager: Optional[CallbackManagerForLLMRun] = None, + **kwargs: Any, ) -> Iterator[GenerationChunk]: """Stream the LLM on the given prompt. @@ -88,3 +89,4 @@ class CustomLLM(LLM): def _llm_type(self) -> str: """Get the type of language model used by this chat model. Used for logging purposes only.""" return "custom" + diff --git a/app/server.py b/app/server.py index d929c97..1c85db8 100644 --- a/app/server.py +++ b/app/server.py @@ -5,7 +5,10 @@ from langserve import add_routes app = FastAPI() -add_routes(app, CustomChatModel(), path="/chat") +configurable_chat_model = CustomChatModel().with_configurable_fields() if hasattr(CustomChatModel, 'with_configurable_fields') else CustomChatModel() +add_routes(app, configurable_chat_model, path="/chat") + +configurable_llm = CustomLLM().with_configurable_fields() if hasattr(CustomLLM, 'with_configurable_fields') else CustomLLM() add_routes(app, CustomLLM()) if __name__ == "__main__": From 40047f6273d4eeb413f21e9a23c605a35b2fac80 Mon Sep 17 00:00:00 2001 From: infra Date: Mon, 3 Jun 2024 15:40:43 -0700 Subject: [PATCH 2/3] add a with_configurable_fields method to implement --- app/custom_model.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/custom_model.py b/app/custom_model.py index f0cfa49..a45c0f9 100644 --- a/app/custom_model.py +++ b/app/custom_model.py @@ -3,7 +3,7 @@ from typing import Any, Dict, Iterator, List, Optional from langchain_core.callbacks.manager import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.runnables import ConfigurableField +from langchain_core.runnables import ConfigurableField, Runnable class CustomLLM(LLM): @@ -90,3 +90,10 @@ class CustomLLM(LLM): """Get the type of language model used by this chat model. Used for logging purposes only.""" return "custom" + def with_configurable_fields(self) -> Runnable: + """Expose fields you want to be configurable in the playground. We will automatically add these""" + return self.configurable_fields(n=ConfigurableField( + id="n", + name="Num Characters", + description="Number of characters to return from the input prompt.", + )) From 51ff83e50e8f993ad5d94987ee6faddd98bb65ca Mon Sep 17 00:00:00 2001 From: infra Date: Mon, 3 Jun 2024 15:52:08 -0700 Subject: [PATCH 3/3] add a with_configurable_fields method to implement --- app/custom_chat_model.py | 4 +++- app/custom_model.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/custom_chat_model.py b/app/custom_chat_model.py index 0248c09..977f82d 100644 --- a/app/custom_chat_model.py +++ b/app/custom_chat_model.py @@ -114,9 +114,11 @@ class CustomChatModel(BaseChatModel): } def with_configurable_fields(self) -> Runnable: - """Expose fields you want to be configurable in the playground. We will automatically add these""" + """Expose fields you want to be configurable in the playground. We will automatically expose these to the + playground. If you don't want to expose any fields, you can remove this method.""" return self.configurable_fields(n=ConfigurableField( id="n", name="Num Characters", description="Number of characters to return from the input prompt.", )) + diff --git a/app/custom_model.py b/app/custom_model.py index a45c0f9..6c32c03 100644 --- a/app/custom_model.py +++ b/app/custom_model.py @@ -91,7 +91,8 @@ class CustomLLM(LLM): return "custom" def with_configurable_fields(self) -> Runnable: - """Expose fields you want to be configurable in the playground. We will automatically add these""" + """Expose fields you want to be configurable in the playground. We will automatically expose these to the + playground. If you don't want to expose any fields, you can remove this method.""" return self.configurable_fields(n=ConfigurableField( id="n", name="Num Characters",