langchain 0.3.9__py3-none-any.whl → 0.3.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langchain might be problematic. Click here for more details.

@@ -20,9 +20,8 @@ from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMP
20
20
  since="0.2.12",
21
21
  removal="1.0",
22
22
  message=(
23
- "Use RunnableLambda to select from multiple prompt templates. See example "
24
- "in API reference: "
25
- "https://api.python.langchain.com/en/latest/chains/langchain.chains.router.multi_prompt.MultiPromptChain.html" # noqa: E501
23
+ "Please see migration guide here for recommended implementation: "
24
+ "https://python.langchain.com/docs/versions/migrating_chains/multi_prompt_chain/" # noqa: E501
26
25
  ),
27
26
  )
28
27
  class MultiPromptChain(MultiRouteChain):
@@ -37,60 +36,109 @@ class MultiPromptChain(MultiRouteChain):
37
36
 
38
37
  from operator import itemgetter
39
38
  from typing import Literal
40
- from typing_extensions import TypedDict
41
39
 
42
40
  from langchain_core.output_parsers import StrOutputParser
43
41
  from langchain_core.prompts import ChatPromptTemplate
44
- from langchain_core.runnables import RunnableLambda, RunnablePassthrough
42
+ from langchain_core.runnables import RunnableConfig
45
43
  from langchain_openai import ChatOpenAI
44
+ from langgraph.graph import END, START, StateGraph
45
+ from typing_extensions import TypedDict
46
46
 
47
47
  llm = ChatOpenAI(model="gpt-4o-mini")
48
48
 
49
+ # Define the prompts we will route to
49
50
  prompt_1 = ChatPromptTemplate.from_messages(
50
51
  [
51
52
  ("system", "You are an expert on animals."),
52
- ("human", "{query}"),
53
+ ("human", "{input}"),
53
54
  ]
54
55
  )
55
56
  prompt_2 = ChatPromptTemplate.from_messages(
56
57
  [
57
58
  ("system", "You are an expert on vegetables."),
58
- ("human", "{query}"),
59
+ ("human", "{input}"),
59
60
  ]
60
61
  )
61
62
 
63
+ # Construct the chains we will route to. These format the input query
64
+ # into the respective prompt, run it through a chat model, and cast
65
+ # the result to a string.
62
66
  chain_1 = prompt_1 | llm | StrOutputParser()
63
67
  chain_2 = prompt_2 | llm | StrOutputParser()
64
68
 
69
+
70
+ # Next: define the chain that selects which branch to route to.
71
+ # Here we will take advantage of tool-calling features to force
72
+ # the output to select one of two desired branches.
65
73
  route_system = "Route the user's query to either the animal or vegetable expert."
66
74
  route_prompt = ChatPromptTemplate.from_messages(
67
75
  [
68
76
  ("system", route_system),
69
- ("human", "{query}"),
77
+ ("human", "{input}"),
70
78
  ]
71
79
  )
72
80
 
73
81
 
82
+ # Define schema for output:
74
83
  class RouteQuery(TypedDict):
75
- \"\"\"Route query to destination.\"\"\"
84
+ \"\"\"Route query to destination expert.\"\"\"
85
+
76
86
  destination: Literal["animal", "vegetable"]
77
87
 
78
88
 
79
- route_chain = (
80
- route_prompt
81
- | llm.with_structured_output(RouteQuery)
82
- | itemgetter("destination")
83
- )
89
+ route_chain = route_prompt | llm.with_structured_output(RouteQuery)
84
90
 
85
- chain = {
86
- "destination": route_chain, # "animal" or "vegetable"
87
- "query": lambda x: x["query"], # pass through input query
88
- } | RunnableLambda(
89
- # if animal, chain_1. otherwise, chain_2.
90
- lambda x: chain_1 if x["destination"] == "animal" else chain_2,
91
- )
92
91
 
