"You are a helpful expert environmental research assistant. Provide an example answer to the given question, that might be found in a document like an annual environmental report."
这是我们创建的函数的第一步:
defaugment_query_generated(user_query):
system_message_prompt = SystemMessagePromptTemplate.from_template(
"You are a helpful expert environmental research assistant. Provide an example answer to the given question, that might be found in a document like an annual environmental report."
)
human_message_prompt = HumanMessagePromptTemplate.from_template("{query}")
chat_prompt = ChatPromptTemplate.from_messages([
system_message_prompt, human_message_prompt])
response = chat_prompt.format_prompt(
query=user_query).to_messages()
result = llm(response)
content = result.content
return content
What are Google's environmental initiatives?
In 2022, Google continued to advance its environmental initiatives, focusing on sustainability and reducing its carbon footprint. Key initiatives included:
1. **Carbon Neutrality and Renewable Energy**: Google has maintained its carbon-neutral status since 2007 and aims to operate on 24/7 carbon-free energy by 2030. In 2022, Google procured over 7 gigawatts of renewable energy, making significant strides towards this goal.
2. **Data Center Efficiency**: Google's data centers are among the most energy-efficient in the world. In 2022, the company achieved an average power usage effectiveness (PUE) of 1.10, significantly lower than the industry average. This was accomplished through advanced cooling technologies and AI-driven energy management systems.
3. **Sustainable Products and Services**…[TRUNCATED]
from IPython.display import Markdown, display
markdown_text_alt = result_alt['answer']['final_answer']
display(Markdown(markdown_text_alt))
以下是输出示例:
Google has implemented a comprehensive set of environmental initiatives aimed at sustainability and reducing its carbon footprint. Here are the key initiatives:
1. Carbon Neutrality and Renewable Energy: Google has been carbon-neutral since 2007 and aims to operate on 24/7 carbon-free energy by 2030. In 2022, Google procured over 7 gigawatts of renewable energy.
2. Data Center Efficiency: Google's data centers are among the most energy-efficient globally, achieving an average power usage effectiveness (PUE) of 1.10 in 2022. This was achieved through advanced cooling technologies and AI-driven energy management systems.
…[TRUNCATED FOR BREVITY]
3. Supplier Engagement: Google works with its suppliers to build an energy-efficient, low-carbon, circular supply chain, focusing on improving environmental performance and integrating sustainability principles.
4. Technological Innovations: Google is investing in breakthrough technologies, such as next-generation geothermal power and battery-based backup power systems, to optimize the carbon footprint of its operations.
These initiatives reflect Google's commitment to sustainability and its role in addressing global environmental challenges. The company continues to innovate and collaborate to create a more sustainable future.
---- END OF OUTPUT ----
prompt_decompose = PromptTemplate.from_template(
"""You are an AI language model assistant.
Your task is to generate five different versions of
the given user query to retrieve relevant documents from
a vector search. By generating multiple perspectives on
the user question, your goal is to help the user
overcome some of the limitations of the distance-based
similarity search. Provide these alternative questions
separated by newlines.
Original question: {question}"""
)
decomposed_queries = decompose_queries_chain.invoke(
{"question": user_query})
print("Five different versions of the user query:")
print(f"Original: {user_query}")
for i, question inenumerate(decomposed_queries, start=1):
print(f"{question.strip()}")
此代码会调用我们设置的链条,并为我们提供原始查询以及通过查询分解提示和 LLM 生成的五个新查询:
Five different versions of the user query:
Original: What are Google's environmental initiatives?
What steps is Google taking to address environmental concerns?
How is Google contributing to environmental sustainability?
Can you list the environmental programs and projects Google is involved in?
What actions has Google implemented to reduce its environmental impact?
What are the key environmental strategies and goals of Google?
defformat_retrieved_docs(documents: list[list]):
flattened_docs = [dumps(doc) for sublist in documents
for doc in sublist]
print(f"FLATTENED DOCS: {len(flattened_docs)}")
deduped_docs = list(set(flattened_docs))
print(f"DEDUPED DOCS: {len(deduped_docs)}")
return [loads(doc) for doc in deduped_docs]
Google has implemented a wide range of environmental initiatives aimed at improving sustainability and reducing its environmental impact. Here are some key initiatives based on the provided context.
1. Campus and Habitat Restoration:
Google has created and restored more than 40 acres of habitat on its campuses and surrounding urban landscapes, primarily in the Bay Area. This includes planting roughly 4,000 native trees and restoring ecosystems like oak woodlands, willow groves, and wetland habitats.
2. Carbon-Free Energy:
Google is working towards achieving net-zero emissions and 24/7 carbon-free energy (CFE) by 2030. This involves clean energy procurement strategies such as reducing carbon emissions across its operations and supply chain. Google has also entered into long-term renewable energy contracts to ensure 100% of its data centers are powered by renewable energy sources.
3. Greener Workplaces and Facilities:
Google is working to make its workplaces and facilities more sustainable by installing features such as energy-efficient buildings, reduced carbon footprints, and better waste management practices. For instance, Google's Mountain View campus uses a blend of natural and renewable energy sources to reduce its environmental impact and improve energy efficiency.
如果你还没有查看过我们在许多实验室中使用的 Google 2023 环境报告 PDF 文件,你可能会以为它只是基于文本的。但打开它,你会看到贯穿其中并伴随文本的精美图像。你看到的一些图表,尤其是那些设计精美的图表,实际上就是图像。如果我们有一个 RAG 应用,想要利用这些图像中的数据呢?让我们开始构建一个吧!
引入 MM-RAG 代码示例
在本实验中,我们将执行以下操作:
使用强大的开源包 unstructured 从 PDF 中提取文本和图像。
使用多模态 LLM 从提取的图像生成文本摘要。
将这些图像摘要(与我们已经使用的文本对象一起)嵌入并检索,并引用原始图像。
使用 Chroma 将图像摘要存储在多向量检索器中,同时存储原始文本和图像及其摘要。
将原始图像和文本块传递给相同的多模态 LLM 进行答案合成。
首先,我们需要安装一些新的包,这些包是 optical character recognition(OCR)组件所需的:
poppler-utils:poppler-utils 是一组用于操作 PDF 文件的命令行工具,在我们的代码中,poppler 被 unstructured 用于从 PDF 文件中提取元素。
tesseract-ocr:tesseract-ocr 引擎是一个开源 OCR 引擎,能够识别和提取图像中的文本。这是 unstructured 支持 PDF 时所需的另一个库,用于从图像中提取文本。
这些包提供了 langchain 和 unstructured 库所需的各种功能,支持文档解析、图像处理、数据验证、分词和 OCR,帮助处理 PDF 文件并生成响应。
接下来,我们导入这些包和其他包,以便在代码中使用:
from langchain.retrievers.multi_vector import MultiVectorRetriever
from langchain_community.document_loaders import UnstructuredPDFLoader
from langchain_core.runnables import RunnableLambda
from langchain.storage import InMemoryStore
from langchain_core.messages import HumanMessage
import base64
import uuid
from IPython.display import HTML, display
from PIL import Image
import matplotlib.pyplot as plt
这些是 Python 包的导入列表,接下来逐个解释它们的作用:
MultiVectorRetriever from langchain.retrievers.multi_vector:MultiVectorRetriever 是一个结合多个向量存储的检索器,允许基于相似性搜索高效地检索文档。在我们的代码中,MultiVectorRetriever 被用来创建一个结合了 vectorstore 和 docstore 的检索器,用于根据用户查询检索相关文档。
UnstructuredPDFLoader from langchain_community.document_loaders:UnstructuredPDFLoader 是一个文档加载器,用于使用 unstructured 库从 PDF 文件中提取元素,包括文本和图像。在我们的代码中,UnstructuredPDFLoader 用于加载和提取指定 PDF 文件(short_pdf_path)中的元素。
total documents after reduction: texts: 78 images: 3
接下来的几个代码块将专注于图像总结,首先是我们的函数,通过该函数将提示应用到图像并获取总结:
defapply_prompt(img_base64):
# Prompt
prompt = """You are an assistant tasked with summarizing images for retrieval. \
These summaries will be embedded and used to retrieve the raw image. \
Give a concise summary of the image that is well optimized for retrieval."""return [HumanMessage(content=[
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url":
f"data:image/jpeg;base64,{img_base64}"},},
])]
defadd_documents(retriever, doc_summaries, doc_contents):
doc_ids = [str(uuid.uuid4()) for _ in doc_contents]
summary_docs = [
Document(page_content=s, metadata={id_key: doc_ids[i]})
for i, s inenumerate(doc_summaries)
]
content_docs = [ Document(page_content=doc.page_content, metadata={id_key: doc_ids[i]})
for i, doc inenumerate(doc_contents)
]
retriever.vectorstore.add_documents(summary_docs)
retriever.docstore.mset(list(zip(doc_ids, doc_contents)))
if text_summaries:
add_documents(retriever_multi_vector, text_summaries, texts)
if image_summaries:
add_documents(retriever_multi_vector, image_summaries, images)
defimg_prompt_func(data_dict):
formatted_texts = "\n".join(data_dict["context"]["texts"])
messages = []
if data_dict["context"]["images"]:
for image in data_dict["context"]["images"]:
image_message = {"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{image}"}}
messages.append(image_message)
text_message = {
"type": "text",
"text": (
f"""You are a helpful assistant tasked with describing what is in an image. The user will ask for a picture of something. Provide text that supports what was asked for. Use this information to provide an in-depth description of the aesthetics of the image. Be clear and concise and don't offer any additional commentary.
User-provided question: {data_dict['question']}
Text and / or images: {formatted_texts}"""
),
}
messages.append(text_message)
return [HumanMessage(content=messages)]
'The image shows a vast array of wind turbines situated in the ocean, extending towards the horizon. The turbines are evenly spaced and stand tall above the water, with their large blades capturing the wind to generate clean energy. The ocean is calm and blue, providing a serene backdrop to the white turbines. The sky above is clear with a few scattered clouds, adding to the tranquil and expansive feel of the scene. The overall aesthetic is one of modernity and sustainability, highlighting the use of renewable energy sources in a natural setting.'