ラベル WrenAI の投稿を表示しています。 すべての投稿を表示
ラベル WrenAI の投稿を表示しています。 すべての投稿を表示

2026年10月3日土曜日

WrenAIを通してOracle Databaseに問い合わせる

WrenAIというオープンソースのプロジェクトがあります。 これはGitHubのページでは以下のように説明されています。
WrenAI is the open-source generative BI (GenBI) engine. It gives the AI agents you already use (Claude Code, Cursor, MCP clients, LangChain) a governed semantic layer and an AI context layer, so they turn business questions into correct SQL, ship the answer as a shareable dashboard, and stay inside your guardrails.

Schemas tell an agent where data lives. Wren tells it what the data means: approved metric definitions, enums, units, joins, worked examples, and the tribal knowledge buried in docs and chat threads. All of it lives as reviewable YAML and Markdown in a repo you own.
NL2SQLのカテゴリに入ると思いますが、セマンティック・レイヤーやAIコンテキスト・レイヤーをYAMLやMarkdown形式で保持し、AIエージェントにそれらのレイヤーの情報を提供した上で、NL2SQLを実行します。

Wren AIにはOpen Source版とCommercial版があります。今回使用するのはOpen Source版です。Open Source版のドキュメントは以下です。


このWren AIですが、Issue#710 - Support Oracle data sourceを見ると、データソースとしてOracleが追加されています。Wren AIのドキュメントにもOracleのサポートについて記載されています。こちらにはOracle Database 23ai移行と記載されていますが、Open Source版でも同様の制限があるかどうかは不明です。今回の作業ではローカルのコンテナで実行しているOracle AI Database 26ai Freeを使用します。macOS上で作業を実施します。

Oracleのサンプル・スキーマに含まれるsales_historyをデータベースにインストールし、CodexのプロジェクトにWren AIを組み込み、NL2SQLを実行してみます。

最初にサンプル・データセットのsales_historyをデータベースにインストールします。インストール手順については、以前の記事「アップデートされたサンプル・スキーマのインストール」を参照してください。sh_install.sqlを実行します。ベルギーのUnited Codes社が公開しているUC Local APEX Devを使って作成した環境を想定しています。

sqlcl -name local-26ai-sys
@sh_install

@sh_installを実行すると、スキーマSHのパスワードの入力を求められます。Wren AIにOracle Databaseの接続先を構成する際に、このスキーマSHとパスワードを使用しますので覚えておいてください。

sales_history % sqlcl -name local-26ai-sys

     

SQLcl: 金 10月 02 21:28:05 2026のリリース26.3 Production


Copyright (c) 1982, 2026, Oracle.  All rights reserved.


接続先:

Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free

Version 23.26.3.0.0


SQL> @sh_install


Thank you for installing the Oracle Sales History Sample Schema.

This installation script will automatically exit your database session

at the end of the installation or if any error is encountered.

The entire installation will be logged into the 'sh_install.log' log file.


Enter a password for the user SH: ******


Enter a tablespace for SH [USERS]: 

Do you want to overwrite the schema, if it already exists? [YES|no]: 


Start time: 02-OCT-26 12.28.16.426810 PM +00:00    

******  Creating COUNTRIES table ....


Table COUNTRIESは作成されました。


******  Creating CUSTOMERS table ....


Table CUSTOMERSは作成されました。


[中略]


End time: 02-OCT-26 12.28.29.116468 PM +00:00    


1行が選択されました。 



Installation verification    

____________________________ 

Verification:                


Table                            provided    actual 

_____________________________ ___________ _________ 

channels                                5         5 

costs                               82112     82112 

countries                              35        35 

customers                           55500     55500 

products                               72        72 

promotions                            503       503 

sales                              918843    918843 

times                                1826      1826 

supplementary_demographics           4500      4500 


Thank you!                                                  

___________________________________________________________ 

The installation of the sample schema is now finished.      

Please check the installation verification output above.    

You will now be disconnected from the database.             

Thank you for using Oracle Database!                        

Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free

Version 23.26.3.0.0から切断されました

sales_history % 


続いてWrenAIのインストールと構成を行います。WrenAIはPythonで記述されています。Pythonの実行環境の構成にuvを使用します。あらかじめuvがインストールされていることを前提とします。

作業ディレクトリとしてwrenai-sales-historyを作成します。このディレクトリは後ほど作成するCodexのプロジェクトに紐づけます。

mkdir wrenai-sales-history
cd wrenai-sales-history


Documents % mkdir wrenai-sales-history

Documents % cd wrenai-sales-history 

wrenai-sales-history % 


仮想環境を作成します。本記事では、macOSに標準でインストールされている3.14を使用しています。

uv python install 3.14
uv venv --python 3.14
source .venv/bin/activate


wrenai-sales-history % uv python install 3.14

Python 3.14 is already installed

wrenai-sales-history % uv venv --python 3.14

Using CPython 3.14.7

Creating virtual environment at: .venv

