prompt = "describe the image"
res = ollama.generate(model="llava", prompt=prompt, images=[img])["response"]
print(res)
图表 - 同样,视觉模型可以读取和总结图表,但结果不如图像准确。
image_file = "data/plot.png"
pyplot.imshow(image.imread(image_file))
pyplot.show()
img = encode_image(image_file)
prompt = "Describe the image in detail. Be specific about graphs, such as bar plots, line graphs, etc."
res = ollama.generate(model="llava", prompt=prompt, images=[img])["response"]
print(res)
{
'embeddings':[[-0.06092095375061035,-0.01741098240017891,0.0484163761138916, ...]],
'metadatas':[{
'keywords': 'Renewable Energy Adoption, Supplier Engagement, Emission Reduction Goals',
'title': 'Business'
}],
'documents':[
'We aim to generate enough renewable energy to match 100% of our global electricity usage for our offices and data centers. In fiscal year 2023, we increased the percentage of our total electricity use matched by renewable energy purchases to 44%. By fiscal year 2026, we aim to engage manufacturing suppliers comprising at least 67% of NVIDIA's scope 3 category 1 GHG emissions with the goal of effecting supplier adoption of science-based targets.'
],
'uris': None,
'data': None
}
文档作为包含向量和元数据的字典保存。
接下来,尝试查询一些信息:
query = "how much is the revenue?"
res_db = collection.query(query_texts=[query])["documents"][0][0:10]
context = ' '.join(res_db).replace("\n", " ")
print(context)
输出示例:
Total revenue forfiscal year 2024 was $60.9 billion, up 126% from a year ago. Data Center revenue forfiscal year 2024 was up 217%. Strong demand was driven by enterprise software and consumer internet applications, and multiple industry verticals including automotive, financial services, and healthcare. Gaming revenue forfiscal year 2024 was up 15%. Professional Visualization revenue forfiscal year 2024 was up 1%. Automotive revenue forthe fiscal year 2024 was up 21%. The increase primarily reflected growth inself-driving platforms. Gross margin increased in fiscal year 2024, primarily driven by Data Center revenue growth and lower net inventory provisions as a percentage of revenue. Operating expenses increased forfiscal year 2024, driven by growth in employees and compensation increases.
res = ollama.chat(
model="phi3",
messages=[
{"role": "system", "content": "Give the most accurate answer using only the following information:"},
{"role": "user", "content": query}
],
stream=True
)
print(res["message"]["content"])
输出示例:
The total recognized revenue for fiscal year 2024 was $60.9 billion, which represents an increase of126% from the previous year. The breakdown by category in millions of dollars is asfollows:
- DataCenter: $47,525 million
- Gaming: $10,447 million
- ProfessionalVisualization: $1,553 million
- Automotive: $1,091 million
- OEM and Other: $306 million
结合知识和上下文进行聊天:
res = ollama.chat(
model="phi3",
messages=[
{"role": "system", "content": "Give the most accurate answer using your knowledge and the following information:"},
{"role": "user", "content": query}
],
stream=True
)
print(res["message"]["content"])
输出示例:
The total recognized revenue for fiscal year 2024 was 60,922 million (or $60.9 billion when expressed in billions). This amount represents an increase of126%from the previous year's revenue. Additionally, there is a breakdown by product categories as follows:
- Data Center: $47,525 million
- Gaming: $10,447 million
- Professional Visualization: $1,553 million
- Automotive: $1,091 million
- OEM and Other: $306 million
It's also important to note that there are deferred revenue amounts of $233 million in fiscal 2024 and $35 million in fiscal 2023 related to customer advances, which will be recognized as revenue over future periods. The remaining performance obligations account for approximately $1.1 billion, with an expectation that about 40% of this amount will be recognized within the next twelve months.
如今,Streamlit 是构建快速 Web 应用程序最常用的 Python 库,因为它通过其流式功能简化了 NLP 应用程序的开发。
首先,定义布局:我的屏幕应有一个侧边栏,用户可以在其中查看聊天历史记录。
import streamlit as st #1.35.0## 布局
st.title("Write your questions")
st.sidebar.title("Chat History")
app = st.session_state
if'messages'notin app:
app['messages'] = [{"role": "assistant", "content": "I'm ready to retrieve information"}]
if'history'notin app:
app['history'] = []
if'full_response'notin app:
app['full_response'] = ''
应用程序对象(或会话状态)是一个具有以下结构的字典:
{
'history':[
': how much is the revenue?',
': The total revenue reported in the given information is 60 million'
],
'messages':[{'role': 'assistant', 'content':"I'm ready to retrieve information"},{'role': 'user', 'content': 'how much is the revenue?'},{'role': 'assistant', 'content': 'The total revenue reported in the given information is 60 million'}],
'full_response': 'The total revenue reported in the given information is 60 million'
}
如果不想聊天中的消息消失,请添加以下代码:
## 保持消息在聊天中for msg in app["messages"]:
if msg["role"] == "user":
st.chat_message(msg["role"], avatar="🧑").write(msg["content"])
elif msg["role"] == "assistant":
st.chat_message(msg["role"], avatar="🤖").write(msg["content"])
import chromadb #0.5.0import ollama #0.5.0classAI:
def__init__(self):
db = chromadb.PersistentClient()
self.collection = db.get_or_create_collection("nvidia")
defquery(self, q, top=10):
res_db = self.collection.query(query_texts=[q])["documents"][0][0:top]
context = ' '.join(res_db).replace("\n", " ")
return context
defrespond(self, lst_messages, use_knowledge=False):
q = lst_messages[-1]["content"]
context = self.query(q)
if use_knowledge:
prompt = "Give the most accurate answer using your knowledge and the following information:"else:
prompt = "Give the most accurate answer using only the following information:"
res_ai = ollama.chat(
model="phi3",
messages=[
{"role": "system", "content": prompt},
*lst_messages
],
stream=True
)
for res in res_ai:
chunk = res["message"]["content"]
app["full_response"] += chunk
yield chunk
ai = AI()
要运行 Streamlit 应用程序,必须使用以下命令:
streamlit run rag_app.py
7. 运行示例
以下是该应用程序的运行示例:
用户输入: 'how much is the revenue?'
AI 响应:
Thetotalreportedrevenueforfiscalyear2024 was60,922million.Thisrepresentsanincreaseof126%fromayearago.Additionally,there is a breakdown by product categories as follows:-Data Center:47,525million-Gaming:10,447million-Professional Visualization:1,553million-Automotive:1,091million-OEM and Other:306million
用户输入: 'how did they make it?'
AI 响应:
NVIDIA achieved the reported revenue through a combination of factors, including:
- **Data Center Revenue Growth**: The Data Center segment saw a significant increase of217%, driven by strong demand from enterprise software and consumer internet applications, as well as various industry verticals such as automotive, financial services, and healthcare. Customers access NVIDIA AI infrastructure both through the cloud andon-premises, with Data Center compute revenue growing by244%and Networking revenue by133%.
- **Gaming Revenue Increase**: Gaming revenue rose by15%, reflecting higher sales to partners following the normalization of channel inventory levels and growing demand.
- **Professional Visualization and Automotive Revenue**: These segments also saw growth, with Professional Visualization revenue increasing by1%and Automotive revenue by21%, primarily due to growth in self-driving platforms.
- **Gross Margin Improvement**: Gross margin increased due to the growth in Data Center revenue and lower net inventory provisions as a percentage of revenue.
- **Operating Expenses**: Operating expenses increased due to the growth in employees and compensation increases.
用户输入: 'What is the significance of their Data Center revenue growth?'
AI 响应:
The significant increase in Data Center revenue, up 217%from the previous year, highlights NVIDIA's growing dominance in the AI and data center markets. This growth is driven by several factors:
- **Enterprise Demand**: Strong demand from enterprise customers for AI infrastructure to support applications such as machine learning, data analytics, and cloud computing.
- **Diverse Industry Verticals**: The adoption of NVIDIA's AI solutions across various industries, including automotive, financial services, and healthcare, indicates a broad-based demand for AI capabilities.
- **Cloud andOn-Premises**: The ability to serve customers through both cloud-based andon-premises solutions provides flexibility and meets different customer needs.
- **Compute and Networking**: The substantial growth in both Data Center compute revenue (up 244%) and Networking revenue (up 133%) underscores the importance of NVIDIA's comprehensive AI platform, which includes both hardware and software components.
用户输入: 'What challenges did they face in the Automotive segment?'
AI 响应:
While the Automotive revenue increased by 21%, the document does not explicitly list specific challenges faced in this segment. However, potential challenges in the automotive industry for NVIDIA could include:
-**Market Competition**: Intense competition from other technology companies and traditional automotive suppliers in the autonomous driving and AI solutions space.
-**Regulatory Hurdles**: Navigating the complex regulatory landscape for autonomous vehicles and AI technologies.
-**Technological Development**: The need to continuously innovate and improve AI algorithms and hardware to meet the demanding requirements of the automotive industry.
-**Supply Chain Issues**: Potential disruptions in the supply chain for critical components needed for NVIDIA's automotive solutions.
用户输入: 'What is NVIDIA's strategy for future growth?'
AI 响应:
NVIDIA's strategy for future growth is likely to focus on several key areas:
- **Continued Expansion in Data Center**: Leveraging the strong growth in the Data Center segment by expanding its AI infrastructure offerings and targeting new industries and applications.
- **Gaming Market**: Maintaining and growing its presence in the gaming market by continuing to innovate and meet the demands of gamers and developers.
- **Professional Visualization**: Further developing its professional visualization solutions to support industries such as architecture, engineering, and design.
- **Automotive Innovation**: Investing in research and development to advance its autonomous driving and AI technologies for the automotive industry.
- **Sustainability**: Focusing on sustainability initiatives, such as increasing the use of renewable energy and setting science-based targets for emissions reductions.
- **Strategic Partnerships**: Forming strategic partnerships with other companies to expand its reach and capabilities in various markets.
8. 结论
本文展示了如何使用 Python 和大型语言模型构建一个检索增强生成(RAG)系统。通过以下步骤,我们成功构建了一个能够从多模态文档中学习知识并回答问题的 AI 应用程序:
数据预处理: 将 PDF 文档转换为图像,并使用 OCR 提取文本。然后,利用 LLM 对文本、表格、图像和图表进行预处理和增强。
数据库存储: 将预处理后的数据存储在 ChromaDB 向量数据库中,以便高效存储、索引和搜索。
后端开发: 使用 Ollama 和 ChromaDB 构建后端逻辑,处理用户查询并生成响应。
前端构建: 使用 Streamlit 构建用户友好的界面,允许用户与 AI 进行交互。
该系统展示了如何利用 LLM 和多模态数据构建一个强大的 AI 应用程序,即使在普通笔记本电脑上也能高效运行。该方法具有以下优势: