langchain/templates/cohere-librarian/cohere_librarian/router.py

44 lines
1.0 KiB
Python
Raw Normal View History

from langchain_core.output_parsers import StrOutputParser
2024-01-03 21:28:05 +00:00
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableBranch
from .blurb_matcher import book_rec_chain
from .chat import chat
from .library_info import library_info
from .rag import librarian_rag
chain = (
ChatPromptTemplate.from_template(
"""Given the user message below,
classify it as either being about `recommendation`, `library` or `other`.
'{message}'
Respond with just one word.
For example, if the message is about a book recommendation,respond with
`recommendation`.
"""
)
| chat
| StrOutputParser()
)
def extract_op_field(x):
return x["output_text"]
branch = RunnableBranch(
(
lambda x: "recommendation" in x["topic"].lower(),
book_rec_chain | extract_op_field,
),
(
lambda x: "library" in x["topic"].lower(),
{"message": lambda x: x["message"]} | library_info,
),
librarian_rag,
)
branched_chain = {"topic": chain, "message": lambda x: x["message"]} | branch