Activate with: source .venv/bin/activate

wrenai-sales-history % source .venv/bin/activate

(wrenai-sales-history) wrenai-sales-history % 


WrenAIをインストールします。データベース接続をブラウザで設定できるように、追加パッケージのuiもインストールします。

uv pip install "wrenai[ui,oracle,memory]"

wrenai-sales-history % uv pip install "wrenai[ui,oracle,memory]"

Resolved 78 packages in 306ms

Installed 78 packages in 672ms

 + annotated-doc==0.0.5

 + annotated-types==0.8.0

 + anyio==4.15.1

 + boto3==1.43.107

 + botocore==1.43.107

 + certifi==2026.7.22

 + cffi==2.1.1

 + charset-normalizer==3.5.2

 + click==8.5.0

 + cloudpickle==3.1.2

 + cryptography==50.0.2

 + deprecation==2.1.0

 + duckdb==1.5.6

 + filelock==4.0.9

 + fsspec==2026.9.0

 + h11==0.16.0

 + hf-xet==1.6.0

 + httpcore==1.0.9

 + httpx==0.28.1

 + huggingface-hub==1.33.0

 + idna==3.20

 + jinja2==3.1.6

 + jmespath==1.1.0

 + joblib==1.6.0

 + lance-namespace==0.13.0

 + lance-namespace-urllib3-client==0.13.0

 + lancedb==0.39.0

 + loguru==0.7.3

 + lxml==6.1.3

 + markdown-it-py==4.2.0

 + markupsafe==3.0.3

 + mdurl==0.1.2

 + mpmath==1.3.0

 + narwhals==2.26.0

 + networkx==3.7

 + numpy==2.5.3

 + opendal==0.47.10

 + oracledb==26.0.1

 + packaging==26.3

 + pandas==3.0.6

 + pyarrow==25.0.1

 + pyarrow-hotfix==0.7

 + pyasn1==0.6.4

 + pycparser==3.0

 + pydantic==2.13.5

 + pydantic-core==2.46.5

 + pygments==2.21.0

 + pyopenssl==26.4.0

 + python-dateutil==2.9.0.post0

 + python-dotenv==1.2.4

 + python-multipart==0.0.32

 + pyyaml==6.0.3

 + regex==2026.9.29

 + requests==2.34.2

 + rich==15.0.0

 + s3transfer==0.19.2

 + safetensors==0.8.0

 + scikit-learn==1.9.1

 + scipy==1.18.1

 + sentence-transformers==6.1.0

 + setuptools==84.0.0

 + shellingham==1.5.4

 + six==1.17.0

 + sqlglot==30.21.0

 + starlette==1.7.0

 + sympy==1.14.0

 + threadpoolctl==3.7.0

 + tokenizers==0.23.2

 + torch==2.14.1

 + tqdm==4.70.1

 + transformers==5.18.0

 + typer==0.27.2

 + typing-extensions==4.16.0

 + typing-inspection==0.4.4

 + urllib3==2.8.0

 + uvicorn==0.54.0

 + wren-core-py==0.8.0

 + wrenai==0.15.0

wrenai-sales-history % 


データベースへの接続を構成します。接続先はローカルのコンテナで実行しているOracle AI Database 26ai Free、PDBはFREEPDB1、スキーマはSHです。

wren profile add my-db --ui

wrenai-sales-history % wren profile add my-db --ui

Opening browser... (press Ctrl+C to cancel)



ブラウザが開きます。Data Sourceにoracleを選択し、Host、Port、Database、User、Passwordをそれぞれ設定し、Save Profileを実行します。接続情報はmy-dbとして保存されます。


プロファイルmy-dbが保存されたら、ブラウザをクローズできます。

WrenAIのスキルをインストールします。今回はAIエージェントとしてCodexを使用します。Codex向けのスキルは、デフォルトの選択肢でインストールされます。

npx skills add Canner/WrenAI

wrenai-sales-history % npx skills add Canner/WrenAI


███████╗██╗  ██╗██╗██╗     ██╗     ███████╗

██╔════╝██║ ██╔╝██║██║     ██║     ██╔════╝

███████╗█████╔╝ ██║██║     ██║     ███████╗

╚════██║██╔═██╗ ██║██║     ██║     ╚════██║

███████║██║  ██╗██║███████╗███████╗███████║

╚══════╝╚═╝  ╚═╝╚═╝╚══════╝╚══════╝╚══════╝


┌   skills 

│

◇  Source: https://github.com/Canner/WrenAI.git

│

◇  Repository cloned

│

◇  Found 1 skill

│

●  Skill: wren

│

