DBpedia 查询技能:自然语言转 SPARQL - Openclaw Skills

作者:互联网

2026-03-31

AI教程

什么是 DBpedia 查询技能?

DBpedia 查询技能是为 AI 智能体设计的尖端工具,旨在弥合人类语言与语义网之间的鸿沟。通过利用 Openclaw Skills,开发者可以让用户使用普通英语查询庞大的 DBpedia 知识图谱(包含来自维基百科的数百万个实体)。该技能可自动将意图翻译为结构化的 SPARQL 代码,针对实时端点执行,并将结果数据格式化为具有参考价值的见解。

无论您是在构建研究助手还是数据可视化工具,此技能都提供了映射属性、处理前缀和确保数据完整性所需的逻辑。它代表了通过 Openclaw Skills 的力量使非技术用户能够访问关联开放数据的重要进展。

下载入口:https://github.com/openclaw/skills/tree/main/skills/kidehen/query-dbpedia

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install query-dbpedia

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 query-dbpedia。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

DBpedia 查询技能 应用场景

  • 对电影、书籍、历史人物和地理位置等实体进行复杂的调研。
  • 自动从维基百科的结构化数据中生成交互式 HTML 报告和数据表。
  • 将简单的英语问题(如 "谁导演了《星际穿越》?")转换为可执行的 SPARQL 查询。
  • 构建基于知识图谱的应用程序,利用 Openclaw Skills 进行实时数据检索。
DBpedia 查询技能 工作原理
  1. 技能分析自然语言输入,识别主题、谓语以及排序或限制等特定约束。
  2. 将识别出的术语映射到 DBpedia 本体 (dbo) 或属性 (dbp) 命名空间。
  3. 使用标准前缀和语言过滤器构建语法正确的 SPARQL 查询,以确保高质量的英语结果。
  4. 技能通过优化的 HTTP 请求,针对 DBpedia SPARQL 端点执行查询。
  5. 检索到的 JSON 数据经过处理并转换为用户选择的格式,例如样式化的 HTML 页面或 Markdown 表格。

DBpedia 查询技能 配置指南

要在 Openclaw Skills 框架内开始使用此技能,请确保您的环境可以向 DBpedia 端点发出出站 HTTP 请求。您可以使用以下命令验证连接性和查询逻辑:

curl -s -G "https://dbpedia.org/sparql" r
  --data-urlencode "query=SELECT DISTINCT ?label WHERE { dbr:Christopher_Nolan rdfs:label ?label . FILTER(LANG(?label) = 'en') }" r
  --data-urlencode "format=json"

由于 DBpedia 是公开的 SPARQL 端点,因此不需要 API 密钥。

DBpedia 查询技能 数据架构与分类体系

该技能将数据组织成适合自动化处理和人类阅读的结构化格式。

组件 描述
SPARQL 前缀 dbo、dbr 和 rdfs 的标准映射,确保查询准确性。
实体映射 将自然语言主体翻译为 DBpedia URI。
结果集 包含图谱中变量、类型和值的结构化 JSON。
HTML 模板 包含统计信息、源查询和交互式表格的响应式包装器。
name: dbpedia-query-skill
description: Transform natural language questions into SPARQL queries for DBpedia and generate beautiful HTML results pages. Query the DBpedia knowledge graph using plain English prompts.

DBpedia Query Skill

When to Use This Skill

Use this skill when users want to:

  • Query DBpedia using natural language
  • Ask questions about people, places, movies, books, organizations, etc.
  • Get structured data from Wikipedia via DBpedia
  • Create visualizations of DBpedia query results
  • Generate HTML reports from SPARQL queries

Core Capabilities

? Natural Language to SPARQL: Convert user questions into valid SPARQL queries ? Query Execution: Execute queries against DBpedia endpoint ? HTML Generation: Create beautiful, interactive HTML result pages ? Multiple Output Formats: JSON, Markdown tables, or HTML ? Error Handling: Graceful handling of malformed queries or no results

DBpedia Endpoint

SPARQL Endpoint: https://dbpedia.org/sparql Format: JSON results (format=json) Method: HTTP GET with URL-encoded query

Common DBpedia Prefixes

PREFIX dbo: 
PREFIX dbr: 
PREFIX dbp: 
PREFIX rdfs: 
PREFIX rdf: 
PREFIX foaf: 
PREFIX dct: 

Query Conversion Workflow

When a user provides a natural language prompt:

1. Analyze the Question

  • Identify the subject (who/what is being asked about)
  • Identify the predicate (what information is requested)
  • Determine if filtering, sorting, or limiting is needed

2. Map to DBpedia Properties

Common mappings:

  • "directed by" → dbo:director
  • "release date" → dbp:date or dbo:releaseDate
  • "budget" → dbo:budget
  • "born in" → dbo:birthPlace
  • "population" → dbo:populationTotal
  • "capital of" → dbo:capital
  • "written by" → dbo:author
  • "starring" → dbo:starring

