MCPcopy
hub / github.com/langroid/langroid / get_recipient_and_message

Method get_recipient_and_message

langroid/language_models/base.py:419–477  ·  view source on GitHub ↗

If `message` or `function_call` of an LLM response contains an explicit recipient name, return this recipient name and `message` stripped of the recipient name if specified. Two cases: (a) `message` contains addressing string ``TO[ ]: ``, or

(
        self,
        recognize_recipient_in_content: bool = True,
    )

Source from the content-addressed store, hash-verified

417 )
418
419 def get_recipient_and_message(
420 self,
421 recognize_recipient_in_content: bool = True,
422 ) -> Tuple[str, str]:
423 """
424 If `message` or `function_call` of an LLM response contains an explicit
425 recipient name, return this recipient name and `message` stripped
426 of the recipient name if specified.
427
428 Two cases:
429 (a) `message` contains addressing string ``TO[<name>]:<content>``, or
430 (b) `message` is empty and function_call/tool_call with explicit `recipient`
431
432 Args:
433 recognize_recipient_in_content (bool): When True (default), parses
434 message text for ``TO[<recipient>]:<content>`` patterns and
435 top-level JSON ``{"recipient": "..."}`` fields. When False,
436 only function_call/tool_call ``recipient`` fields are checked.
437
438 Returns:
439 (str): name of recipient, which may be empty string if no recipient
440 (str): content of message
441
442 """
443
444 if self.function_call is not None:
445 # in this case we ignore message, since all information is in function_call
446 msg = ""
447 args = self.function_call.arguments
448 recipient = ""
449 if isinstance(args, dict):
450 recipient = args.get("recipient", "")
451 return recipient, msg
452 else:
453 msg = self.message
454 if self.oai_tool_calls is not None:
455 # get the first tool that has a recipient field, if any
456 for tc in self.oai_tool_calls:
457 if tc.function is not None and tc.function.arguments is not None:
458 recipient = tc.function.arguments.get(
459 "recipient"
460 ) # type: ignore
461 if recipient is not None and recipient != "":
462 return recipient, ""
463
464 if not recognize_recipient_in_content:
465 return "", msg
466
467 # It's not a function or tool call, so continue looking to see
468 # if a recipient is specified in the message.
469
470 # First check if message contains "TO: <recipient> <content>"
471 recipient_name, content = parse_message(msg) if msg is not None else ("", "")
472 # check if there is a top level json that specifies 'recipient',
473 # and retain the entire message as content.
474 if recipient_name == "":
475 recipient_name = top_level_json_field(msg, "recipient") if msg else ""
476 content = msg

Callers 1

from_LLMResponseMethod · 0.80

Calls 3

parse_messageFunction · 0.90
top_level_json_fieldFunction · 0.90
getMethod · 0.80

Tested by

no test coverage detected