Issue with HuggingFace pipeline with RouterOutputParser OutputParserException: Got invalid return object. Expected key destination to be present, but got {}

#144
by Jyotiyadav - opened

import re
import os
import requests
from PIL import Image
import gradio as gr
from langchain_groq import ChatGroq
from langchain.prompts import PromptTemplate
from langchain.chains import ConversationChain
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain.agents import Tool, initialize_agent
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import LLMChain
from langchain_community.llms import HuggingFaceHub
from langchain.chains.router import MultiPromptChain
from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser
from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE
from langchain_community.llms import HuggingFaceEndpoint
#auth_token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
from google.colab import userdata
HUGGINGFACE_TOKEN=userdata.get('HUGGINGFACE_TOKEN')
from transformers import AutoModelForCausalLM, AutoTokenizer,pipeline
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from langchain import HuggingFaceHub
import warnings
warnings.filterwarnings("ignore")
from transformers import pipeline
import torch

llm = HuggingFacePipeline.from_model_id(
model_id="mistralai/Mistral-7B-v0.1",
task="text-generation",
pipeline_kwargs={"max_new_tokens": 1000},
)

wikipedia = WikipediaAPIWrapper()
search = DuckDuckGoSearchRun()

wikipedia_tool = Tool(
name='wikipedia',
func= wikipedia.run,
description="This tool leverages Wikipedia to gather information about Ingredients name ,description of the dish , Allergens , additional information . It's particularly useful for obtaining detailed and reliable information on various topics"
)

duckduckgo_tool = Tool(
name='DuckDuckGo Search',
func= search.run,
description="Useful for when you need to do a search on the internet to find information that another tool can't find. Always be specific with your input."
)

tools = [
Tool(
name = "DuckDuckGo Search",
func=duckduckgo_tool.run,
description="useful for when you need answer questions from internet"
)
]

tools.append(wikipedia_tool)

zero_shot_agent = initialize_agent(
agent="zero-shot-react-description",
tools=tools,
llm=llm,
verbose=True,
handle_parsing_errors=True,
max_iterations=10,
)

def menu_prompt(title):
prompt_menu = f'''
As a restaurant menu manager, your role is to gather below informations based on input data {title} (Name of the dish).
generate the output

### information to be extracted :
<Ingredients>: Only Ingredients included in the dish.
<Description>: Briefly describe the dish.
<Allergens>: Only Choose relevant options from this list - [Cereals, Crustaceans, Egg, Fish, Peanuts, SOYBEAN, Latte, Nuts, Celery, Mustard, Sesame seeds, Sulfur dioxide and sulphites, Shell, Clams].
<Additional Information>: Only Choose relevant options from this list - [Spicy, Vegan, Gluten free, Vegetarian].

### Output Format 
"""
  "ingredients": All Ingredients in a List,
  "description": Description in a string,
  "allergen": All allergen in a List,
  "Additional_information": All Additional_information in a List
"""

### Input data:
 {title}

### Output:
'''
return prompt_menu

def get_router(title):
prompt_menu=menu_prompt(title)

prompt_infos = [
  {
      "name": "Menu Manager",
      "description": "Good for answering questions about Italian Dish[ingredients,description,allergens,additional_information]",
      "prompt_template": prompt_menu,
  }
  ]

# map destination chains
destination_chains = {}
for prompt_info in prompt_infos:
    name = prompt_info["name"]
    prompt_template = prompt_info["prompt_template"]
    prompt = PromptTemplate(template=prompt_template, input_variables=["input"])
    print("prompt: ", prompt)
    chain = LLMChain(llm=llm, prompt=prompt)
    destination_chains[name] = chain
default_chain = ConversationChain(llm=llm)

# Creating LLMRouterChain
destinations = [f"{p['name']}: {p['description']}" for p in prompt_infos]
destinations_str = "\n".join(destinations)
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=destinations_str)
router_prompt = PromptTemplate(
    template=router_template,
    input_variables=["input"],
    output_parser=RouterOutputParser(),
)

# creating the router chain
router_chain = LLMRouterChain.from_llm(llm, router_prompt)

# Multiple Prompt Chain
chain = MultiPromptChain(
    router_chain=router_chain,
    destination_chains=destination_chains,
    default_chain=default_chain,
    verbose=True,
)

# Get response from the agent
response = chain.run(title)
return response

response=get_router("Pizza Margherita")
response

Error Message and Stack Trace (if applicable)

Entering new MultiPromptChain chain...


OutputParserException Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/langchain/chains/router/llm_router.py in parse(self, text)
98 expected_keys = ["destination", "next_inputs"]
---> 99 parsed = parse_and_check_json_markdown(text, expected_keys)
100 if not isinstance(parsed["destination"], str):

16 frames
OutputParserException: Got invalid return object. Expected key destination to be present, but got {}

During handling of the above exception, another exception occurred:

OutputParserException Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/langchain/chains/router/llm_router.py in parse(self, text)
114 return parsed
115 except Exception as e:
--> 116 raise OutputParserException(
117 f"Parsing text\n{text}\n raised following error:\n{e}"
118 )

OutputParserException: Parsing text
Given a raw text input to a language model select the model prompt best suited for the input. You will be given the names of the available prompts and a description of what the prompt is best suited for. You may also revise the original input if you think that revisingit will ultimately lead to a better response from the language model.

<< FORMATTING >>
Return a markdown code snippet with a JSON object formatted to look like:

{
    "destination": string \ name of the prompt to use or "DEFAULT"
    "next_inputs": string \ a potentially modified version of the original input
}
REMEMBER: "destination" MUST be one of the candidate prompt names specified below OR it can be "DEFAULT" if the input is notwell suited for any of the candidate prompts.
REMEMBER: "next_inputs" can just be the original input if you don't think any modifications are needed.

<< CANDIDATE PROMPTS >>
Menu Manager: Good for answering questions about Italian Dish[ingredients,description,allergens,additional_information]

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "Menu Manager",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "DEFAULT",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "Menu Manager",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "DEFAULT",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "Menu Manager",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "DEFAULT",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "Menu Manager",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "DEFAULT",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "Menu Manager",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "DEFAULT",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "Menu Manager",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "DEFAULT",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "Menu Manager",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "DEFAULT",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "Menu Manager",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "DEFAULT",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "Menu Manager",
"next_inputs": "Pizza Margherita"
}

<< INPUT >>
Pizza Margherita

<< OUTPUT (remember to include the json and )>>
{
"destination": "DEFAULT",
"next_inputs": "Pizza Margh
raised following error:
Got invalid return object. Expected key destination to be present, but got {}

Description
OutputParserException: Got invalid return object. Expected key destination to be present, but got {}

Sign up or log in to comment