|
| 1 | +alias:: [[LangGraph Template]], [[LangGraph Templates]] |
| 2 | + |
| 3 | +- # [LangGraph Templates](https://studio.langchain.com/?ref=blog.langchain.dev) |
| 4 | + - See also [LangChain - Changelog | 📐 LangGraph Templates: Build configurable](https://changelog.langchain.com/announcements/langgraph-templates-build-configurable-agentic-workflows) |
| 5 | + - ## [[My Notes]] |
| 6 | + - contains a bunch of different [[GitHub/Repo]]s showing how to do things like use configurable fields |
| 7 | + - Example |
| 8 | + - [rag-research-agent-template/src/retrieval_graph/configuration.py at main · langchain-ai/rag-research-agent-template](https://github.com/langchain-ai/rag-research-agent-template/blob/main/src/retrieval_graph/configuration.py) |
| 9 | + - [retrieval-agent-template/src/retrieval_graph/retrieval.py at main · langchain-ai/retrieval-agent-template](https://github.com/langchain-ai/retrieval-agent-template/blob/main/src/retrieval_graph/retrieval.py) has this pattern using [[Py/Context Manager]] and [[Py/Generator]] |
| 10 | + - ```python |
| 11 | + @contextmanager |
| 12 | + def make_retriever( |
| 13 | + config: RunnableConfig, |
| 14 | + ) -> Generator[VectorStoreRetriever, None, None]: |
| 15 | + """Create a retriever for the agent, based on the current configuration.""" |
| 16 | + configuration = IndexConfiguration.from_runnable_config(config) |
| 17 | + embedding_model = make_text_encoder(configuration.embedding_model) |
| 18 | + user_id = configuration.user_id |
| 19 | + if not user_id: |
| 20 | + raise ValueError("Please provide a valid user_id in the configuration.") |
| 21 | + match configuration.retriever_provider: |
| 22 | + case "elastic" | "elastic-local": |
| 23 | + with make_elastic_retriever(configuration, embedding_model) as retriever: |
| 24 | + yield retriever |
| 25 | + |
| 26 | + case "pinecone": |
| 27 | + with make_pinecone_retriever(configuration, embedding_model) as retriever: |
| 28 | + yield retriever |
| 29 | + |
| 30 | + case "mongodb": |
| 31 | + with make_mongodb_retriever(configuration, embedding_model) as retriever: |
| 32 | + yield retriever |
| 33 | + |
| 34 | + case _: |
| 35 | + raise ValueError( |
| 36 | + "Unrecognized retriever_provider in configuration. " |
| 37 | + f"Expected one of: {', '.join(Configuration.__annotations__['retriever_provider'].__args__)}\n" |
| 38 | + f"Got: {configuration.retriever_provider}" |
| 39 | + ) |
| 40 | + ``` |
| 41 | + - note that a sample configuration is using [[Py/Dataclass]] - what exactly is this `__template_metadata__` - it's probably something specific to the templates, but is this how we make it show up in teh UI? |
| 42 | + - ```python |
| 43 | + @dataclass(kw_only=True) |
| 44 | + class IndexConfiguration: |
| 45 | + """Configuration class for indexing and retrieval operations. |
| 46 | + |
| 47 | + This class defines the parameters needed for configuring the indexing and |
| 48 | + retrieval processes, including user identification, embedding model selection, |
| 49 | + retriever provider choice, and search parameters. |
| 50 | + """ |
| 51 | + |
| 52 | + user_id: str = field(metadata={"description": "Unique identifier for the user."}) |
| 53 | + |
| 54 | + embedding_model: Annotated[ |
| 55 | + str, |
| 56 | + {"__template_metadata__": {"kind": "embeddings"}}, |
| 57 | + ] = field( |
| 58 | + default="openai/text-embedding-3-small", |
| 59 | + metadata={ |
| 60 | + "description": "Name of the embedding model to use. Must be a valid embedding model name." |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + retriever_provider: Annotated[ |
| 65 | + Literal["elastic", "elastic-local", "pinecone", "mongodb"], |
| 66 | + {"__template_metadata__": {"kind": "retriever"}}, |
| 67 | + ] = field( |
| 68 | + default="elastic", |
| 69 | + metadata={ |
| 70 | + "description": "The vector store provider to use for retrieval. Options are 'elastic', 'pinecone', or 'mongodb'." |
| 71 | + }, |
| 72 | + ) |
| 73 | + |
| 74 | + search_kwargs: dict[str, Any] = field( |
| 75 | + default_factory=dict, |
| 76 | + metadata={ |
| 77 | + "description": "Additional keyword arguments to pass to the search function of the retriever." |
| 78 | + }, |
| 79 | + ) |
| 80 | + |
| 81 | + @classmethod |
| 82 | + def from_runnable_config( |
| 83 | + cls: Type[T], config: Optional[RunnableConfig] = None |
| 84 | + ) -> T: |
| 85 | + """Create an IndexConfiguration instance from a RunnableConfig object. |
| 86 | + |
| 87 | + Args: |
| 88 | + cls (Type[T]): The class itself. |
| 89 | + config (Optional[RunnableConfig]): The configuration object to use. |
| 90 | + |
| 91 | + Returns: |
| 92 | + T: An instance of IndexConfiguration with the specified configuration. |
| 93 | + """ |
| 94 | + config = ensure_config(config) |
| 95 | + configurable = config.get("configurable") or {} |
| 96 | + _fields = {f.name for f in fields(cls) if f.init} |
| 97 | + return cls(**{k: v for k, v in configurable.items() if k in _fields}) |
| 98 | + ``` |
| 99 | + - #Via [Launching LangGraph Templates](https://blog.langchain.dev/launching-langgraph-templates/) |
| 100 | + - |
0 commit comments