|
4 | 4 | from typing import Optional, Callable, Any, overload, Dict, Generic, Iterator, TypeVar, List, cast, AsyncIterator |
5 | 5 | from functools import wraps |
6 | 6 |
|
| 7 | +from langchain_core.runnables import RunnableLambda |
| 8 | + |
7 | 9 | from cozeloop import Client, Span, start_span |
8 | 10 | from cozeloop.decorator.utils import is_async_func, is_gen_func, is_async_gen_func, is_class_func |
9 | 11 |
|
@@ -312,6 +314,136 @@ async def async_stream_wrapper(*args: Any, **kwargs: Any): |
312 | 314 | return decorator(func) |
313 | 315 |
|
314 | 316 |
|
| 317 | + def to_runnable( |
| 318 | + self, |
| 319 | + func: Callable = None, |
| 320 | + ) -> Callable: |
| 321 | + """ |
| 322 | + Decorator to be RunnableLambda. |
| 323 | +
|
| 324 | + :param func: The function to be decorated, Requirements are as follows: |
| 325 | + 1. The input parameter should only have one field, not multiple fields. You can use dict or Class as the input parameter. |
| 326 | + 2. When the func is called, parameter config(RunnableConfig) is required, you must use the config containing cozeloop callback handler of 'current request', otherwise, the trace may be lost! |
| 327 | +
|
| 328 | + Examples: |
| 329 | + @to_runnable |
| 330 | + def runnable_func(my_input: dict) -> str: |
| 331 | + return input |
| 332 | +
|
| 333 | + async def scorer_leader(state: MyState) -> dict | str: |
| 334 | + await runnable_func({"a": "111", "b": 222, "c": "333"}, config=state.config) # config is required |
| 335 | + """ |
| 336 | + |
| 337 | + def decorator(func: Callable): |
| 338 | + |
| 339 | + @wraps(func) |
| 340 | + def sync_wrapper(*args: Any, **kwargs: Any): |
| 341 | + config = kwargs.pop("config", None) |
| 342 | + res = None |
| 343 | + try: |
| 344 | + inp = None |
| 345 | + if len(args) == 1: |
| 346 | + inp = args[0] |
| 347 | + else: |
| 348 | + inp = kwargs |
| 349 | + res = RunnableLambda(func).invoke(input=inp, config=config, **kwargs) |
| 350 | + if hasattr(res, "__iter__"): |
| 351 | + return res |
| 352 | + except StopIteration: |
| 353 | + pass |
| 354 | + except Exception as e: |
| 355 | + raise e |
| 356 | + finally: |
| 357 | + if res is not None: |
| 358 | + return res |
| 359 | + |
| 360 | + @wraps(func) |
| 361 | + async def async_wrapper(*args: Any, **kwargs: Any): |
| 362 | + config = kwargs.pop("config", None) |
| 363 | + res = None |
| 364 | + try: |
| 365 | + inp = None |
| 366 | + if len(args) == 1: |
| 367 | + inp = args[0] |
| 368 | + else: |
| 369 | + inp = kwargs |
| 370 | + res = await RunnableLambda(func).ainvoke(input=inp, config=config, **kwargs) |
| 371 | + if hasattr(res, "__aiter__"): |
| 372 | + return res |
| 373 | + except StopIteration: |
| 374 | + pass |
| 375 | + except StopAsyncIteration: |
| 376 | + pass |
| 377 | + except Exception as e: |
| 378 | + if e.args and e.args[0] == 'coroutine raised StopIteration': # coroutine StopIteration |
| 379 | + pass |
| 380 | + else: |
| 381 | + raise e |
| 382 | + finally: |
| 383 | + if res is not None: |
| 384 | + return res |
| 385 | + |
| 386 | + @wraps(func) |
| 387 | + def gen_wrapper(*args: Any, **kwargs: Any): |
| 388 | + config = kwargs.pop("config", None) |
| 389 | + try: |
| 390 | + inp = None |
| 391 | + if len(args) == 1: |
| 392 | + inp = args[0] |
| 393 | + else: |
| 394 | + inp = kwargs |
| 395 | + gen = RunnableLambda(func).invoke(input=inp, config=config, **kwargs) |
| 396 | + try: |
| 397 | + for item in gen: |
| 398 | + yield item |
| 399 | + except StopIteration: |
| 400 | + pass |
| 401 | + except Exception as e: |
| 402 | + raise e |
| 403 | + |
| 404 | + @wraps(func) |
| 405 | + async def async_gen_wrapper(*args: Any, **kwargs: Any): |
| 406 | + config = kwargs.pop("config", None) |
| 407 | + try: |
| 408 | + inp = None |
| 409 | + if len(args) == 1: |
| 410 | + inp = args[0] |
| 411 | + else: |
| 412 | + inp = kwargs |
| 413 | + gen = RunnableLambda(func).invoke(input=inp, config=config, **kwargs) |
| 414 | + items = [] |
| 415 | + try: |
| 416 | + async for item in gen: |
| 417 | + items.append(item) |
| 418 | + yield item |
| 419 | + finally: |
| 420 | + pass |
| 421 | + except StopIteration: |
| 422 | + pass |
| 423 | + except StopAsyncIteration: |
| 424 | + pass |
| 425 | + except Exception as e: |
| 426 | + if e.args and e.args[0] == 'coroutine raised StopIteration': |
| 427 | + pass |
| 428 | + else: |
| 429 | + raise e |
| 430 | + |
| 431 | + |
| 432 | + if is_async_gen_func(func): |
| 433 | + return async_gen_wrapper |
| 434 | + if is_gen_func(func): |
| 435 | + return gen_wrapper |
| 436 | + elif is_async_func(func): |
| 437 | + return async_wrapper |
| 438 | + else: |
| 439 | + return sync_wrapper |
| 440 | + |
| 441 | + if func is None: |
| 442 | + return decorator |
| 443 | + else: |
| 444 | + return decorator(func) |
| 445 | + |
| 446 | + |
315 | 447 | class _CozeLoopTraceStream(Generic[S]): |
316 | 448 | def __init__( |
317 | 449 | self, |
|
0 commit comments