mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-20 05:43:55 +00:00
Adds - ASYNC - COM - DJ - EXE - FLY - FURB - ICN - INT - LOG - NPY - PD - Q - RSE - SLOT - T10 - TID - YTT Co-authored-by: Erick Friis <erick@langchain.dev>
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
from typing import Any
|
|
|
|
from langchain_core.documents import Document
|
|
from langchain_core.messages import AIMessage, AIMessageChunk, HumanMessage
|
|
|
|
|
|
class AnyStr(str):
|
|
__slots__ = ()
|
|
|
|
def __eq__(self, other: Any) -> bool:
|
|
return isinstance(other, str)
|
|
|
|
|
|
# The code below creates version of pydantic models
|
|
# that will work in unit tests with AnyStr as id field
|
|
# Please note that the `id` field is assigned AFTER the model is created
|
|
# to workaround an issue with pydantic ignoring the __eq__ method on
|
|
# subclassed strings.
|
|
|
|
|
|
def _any_id_document(**kwargs: Any) -> Document:
|
|
"""Create a document with an id field."""
|
|
message = Document(**kwargs)
|
|
message.id = AnyStr()
|
|
return message
|
|
|
|
|
|
def _any_id_ai_message(**kwargs: Any) -> AIMessage:
|
|
"""Create ai message with an any id field."""
|
|
message = AIMessage(**kwargs)
|
|
message.id = AnyStr()
|
|
return message
|
|
|
|
|
|
def _any_id_ai_message_chunk(**kwargs: Any) -> AIMessageChunk:
|
|
"""Create ai message with an any id field."""
|
|
message = AIMessageChunk(**kwargs)
|
|
message.id = AnyStr()
|
|
return message
|
|
|
|
|
|
def _any_id_human_message(**kwargs: Any) -> HumanMessage:
|
|
"""Create a human with an any id field."""
|
|
message = HumanMessage(**kwargs)
|
|
message.id = AnyStr()
|
|
return message
|