3. Construct SPARQL Query

Template:

PREFIX dbo: 
PREFIX dbr: 
PREFIX rdfs: 

SELECT DISTINCT ?variable ?label
WHERE {
  ?variable   ;
           rdfs:label ?label .
  FILTER(LANG(?label) = 'en')
}
ORDER BY 
LIMIT 

4. Execute Query

Use curl to execute against DBpedia:

curl -s -G "https://dbpedia.org/sparql" r
  --data-urlencode "query=" r
  --data-urlencode "format=json"

5. Generate Output

Options:

  • JSON: Raw query results
  • Markdown Table: Formatted for terminal display
  • HTML Page: Interactive, styled results page

Example Query Patterns

Pattern 1: Films by Director

User: "Show me movies directed by Christopher Nolan"

SPARQL:

PREFIX dbo: 
PREFIX dbr: 
PREFIX rdfs: 

SELECT DISTINCT ?film ?title ?releaseDate
WHERE {
  ?film dbo:director dbr:Christopher_Nolan ;
        a dbo:Film ;
        rdfs:label ?title .
  OPTIONAL { ?film dbo:releaseDate ?releaseDate }
  FILTER(LANG(?title) = 'en')
}
ORDER BY DESC(?releaseDate)

Pattern 2: Population Queries

User: "What are the 10 most populous cities in France?"

SPARQL:

PREFIX dbo: 
PREFIX dbr: 
PREFIX rdfs: 

SELECT ?city ?name ?population
WHERE {
  ?city dbo:country dbr:France ;
        a dbo:City ;
        rdfs:label ?name ;
        dbo:populationTotal ?population .
  FILTER(LANG(?name) = 'en')
}
ORDER BY DESC(?population)
LIMIT 10

Pattern 3: Person Information

User: "Tell me about Albert Einstein - when was he born and where?"

SPARQL:

PREFIX dbo: 
PREFIX dbr: 
PREFIX rdfs: 

SELECT ?birthDate ?birthPlace ?placeLabel
WHERE {
  dbr:Albert_Einstein dbo:birthDate ?birthDate ;
                      dbo:birthPlace ?birthPlace .
  ?birthPlace rdfs:label ?placeLabel .
  FILTER(LANG(?placeLabel) = 'en')
}

HTML Template Generation

When generating HTML results:

Required Elements

  1. Title: Question or query description
  2. Statistics: Number of results, query execution time
  3. Table: Results with hyperlinked DBpedia URIs
  4. SPARQL Query Display: Show the executed query
  5. Footer: Link to DBpedia, data source attribution

Styling Guidelines

  • Use gradient backgrounds
  • Responsive design (mobile-friendly)
  • Hover effects on table rows
  • Hyperlink all DBpedia resources
  • Color-code different data types
  • Include icons for visual appeal

HTML Template Structure




    
    [Query Description] - DBpedia
    


    

[Question/Title]

[Results count]
[Results]
[Query code]

Error Handling

No Results

If query returns 0 results:

  • Inform user clearly
  • Suggest alternative phrasings
  • Check for typos in entity names

Query Errors

If SPARQL syntax error:

  • Display error message
  • Show attempted query
  • Offer to reformulate

Timeout

If query times out:

  • Add LIMIT clause
  • Simplify query complexity
  • Suggest narrowing criteria

Output Preferences

Always ask the user:

"Would you like the results as:
1. JSON (raw data)
2. Markdown table (terminal display)
3. HTML page (interactive visualization)"

Best Practices

  1. Always use DISTINCT: Avoid duplicate results
  2. Filter by language: Use FILTER(LANG(?label) = 'en')
  3. Add LIMIT: Default to LIMIT 100 unless specified
  4. Use OPTIONAL: For properties that may not exist
  5. Order results: Make results meaningful with ORDER BY
  6. Hyperlink in HTML: All DBpedia URIs should be clickable

Example Session

User: "List books written by J.K. Rowling with publication dates"

Assistant: "I'll query DBpedia for books authored by J.K. Rowling.

Executing SPARQL query against DBpedia endpoint..."

[Constructs and executes query]

"Found 15 books! Would you like the results as:

  1. JSON
  2. Markdown table
  3. HTML page"

User: "HTML page"

Assistant: [Generates beautiful HTML page with results]

"? HTML page generated and saved to: ./jk_rowling_books.html ? 15 books found with publication dates"

Scope

This skill handles:

  • Queries about entities in DBpedia
  • Structured data extraction
  • Result formatting and visualization

This skill does NOT handle:

  • Text search (use DBpedia Lookup API instead)
  • Data modification (read-only queries)
  • Real-time data (DBpedia updates periodically)

Version: 1.0.0 Endpoint: https://dbpedia.org/sparql Data Source: DBpedia (Wikipedia structured data)

相关推荐