│  Wren CLI for AI agents — a semantic SQL layer over 22+ databases (Postgres, MySQL, BigQuery, Snowflake, Spark, …). The actual workflow guides live inside the `wren` CLI itself; this is just a discovery stub. Use whenever the user asks a data question (how many, show me, top N, compare, trend, breakdown, metric, revenue, customers, orders), wants to install / set up Wren Engine, connect a new database, connect SaaS data via dlt (HubSpot, Stripe, Salesforce, GitHub, Slack), generate or regenerate an MDL project from a database schema, enrich a project with business context (enum meanings, units, cubes like ARR / DAU / churn), or turn a project's context layer into a shareable GenBI web app / dashboard and deploy it to Vercel or Cloudflare. Triggers: 'install wren', 'set up wren engine', 'connect database to wren', 'connect SaaS to wren', 'load hubspot / stripe / salesforce data', 'generate mdl', 'scaffold wren project', 'enrich wren context', 'augment my project', 'add cubes', 'build a dashboard', 'make a shareable analytics app', 'deploy my context layer as a web app', 'genbi app', 'wren onboarding', 'wren usage', 'wren generate mdl', 'wren dlt connector', 'wren enrich context', 'wren genbi'.

│

◇  79 agents

◇  Which agents do you want to install to?

│  Amp, Cline, Codex, Cursor, Droid, Gemini CLI, GitHub Copilot, Kilo Code, Kimi Code CLI, OpenCode, Warp, Zed

│

◇  Installation scope

│  Project


│

◇  Installation Summary ─────────────────────────────────╮

│                                                        │

│  ~/Documents/wrenai-sales-history/.agents/skills/wren  │

│    copy → Amp, Cline, Codex, Cursor, Droid +7 more     │

│                                                        │

├────────────────────────────────────────────────────────╯

│

◇  Security Risk Assessments ──────────────────────────╮

│                                                      │

│        Gen               Socket            Snyk      │

│  wren  Safe              0 alerts          Low Risk  │

│                                                      │

│  Details: https://skills.sh/Canner/WrenAI            │

│                                                      │

├──────────────────────────────────────────────────────╯

│

◇  Proceed with installation?

│  Yes

│

◇  Installation complete


│

◇  Installed 1 skill ────────────────────────────────────────╮

│                                                            │

│  ✓ wren (copied)                                           │

│    → ~/Documents/wrenai-sales-history/.agents/skills/wren  │

│                                                            │

├────────────────────────────────────────────────────────────╯


│

└  Done!  Review skills before use; they run with full agent permissions.


wrenai-sales-history % 


これからはCodexにプロジェクトを作成し、作業を進めます。

OpenAIのデスクトップ・アプリでCodexを選択し、プロジェクトを作成します。


作成するプロジェクトの名前をWrenAI Sales History、ソールフォルダーとして、これまで準備してきたwrenai-sales-historyを紐づけます。

以上でプロジェクトを作成します。


プロジェクトが作成されたら、そのプロジェクトでタスクを開始します。まずは、Oracleデータベースのセットアップを行います。

「プロファイルmy-dbに構成されているOracle Databaseに接続して、Wrenによる問い合わせができるようにセットアップして。」

インストール済みのWrenのスキルを参照して、構成作業が実行されます。


Codexからの案内に従って、ディレクトリwrenai-sales-historyで以下のコマンドを実行します。

.venv/bin/wren --sql 'SELECT count(*) from sales'

wrenai-sales-history % .venv/bin/wren --sql 'SELECT count(*) from sales'

 COUNT(*)

   918843


# To save this query:

# wren memory store --nl '<natural language question>' --sql 'SELECT count(*) from sales'

wrenai-sales-history % 


あとは、自然言語で思ったように問い合わせます。

「どんな質問に答えられますか?」


ここまでの作業で、WrenAIを通してOracle Databaseに問い合わせることができるようになりました。

GitHubのWrenAIのページに、これからWrenAIでできることが説明されています。WrenAIについては、日本語による紹介記事も複数見つかるので、それらを参照するとWrenAIへの理解も深まるかと思います。

以下はWrenAIで印象的だった内容です。

1. 多数のデータソースに対応していること。


プロファイル作成時にData Sourceとして、以下を選択できます。モデルの情報などは、データベースから取り出しファイルとして管理するため、WrenAI側ではあまりデータソースの違いは意識しないようです。データソースの引越し時にも、それまでWrenAIで溜め込んだビジネス知識が無駄になる危険を回避できます。


2. AIエージェントを選ばないこと。


"npx skills add Canner/WrenAI"でインストールされたスキルを認識できるAIエージェントであれば、概ね何でも使用できそうです。(Pythonのコードは別途pipでインストールする必要はあります。)

API呼び出しに限らないため、AIエージェントによってはサブスクリプションの範囲で活用できます。

3. テキストファイルでビジネス知識を記述でき、Gitで管理できること。


個別の利用者がテキストファイルに追加したビジネス知識がうまく動くようであれば、それを他のユーザーにも共有できます。概ね一般的な、アプリケーション開発に近いフローでビジネス知識を拡張できます。

それぞれの利用者が手元で動かすCodexやClaude Codeで使用するのも良いですが、Hermes Agentと組み合わせても面白そうです。

今回の記事は以上になります。

完