mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
7e4dbb26a8
Adding the example I build for the Cohere hackathon. It can: use a vector database to reccommend books <img width="840" alt="image" src="https://github.com/langchain-ai/langchain/assets/144115527/96543a18-217b-4445-ab4b-950c7cced915"> Use a prompt template to provide information about the library <img width="834" alt="image" src="https://github.com/langchain-ai/langchain/assets/144115527/996c8e0f-cab0-4213-bcc9-9baf84f1494b"> Use Cohere RAG to provide grounded results <img width="822" alt="image" src="https://github.com/langchain-ai/langchain/assets/144115527/7bb4a883-5316-41a9-9d2e-19fd49a43dcb"> --------- Co-authored-by: Erick Friis <erick@langchain.dev>
44 lines
1.0 KiB
Python
44 lines
1.0 KiB
Python
from langchain.prompts import ChatPromptTemplate
|
|
from langchain.schema.output_parser import StrOutputParser
|
|
from langchain.schema.runnable 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
|