from langchain_community.tools import DuckDuckGoSearchRun
search = DuckDuckGoSearchRun()
search.invoke("Obama's first name?")
API Reference:DuckDuckGoSearchRun
'When Ann Dunham and Barack Obama Sr. tied the knot, they kept the news to themselves. "Nobody was invited," Neil Abercrombie, a college friend of Obama Sr., told Time in 2008. The wedding came as ... As the head of the government of the United States, the president is arguably the most powerful government official in the world. The president is elected to a four-year term via an electoral college system. Since the Twenty-second Amendment was adopted in 1951, the American presidency has been Most common names of U.S. presidents 1789-2021. Published by. Aaron O'Neill , Jul 4, 2024. The most common first name for a U.S. president is James, followed by John and then William. Six U.S ... Obama's personal charisma, stirring oratory, and his campaign promise to bring change to the established political system resonated with many Democrats, especially young and minority voters. On January 3, 2008, Obama won a surprise victory in the first major nominating contest, the Iowa caucus, over Sen. Hillary Clinton, who was the overwhelming favorite to win the nomination. Former President Barack Obama released a letter about President Biden's decision to drop out of the 2024 presidential race. Notably, Obama did not name or endorse Vice President Kamala Harris.'
To get more additional information (e.g. link, source) use DuckDuckGoSearchResults()
from langchain_community.tools import DuckDuckGoSearchResults
search = DuckDuckGoSearchResults()
search.invoke("Obama")
API Reference:DuckDuckGoSearchResults
"[snippet: Why Obama Hasn't Endorsed Harris. The former president has positioned himself as an impartial elder statesman above intraparty machinations and was neutral during the 2020 Democratic primaries., title: Why Obama Hasn't Endorsed Harris - The New York Times, link: https://www.nytimes.com/2024/07/21/us/politics/why-obama-hasnt-endorsed-harris.html], [snippet: Former President Barack Obama released a letter about President Biden's decision to drop out of the 2024 presidential race. Notably, Obama did not name or endorse Vice President Kamala Harris., title: Read Obama's full statement on Biden dropping out - CBS News, link: https://www.cbsnews.com/news/barack-obama-biden-dropping-out-2024-presidential-race-full-statement/], [snippet: USA TODAY. 0:04. 0:48. Former President Barack Obama recently said, in private, that President Joe Biden's chances at a successful presidential run in 2024 have declined and that he needs to ..., title: Did Obama tell Biden to quit? What the former president said - USA TODAY, link: ], [snippet: Many of the marquee names in Democratic politics began quickly lining up behind Vice President Kamala Harris on Sunday, but one towering presence in the party held back: Barack Obama. The former president has not yet endorsed Harris; in fact, he did not mention her once in an affectionate — if tautly written — tribute to President Joe Biden that was posted on Medium shortly after Biden ..., title: Why Obama Hasn't Endorsed Harris - Yahoo News, link: ]"
Assistant
User
make a react agent that can do these tasks
Assistant
User
no use gradio
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
here is the code:
%%capture --no-stderr
%pip install --quiet -U langchain langchain_community tiktoken langchain-nomic "nomic[local]" langchain-ollama scikit-learn langgraph tavily-python bs4
Local RAG agent with LLaMA3
We'll combine ideas from paper RAG papers into a RAG agent:
Routing: Adaptive RAG (paper). Route questions to different retrieval approaches
Fallback: Corrective RAG (paper). Fallback to web search if docs are not relevant to query
Self-correction: Self-RAG (paper). Fix answers w/ hallucinations or don’t address question
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import SKLearnVectorStore
from langchain_nomic.embeddings import NomicEmbeddings
USER_AGENT environment variable not set, consider setting it to identify your requests.
Components
Router
import json
from langchain_core.messages import HumanMessage, SystemMessage
Prompt
router_instructions = """You are an expert at routing a user question to a vectorstore or web search.
The vectorstore contains documents related to agents, prompt engineering, and adversarial attacks.
Use the vectorstore for questions on these topics. For all else, and especially for current events, use web-search.
Return JSON with single key, datasource, that is 'websearch' or 'vectorstore' depending on the question."""
Test router
test_web_search = llm_json_mode.invoke([SystemMessage(content=router_instructions)] + [HumanMessage(content="Who is favored to win the NFC Championship game in the 2024 season?")])
test_web_search_2 = llm_json_mode.invoke([SystemMessage(content=router_instructions)] + [HumanMessage(content="What are the models released today for llama3.2?")])
test_vector_store = llm_json_mode.invoke([SystemMessage(content=router_instructions)] + [HumanMessage(content="What are the types of agent memory?")])
print(json.loads(test_web_search.content), json.loads(test_web_search_2.content), json.loads(test_vector_store.content))
doc_grader_instructions = """You are a grader assessing relevance of a retrieved document to a user question.
If the document contains keyword(s) or semantic meaning related to the question, grade it as relevant."""
Grader prompt
doc_grader_prompt = """Here is the retrieved document: \n\n {document} \n\n Here is the user question: \n\n {question}.
This carefully and objectively assess whether the document contains at least some information that is relevant to the question.
Return JSON with single key, binary_score, that is 'yes' or 'no' score to indicate whether the document contains at least some information that is relevant to the question."""
Test
question = "What is Chain of thought prompting?"
docs = retriever.invoke(question)
doc_txt = docs[1].page_content
doc_grader_prompt_formatted = doc_grader_prompt.format(document=doc_txt, question=question)
result = llm_json_mode.invoke([SystemMessage(content=doc_grader_instructions)] + [HumanMessage(content=doc_grader_prompt_formatted)])
json.loads(result.content)
{'binary_score': 'yes'}
Generate
Prompt
rag_prompt = """You are an assistant for question-answering tasks.
Here is the context to use to answer the question:
{context}
Think carefully about the above context.
Now, review the user question:
{question}
Provide an answer to this questions using only the above context.
Use three sentences maximum and keep the answer concise.
Answer:"""
Post-processing
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
Chain of Thought (CoT) prompting is a technique used in natural language processing to generate human-like responses by iteratively asking questions and refining the search space through external search queries, such as Wikipedia APIs. CoT prompting involves decomposing problems into multiple thought steps, generating multiple thoughts per step, and evaluating each state using a classifier or majority vote. The goal is to find an optimal instruction that leads to the desired output, which can be achieved by optimizing prompt parameters directly on the embedding space via gradient descent or searching over a pool of model-generated instruction candidates.
Hallucination Grader
Hallucination grader instructions
hallucination_grader_instructions = """
You are a teacher grading a quiz.
You will be given FACTS and a STUDENT ANSWER.
Here is the grade criteria to follow:
(1) Ensure the STUDENT ANSWER is grounded in the FACTS.
(2) Ensure the STUDENT ANSWER does not contain "hallucinated" information outside the scope of the FACTS.
Score:
A score of yes means that the student's answer meets all of the criteria. This is the highest (best) score.
A score of no means that the student's answer does not meet all of the criteria. This is the lowest possible score you can give.
Explain your reasoning in a step-by-step manner to ensure your reasoning and conclusion are correct.
Avoid simply stating the correct answer at the outset."""
Return JSON with two two keys, binary_score is 'yes' or 'no' score to indicate whether the STUDENT ANSWER is grounded in the FACTS. And a key, explanation, that contains an explanation of the score."""
Test using documents and generation from above
hallucination_grader_prompt_formatted = hallucination_grader_prompt.format(documents=docs_txt, generation=generation.content)
result = llm_json_mode.invoke([SystemMessage(content=hallucination_grader_instructions)] + [HumanMessage(content=hallucination_grader_prompt_formatted)])
json.loads(result.content)
{'binary_score': 'yes',
'explanation': 'The student answer provides a clear and accurate description of Chain of Thought (CoT) prompting, its components, and its goals. It also mentions various techniques used in CoT prompting, such as external search queries, prompt tuning, and automatic prompt engineering. The answer demonstrates an understanding of the concept and its applications in natural language processing.'}
Answer Grader
Answer grader instructions
answer_grader_instructions = """You are a teacher grading a quiz.
You will be given a QUESTION and a STUDENT ANSWER.
Here is the grade criteria to follow:
(1) The STUDENT ANSWER helps to answer the QUESTION
Score:
A score of yes means that the student's answer meets all of the criteria. This is the highest (best) score.
The student can receive a score of yes if the answer contains extra information that is not explicitly asked for in the question.
A score of no means that the student's answer does not meet all of the criteria. This is the lowest possible score you can give.
Explain your reasoning in a step-by-step manner to ensure your reasoning and conclusion are correct.
Avoid simply stating the correct answer at the outset."""
Return JSON with two two keys, binary_score is 'yes' or 'no' score to indicate whether the STUDENT ANSWER meets the criteria. And a key, explanation, that contains an explanation of the score."""
Test
question = "What are the vision models released today as part of Llama 3.2?"
answer = "The Llama 3.2 models released today include two vision models: Llama 3.2 11B Vision Instruct and Llama 3.2 90B Vision Instruct, which are available on Azure AI Model Catalog via managed compute. These models are part of Meta's first foray into multimodal AI and rival closed models like Anthropic's Claude 3 Haiku and OpenAI's GPT-4o mini in visual reasoning. They replace the older text-only Llama 3.1 models."
Test using question and generation from above
answer_grader_prompt_formatted = answer_grader_prompt.format(question=question, generation=answer)
result = llm_json_mode.invoke([SystemMessage(content=answer_grader_instructions)] + [HumanMessage(content=answer_grader_prompt_formatted)])
json.loads(result.content)
{'binary_score': 'yes',
'explanation': "The student's answer helps to answer the question by providing specific details about the vision models released as part of Llama 3.2. The answer mentions two vision models (Llama 3.2 11B Vision Instruct and Llama 3.2 90B Vision Instruct) and their availability on Azure AI Model Catalog via managed compute. Additionally, the student provides context about Meta's first foray into multimodal AI and compares these models to other visual reasoning models like Claude 3 Haiku and GPT-4o mini. This extra information is not explicitly asked for in the question, but it demonstrates a thorough understanding of the topic. The answer also correctly states that these models replace the older text-only Llama 3.1 models, which meets all the criteria specified in the question."}
Web Search Tool
Search
from langchain_community.tools.tavily_search import TavilySearchResults
web_search_tool = TavilySearchResults(k=3)
Graph
We build the above workflow as a graph using LangGraph.
Graph state
The graph state schema contains keys that we want to:
Pass to each node in our graph
Optionally, modify in each node of our graph
See conceptual docs here.
import operator
from typing_extensions import TypedDict
from typing import List, Annotated
class GraphState(TypedDict):
"""
Graph state is a dictionary that contains information we want to propagate to, and modify in, each graph node.
"""
question : str # User question
generation : str # LLM generation
web_search : str # Binary decision to run web search
max_retries : int # Max number of retries for answer generation
answers : int # Number of answers generated
loop_step: Annotated[int, operator.add]
documents : List[str] # List of retrieved documents
Each node in our graph is simply a function that:
(1) Take state as an input
(2) Modifies state
(3) Write the modified state to the state schema (dict)
See conceptual docs here.
Each edge routes between nodes in the graph.
See conceptual docs here.
from langchain.schema import Document
from langgraph.graph import END
Nodes
def retrieve(state):
"""
Retrieve documents from vectorstore
Args:
state (dict): The current graph state
Returns:
state (dict): New key added to state, documents, that contains retrieved documents
"""
print("---RETRIEVE---")
question = state["question"]
# Write retrieved documents to documents key in state
documents = retriever.invoke(question)
return {"documents": documents}
def generate(state):
"""
Generate answer using RAG on retrieved documents
Args:
state (dict): The current graph state
Returns:
state (dict): New key added to state, generation, that contains LLM generation
"""
print("---GENERATE---")
question = state["question"]
documents = state["documents"]
loop_step = state.get("loop_step", 0)
# RAG generation
docs_txt = format_docs(documents)
rag_prompt_formatted = rag_prompt.format(context=docs_txt, question=question)
generation = llm.invoke([HumanMessage(content=rag_prompt_formatted)])
return {"generation": generation, "loop_step": loop_step+1}
def grade_documents(state):
"""
Determines whether the retrieved documents are relevant to the question
If any document is not relevant, we will set a flag to run web search
Args:
state (dict): The current graph state
Returns:
state (dict): Filtered out irrelevant documents and updated web_search state
"""
print("---CHECK DOCUMENT RELEVANCE TO QUESTION---")
question = state["question"]
documents = state["documents"]
# Score each doc
filtered_docs = []
web_search = "No"
for d in documents:
doc_grader_prompt_formatted = doc_grader_prompt.format(document=d.page_content, question=question)
result = llm_json_mode.invoke([SystemMessage(content=doc_grader_instructions)] + [HumanMessage(content=doc_grader_prompt_formatted)])
grade = json.loads(result.content)['binary_score']
# Document relevant
if grade.lower() == "yes":
print("---GRADE: DOCUMENT RELEVANT---")
filtered_docs.append(d)
# Document not relevant
else:
print("---GRADE: DOCUMENT NOT RELEVANT---")
# We do not include the document in filtered_docs
# We set a flag to indicate that we want to run web search
web_search = "Yes"
continue
return {"documents": filtered_docs, "web_search": web_search}
def web_search(state):
"""
Web search based based on the question
Args:
state (dict): The current graph state
Returns:
state (dict): Appended web results to documents
"""
print("---WEB SEARCH---")
question = state["question"]
documents = state.get("documents", [])
# Web search
docs = web_search_tool.invoke({"query": question})
web_results = "\n".join([d["content"] for d in docs])
web_results = Document(page_content=web_results)
documents.append(web_results)
return {"documents": documents}
Edges
def route_question(state):
"""
Route question to web search or RAG
Args:
state (dict): The current graph state
Returns:
str: Next node to call
"""
print("---ROUTE QUESTION---")
route_question = llm_json_mode.invoke([SystemMessage(content=router_instructions)] + [HumanMessage(content=state["question"])])
source = json.loads(route_question.content)['datasource']
if source == 'websearch':
print("---ROUTE QUESTION TO WEB SEARCH---")
return "websearch"
elif source == 'vectorstore':
print("---ROUTE QUESTION TO RAG---")
return "vectorstore"
def decide_to_generate(state):
"""
Determines whether to generate an answer, or add web search
Args:
state (dict): The current graph state
Returns:
str: Binary decision for next node to call
"""
print("---ASSESS GRADED DOCUMENTS---")
question = state["question"]
web_search = state["web_search"]
filtered_documents = state["documents"]
if web_search == "Yes":
# All documents have been filtered check_relevance
# We will re-generate a new query
print("---DECISION: NOT ALL DOCUMENTS ARE RELEVANT TO QUESTION, INCLUDE WEB SEARCH---")
return "websearch"
else:
# We have relevant documents, so generate answer
print("---DECISION: GENERATE---")
return "generate"
def grade_generation_v_documents_and_question(state):
"""
Determines whether the generation is grounded in the document and answers question
Args:
state (dict): The current graph state
Returns:
str: Decision for next node to call
"""
print("---CHECK HALLUCINATIONS---")
question = state["question"]
documents = state["documents"]
generation = state["generation"]
max_retries = state.get("max_retries", 3) # Default to 3 if not provided
hallucination_grader_prompt_formatted = hallucination_grader_prompt.format(documents=format_docs(documents), generation=generation.content)
result = llm_json_mode.invoke([SystemMessage(content=hallucination_grader_instructions)] + [HumanMessage(content=hallucination_grader_prompt_formatted)])
grade = json.loads(result.content)['binary_score']
# Check hallucination
if grade == "yes":
print("---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---")
# Check question-answering
print("---GRADE GENERATION vs QUESTION---")
# Test using question and generation from above
answer_grader_prompt_formatted = answer_grader_prompt.format(question=question, generation=generation.content)
result = llm_json_mode.invoke([SystemMessage(content=answer_grader_instructions)] + [HumanMessage(content=answer_grader_prompt_formatted)])
grade = json.loads(result.content)['binary_score']
if grade == "yes":
print("---DECISION: GENERATION ADDRESSES QUESTION---")
return "useful"
elif state["loop_step"] <= max_retries:
print("---DECISION: GENERATION DOES NOT ADDRESS QUESTION---")
return "not useful"
else:
print("---DECISION: MAX RETRIES REACHED---")
return "max retries"
elif state["loop_step"] <= max_retries:
print("---DECISION: GENERATION IS NOT GROUNDED IN DOCUMENTS, RE-TRY---")
return "not supported"
else:
print("---DECISION: MAX RETRIES REACHED---")
return "max retries"
Control Flow
from langgraph.graph import StateGraph
from IPython.display import Image, display
inputs = {"question": "What are the models released today for llama3.2?", "max_retries": 3}
for event in graph.stream(inputs, stream_mode="values"):
print(event)
To create a Gradio interface that allows users to perform searches using the DuckDuckGo Search tool, we need to focus on setting up a web-based interface via Gradio. This interface should allow end-users to input their queries and receive search results using the DuckDuckGo Search component. Here’s how you can set up a simple Gradio app for this purpose:
First, ensure you have the necessary packages installed:
Here’s an example Gradio application that utilizes the DuckDuckGoSearchRun and DuckDuckGoSearchResults to perform web searches:
import gradio as gr
from langchain_community.tools import DuckDuckGoSearchRun, DuckDuckGoSearchResults
# Initialize the DuckDuckGo Search components
simple_search = DuckDuckGoSearchRun()
detailed_search = DuckDuckGoSearchResults()
def run_simple_search(query):
# Execute a simple search and return only the text response
result = simple_search.invoke(query)
return result
def run_detailed_search(query):
# Execute a detailed search and return the titles and links
results = detailed_search.invoke(query)
formatted_results = "\n\n".join(
f"Title: {res['title']}\nSnippet: {res['snippet']}\nLink: {res['link']}"
for res in results
)
return formatted_results
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# DuckDuckGo Search Tool")
with gr.Tab("Simple Search"):
query_input = gr.Textbox(label="Enter your query")
simple_result = gr.Textbox(label="Simple Search Result", lines=10)
simple_btn = gr.Button("Search")
simple_btn.click(run_simple_search, query_input, simple_result)
with gr.Tab("Detailed Search"):
query_input_detailed = gr.Textbox(label="Enter your query")
detailed_result = gr.Textbox(label="Detailed Search Results", lines=10)
detailed_btn = gr.Button("Search")
detailed_btn.click(run_detailed_search, query_input_detailed, detailed_result)
demo.launch()
Explanation:
Gradio Interface: We use Gradio's Blocks and Tabs to create an organized and user-friendly interface.
Simple Search Function: This function uses DuckDuckGoSearchRun to execute a query and return the raw response.
Detailed Search Function: This utilizes DuckDuckGoSearchResults to fetch more detailed results, including titles, snippets, and links.
Launching the App: Use demo.launch() to start the Gradio interface.
This setup lets you perform basic or detailed searches using DuckDuckGo within an interactive Gradio interface. You can extend this functionality by incorporating elements of your local RAG setup with LLaMA3 as needed, integrating other models, or adding logging to trace requests.
Creating a React application to interface with a backend service running a DuckDuckGo search tool requires setting up both the frontend and a backend API. The API will handle search requests, while the React frontend will communicate with this API. Here's a general guide to building this application:
Step 1: Set Up the Backend
First, you'll need to create a simple backend API using a framework like Flask (Python) or Express (Node.js) to interface with the DuckDuckGo search tools.
Flask Backend: Handles search requests and returns results using DuckDuckGo search components.
React Frontend: Provides a user interface to input queries and display search results.
Axios: Facilitates HTTP POST requests to communicate with the backend API endpoints for search operations.
This setup should enable a basic setup for interacting with the DuckDuckGo search service from a React frontend. You can extend this further with error handling, more advanced UI components, or cache search results to improve performance.
To integrate a Gradio interface into a web application that can be deployed and accessed in a similar style to a React application, you can embed the Gradio app directly into a website or a web page by leveraging Gradio's capability to host interfaces or export them to static HTML files. Here’s how you can achieve this:
Step 1: Set Up the Gradio App
First, ensure your Gradio app is functioning as needed. Here's a concise example with the DuckDuckGo search functionality:
# app.py
import gradio as gr
from langchain_community.tools import DuckDuckGoSearchRun, DuckDuckGoSearchResults
# DuckDuckGo search setup
simple_search = DuckDuckGoSearchRun()
detailed_search = DuckDuckGoSearchResults()
def run_simple_search(query):
# Execute a simple search
result = simple_search.invoke(query)
return result
def run_detailed_search(query):
# Execute a detailed search
results = detailed_search.invoke(query)
formatted_results = "\n\n".join(
f"Title: {res['title']}\nSnippet: {res['snippet']}\nLink: {res['link']}"
for res in results
)
return formatted_results
# Setup Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# DuckDuckGo Search Tool")
# Simple Search Tab
with gr.Tab("Simple Search"):
query_input_simple = gr.Textbox(label="Enter your query")
simple_output = gr.Textbox(label="Simple Search Result", lines=10)
gr.Button("Search").click(run_simple_search, query_input_simple, simple_output)
# Detailed Search Tab
with gr.Tab("Detailed Search"):
query_input_detailed = gr.Textbox(label="Enter your query")
detailed_output = gr.Textbox(label="Detailed Search Results", lines=10)
gr.Button("Search").click(run_detailed_search, query_input_detailed, detailed_output)
demo.launch(server_name="0.0.0.0", server_port=7860)
Step 2: Deploy the Gradio App
You have a few methods to host this Gradio app:
1. Local Hosting
Run the app script, which launches it locally on the specified address, often http://localhost:7860. This is suitable for local usage or testing.
python app.py
2. Use a Cloud Provider
You can deploy your Flask backend or Gradio app to cloud providers like AWS, Google Cloud, or Heroku. This approach is suitable if you want others to access the app over the internet.
3. Generate a Static Page
Gradio can export the interface as an HTML file, which makes it easy to host as part of static websites.
Export to HTML:
# At the end of app.py
demo.launch(enable_queue=True, share=True)
demo.save_to_file("demo.html")
Notes:
If you save your Gradio app to a static HTML file, the interactivity is limited compared to the live version because it relies on pre-rendered responses.
Ensure you handle CORS and network configurations appropriately when hosting on cloud platforms, allowing your Gradio app to be accessed publicly if desired.
Integration with a Web-App:
Once deployed, you can embed the Gradio app or its generated HTML pages directly within your existing web solutions. If you deploy using a stable URL (like through Heroku), you can use an iframe to display in-page if your web framework supports it.
<!-- Example HTML to embed Gradio App -->
<iframe src="http://<your-deployed-url>:<port>" height="600" width="100%" frameborder="0"></iframe>
Conclusion
Using Gradio simplifies deploying ML models and tools in interactive formats by leveraging Pythonic interface creations. It abstracts the need for extensive front-end development while allowing integration through Python scripts and effective web serving capabilities.