MultiPromptChain Router

How can we use router to choose the best prompt for a given task?

Setup

Load the API key and relevant Python libaries.

If you use google colab please install the following packages:

pip install "langchain>=0.0.200"
from langchain.chat_models import ChatOpenAI
chat = ChatOpenAI(
    temperature=0.0,
    # openai_api_key="sk-<your key here>"  # uncomment to use your own API key, othewise uses default .env key
)
from langchain.prompts import ChatPromptTemplate
summary_template = """
Summarize the following text delimited by triple backticks with 1 sentence.
Paper:
```{input}```
"""
general_audience_summary_template = """
Generate general audience a title for research paper.
Paper:
```{input}```
"""
twitter_template = """
Prepare a tweet thread with 3 tweets based on paper description.
Paper:
```{input}```
"""
linkedin_template = """
Prepare a longer linkedin post based on paper description. 
Paper:
```{input}```
"""
from langchain.chains.router import MultiPromptChain
from langchain.llms import OpenAI
prompt_infos = [
    {
        "name": "summary", 
        "description": "Good for academic summarizing research papers",
        "prompt_template": summary_template
    },
    {
        "name": "general_title", 
        "description": "Good for general audience title of paper", 
        "prompt_template": general_audience_summary_template
    },
    {
        "name": "twitter",
        "description": "Good for twitter thread",
        "prompt_template": twitter_template
    },
    {
        "name": "linkedin",
        "description": "Good for linkedin post",
        "prompt_template": linkedin_template
    }
]
chain = MultiPromptChain.from_prompts(OpenAI(), prompt_infos, verbose=True)
chain.run("""
generale academic summary for 
Massively Multilingual Corpus of Sentiment Datasets and Multi-faceted Sentiment Classification Benchmark
Łukasz Augustyniak, Szymon Woźniak, Marcin Gruza, Piotr Gramacki, Krzysztof Rajda, Mikołaj Morzy, Tomasz Kajdanowicz
Despite impressive advancements in multilingual corpora collection and model training, developing large-scale deployments of multilingual models still presents a significant challenge. This is particularly true for language tasks that are culture-dependent. One such example is the area of multilingual sentiment analysis, where affective markers can be subtle and deeply ensconced in culture. This work presents the most extensive open massively multilingual corpus of datasets for training sentiment models. The corpus consists of 79 manually selected datasets from over 350 datasets reported in the scientific literature based on strict quality criteria. The corpus covers 27 languages representing 6 language families. Datasets can be queried using several linguistic and functional features. In addition, we present a multi-faceted sentiment classification benchmark summarizing hundreds of experiments conducted on different base models, training objectives, dataset collections, and fine-tuning strategies.
""")


> Entering new  chain...
summary: {'input': 'This work presents the most extensive open massively multilingual corpus of datasets for training sentiment models. The corpus consists of 79 manually selected datasets from over 350 datasets reported in the scientific literature based on strict quality criteria. The corpus covers 27 languages representing 6 language families. Datasets can be queried using several linguistic and functional features. In addition, we present a multi-faceted sentiment classification benchmark summarizing hundreds of experiments conducted on different base models, training objectives, dataset collections, and fine-tuning strategies.'}
> Finished chain.

This paper presents an extensive open corpus of datasets and a multi-faceted sentiment classification benchmark for 27 languages from 6 language families.
chain.run("""
I need it for twitter 
Massively Multilingual Corpus of Sentiment Datasets and Multi-faceted Sentiment Classification Benchmark
Łukasz Augustyniak, Szymon Woźniak, Marcin Gruza, Piotr Gramacki, Krzysztof Rajda, Mikołaj Morzy, Tomasz Kajdanowicz
Despite impressive advancements in multilingual corpora collection and model training, developing large-scale deployments of multilingual models still presents a significant challenge. This is particularly true for language tasks that are culture-dependent. One such example is the area of multilingual sentiment analysis, where affective markers can be subtle and deeply ensconced in culture. This work presents the most extensive open massively multilingual corpus of datasets for training sentiment models. The corpus consists of 79 manually selected datasets from over 350 datasets reported in the scientific literature based on strict quality criteria. The corpus covers 27 languages representing 6 language families. Datasets can be queried using several linguistic and functional features. In addition, we present a multi-faceted sentiment classification benchmark summarizing hundreds of experiments conducted on different base models, training objectives, dataset collections, and fine-tuning strategies.
""")


> Entering new  chain...
twitter: {'input': 'Introducing the most extensive open massively multilingual corpus of datasets for training sentiment models. With 79 manually selected datasets from over 350 datasets reported in the scientific literature, the corpus covers 27 languages representing 6 language families. Learn more about our multi-faceted sentiment classification benchmark!'}
> Finished chain.
'\n1. Introducing the most extensive open massively multilingual corpus of datasets for training sentiment models! 79 manually selected datasets from over 350 datasets reported in the scientific literature. Learn more about our multi-faceted sentiment classification benchmark. #SentimentAnalysis #DataScience #AI \n\n2. Covering 27 languages from 6 language families, our corpus provides a comprehensive resource for researchers working in sentiment analysis. Check out our benchmark and explore the potential of sentiment models! #SentimentAnalysis #DataScience #AI \n\n3. Ready to take your sentiment analysis models to the next level? Our multilingual corpus provides the resources you need. Download it now and take advantage of the best sentiment models available! #SentimentAnalysis #DataScience #AI'