93
- chain.invoke({"query": "what color are carrots"})
92
+ # For LangGraph, we will define the state of the graph to hold the query,
93
+ # destination, and final answer.
94
+ class State(TypedDict):
95
+ query: str
96
+ destination: RouteQuery
97
+ answer: str
98
+
99
+
100
+ # We define functions for each node, including routing the query:
101
+ async def route_query(state: State, config: RunnableConfig):
102
+ destination = await route_chain.ainvoke(state["query"], config)
103
+ return {"destination": destination}
104
+
105
+
106
+ # And one node for each prompt
107
+ async def prompt_1(state: State, config: RunnableConfig):
108
+ return {"answer": await chain_1.ainvoke(state["query"], config)}
109
+
110
+
111
+ async def prompt_2(state: State, config: RunnableConfig):
112
+ return {"answer": await chain_2.ainvoke(state["query"], config)}
113
+
114
+
115
+ # We then define logic that selects the prompt based on the classification
116
+ def select_node(state: State) -> Literal["prompt_1", "prompt_2"]:
117
+ if state["destination"] == "animal":
118
+ return "prompt_1"
119
+ else:
120
+ return "prompt_2"
121
+
122
+
123
+ # Finally, assemble the multi-prompt chain. This is a sequence of two steps:
124
+ # 1) Select "animal" or "vegetable" via the route_chain, and collect the answer
125
+ # alongside the input query.
126
+ # 2) Route the input query to chain_1 or chain_2, based on the
127
+ # selection.
128
+ graph = StateGraph(State)
129
+ graph.add_node("route_query", route_query)
130
+ graph.add_node("prompt_1", prompt_1)
131
+ graph.add_node("prompt_2", prompt_2)
132
+
133
+ graph.add_edge(START, "route_query")
134
+ graph.add_conditional_edges("route_query", select_node)
135
+ graph.add_edge("prompt_1", END)
136
+ graph.add_edge("prompt_2", END)
137
+ app = graph.compile()
138
+
139
+ result = await app.ainvoke({"query": "what color are carrots"})
140
+ print(result["destination"])
141
+ print(result["answer"])
94
142
  """ # noqa: E501
95
143
 
96
144
  @property
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langchain
3
- Version: 0.3.9
3
+ Version: 0.3.10
4
4
  Summary: Building applications with LLMs through composability
5
5
  Home-page: https://github.com/langchain-ai/langchain
6
6
  License: MIT
@@ -11,11 +11,12 @@ Classifier: Programming Language :: Python :: 3.9
11
11
  Classifier: Programming Language :: Python :: 3.10
12
12
  Classifier: Programming Language :: Python :: 3.11
13
13
  Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
14
15
  Requires-Dist: PyYAML (>=5.3)
15
16
  Requires-Dist: SQLAlchemy (>=1.4,<3)
16
17
  Requires-Dist: aiohttp (>=3.8.3,<4.0.0)
17
18
  Requires-Dist: async-timeout (>=4.0.0,<5.0.0) ; python_version < "3.11"
18
- Requires-Dist: langchain-core (>=0.3.21,<0.4.0)
19
+ Requires-Dist: langchain-core (>=0.3.22,<0.4.0)
19
20
  Requires-Dist: langchain-text-splitters (>=0.3.0,<0.4.0)
20
21
  Requires-Dist: langsmith (>=0.1.17,<0.2.0)
21
22
  Requires-Dist: numpy (>=1.22.4,<2) ; python_version < "3.12"
@@ -327,7 +327,7 @@ langchain/chains/router/__init__.py,sha256=r66J28FWIORVB5QIZ1d8R_HsiBaV1eQMZDZvM
327
327
  langchain/chains/router/base.py,sha256=ws6i8C4nk7YWmBqkXBcJ-FybNx4OeDJE-L1IELLK3M4,4517
328
328
  langchain/chains/router/embedding_router.py,sha256=hR5hOuwBdMBo_U3lo9SSwBfnVACR0ZpNc-nmDpei5hw,3069
329
329
  langchain/chains/router/llm_router.py,sha256=6FQUTXvZ9pekVkPeTNvQsj1jD9JdmfpMkxIPMe4oTMU,6994
330
- langchain/chains/router/multi_prompt.py,sha256=pqBIW8fLH6esLd9uiuGS5aJeXuuPTQcv7jaOHU8YhSE,4887
330
+ langchain/chains/router/multi_prompt.py,sha256=lLpJsYShzRBnvwtV3AaBbUcB8x6sK1PSxqDveCSC65A,6994
331
331
  langchain/chains/router/multi_prompt_prompt.py,sha256=T8UbIuxblnI6Byhw-BMAzwQcbB5ww3N6BiMqMJxS6Jc,1156
332
332
  langchain/chains/router/multi_retrieval_prompt.py,sha256=VUYGLWbwGiv03aSMW5sjdGNwsEa9FKgq0RcK5o3lkH4,1079
333
333
  langchain/chains/router/multi_retrieval_qa.py,sha256=tjIhHEbOwtF3CLq0qQ8Kd78ao5BXRKZLsm9UlmHrdtQ,4254
@@ -1335,8 +1335,8 @@ langchain/vectorstores/xata.py,sha256=HW_Oi5Hz8rH2JaUhRNWQ-3hLYmNzD8eAz6K5YqPArm
1335
1335
  langchain/vectorstores/yellowbrick.py,sha256=-lnjGcRE8Q1nEPOTdbKYTw5noS2cy2ce1ePOU804-_o,624
1336
1336
  langchain/vectorstores/zep.py,sha256=RJ2auxoA6uHHLEZknw3_jeFmYJYVt-PWKMBcNMGV6TM,798
1337
1337
  langchain/vectorstores/zilliz.py,sha256=XhPPIUfKPFJw0_svCoBgCnNkkBLoRVVcyuMfOnE5IxU,609
1338
- langchain-0.3.9.dist-info/LICENSE,sha256=TsZ-TKbmch26hJssqCJhWXyGph7iFLvyFBYAa3stBHg,1067
1339
- langchain-0.3.9.dist-info/METADATA,sha256=kWM48_6bifCEFs4A4ok2IMmndji2jzfNT7dbLA8WpiU,7077
1340
- langchain-0.3.9.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
1341
- langchain-0.3.9.dist-info/entry_points.txt,sha256=IgKjoXnkkVC8Nm7ggiFMCNAk01ua6RVTb9cmZTVNm5w,58
1342
- langchain-0.3.9.dist-info/RECORD,,
1338
+ langchain-0.3.10.dist-info/LICENSE,sha256=TsZ-TKbmch26hJssqCJhWXyGph7iFLvyFBYAa3stBHg,1067
1339
+ langchain-0.3.10.dist-info/METADATA,sha256=P5V_UT3Cp3vgRhV8lYufDJquT0X09f0J-UoJ_dFGxPA,7129
1340
+ langchain-0.3.10.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
1341
+ langchain-0.3.10.dist-info/entry_points.txt,sha256=IgKjoXnkkVC8Nm7ggiFMCNAk01ua6RVTb9cmZTVNm5w,58
1342
+ langchain-0.3.10.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.8.1
2
+ Generator: poetry-core 1.9.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any