community: avoid double templating in langchain_prompty (#25777)

## Description

In `langchain_prompty`, messages are templated by Prompty. However, a
call to `ChatPromptTemplate` was initiating a second templating. We now
convert parsed messages to `Message` objects before calling
`ChatPromptTemplate`, signifying clearly that they are already
templated.

We also revert #25739 , which applied to this second templating, which
we now avoid, and did not fix the original issue.

## Issue

Closes #25703
This commit is contained in:
Matthieu
2024-08-29 00:18:18 +02:00
committed by GitHub
parent afe8ccaaa6
commit 783397eacb
4 changed files with 65 additions and 8 deletions

View File

@@ -1,18 +1,41 @@
import base64
import re
from typing import List, Union
from typing import Dict, List, Type, Union
from langchain_core.messages import (
AIMessage,
BaseMessage,
FunctionMessage,
HumanMessage,
SystemMessage,
)
from pydantic import BaseModel
from .core import Invoker, Prompty, SimpleModel
class RoleMap:
_ROLE_MAP: Dict[str, Type[BaseMessage]] = {
"system": SystemMessage,
"user": HumanMessage,
"human": HumanMessage,
"assistant": AIMessage,
"ai": AIMessage,
"function": FunctionMessage,
}
ROLES = _ROLE_MAP.keys()
@classmethod
def get_message_class(cls, role: str) -> Type[BaseMessage]:
return cls._ROLE_MAP[role]
class PromptyChatParser(Invoker):
"""Parse a chat prompt into a list of messages."""
def __init__(self, prompty: Prompty) -> None:
self.prompty = prompty
self.roles = ["assistant", "function", "system", "user", "human", "ai"]
self.roles = RoleMap.ROLES
self.path = self.prompty.file.parent
def inline_image(self, image_item: str) -> str: