-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathlanggraph.py
More file actions
713 lines (629 loc) · 27.1 KB
/
Copy pathlanggraph.py
File metadata and controls
713 lines (629 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterable,
Mapping,
Optional,
Sequence,
Union,
)
if TYPE_CHECKING:
try:
from langchain_core.language_models import base as lc_language_models
BaseLanguageModel = lc_language_models.BaseLanguageModel
except ImportError:
BaseLanguageModel = Any
try:
from langchain_google_genai.functions_utils import _ToolsType
_ToolLike = _ToolsType
except ImportError:
try:
from langchain_google_vertexai.functions_utils import _ToolsType
_ToolLike = _ToolsType
except ImportError:
_ToolLike = Any
try:
from opentelemetry.sdk import trace
TracerProvider = trace.TracerProvider
SpanProcessor = trace.SpanProcessor
SynchronousMultiSpanProcessor = trace.SynchronousMultiSpanProcessor
except ImportError:
TracerProvider = Any
SpanProcessor = Any
SynchronousMultiSpanProcessor = Any
try:
from langgraph_checkpoint.checkpoint import base
BaseCheckpointSaver = base.BaseCheckpointSaver
except ImportError:
try:
from langgraph.checkpoint import base
BaseCheckpointSaver = base.BaseCheckpointSaver
except ImportError:
BaseCheckpointSaver = Any
def _default_model_builder(
model_name: str,
*,
project: str,
location: str,
model_kwargs: Optional[Mapping[str, Any]] = None,
) -> "BaseLanguageModel":
"""Default callable for building a language model.
Args:
model_name (str):
Required. The name of the model (e.g. "gemini-1.0-pro").
project (str):
Required. The Google Cloud project ID.
location (str):
Required. The Google Cloud location.
model_kwargs (Mapping[str, Any]):
Optional. Additional keyword arguments for the constructor of
chat_models.ChatVertexAI.
Returns:
BaseLanguageModel: The language model.
"""
model_kwargs = model_kwargs or {}
try:
from langchain_google_genai import ChatGoogleGenerativeAI
model = ChatGoogleGenerativeAI(
model=model_name,
project=project,
location=location,
vertexai=True,
**model_kwargs,
)
return model
except ImportError:
import agentplatform
from google.cloud.aiplatform import initializer
from langchain_google_vertexai import ChatVertexAI
current_project = initializer.global_config.project
current_location = initializer.global_config.location
agentplatform.init(project=project, location=location)
model = ChatVertexAI(model_name=model_name, **model_kwargs)
agentplatform.init(project=current_project, location=current_location)
return model
def _default_runnable_builder(
model: "BaseLanguageModel",
*,
tools: Optional[Sequence["_ToolLike"]] = None,
checkpointer: Optional[Any] = None,
model_tool_kwargs: Optional[Mapping[str, Any]] = None,
runnable_kwargs: Optional[Mapping[str, Any]] = None,
):
"""Default callable for building a runnable.
Args:
model (BaseLanguageModel):
Required. The language model.
tools (Optional[Sequence[_ToolLike]]):
Optional. The tools for the agent to be able to use.
checkpointer (Optional[Checkpointer]):
Optional. The checkpointer for the agent.
model_tool_kwargs (Optional[Mapping[str, Any]]):
Optional. Additional keyword arguments when binding tools to the model.
runnable_kwargs (Optional[Mapping[str, Any]]):
Optional. Additional keyword arguments for the runnable.
Returns:
RunnableSerializable: The runnable.
"""
from langgraph import prebuilt as langgraph_prebuilt
model_tool_kwargs = model_tool_kwargs or {}
runnable_kwargs = runnable_kwargs or {}
if tools:
model = model.bind_tools(tools=tools, **model_tool_kwargs)
else:
tools = []
if checkpointer:
if "checkpointer" in runnable_kwargs:
from google.cloud.aiplatform import base
base.Logger(__name__).warning(
"checkpointer is being specified in both checkpointer_builder "
"and runnable_kwargs. Please specify it in only one of them. "
"Overriding the checkpointer in runnable_kwargs."
)
runnable_kwargs["checkpointer"] = checkpointer
return langgraph_prebuilt.create_react_agent(
model,
tools=tools,
**runnable_kwargs,
)
def _default_instrumentor_builder(project_id: str):
from agentplatform._genai import _agent_engines_utils
cloud_trace_exporter = _agent_engines_utils._import_cloud_trace_exporter_or_warn()
cloud_trace_v2 = _agent_engines_utils._import_cloud_trace_v2_or_warn()
openinference_langchain = (
_agent_engines_utils._import_openinference_langchain_or_warn()
)
opentelemetry = _agent_engines_utils._import_opentelemetry_or_warn()
opentelemetry_sdk_trace = (
_agent_engines_utils._import_opentelemetry_sdk_trace_or_warn()
)
if all(
(
cloud_trace_exporter,
cloud_trace_v2,
openinference_langchain,
opentelemetry,
opentelemetry_sdk_trace,
)
):
import google.auth
credentials, _ = google.auth.default()
span_exporter = cloud_trace_exporter.CloudTraceSpanExporter(
project_id=project_id,
client=cloud_trace_v2.TraceServiceClient(
credentials=credentials.with_quota_project(project_id),
),
)
span_processor: SpanProcessor = (
opentelemetry_sdk_trace.export.SimpleSpanProcessor(
span_exporter=span_exporter,
)
)
tracer_provider: TracerProvider = opentelemetry.trace.get_tracer_provider()
# Get the appropriate tracer provider:
# 1. If _TRACER_PROVIDER is already set, use that.
# 2. Otherwise, if the OTEL_PYTHON_TRACER_PROVIDER environment
# variable is set, use that.
# 3. As a final fallback, use _PROXY_TRACER_PROVIDER.
# If none of the above is set, we log a warning, and
# create a tracer provider.
if not tracer_provider:
from google.cloud.aiplatform import base
base.Logger(__name__).warning(
"No tracer provider. By default, "
"we should get one of the following providers: "
"OTEL_PYTHON_TRACER_PROVIDER, _TRACER_PROVIDER, "
"or _PROXY_TRACER_PROVIDER."
)
tracer_provider = opentelemetry_sdk_trace.TracerProvider()
opentelemetry.trace.set_tracer_provider(tracer_provider)
# Avoids AttributeError:
# 'ProxyTracerProvider' and 'NoOpTracerProvider' objects has no
# attribute 'add_span_processor'.
if _agent_engines_utils.is_noop_or_proxy_tracer_provider(tracer_provider):
tracer_provider = opentelemetry_sdk_trace.TracerProvider()
opentelemetry.trace.set_tracer_provider(tracer_provider)
# Avoids OpenTelemetry client already exists error.
_override_active_span_processor(
tracer_provider,
opentelemetry_sdk_trace.SynchronousMultiSpanProcessor(),
)
tracer_provider.add_span_processor(span_processor)
# Keep the instrumentation up-to-date.
# When creating multiple LangchainAgents,
# we need to keep the instrumentation up-to-date.
# We deliberately override the instrument each time,
# so that if different agents end up using different
# instrumentations, we guarantee that the user is always
# working with the most recent agent's instrumentation.
instrumentor = openinference_langchain.LangChainInstrumentor()
if instrumentor.is_instrumented_by_opentelemetry:
instrumentor.uninstrument()
instrumentor.instrument()
return instrumentor
else:
from google.cloud.aiplatform import base
_LOGGER = base.Logger(__name__)
_LOGGER.warning(
"enable_tracing=True but proceeding with tracing disabled "
"because not all packages for tracing have been installed"
)
return None
def _validate_callable_parameters_are_annotated(callable: Callable):
"""Validates that the parameters of the callable have type annotations.
This ensures that they can be used for constructing LangChain tools that are
usable with Gemini function calling.
Args:
callable (Callable): The callable to validate.
Raises:
TypeError: If any parameter is not annotated.
"""
import inspect
parameters = dict(inspect.signature(callable).parameters)
for name, parameter in parameters.items():
if parameter.annotation == inspect.Parameter.empty:
raise TypeError(
f"Callable={callable.__name__} has untyped input_arg={name}. "
f"Please specify a type when defining it, e.g. `{name}: str`."
)
def _validate_tools(tools: Sequence["_ToolLike"]):
"""Validates that the tools are usable for tool calling.
Args:
tools (Sequence[_ToolLike]): The tools to validate.
Raises:
TypeError: If any tool is a callable with untyped parameters.
"""
for tool in tools:
if isinstance(tool, Callable):
_validate_callable_parameters_are_annotated(tool)
def _override_active_span_processor(
tracer_provider: "TracerProvider",
active_span_processor: "SynchronousMultiSpanProcessor",
):
"""Overrides the active span processor.
When working with multiple LangchainAgents in the same environment,
it's crucial to manage trace exports carefully.
Each agent needs its own span processor tied to a unique project ID.
While we add a new span processor for each agent, this can lead to
unexpected behavior.
For instance, with two agents linked to different projects, traces from the
second agent might be sent to both projects.
To prevent this and guarantee traces go to the correct project, we overwrite
the active span processor whenever a new LangchainAgent is created.
Args:
tracer_provider (TracerProvider):
The tracer provider to use for the project.
active_span_processor (SynchronousMultiSpanProcessor):
The active span processor overrides the tracer provider's
active span processor.
"""
if tracer_provider._active_span_processor:
tracer_provider._active_span_processor.shutdown()
tracer_provider._active_span_processor = active_span_processor
class LanggraphAgent:
"""A LangGraph Agent."""
agent_framework = "langgraph"
def __init__(
self,
model: str,
*,
tools: Optional[Sequence["_ToolLike"]] = None,
model_kwargs: Optional[Mapping[str, Any]] = None,
model_tool_kwargs: Optional[Mapping[str, Any]] = None,
model_builder: Optional[Callable[..., "BaseLanguageModel"]] = None,
runnable_kwargs: Optional[Mapping[str, Any]] = None,
runnable_builder: Optional[Callable[..., Any]] = None,
checkpointer_kwargs: Optional[Mapping[str, Any]] = None,
checkpointer_builder: Optional[Callable[..., "BaseCheckpointSaver"]] = None,
enable_tracing: bool = False,
instrumentor_builder: Optional[Callable[..., Any]] = None,
):
"""Initializes the LangGraph Agent.
Under-the-hood, assuming .set_up() is called, this will correspond to
```python
model = model_builder(model_name=model, model_kwargs=model_kwargs)
runnable = runnable_builder(
model=model,
tools=tools,
model_tool_kwargs=model_tool_kwargs,
runnable_kwargs=runnable_kwargs,
)
```
When everything is based on their default values, this corresponds to
```python
# model_builder
from langchain_google_vertexai import ChatVertexAI
llm = ChatVertexAI(model_name=model, **model_kwargs)
# runnable_builder
from langgraph.prebuilt import create_react_agent
llm_with_tools = llm.bind_tools(tools=tools, **model_tool_kwargs)
runnable = create_react_agent(
llm_with_tools,
tools=tools,
**runnable_kwargs,
)
```
By default, no checkpointer is used (i.e. there is no state history). To
enable checkpointing, provide a `checkpointer_builder` function that
returns a checkpointer instance.
**Example using Spanner:**
```python
def checkpointer_builder(instance_id, database_id, project_id, **kwargs):
from langchain_google_spanner import SpannerCheckpointSaver
checkpointer = SpannerCheckpointSaver(instance_id, database_id, project_id)
with checkpointer.cursor() as cur:
cur.execute("DROP TABLE IF EXISTS checkpoints")
cur.execute("DROP TABLE IF EXISTS checkpoint_writes")
checkpointer.setup()
return checkpointer
```
**Example using an in-memory checkpointer:**
```python
def checkpointer_builder(**kwargs):
from langgraph.checkpoint.memory import MemorySaver
return MemorySaver()
```
The `checkpointer_builder` function will be called with any keyword
arguments passed to the agent's constructor. Ensure your
`checkpointer_builder` function accepts `**kwargs` to handle these
arguments, even if unused.
Args:
model (str):
Optional. The name of the model (e.g. "gemini-1.0-pro").
tools (Sequence[langchain_core.tools.BaseTool, Callable]):
Optional. The tools for the agent to be able to use. All input
callables (e.g. function or class method) will be converted
to a langchain.tools.base.StructuredTool. Defaults to None.
model_kwargs (Mapping[str, Any]):
Optional. Additional keyword arguments for the constructor of
chat_models.ChatVertexAI. An example would be
```
{
# temperature (float): Sampling temperature, it controls the
# degree of randomness in token selection.
"temperature": 0.28,
# max_output_tokens (int): Token limit determines the
# maximum amount of text output from one prompt.
"max_output_tokens": 1000,
# top_p (float): Tokens are selected from most probable to
# least, until the sum of their probabilities equals the
# top_p value.
"top_p": 0.95,
# top_k (int): How the model selects tokens for output, the
# next token is selected from among the top_k most probable
# tokens.
"top_k": 40,
}
```
model_tool_kwargs (Mapping[str, Any]):
Optional. Additional keyword arguments when binding tools to the
model using `model.bind_tools()`.
model_builder (Callable[..., BaseLanguageModel]):
Optional. Callable that returns a new language model. Defaults
to a a callable that returns ChatVertexAI based on `model`,
`model_kwargs` and the parameters in `agentplatform.init`.
runnable_kwargs (Mapping[str, Any]):
Optional. Additional keyword arguments for the constructor of
langchain.runnables.history.RunnableWithMessageHistory if
chat_history is specified. If chat_history is None, this will be
ignored.
runnable_builder (Callable[..., RunnableSerializable]):
Optional. Callable that returns a new runnable. This can be used
for customizing the orchestration logic of the Agent based on
the model returned by `model_builder` and the rest of the input
arguments.
checkpointer_kwargs (Mapping[str, Any]):
Optional. Additional keyword arguments for the constructor of
the checkpointer returned by `checkpointer_builder`.
checkpointer_builder (Callable[..., "BaseCheckpointSaver"]):
Optional. Callable that returns a checkpointer. This can be used
for defining the checkpointer of the Agent. Defaults to None.
enable_tracing (bool):
Optional. Whether to enable tracing in Cloud Trace. Defaults to
False.
instrumentor_builder (Callable[..., Any]):
Optional. Callable that returns a new instrumentor. This can be
used for customizing the instrumentation logic of the Agent.
If not provided, a default instrumentor builder will be used.
This parameter is ignored if `enable_tracing` is False.
Raises:
TypeError: If there is an invalid tool (e.g. function with an input
that did not specify its type).
"""
from google.cloud.aiplatform import initializer
self._tmpl_attrs: dict[str, Any] = {
"project": initializer.global_config.project,
"location": initializer.global_config.location,
"tools": [],
"model_name": model,
"model_kwargs": model_kwargs,
"model_tool_kwargs": model_tool_kwargs,
"runnable_kwargs": runnable_kwargs,
"checkpointer_kwargs": checkpointer_kwargs,
"model": None,
"model_builder": model_builder,
"runnable": None,
"runnable_builder": runnable_builder,
"checkpointer": None,
"checkpointer_builder": checkpointer_builder,
"enable_tracing": enable_tracing,
"instrumentor": None,
"instrumentor_builder": instrumentor_builder,
}
if tools:
# We validate tools at initialization for actionable feedback before
# they are deployed.
_validate_tools(tools)
self._tmpl_attrs["tools"] = tools
def set_up(self):
"""Sets up the agent for execution of queries at runtime.
It initializes the model, binds the model with tools, and connects it
with the prompt template and output parser.
This method should not be called for an object that being passed to
the ReasoningEngine service for deployment, as it initializes clients
that can not be serialized.
"""
if self._tmpl_attrs.get("enable_tracing"):
instrumentor_builder = (
self._tmpl_attrs.get("instrumentor_builder")
or _default_instrumentor_builder
)
self._tmpl_attrs["instrumentor"] = instrumentor_builder(
project_id=self._tmpl_attrs.get("project")
)
model_builder = self._tmpl_attrs.get("model_builder") or _default_model_builder
self._tmpl_attrs["model"] = model_builder(
model_name=self._tmpl_attrs.get("model_name"),
model_kwargs=self._tmpl_attrs.get("model_kwargs"),
project=self._tmpl_attrs.get("project"),
location=self._tmpl_attrs.get("location"),
)
checkpointer_builder = self._tmpl_attrs.get("checkpointer_builder")
if checkpointer_builder:
checkpointer_kwargs = self._tmpl_attrs.get("checkpointer_kwargs") or {}
self._tmpl_attrs["checkpointer"] = checkpointer_builder(
**checkpointer_kwargs
)
runnable_builder = (
self._tmpl_attrs.get("runnable_builder") or _default_runnable_builder
)
self._tmpl_attrs["runnable"] = runnable_builder(
model=self._tmpl_attrs.get("model"),
tools=self._tmpl_attrs.get("tools"),
checkpointer=self._tmpl_attrs.get("checkpointer"),
model_tool_kwargs=self._tmpl_attrs.get("model_tool_kwargs"),
runnable_kwargs=self._tmpl_attrs.get("runnable_kwargs"),
)
def clone(self) -> "LanggraphAgent":
"""Returns a clone of the LanggraphAgent."""
import copy
return LanggraphAgent(
model=self._tmpl_attrs.get("model_name"),
tools=copy.deepcopy(self._tmpl_attrs.get("tools")),
model_kwargs=copy.deepcopy(self._tmpl_attrs.get("model_kwargs")),
model_tool_kwargs=copy.deepcopy(self._tmpl_attrs.get("model_tool_kwargs")),
runnable_kwargs=copy.deepcopy(self._tmpl_attrs.get("runnable_kwargs")),
checkpointer_kwargs=copy.deepcopy(
self._tmpl_attrs.get("checkpointer_kwargs")
),
model_builder=self._tmpl_attrs.get("model_builder"),
runnable_builder=self._tmpl_attrs.get("runnable_builder"),
checkpointer_builder=self._tmpl_attrs.get("checkpointer_builder"),
enable_tracing=self._tmpl_attrs.get("enable_tracing"),
instrumentor_builder=self._tmpl_attrs.get("instrumentor_builder"),
)
def query(
self,
*,
input: Union[str, Mapping[str, Any]],
config: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> Dict[str, Any]:
"""Queries the Agent with the given input and config.
Args:
input (Union[str, Mapping[str, Any]]):
Required. The input to be passed to the Agent.
config (langchain_core.runnables.RunnableConfig):
Optional. The config (if any) to be used for invoking the Agent.
**kwargs:
Optional. Any additional keyword arguments to be passed to the
`.invoke()` method of the corresponding AgentExecutor.
Returns:
The output of querying the Agent with the given input and config.
"""
try:
from langchain_core.load import dumpd
except ImportError:
from langchain.load.dump import dumpd
if isinstance(input, str):
input = {"input": input, "messages": [("user", input)]}
if not self._tmpl_attrs.get("runnable"):
self.set_up()
return dumpd(
self._tmpl_attrs.get("runnable").invoke(
input=input, config=config, **kwargs
)
)
def stream_query(
self,
*,
input: Union[str, Mapping[str, Any]],
config: Optional[dict[str, Any]] = None,
**kwargs,
) -> Iterable[Any]:
"""Stream queries the Agent with the given input and config.
Args:
input (Union[str, Mapping[str, Any]]):
Required. The input to be passed to the Agent.
config (langchain_core.runnables.RunnableConfig):
Optional. The config (if any) to be used for invoking the Agent.
**kwargs:
Optional. Any additional keyword arguments to be passed to the
`.invoke()` method of the corresponding AgentExecutor.
Yields:
The output of querying the Agent with the given input and config.
"""
try:
from langchain_core.load import dumpd
except ImportError:
from langchain.load.dump import dumpd
if isinstance(input, str):
input = {"input": input, "messages": [("user", input)]}
if not self._tmpl_attrs.get("runnable"):
self.set_up()
for chunk in self._tmpl_attrs.get("runnable").stream(
input=input,
config=config,
**kwargs,
):
yield dumpd(chunk)
def get_state_history(
self,
config: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> Iterable[Any]:
"""Gets the state history of the Agent.
Args:
config (Optional[RunnableConfig]):
Optional. The config for invoking the Agent.
**kwargs:
Optional. Additional keyword arguments for the `.invoke()` method.
Yields:
Dict[str, Any]: The state history of the Agent.
"""
if not self._tmpl_attrs.get("runnable"):
self.set_up()
for state_snapshot in self._tmpl_attrs.get("runnable").get_state_history(
config=config,
**kwargs,
):
yield state_snapshot._asdict()
def get_state(
self,
config: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> Dict[str, Any]:
"""Gets the current state of the Agent.
Args:
config (Optional[RunnableConfig]):
Optional. The config for invoking the Agent.
**kwargs:
Optional. Additional keyword arguments for the `.invoke()` method.
Returns:
Dict[str, Any]: The current state of the Agent.
"""
if not self._tmpl_attrs.get("runnable"):
self.set_up()
return (
self._tmpl_attrs.get("runnable")
.get_state(config=config, **kwargs)
._asdict()
)
def update_state(
self,
config: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> Dict[str, Any]:
"""Updates the state of the Agent.
Args:
config (Optional[RunnableConfig]):
Optional. The config for invoking the Agent.
**kwargs:
Optional. Additional keyword arguments for the `.invoke()` method.
Returns:
Dict[str, Any]: The updated state of the Agent.
"""
if not self._tmpl_attrs.get("runnable"):
self.set_up()
return self._tmpl_attrs.get("runnable").update_state(config=config, **kwargs)
def register_operations(self) -> Mapping[str, Sequence[str]]:
"""Registers the operations of the Agent.
This mapping defines how different operation modes (e.g., "", "stream")
are implemented by specific methods of the Agent. The "default" mode,
represented by the empty string ``, is associated with the `query` API,
while the "stream" mode is associated with the `stream_query` API.
Returns:
Mapping[str, Sequence[str]]: A mapping of operation modes to a list
of method names that implement those operation modes.
"""
return {
"": ["query", "get_state", "update_state"],
"stream": ["stream_query", "get_state_history"],
}