langfun 0.1.2.dev202510280805__py3-none-any.whl → 0.1.2.dev202510300805__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 langfun might be problematic. Click here for more details.
- langfun/core/agentic/action.py +31 -3
- langfun/core/agentic/action_test.py +25 -0
- langfun/core/llms/gemini.py +3 -3
- langfun/env/__init__.py +4 -0
- langfun/env/base_environment.py +247 -105
- langfun/env/base_feature.py +15 -0
- langfun/env/base_sandbox.py +47 -240
- langfun/env/base_test.py +813 -603
- langfun/env/event_handlers/event_logger_test.py +2 -2
- langfun/env/event_handlers/metric_writer.py +49 -0
- langfun/env/event_handlers/metric_writer_test.py +19 -2
- langfun/env/interface.py +197 -13
- langfun/env/interface_test.py +81 -2
- langfun/env/load_balancers_test.py +19 -0
- langfun/env/test_utils.py +10 -7
- {langfun-0.1.2.dev202510280805.dist-info → langfun-0.1.2.dev202510300805.dist-info}/METADATA +1 -1
- {langfun-0.1.2.dev202510280805.dist-info → langfun-0.1.2.dev202510300805.dist-info}/RECORD +20 -20
- {langfun-0.1.2.dev202510280805.dist-info → langfun-0.1.2.dev202510300805.dist-info}/WHEEL +0 -0
- {langfun-0.1.2.dev202510280805.dist-info → langfun-0.1.2.dev202510300805.dist-info}/licenses/LICENSE +0 -0
- {langfun-0.1.2.dev202510280805.dist-info → langfun-0.1.2.dev202510300805.dist-info}/top_level.txt +0 -0
langfun/core/agentic/action.py
CHANGED
|
@@ -538,6 +538,18 @@ class ExecutionTrace(pg.Object, pg.views.html.HtmlTreeView.Extension):
|
|
|
538
538
|
remove_class=['not-started'],
|
|
539
539
|
)
|
|
540
540
|
|
|
541
|
+
def remove(self, item: TracedItem) -> None:
|
|
542
|
+
"""Removes an item from the sequence."""
|
|
543
|
+
index = self.items.index(item)
|
|
544
|
+
if index == -1:
|
|
545
|
+
raise ValueError(f'Item not found in execution trace: {item!r}')
|
|
546
|
+
|
|
547
|
+
with pg.notify_on_change(False):
|
|
548
|
+
self.items.pop(index)
|
|
549
|
+
|
|
550
|
+
if self._tab_control is not None:
|
|
551
|
+
self._tab_control.remove(index)
|
|
552
|
+
|
|
541
553
|
def extend(self, items: Iterable[TracedItem]) -> None:
|
|
542
554
|
"""Extends the sequence with a list of items."""
|
|
543
555
|
for item in items:
|
|
@@ -1648,13 +1660,20 @@ class Session(pg.Object, pg.views.html.HtmlTreeView.Extension):
|
|
|
1648
1660
|
@contextlib.contextmanager
|
|
1649
1661
|
def track_queries(
|
|
1650
1662
|
self,
|
|
1651
|
-
phase: str | None = None
|
|
1663
|
+
phase: str | None = None,
|
|
1664
|
+
track_if: Callable[
|
|
1665
|
+
[lf_structured.QueryInvocation],
|
|
1666
|
+
bool
|
|
1667
|
+
] | None = None,
|
|
1652
1668
|
) -> Iterator[list[lf_structured.QueryInvocation]]:
|
|
1653
1669
|
"""Tracks `lf.query` made within the context.
|
|
1654
1670
|
|
|
1655
1671
|
Args:
|
|
1656
1672
|
phase: The name of a new phase to track the queries in. If not provided,
|
|
1657
1673
|
the queries will be tracked in the parent phase.
|
|
1674
|
+
track_if: A function that takes a `lf_structured.QueryInvocation` and
|
|
1675
|
+
returns True if the query should be included in the result. If None,
|
|
1676
|
+
all queries (including failed queries) will be included.
|
|
1658
1677
|
|
|
1659
1678
|
Yields:
|
|
1660
1679
|
A list of `lf.QueryInvocation` objects, each for a single `lf.query`
|
|
@@ -1673,6 +1692,11 @@ class Session(pg.Object, pg.views.html.HtmlTreeView.Extension):
|
|
|
1673
1692
|
self.event_handler.on_query_start(self, self._current_action, invocation)
|
|
1674
1693
|
|
|
1675
1694
|
def _query_end(invocation: lf_structured.QueryInvocation):
|
|
1695
|
+
if track_if is not None and not track_if(invocation):
|
|
1696
|
+
self._current_execution.remove(invocation)
|
|
1697
|
+
# Even if the query is not included in the execution trace, we still
|
|
1698
|
+
# count the usage summary to the current execution and trigger the
|
|
1699
|
+
# event handler to log the query.
|
|
1676
1700
|
self._current_execution.merge_usage_summary(invocation.usage_summary)
|
|
1677
1701
|
self.event_handler.on_query_end(self, self._current_action, invocation)
|
|
1678
1702
|
|
|
@@ -1705,8 +1729,9 @@ class Session(pg.Object, pg.views.html.HtmlTreeView.Extension):
|
|
|
1705
1729
|
*,
|
|
1706
1730
|
lm: lf.LanguageModel,
|
|
1707
1731
|
examples: list[lf_structured.MappingExample] | None = None,
|
|
1732
|
+
track_if: Callable[[lf_structured.QueryInvocation], bool] | None = None,
|
|
1708
1733
|
**kwargs
|
|
1709
|
-
|
|
1734
|
+
) -> Any:
|
|
1710
1735
|
"""Calls `lf.query` and associates it with the current invocation.
|
|
1711
1736
|
|
|
1712
1737
|
The following code are equivalent:
|
|
@@ -1731,12 +1756,15 @@ class Session(pg.Object, pg.views.html.HtmlTreeView.Extension):
|
|
|
1731
1756
|
default: The default value to return if the query fails.
|
|
1732
1757
|
lm: The language model to use for the query.
|
|
1733
1758
|
examples: The examples to use for the query.
|
|
1759
|
+
track_if: A function that takes a `lf_structured.QueryInvocation`
|
|
1760
|
+
and returns True if the query should be tracked.
|
|
1761
|
+
If None, all queries (including failed queries) will be tracked.
|
|
1734
1762
|
**kwargs: Additional keyword arguments to pass to `lf.query`.
|
|
1735
1763
|
|
|
1736
1764
|
Returns:
|
|
1737
1765
|
The result of the query.
|
|
1738
1766
|
"""
|
|
1739
|
-
with self.track_queries():
|
|
1767
|
+
with self.track_queries(track_if=track_if):
|
|
1740
1768
|
return lf_structured.query(
|
|
1741
1769
|
prompt,
|
|
1742
1770
|
schema=schema,
|
|
@@ -530,6 +530,31 @@ class SessionTest(unittest.TestCase):
|
|
|
530
530
|
self.assertIn('agent@', session.id)
|
|
531
531
|
self.assertIsInstance(session.as_message(), lf.AIMessage)
|
|
532
532
|
|
|
533
|
+
def test_query_with_track_if(self):
|
|
534
|
+
lm = fake.StaticResponse('lm response')
|
|
535
|
+
session = action_lib.Session()
|
|
536
|
+
|
|
537
|
+
# Render session to trigger javascript updates to the HTML when
|
|
538
|
+
# operating on the session.
|
|
539
|
+
_ = session.to_html()
|
|
540
|
+
with session:
|
|
541
|
+
# This query will succeed.
|
|
542
|
+
session.query(
|
|
543
|
+
'prompt1',
|
|
544
|
+
schema=None,
|
|
545
|
+
lm=lm,
|
|
546
|
+
track_if=lambda q: not q.has_error,
|
|
547
|
+
default=None)
|
|
548
|
+
# This query will fail during parsing.
|
|
549
|
+
session.query(
|
|
550
|
+
'prompt2',
|
|
551
|
+
schema=int,
|
|
552
|
+
lm=lm,
|
|
553
|
+
track_if=lambda q: not q.has_error,
|
|
554
|
+
default=None)
|
|
555
|
+
self.assertEqual(len(session.root.queries), 1)
|
|
556
|
+
self.assertIsNone(session.root.queries[0].error)
|
|
557
|
+
|
|
533
558
|
|
|
534
559
|
if __name__ == '__main__':
|
|
535
560
|
unittest.main()
|
langfun/core/llms/gemini.py
CHANGED
|
@@ -830,9 +830,9 @@ class Gemini(rest.REST):
|
|
|
830
830
|
)
|
|
831
831
|
|
|
832
832
|
def _error(self, status_code: int, content: str) -> lf.LMError:
|
|
833
|
-
if (
|
|
834
|
-
|
|
835
|
-
|
|
833
|
+
if status_code == 400 and (
|
|
834
|
+
b'exceeds the maximum number of tokens' in content
|
|
835
|
+
or b'Reduce the input token count and try again.' in content
|
|
836
836
|
):
|
|
837
837
|
return lf.ContextLimitError(f'{status_code}: {content}')
|
|
838
838
|
return super()._error(status_code, content)
|
langfun/env/__init__.py
CHANGED
|
@@ -24,6 +24,10 @@ from langfun.env.interface import Environment
|
|
|
24
24
|
from langfun.env.interface import Sandbox
|
|
25
25
|
from langfun.env.interface import Feature
|
|
26
26
|
|
|
27
|
+
# Decorators for sandbox/feature methods.
|
|
28
|
+
from langfun.env.interface import treat_as_sandbox_state_error
|
|
29
|
+
from langfun.env.interface import log_sandbox_activity
|
|
30
|
+
|
|
27
31
|
from langfun.env.base_environment import BaseEnvironment
|
|
28
32
|
from langfun.env.base_sandbox import BaseSandbox
|
|
29
33
|
from langfun.env.base_feature import BaseFeature
|