• The Musings On AI
  • Posts
  • 🌻 Edition 19b: Learn ReAct - Advanced Way of Prompting - Debugging - Less Hallucination

🌻 Edition 19b: Learn ReAct - Advanced Way of Prompting - Debugging - Less Hallucination

Have you searched "Matthew Perry Death Date" on Gpt4

Imagine having a conversation with someone, and just when you think you've said it all, you dive deeper into the topic, unraveling new layers of insight. It's like a dance of words and ideas. Well, guess what? Large language models (LLMs) like the ones we're going to discuss today have a similar knack. They don't just talk; they hallucinate and explore the depths of language and thought.

But here's where it gets fascinating. Google has crafted a remarkable framework that empowers these LLMs to seamlessly blend their creative thinking with practical, task-oriented actions. It's a fusion of imagination and execution that we're about to delve into right here.

ReAct enables language models to generate both verbal reasoning traces and text actions in an interleaved manner. While actions lead to observation feedback from an external environment (“Env” in the figure below), reasoning traces do not affect the external environment. Instead, they affect the internal state of the model by reasoning over the context and updating it with useful information to support future reasoning and acting.

ℹ️ Details

When you ask a question to the LLM model without providing any detail, it tends to provide an answer. After that, it often tries to justify that answer by providing reasoning. In other words, the LLM model tends to double down on the initial answer it gives you. It hallucinates many times in the justification.

We have noticed more satisfactory performance after using the Chain of Thought. The ReAct is a higher version of the CoT. In the CoT philosophy, we prompt the reasons and answer that primes the better answer. In this context learning process, we have observed better results for the logical reasoning questions and decision-making scenarios.

There is also another paradigm that if we can get them to actually do something in an environment(read it Langchain), take an action in the environment, and then use that as an observation back in there.

Here ReAct comes in, it couples both ideas and produces better results with it.

đź‘ş An Example from the Blogpost

  • Ask this question and look at the result (The correct result is 2000)

  • If we use only Chain-of-Thought philosophy then…Still not the correct response.

  • Let’s try the Search Agent (External Environment) and retrieve the data from the environments only.

  • Now Explore it with ReAct

  • More examples from the Paper.

🎋 Lets Code A Mutihop Example

We will search the answer of the following question : “What is the first film that ar rahman won an Oscar for, and who directed that movie?“

The answer should be “Slumdog Millionaire and Danny Boyle”

But how? How can we debug? Can we implement a ReAct with it - Lets try.

