Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
prompt_values.py162 linesDownload Raw Back to langchain_core
1"""**Prompt values** for language model prompts.2 3Prompt values are used to represent different pieces of prompts. They can be used to4represent text, images, or chat message pieces.5"""6 7from __future__ import annotations8 9from abc import ABC, abstractmethod10from collections.abc import Sequence11from typing import Literal, cast12 13from typing_extensions import TypedDict14 15from langchain_core.load.serializable import Serializable16from langchain_core.messages import (17    AnyMessage,18    BaseMessage,19    HumanMessage,20    get_buffer_string,21)22 23 24class PromptValue(Serializable, ABC):25    """Base abstract class for inputs to any language model.26 27    `PromptValues` can be converted to both LLM (pure text-generation) inputs and28    chat model inputs.29    """30 31    @classmethod32    def is_lc_serializable(cls) -> bool:33        """Return `True` as this class is serializable."""34        return True35 36    @classmethod37    def get_lc_namespace(cls) -> list[str]:38        """Get the namespace of the LangChain object.39 40        Returns:41            `["langchain", "schema", "prompt"]`42        """43        return ["langchain", "schema", "prompt"]44 45    @abstractmethod46    def to_string(self) -> str:47        """Return prompt value as string."""48 49    @abstractmethod50    def to_messages(self) -> list[BaseMessage]:51        """Return prompt as a list of messages."""52 53 54class StringPromptValue(PromptValue):55    """String prompt value."""56 57    text: str58    """Prompt text."""59 60    type: Literal["StringPromptValue"] = "StringPromptValue"61 62    @classmethod63    def get_lc_namespace(cls) -> list[str]:64        """Get the namespace of the LangChain object.65 66        Returns:67            `["langchain", "prompts", "base"]`68        """69        return ["langchain", "prompts", "base"]70 71    def to_string(self) -> str:72        """Return prompt as string."""73        return self.text74 75    def to_messages(self) -> list[BaseMessage]:76        """Return prompt as messages."""77        return [HumanMessage(content=self.text)]78 79 80class ChatPromptValue(PromptValue):81    """Chat prompt value.82 83    A type of a prompt value that is built from messages.84    """85 86    messages: Sequence[BaseMessage]87    """List of messages."""88 89    def to_string(self) -> str:90        """Return prompt as string."""91        return get_buffer_string(self.messages)92 93    def to_messages(self) -> list[BaseMessage]:94        """Return prompt as a list of messages."""95        return list(self.messages)96 97    @classmethod98    def get_lc_namespace(cls) -> list[str]:99        """Get the namespace of the LangChain object.100 101        Returns:102            `["langchain", "prompts", "chat"]`103        """104        return ["langchain", "prompts", "chat"]105 106 107class ImageURL(TypedDict, total=False):108    """Image URL for multimodal model inputs (OpenAI format).109 110    Represents the inner `image_url` object in OpenAI's Chat Completion API format. This111    is used by `ImagePromptTemplate` and `ChatPromptTemplate`.112 113    See Also:114        `ImageContentBlock`: LangChain's provider-agnostic image format used in message115        content blocks. Use `ImageContentBlock` when working with the standardized116        message format across different providers.117 118    Note:119        The `detail` field values are not validated locally. Invalid values120        will be rejected by the downstream API, allowing new valid values to121        be used without requiring a LangChain update.122    """123 124    detail: Literal["auto", "low", "high"]125    """Specifies the detail level of the image.126 127    Defaults to ``'auto'`` if not specified. Higher detail levels consume128    more tokens but provide better image understanding.129    """130 131    url: str132    """URL of the image or base64-encoded image data."""133 134 135class ImagePromptValue(PromptValue):136    """Image prompt value."""137 138    image_url: ImageURL139    """Image URL."""140 141    type: Literal["ImagePromptValue"] = "ImagePromptValue"142 143    def to_string(self) -> str:144        """Return prompt (image URL) as string."""145        return self.image_url.get("url", "")146 147    def to_messages(self) -> list[BaseMessage]:148        """Return prompt (image URL) as messages."""149        return [HumanMessage(content=[cast("dict", self.image_url)])]150 151 152class ChatPromptValueConcrete(ChatPromptValue):153    """Chat prompt value which explicitly lists out the message types it accepts.154 155    For use in external schemas.156    """157 158    messages: Sequence[AnyMessage]159    """Sequence of messages."""160 161    type: Literal["ChatPromptValueConcrete"] = "ChatPromptValueConcrete"162 
codekingpro/portable-devtools · Team Ai