跳转到主内容
思享编程网:思考分享,玩转编程世界!

【RAG】【query_engine11】JSONalyze Query Engine - JSON数据分析查询引擎

JSONalyze Query Engine(JSON分析查询引擎)是一个专门设计用于分析JSON数据的查询引擎,特别适用于处理API返回的大量JSON数据。

它通过创建内存SQLite表来存储JSON数据,并能够执行SQL查询来回答数据分析问题。

1. 案例目标 本案例的主要目标是: 演示如何使用JSONalyze Query Engine分析JSON数据 展示如何将JSON列表转换为可查询的数据库表 演示如何通过自然语言查询执行SQL分析 展示如何对JSON数据进行统计分析、聚合计算等操作 2. 技术栈与核心依赖 LlamaIndex - 用于构建查询引擎的核心框架 OpenAI API - 用于自然语言到SQL的转换 sqlite-utils - 用于创建和管理内存SQLite数据库 JSONalyzeQueryEngine - LlamaIndex提供的专门用于JSON数据分析的查询引擎 IPython.display - 用于格式化显示查询结果 3. 环境配置 在运行本案例前,需要完成以下环境配置:

# 安装必要的依赖包

%pip install llama-index-llms-openai %pip install llama-index %pip install sqlite-utils

# 设置日志 import logging import sys logging.basicConfig(stream=sys.stdout, level=logging.INFO) logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))

# 设置OpenAI API密钥 import os import openai os.environ["OPENAI_API_KEY"] = "YOUR_KEY_HERE" openai.api_key = os.environ["OPENAI_API_KEY"] 4. 案例实现 4.1 准备JSON数据 首先,我们准备一个包含个人信息的JSON列表作为示例数据:

json_list = [

{ "name": "John Doe", "age": 25, "major": "Computer Science", "email": "john.doe@example.com", "address": "123 Main St", "city": "New York", "state": "NY", "country": "USA", "phone": "+1 123-456-7890", "occupation": "Software Engineer", }, { "name": "Jane Smith", "age": 30, "major": "Business Administration", "email": "jane.smith@example.com", "address": "456 Elm St", "city": "San Francisco", "state": "CA", "country": "USA", "phone": "+1 234-567-8901", "occupation": "Marketing Manager", }, # ... 更多数据 ] 4.2 创建JSONalyze查询引擎 使用JSON列表创建查询引擎实例:

from llama_index.llms.openai import OpenAI

from llama_index.core.query_engine import JSONalyzeQueryEngine

# 创建LLM实例 llm = OpenAI(model="gpt-3.5-turbo")

# 创建JSONalyze查询引擎 json_stats_query_engine = JSONalyzeQueryEngine( list_of_dict=json_list, llm=llm, verbose=True, ) 4.3 准备查询问题 定义一系列统计分析问题:

questions = [

"What is the average age of the individuals in the dataset?", "What is the maximum age among the individuals?", "What is the minimum age among the individuals?", "How many individuals have a major in Psychology?", "What is the most common major among the individuals?", "What is the percentage of individuals residing in California (CA)?", "How many individuals have an occupation related to science or engineering?", "What is the average length of the email addresses in the dataset?", "How many individuals have a phone number starting with '+1 234'?", "What is the distribution of ages among the individuals?", ] 5. 案例效果 5.1 平均年龄查询 查询数据集中个人的平均年龄:

response = json_stats_query_engine.query(

"What is the average age of the individuals in the dataset?" ) 输出结果 : The average age of the individuals in the dataset is 29.375. 内部执行过程 : 生成的SQL查询:

SELECT AVG(age) FROM items

SQL响应:

[{'AVG(age)': 29.375}]

5.2 最常见专业查询 查询数据集中最常见的专业:

response = json_stats_query_engine.query(

"What is the most common major among the individuals?" ) 输出结果 : The most common major among the individuals is Sociology. 内部执行过程 : 生成的SQL查询:

SELECT major, COUNT(*) as count

FROM items GROUP BY major ORDER BY count DESC LIMIT 1; SQL响应:

[{'major': 'Sociology', 'count': 1}]

5.3 邮箱地址平均长度查询 查询数据集中邮箱地址的平均长度:

response = json_stats_query_engine.query(

"What is the average length of the email addresses in the dataset?" ) 输出结果 : The average length of the email addresses in the dataset is 24.5 characters. 内部执行过程 : 生成的SQL查询:

SELECT AVG(LENGTH(email)) FROM items

SQL响应:

[{'AVG(LENGTH(email))': 24.5}]

5.4 加州居民百分比查询 查询居住在加利福尼亚州的个人百分比:

response = json_stats_query_engine.query(

"What is the percentage of individuals residing in California (CA)?" ) 输出结果 : The percentage of individuals residing in California (CA) is 18.75%. 内部执行过程 : 生成的SQL查询:

SELECT (COUNT(*) * 100.0 / (SELECT COUNT(*) FROM items)) AS percentage

FROM items WHERE state = 'CA' SQL响应:

[{'percentage': 18.75}]

5.5 年龄分布查询 查询数据集中年龄的分布情况:

response = json_stats_query_engine.query(

"What is the distribution of ages among the individuals?" ) 输出结果 : The distribution of ages among the individuals is as follows: 1 individual is 25 years old 1 individual is 26 years old 2 individuals are 27 years old 3 individuals are 28 years old 2 individuals are 29 years old 2 individuals are 30 years old 1 individual is 31 years old 2 individuals are 32 years old 1 individual is 33 years old 1 individual is 35 years old 内部执行过程 : 生成的SQL查询:

SELECT age, COUNT(*) as count

FROM items GROUP BY age SQL响应:

[{'age': 25, 'count': 1}, {'age': 26, 'count': 1}, ...]

6. 案例实现思路 本案例的核心实现思路如下: 数据转换 :将JSON列表转换为内存SQLite数据库表,使数据可以通过SQL查询 自然语言到SQL转换 :利用LLM将自然语言问题转换为SQL查询语句 查询执行 :在内存SQLite数据库上执行生成的SQL查询 结果处理 :将SQL查询结果转换为自然语言回答 异步支持 :支持异步查询模式,提高性能 7. 扩展建议 数据可视化 :集成数据可视化库,将查询结果以图表形式展示 复杂数据类型支持 :扩展对嵌套JSON、数组等复杂数据类型的支持 缓存机制 :添加查询结果缓存,提高重复查询的性能 多表关联 :支持多个JSON列表之间的关联查询 自定义函数 :允许用户定义自定义SQL函数,扩展分析能力 数据导出 :添加将查询结果导出为CSV、Excel等格式的功能 查询历史 :记录查询历史,方便用户回顾和重用之前的查询 权限控制 :添加数据访问权限控制,确保敏感数据的安全 8. 总结 JSONalyze Query Engine是一个强大的工具,它将JSON数据分析与自然语言查询相结合,使用户能够通过简单的自然语言问题对JSON数据进行复杂的统计分析。

通过将JSON数据转换为SQLite表,并利用LLM进行自然语言到SQL的转换,该引擎大大降低了数据分析的门槛,使非技术人员也能轻松进行数据探索。

本案例展示了JSONalyze Query Engine的基本用法,包括数据准备、引擎创建、查询执行等步骤。

通过多个示例查询,我们看到了该引擎在统计分析、聚合计算等方面的强大能力。

这种工具特别适用于处理API返回的JSON数据,能够快速从中提取有价值的信息和洞察。

相关文章