import osos.environ["OPENAI_API_KEY"] = ""from langchain import OpenAI, Wikipediafrom langchain.agents import initialize_agent, Toolfrom langchain.agents import AgentTypefrom langchain.agents.react.base import DocstoreExplorerimport langchainlangchain.debug = Truedocstore=DocstoreExplorer(Wikipedia())tools = [ Tool( name="Search", func=docstore.search, description="useful for when you need to ask with search" ), Tool( name="Lookup", func=docstore.lookup, description="useful for when you need to ask with lookup" )]llm = OpenAI(temperature=0, model_name="text-davinci-003")react = initialize_agent(tools, llm, agent=AgentType.REACT_DOCSTORE, verbose=True)# multi hop examplequestion = "What is the first film that ar rahman won an Oscar for, and who directed that movie?"react.run(question)#########Output#########################################################> Entering new AgentExecutor chain...Thought: I need to search ar rahman, find the first film he won an Oscar for, and find who directed that movie.Action: Search[ar rahman]Observation: Could not find [ar rahman]. Similar: ['Ar-Rahman', 'A. R. Rahman', 'Abd al-Rahman', 'A. R. Rahman discography', 'Abd al-Rahman I', 'Abd al-Rahman V', 'Abd ar-Rahman II', 'Abd al-Rahman III', 'Abd al-Rahman ibn Awf', 'Abd al-Rahman IV']Thought: To find the first film ar rahman won an Oscar for, I can search A. R. Rahman.Action: Search[A. R. Rahman]WARNING:langchain.llms.base:Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for text-davinci-003 in organization org-JiZ3N9IjnE61FIMO3rVVK2NL on requests per min. Limit: 3 / min. Please try again in 20s. Visit https://platform.openai.com/account/rate-limits to learn more. You can increase your rate limit by adding a payment method to your account at https://platform.openai.com/account/billing..Observation: Allah Rakha Rahman (; born A. S. Dileep Kumar; 6 January 1967) is an Indian music composer, record producer, singer, songwriter, musician, multi-instrumentalist and philanthropist, popular for his works in Indian cinema; predominantly in Tamil and Hindi films, with occasional forays in international cinema. He is a winner of six National Film Awards, two Academy Awards, two Grammy Awards, a BAFTA Award, a Golden Globe Award, fifteen Filmfare Awards and seventeen Filmfare Awards South. In 2010, the Indian government conferred him with the Padma Bhushan, the nation's third-highest civilian award.With his in-house studio Panchathan Record Inn, Rahman's film-scoring career began during the early 1990s with the Tamil film Roja. Following that, he went on to score several songs for Tamil language films, including Ratnam's politically charged Bombay, the urban Kadhalan, Thiruda Thiruda, and S. Shankar's debut film Gentleman. Rahman's score for his first Hollywood film, the comedy Couples Retreat (2009), won the BMI Award for Best Score. His music for Slumdog Millionaire (2008) earned him Best Original Score and Best Original Song at the 81st Academy Awards. He was also awarded Best Compilation Soundtrack Album and Best Song Written for Visual Media at the 2010 Grammy Awards. He is nicknamed "Isai Puyal" (musical storm) and "Mozart of Madras".Rahman has also become a humanitarian and philanthropist, donating and raising money for a number of causes and charities. In 2006, he was honoured by Stanford University for his contributions to global music. In 2008, he received Lifetime Achievement Award from the Rotary Club of Madras. In 2009, he was included on the Time list of the world's 100 most influential people. In 2013, he introduced 7.1 surround sound technology to South Indian films. In 2014, he was awarded an honorary doctorate from Berklee College of Music. He has also received honorary doctorate from Aligarh Muslim University. In 2017, he made his debut as a director and writer for the film Le Musk.Thought:WARNING:langchain.llms.base:Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for text-davinci-003 in organization org-JiZ3N9IjnE61FIMO3rVVK2NL on requests per min. Limit: 3 / min. Please try again in 20s. Visit https://platform.openai.com/account/rate-limits to learn more. You can increase your rate limit by adding a payment method to your account at https://platform.openai.com/account/billing.. A. R. Rahman won an Oscar for Slumdog Millionaire (2008). I need to search Slumdog Millionaire and find who directed it.Action: Search[Slumdog Millionaire]WARNING:langchain.llms.base:Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for text-davinci-003 in organization org-JiZ3N9IjnE61FIMO3rVVK2NL on requests per min. Limit: 3 / min. Please try again in 20s. Visit https://platform.openai.com/account/rate-limits to learn more. You can increase your rate limit by adding a payment method to your account at https://platform.openai.com/account/billing..Observation: Slumdog Millionaire is a 2008 British drama film that is a loose adaptation of the novel Q & A (2005) by Indian author Vikas Swarup. It narrates the story of 18-year-old Jamal Malik from the Juhu slums of Mumbai. Starring Dev Patel in his film debut as Jamal, and filmed in India, the film was directed by Danny Boyle, written by Simon Beaufoy, and produced by Christian Colson, with Loveleen Tandan credited as co-director. As a contestant on Kaun Banega Crorepati, an Indian-Hindi version of Who Wants to Be a Millionaire?, Jamal surprises everyone by being able to answer every question correctly, winning ₹2 crore (US$460,000). Accused of cheating, Jamal recounts his life story to the police, illustrating how he is able to answer each question correctly.After its world premiere at the Telluride Film Festival and later screenings at the Toronto International Film Festival and the London Film Festival, Slumdog Millionaire had a nationwide release in the United Kingdom on 9 January 2009, in India on 23 January 2009, where it saw the majority of its original success and notoriety. In the United States, the film was released on 25 December 2008. Regarded as a sleeper hit, Slumdog Millionaire was widely acclaimed, being praised for its plot, soundtrack, cinematography, editing, direction, and performances (especially Patel's). It was nominated for ten Academy Awards in 2009 and won eight—the most for any 2008 film—including Best Picture, Best Director, and Best Adapted Screenplay. It won seven BAFTA Awards including Best Film, five Critics' Choice Awards and four Golden Globes. However, reception in India and among Indian diaspora was mixed, and the film was the subject of controversy over its depiction of poverty in India and other issues. The Hindustan Times called it "an assault on Indian self-esteem".Thought:WARNING:langchain.llms.base:Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for text-davinci-003 in organization org-JiZ3N9IjnE61FIMO3rVVK2NL on requests per min. Limit: 3 / min. Please try again in 20s. Visit https://platform.openai.com/account/rate-limits to learn more. You can increase your rate limit by adding a payment method to your account at https://platform.openai.com/account/billing..WARNING:langchain.llms.base:Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for text-davinci-003 in organization org-JiZ3N9IjnE61FIMO3rVVK2NL on requests per min. Limit: 3 / min. Please try again in 20s. Visit https://platform.openai.com/account/rate-limits to learn more. You can increase your rate limit by adding a payment method to your account at https://platform.openai.com/account/billing..WARNING:langchain.llms.base:Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 8.0 seconds as it raised RateLimitError: Rate limit reached for text-davinci-003 in organization org-JiZ3N9IjnE61FIMO3rVVK2NL on requests per min. Limit: 3 / min. Please try again in 20s. Visit https://platform.openai.com/account/rate-limits to learn more. You can increase your rate limit by adding a payment method to your account at https://platform.openai.com/account/billing.. Slumdog Millionaire was directed by Danny Boyle. So the answer is Slumdog Millionaire and Danny Boyle.Action: Finish[Slumdog Millionaire and Danny Boyle]> Finished chain.Slumdog Millionaire and Danny Boyle

Yeah we found the result.

🥺 When did chandler bing from friends die?

Now Let’s ask the saddest question of the time - Unfortunately, wrong answer.

Now lets try ReAct

Code

from langchain import OpenAI, Wikipediafrom langchain.agents import initialize_agent, Toolfrom langchain.agents import AgentTypefrom langchain.agents.react.base import DocstoreExplorerimport langchainlangchain.debug = Truedocstore=DocstoreExplorer(Wikipedia())tools = [ Tool( name="Search", func=docstore.search, description="useful for when you need to ask with search" ), Tool( name="Lookup", func=docstore.lookup, description="useful for when you need to ask with lookup" )]llm = OpenAI(temperature=0, model_name="text-davinci-003")react = initialize_agent(tools, llm, agent=AgentType.REACT_DOCSTORE, verbose=True)question = "when did chandler bing from friends die?"react.run(question)

Final Result with proper Debugging

Am not adding the debugging result sake of space. You can see the text here.

[21.46s] Exiting LLM run with output:{ "generations": [ [ { "text": " The paragraph mentions Matthew Perry death date is October 28, 2023, so the answer is October 28, 2023.\nAction: Finish[October 28, 2023]", "generation_info": { "finish_reason": "stop", "logprobs": null }, "type": "Generation" } ] ], "llm_output": { "token_usage": { "total_tokens": 2576, "prompt_tokens": 2541, "completion_tokens": 35 }, "model_name": "text-davinci-003" }, "run": null}[chain/end] [1:chain:AgentExecutor > 17:chain:LLMChain] [21.46s] Exiting Chain run with output:{ "text": " The paragraph mentions Matthew Perry death date is October 28, 2023, so the answer is October 28, 2023.\nAction: Finish[October 28, 2023]"}[chain/end] [1:chain:AgentExecutor] [101.99s] Exiting Chain run with output:{ "output": "October 28, 2023"}October 28, 2023

And we got the correct answer because it was searching the data on wiki and generates the data with proper debugging.

**

I will publish the next Edition on Sunday.

This is the 19th Edition, If you have any feedback please don’t hesitate to share it with me, And if you love my work, do share it with your colleagues.

It takes time to research and document it - Please be a paid subscriber and support my work.

Cheers!!

Raahul

**

Reply

or to participate.