templates[patch]: Add cohere librarian template (#14601)

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>
pull/14686/head
billytrend-cohere 7 months ago committed by GitHub
parent 47451951a1
commit 7e4dbb26a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,71 @@
# cohere-librarian
This template turns Cohere into a librarian.
It demonstrates the use of a router to switch between chains that can handle different things: a vector database with Cohere embeddings; a chat bot that has a prompt with some information about the library; and finally a RAG chatbot that has access to the internet.
For a fuller demo of the book recomendation, consider replacing books_with_blurbs.csv with a larger sample from the following dataset: https://www.kaggle.com/datasets/jdobrow/57000-books-with-metadata-and-blurbs/ .
## Environment Setup
Set the `COHERE_API_KEY` environment variable to access the Cohere models.
## Usage
To use this package, you should first have the LangChain CLI installed:
```shell
pip install -U langchain-cli
```
To create a new LangChain project and install this as the only package, you can do:
```shell
langchain app new my-app --package cohere-librarian
```
If you want to add this to an existing project, you can just run:
```shell
langchain app add cohere-librarian
```
And add the following code to your `server.py` file:
```python
from cohere_librarian.chain import chain as cohere_librarian_chain
add_routes(app, cohere_librarian_chain, path="/cohere-librarian")
```
(Optional) Let's now configure LangSmith.
LangSmith will help us trace, monitor and debug LangChain applications.
LangSmith is currently in private beta, you can sign up [here](https://smith.langchain.com/).
If you don't have access, you can skip this section
```shell
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=<your-api-key>
export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"
```
If you are inside this directory, then you can spin up a LangServe instance directly by:
```shell
langchain serve
```
This will start the FastAPI app with a server is running locally at
[http://localhost:8000](http://localhost:8000)
We can see all templates at [http://localhost:8000/docs](http://localhost:8000/docs)
We can access the playground at [http://localhost:8000/cohere-librarian/playground](http://localhost:8000/cohere-librarian/playground)
We can access the template from code with:
```python
from langserve.client import RemoteRunnable
runnable = RemoteRunnable("http://localhost:8000/cohere-librarian")
```

@ -0,0 +1,3 @@
from .chain import chain
__all__ = ["chain"]

@ -0,0 +1,49 @@
import csv
from langchain.chains.question_answering import load_qa_chain
from langchain.embeddings import CohereEmbeddings
from langchain.prompts import PromptTemplate
from langchain.vectorstores import Chroma
from .chat import chat
csv_file = open("data/books_with_blurbs.csv", "r")
csv_reader = csv.reader(csv_file)
csv_data = list(csv_reader)
parsed_data = [
{
"id": x[0],
"title": x[1],
"author": x[2],
"year": x[3],
"publisher": x[4],
"blurb": x[5],
}
for x in csv_data
]
parsed_data[1]
embeddings = CohereEmbeddings()
docsearch = Chroma.from_texts(
[x["title"] for x in parsed_data], embeddings, metadatas=parsed_data
).as_retriever()
prompt_template = """
{context}
Use the book reccommendations to suggest books for the user to read.
Only use the titles of the books, do not make up titles. Format the response as
a bulleted list prefixed by a relevant message.
User: {message}"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "message"]
)
book_rec_chain = {
"input_documents": lambda x: docsearch.get_relevant_documents(x["message"]),
"message": lambda x: x["message"],
} | load_qa_chain(chat, chain_type="stuff", prompt=PROMPT)

@ -0,0 +1,10 @@
from langchain.pydantic_v1 import BaseModel
from .router import branched_chain
class ChainInput(BaseModel):
message: str
chain = branched_chain.with_types(input_type=ChainInput)

@ -0,0 +1,3 @@
from langchain.llms import Cohere
chat = Cohere()

@ -0,0 +1,27 @@
from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
from .chat import chat
librarian_prompt = ChatPromptTemplate.from_messages(
[
SystemMessagePromptTemplate.from_template(
"""
You are a librarian at cohere community library. Your job is to
help recommend people books to read based on their interests and
preferences. You also give information about the library.
The library opens at 8am and closes at 9pm daily. It is closed on
Sundays.
Please answer the following message:
"""
),
HumanMessagePromptTemplate.from_template("{message}"),
]
)
library_info = librarian_prompt | chat

@ -0,0 +1,16 @@
from langchain.chat_models import ChatCohere
from langchain.retrievers import CohereRagRetriever
rag = CohereRagRetriever(llm=ChatCohere())
def get_docs_message(message):
docs = rag.get_relevant_documents(message)
message_doc = next(
(x for x in docs if x.metadata.get("type") == "model_response"), None
)
return message_doc.page_content
def librarian_rag(x):
return get_docs_message(x["message"])

@ -0,0 +1,43 @@
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

@ -0,0 +1,21 @@
ISBN,Title,Author,Year,Publisher,Blurb, ** grab this file from https://www.kaggle.com/datasets/jdobrow/57000-books-with-metadata-and-blurbs/ **
0060973129,Decision in Normandy,Carlo D'Este,1991,HarperPerennial,"Here, for the first time in paperback, is an outstanding military history that offers a dramatic new perspective on the Allied campaign that began with the invasion of the D-Day beaches of Normandy. Nationa advertising in Military History."
0374157065,Flu: The Story of the Great Influenza Pandemic of 1918 and the Search for the Virus That Caused It,Gina Bari Kolata,1999,Farrar Straus Giroux,"The fascinating, true story of the world's deadliest disease. ,In 1918, the Great Flu Epidemic felled the young and healthy virtually overnight. An estimated forty million people died as the epidemic raged. Children were left orphaned and families were devastated. As many American soldiers were killed by the 1918 flu as were killed in battle during World War I. And no area of the globe was safe. Eskimos living in remote outposts in the frozen tundra were sickened and killed by the flu in such numbers that entire villages were wiped out. ,Scientists have recently rediscovered shards of the flu virus frozen in Alaska and preserved in scraps of tissue in a government warehouse. Gina Kolata, an acclaimed reporter for ""The New York Times,"" unravels the mystery of this lethal virus with the high drama of a great adventure story. Delving into the history of the flu and previous epidemics, detailing the science and the latest understanding of this mortal disease, Kolata addresses the prospects for a great epidemic recurring, and, most important, what can be done to prevent it."
0399135782,The Kitchen God's Wife,Amy Tan,1991,Putnam Pub Group,"Winnie and Helen have kept each others worst secrets for more than fifty years. Now, because she believes she is dying, Helen wants to expose everything. And Winnie angrily determines that she must be the one to tell her daughter, Pearl, about the past—including the terrible truth even Helen does not know. And so begins Winnie's story of her life on a small island outside Shanghai in the 1920s, and other places in China during World War II, and traces the happy and desperate events that led to Winnie's coming to America in 1949."
0425176428,What If?: The World's Foremost Military Historians Imagine What Might Have Been,Robert Cowley,2000,Berkley Publishing Group,"Historians and inquisitive laymen alike love to ponder the dramatic what-its of history. In these twenty never-before-published essays, some of the keenest minds of our time ask the big, tantalizing questions:, Where might we be if history had not unfolded the way it did? , Why, how, and when was our fortune made real? ,The answers are surprising, sometimes frightening, and always entertaining.."
1881320189,Goodbye to the Buttermilk Sky,Julia Oliver,1994,River City Pub,"This highly praised first novel by fiction writer Julia Oliver is the story of one young woman's struggle with fidelity and identity in depression-era rural Alabama. A beautifully narrated novel of time and place, Goodbye to the Buttermilk Sky re-creates a southern summer when the depression and the boll weevil turned hopes to dust. With the extraordinary talent to make the reader see the Ball canning jars on the kitchen table, hear the clicks on the party line, and feel the bittersweet moments of 20-year-old Callie Tatum's first experiences with adult desire, Oliver portrays a young wifes increasingly dangerous infidelity with cinematic precision and palpable suspense. Soon, with only her housekeeper as a confidant, Callie breaks societys rules about race and class as well as her marriage vows. The result is a chain of events that will lead to tragedy and a womans stunning decision about love, passion, and the future of her life.Originally published in cloth in 1994, Goodbye to the Buttermilk Sky received considerable attention nationally and became a featured selection of the Quality Paperback Book Club. Its inclusion in the Deep South Books series from The University of Alabama Press will extend the books reach and its life, while offering new readers access to the enthralling story.The richly drawn, fully developed characters of Buttermilk Sky live on in the readers mind long after the book has been finished. Against the emotional and physical isolation of rural Alabama in 1938, the threads of family ties, whispered gossip, old secrets, and unfulfilled dreams weave a powerful, evocative story that captivates its reader until the very last word."
0440234743,The Testament,John Grisham,1999,Dell,"In a plush Virginia office, a rich, angry old man is furiously rewriting his will. With his death just hours away, Troy Phelan wants to send a message to his children, his ex-wives, and his minions, a message that will touch off a vicious legal battle and transform dozens of lives.,Because Troy Phelan's new will names a sole surprise heir to his eleven-billion-dollar fortune: a mysterious woman named Rachel Lane, a missionary living deep in the jungles of Brazil.,Enter the lawyers. Nate O'Riley is fresh out of rehab, a disgraced corporate attorney handpicked for his last job: to find Rachel Lane at any cost. As Phelan's family circles like vultures in D.C., Nate is crashing through the Brazilian jungle, entering a world where money means nothing, where death is just one misstep away, and where a woman - pursued by enemies and friends alike - holds a stunning surprise of her own."
0452264464,Beloved (Plume Contemporary Fiction),Toni Morrison,1994,Plume,"In the troubled years following the Civil War, the spirit of a murdered child haunts the Ohio home of a former slave. This angry, destructive ghost breaks mirrors, leaves its fingerprints in cake icing, and generally makes life difficult for Sethe and her family; nevertheless, the woman finds the haunting oddly comforting for the spirit is that of her own dead baby, never named, thought of only as Beloved. A dead child, a runaway slave, a terrible secret--these are the central concerns of Toni Morrison's Pulitzer Prize-winning Beloved. Morrison, a Nobel laureate, has written many fine novels, including Song of Solomon, The Bluest Eye, and Paradise--but Beloved is arguably her best. To modern readers, antebellum slavery is a subject so familiar that it is almost impossible to render its horrors in a way that seems neither clichéd nor melodramatic. Rapes, beatings, murders, and mutilations are recounted here, but they belong to characters so precisely drawn that the tragedy remains individual, terrifying to us because it is terrifying to the sufferer. And Morrison is master of the telling detail: in the bit, for example, a punishing piece of headgear used to discipline recalcitrant slaves, she manages to encapsulate all of slavery's many cruelties into one apt symbol--a device that deprives its wearer of speech. ""Days after it was taken out, goose fat was rubbed on the corners of the mouth but nothing to soothe the tongue or take the wildness out of the eye."" Most importantly, the language here, while often lyrical, is never overheated. Even as she recalls the cruelties visited upon her while a slave, Sethe is evocative without being overemotional: ""Add my husband to it, watching, above me in the loft--hiding close by--the one place he thought no one would look for him, looking down on what I couldn't look at at all. And not stopping them--looking and letting it happen.... And if he was that broken then, then he is also and certainly dead now."" Even the supernatural is treated as an ordinary fact of life: ""Not a house in the country ain't packed to its rafters with some dead Negro's grief. We lucky this ghost is a baby,"" comments Sethe's mother-in-law. Beloved is a dense, complex novel that yields up its secrets one by one. As Morrison takes us deeper into Sethe's history and her memories, the horrifying circumstances of her baby's death start to make terrible sense. And as past meets present in the shape of a mysterious young woman about the same age as Sethe's daughter would have been, the narrative builds inexorably to its powerful, painful conclusion. Beloved may well be the defining novel of slavery in America, the one that all others will be measured by. --Alix Wilber"
0609804618,Our Dumb Century: The Onion Presents 100 Years of Headlines from America's Finest News Source,The Onion,1999,Three Rivers Press," has quickly become the world's most popular humor publication, misinforming half a million readers a week with one-of-a-kind social satire both in print (on newsstands nationwide) and online from its remote office in Madison, Wisconsin.,Witness the march of history as Editor-in-Chief Scott Dikkers and , award-winning writing staff present the twentieth century like you've never seen it before."
1841721522,New Vegetarian: Bold and Beautiful Recipes for Every Occasion,Celia Brooks Brown,2001,Ryland Peters &amp; Small Ltd,"Filled with fresh and eclectic recipes by Celia Brooks Brown -- one of the talented team of chefs at Books for Cooks, the world-famous bookshop-restaurant in London's Notting Hill -- New Vegetarian presents an innovative approach to vegetarian cooking. No longer the exclusive domain of vegetarians, meat-free food is now appreciated by all for its bright and assertive flavors, its marvelous colors, and its easy-to-make convenience. Celia gives sensible advice on choosing and preparing the major vegetarian ingredients, then presents 50 original and stylish recipes -- ranging from quick breakfasts to party foods, from salads to sweet treats -- all photographed by Philip Webb. Whether it is burritos bursting with flavor or Thai Glazed Vegetable Skewers fresh from the barbecue, Celia's enthusiasm and imagination will tempt even the most confirmed carnivore."
0439095026,Tell Me This Isn't Happening,Robynn Clairday,1999,Scholastic,"Robynn Clairday interviewed kids throughout America to collect these real tales of awkward situations. From hilarious to poignant to painful, these stories are accompanied by advice about dealing with embarrassment and finding grace under pressure."
0971880107,Wild Animus,Rich Shapero,2004,Too Far,"Newly graduated from college, Sam Altman is gripped by an inexplicable urge to lose himself in the wilderness and teams up with an enigmatic young woman who seems bent on helping him realize his dreams."
0345402871,Airframe,Michael Crichton,1997,Ballantine Books,"Three passengers are dead. Fifty-six are injured. The interior cabin virtually destroyed. But the pilot manages to land the plane. . . .,At a moment when the issue of safety and death in the skies is paramount in the public mind, a lethal midair disaster aboard a commercial twin-jet airliner bound from Hong Kong to Denver triggers a pressured and frantic investigation.,AIRFRAME is nonstop reading: the extraordinary mixture of super suspense and authentic information on a subject of compelling interest that has been a Crichton landmark since The Andromeda Strain.,(back cover)"
0345417623,Timeline,MICHAEL CRICHTON,2000,Ballantine Books,"In an Arizona desert, a man wanders in a daze, speaking words that make no sense. Within twenty-four hours he is dead, his body swiftly cremated by his only known associates. Halfway around the world, archaeologists make a shocking discovery at a medieval site. Suddenly they are swept off to the headquarters of a secretive multinational corporation that has developed an astounding technology. Now this group is about to get a chance not to study the past but to enter it. And with history opened up to the present, the dead awakened to the living, these men and women will soon find themselves fighting for their very survival six hundred years ago."
0684823802,OUT OF THE SILENT PLANET,C.S. Lewis,1996,Scribner,"Dr. Ransom is abducted by a megalomaniacal physicist and taken via space ship to the planet Malacandra (Mars). There, Dr. Ransom finds Malacandra similar to, and yet distinct from, Earth."
0375759778,Prague : A Novel,ARTHUR PHILLIPS,2003,Random House Trade Paperbacks,"A novel of startling scope and ambition, , depicts an intentionally lost Lost Generation as it follows five American expats who come to Budapest in the early 1990s to seek their fortune. They harbor the vague suspicion that their counterparts in Prague have it better, but still they hope to find adventure, inspiration, a gold rush, or history in the making."
0425163091,Chocolate Jesus,Stephan Jaramillo,1998,Berkley Publishing Group,"The deliciously funny new novel from the acclaimed author of Going Postal.Now, in Chocolate Jesus, Jaramillo introduces Sydney Corbet, a self-proclaimed JFK assassination scholar who has just come up with the idea of a lifetime -- Chocolate Jesus. This semisweet chocolate Messiah offers salvation for many, especially the nearly extinct Bea's Candies, whose Easter promotion just might turn things around for the company. Everyone knows that the Easter Bunny can't compete with the King of Kings. But no one counted on the Reverend Willy Domingo and his vegetarian fitness zealots, who gather on a crusade against a graven image of Christ that consists of nothing more than empty calories...,""Capture(s) the mood and voice of a certain distinctive type of apprentice grown-up"". -- New York Times Book Review,""Enormously entertaining moments, reverberating with a cynical wit underscored...with heartbreaking poignancy and disillusion...haunting, honest"". -- St. Petersburg Times,""A very funny book... Jaramillo deserves credit for a crisp and funny style, a dead-on ear for dialogue, but more impressive, an emotional honesty that's rare and wonderful"". -- Rocky Mountain News"
0375406328,Lying Awake,Mark Salzman,2000,Alfred A. Knopf,"In a Carmelite monastery outside present-day Los Angeles, life goes on in a manner virtually un-changed for centuries. Sister John of the Cross has spent years there in the service of God. And there, she alone experiences visions of such dazzling power and insight that she is looked upon as a spiritual master. ,But Sister John's visions are accompanied by powerful headaches, and when a doctor reveals that they may be dangerous, she faces a devastating choice. For if her spiritual gifts are symptoms of illness rather than grace, will a ""cure"" mean the end of her visions and a soul once again dry and searching?,This is the dilemma at the heart of Mark Salzman's spare, astonishing new novel. With extraordinary dexterity, the author of the best-selling , and , brings to life the mysterious world of the cloister, giving us a brilliantly realized portrait of women today drawn to the rigors of an ancient religious life, and of one woman's trial at the perilous intersection of faith and reason. , is a novel of remarkable empathy and imagination, and Mark Salzman's most provocative work to date."
0446310786,To Kill a Mockingbird,Harper Lee,1988,Little Brown &amp; Company,"The unforgettable novel of a childhood in a sleepy Southern town and the crisis of conscience that rocked it, To Kill A Mockingbird became both an instant bestseller and a critical success when it was first published in 1960. It went on to win the Pulitzer Prize in 1961 and was later made into an Academy Award-winning film, also a classic.,Compassionate, dramatic, and deeply moving, To Kill A Mockingbird takes readers to the roots of human behavior - to innocence and experience, kindness and cruelty, love and hatred, humor and pathos. Now with over 18 million copies in print and translated into forty languages, this regional story by a young Alabama woman claims universal appeal. Harper Lee always considered her book to be a simple love story. Today it is regarded as a masterpiece of American literature."
0449005615,Seabiscuit: An American Legend,LAURA HILLENBRAND,2002,Ballantine Books,"Seabiscuit was one of the most electrifying and popular attractions in sports history and the single biggest newsmaker in the world in 1938, receiving more coverage than FDR, Hitler, or Mussolini. But his success was a surprise to the racing establishment, which had written off the crooked-legged racehorse with the sad tail. Three men changed Seabiscuits fortunes:,Charles Howard was a onetime bicycle repairman who introduced the automobile to the western United States and became an overnight millionaire. When he needed a trainer for his new racehorses, he hired Tom Smith, a mysterious mustang breaker from the Colorado plains. Smith urged Howard to buy Seabiscuit for a bargain-basement price, then hired as his jockey Red Pollard, a failed boxer who was blind in one eye, half-crippled, and prone to quoting passages from Ralph Waldo Emerson. Over four years, these unlikely partners survived a phenomenal run of bad fortune, conspiracy, and severe injury to transform Seabiscuit from a neurotic, pathologically indolent also-ran into an American sports icon. ,Author Laura Hillenbrand brilliantly re-creates a universal underdog story, one that proves life is a horse race."
0060168013,Pigs in Heaven,Barbara Kingsolver,1993,Harpercollins,"Brings together Taylor, Turtle and Alice from The Bean Trees together with a new cast - Jax, Barbie Sugar Boss, Oklahoma and Annawake Fourkiller. When six-year-old Turtle witnesses a freak accident at the Hoover Dam, her insistence, and her mother's belief in her, leads to a man's rescue."
Can't render this file because it has a wrong number of fields in line 2.

File diff suppressed because it is too large Load Diff

@ -0,0 +1,33 @@
[tool.poetry]
name = "cohere-librarian"
version = "0.0.1"
description = "Get started with a simple template that acts as a librarian"
authors = []
readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
langchain = ">=0.0.325"
cohere = "^4.37"
chromadb = "^0.4.18"
[tool.poetry.group.dev.dependencies]
langchain-cli = ">=0.0.15"
fastapi = "^0.104.0"
sse-starlette = "^1.6.5"
[tool.langserve]
export_module = "cohere_librarian"
export_attr = "chain"
[tool.templates-hub]
use-case = "chatbot"
author = "Cohere"
integrations = ["Cohere"]
tags = ["getting-started"]
[build-system]
requires = [
"poetry-core",
]
build-backend = "poetry.core.masonry.api"
Loading…
Cancel
Save