2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[OCI]Compute上のjupyter notebookからOCI生成AIサービスを利用する(OL9)

Posted at

1. はじめに

OCI Compute上に導入したjupyter notebookからOCI生成AIサービスを利用してLLMとの会話を実装します。
最後にはADBの情報を利用した簡易なRAGの実装も記載しています。

OCI生成AIサービスは現在Tokyoリージョンではサービス提供されていません。
Osakaリージョンの生成AIサービスを利用します。

前提条件

https://qiita.com/twakimura/items/e2b145e2460c388ba0cc
この記事を参考に Compute インスタンス上にjupyter notebookを導入しています。
その際、Osakaリージョンで構築します。
上記記事ではOL8で構築していましたが、Osakaリージョンを利用するため、OL9で作り直しています。
※Osakaリージョンの生成AIサービスをTokyoリージョンから利用することは可能です。

2. APIキーの登録

右上のアイコンからユーザー設定を選択します。

image.png

トークンおよびキーからAPIの追加を選択します。

image.png

秘密キーと公開キーをダウンロードし、追加します。

image.png

構成ファイルのプレビューが表示されるので、構成ファイルの内容をコピーしてテキスト等に保存しておきます。

image.png

ダウンロードした秘密キーファイルをサーバーのユーザーのホームディレクトリに「.oci」フォルダを作成し、その中に格納します。
パスは以下です。

/home/<ユーザー名>/.oci/<秘密キーファイル名>.pem

2. OCI CLIの設定

以下を参考に、OCI CLIをインストールします。
https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm#InstallingCLI__oraclelinux9

$ sudo dnf -y install oraclelinux-developer-release-el9
$ sudo dnf install python39-oci-cli

以下のコマンドでconfigファイルを作成します。

$ oci setup config

途中で以下が聞かれるのでAPIキーの構成ファイルに記載されていた値を入力して進めます。

$ oci setup config
    This command provides a walkthrough of creating a valid CLI config file.

    The following links explain where to find the information required by this
    script:

    User API Signing Key, OCID and Tenancy OCID:

        https://docs.cloud.oracle.com/Content/API/Concepts/apisigningkey.htm#Other

    Region:

        https://docs.cloud.oracle.com/Content/General/Concepts/regions.htm

    General config documentation:

        https://docs.cloud.oracle.com/Content/API/Concepts/sdkconfig.htm


Enter a location for your config [/home/oracle/.oci/config]:
Enter a user OCID: < [構成ファイルのuser部分ocid1.user.oc1..xxxxxxxxxxxxxxxxxxxxという文字列]>
Enter a tenancy OCID: < [構成ファイルのtenancy部分ocid1.tenancy.oc1..xxxxxxxxxxxxxxxxxxxxという文字列]>
Enter a region by index or name(e.g.
1: af-johannesburg-1, 2: ap-batam-1, 3: ap-chiyoda-1, 4: ap-chuncheon-1, 5: ap-chuncheon-2,
6: ap-dcc-canberra-1, 7: ap-dcc-gazipur-1, 8: ap-delhi-1, 9: ap-hyderabad-1, 10: ap-ibaraki-1,
11: ap-melbourne-1, 12: ap-mumbai-1, 13: ap-osaka-1, 14: ap-seoul-1, 15: ap-seoul-2,
16: ap-singapore-1, 17: ap-singapore-2, 18: ap-suwon-1, 19: ap-sydney-1, 20: ap-tokyo-1,
21: ca-montreal-1, 22: ca-toronto-1, 23: eu-amsterdam-1, 24: eu-budapest-1, 25: eu-crissier-1,
26: eu-dcc-dublin-1, 27: eu-dcc-dublin-2, 28: eu-dcc-milan-1, 29: eu-dcc-milan-2, 30: eu-dcc-rating-1,
31: eu-dcc-rating-2, 32: eu-dcc-zurich-1, 33: eu-frankfurt-1, 34: eu-frankfurt-2, 35: eu-jovanovac-1,
36: eu-madrid-1, 37: eu-madrid-2, 38: eu-madrid-3, 39: eu-marseille-1, 40: eu-milan-1,
41: eu-paris-1, 42: eu-stockholm-1, 43: eu-turin-1, 44: eu-zurich-1, 45: il-jerusalem-1,
46: me-abudhabi-1, 47: me-abudhabi-2, 48: me-abudhabi-3, 49: me-abudhabi-4, 50: me-alain-1,
51: me-dcc-doha-1, 52: me-dcc-muscat-1, 53: me-dubai-1, 54: me-ibri-1, 55: me-jeddah-1,
56: me-riyadh-1, 57: mx-monterrey-1, 58: mx-queretaro-1, 59: sa-bogota-1, 60: sa-santiago-1,
61: sa-saopaulo-1, 62: sa-valparaiso-1, 63: sa-vinhedo-1, 64: uk-cardiff-1, 65: uk-gov-cardiff-1,
66: uk-gov-london-1, 67: uk-london-1, 68: us-ashburn-1, 69: us-ashburn-2, 70: us-chicago-1,
71: us-gov-ashburn-1, 72: us-gov-chicago-1, 73: us-gov-phoenix-1, 74: us-langley-1, 75: us-luke-1,
76: us-newark-1, 77: us-phoenix-1, 78: us-saltlake-2, 79: us-sanjose-1, 80: us-somerset-1,
81: us-thames-1): < [構成ファイルregionに該当するコードOsakaは13]>
Do you want to generate a new API Signing RSA key pair? (If you decline you will be asked to supply the path to an existing key.) [Y/n]: < [n]>
Enter the location of your API Signing private key file: < [/home/ユーザー名/.oci/秘密鍵ファイル名.pem]>
Fingerprint: 3c:c8:e3:61:02:b4:34:4f:ea:2e:bc:0a:96:14:6d:6b
Config written to /home/oracle/.oci/config


    If you haven't already uploaded your API Signing public key through the
    console, follow the instructions on the page linked below in the section
    'How to upload the public key':

        https://docs.cloud.oracle.com/Content/API/Concepts/apisigningkey.htm#How2


$

4. OCI生成AIサービス

OsakaリージョンでアナリティクスととAI->生成AIメニューを選択します。

image.png

生成AIメニューのチャットを選択します。

image.png

モデルを選択し、コードの表示を選択します。

image.png

参考に、現在選択できるモデルの特徴を生成AIにまとめてもらった結果を添付します。
利用できるモデルは随時更新される可能性がありますので、用途に適したモデルを選択します。

モデル名 提供元 パラメータ規模/バージョン 主な用途・特徴 補足
cohere.command-a-03-2025 v1.0 Cohere 次世代モデル (大規模) 高精度の生成タスク・複雑な指示処理・情報抽出 日本語対応強化・2025向けバージョン
cohere.command-latest Cohere 最新安定版 高品質な自然言語処理、質問応答・要約・対話に強み 標準モデル
cohere.command-plus-latest Cohere 最新・強化版 command-latestより拡張(精度向上・大規模データ学習) コスト高/高精度
cohere.command-r-08-2024 v1.7 Cohere 2024年8月リリース版 分野特化や改良点あり(バージョン注意)
cohere.command-r-plus-08-2024 v1.6 Cohere Plus機能追加版(2024/8版) command-r-08の強化版、より高性能
meta.llama-3.2-90b-vision-instruct Meta (LLAMA3) 約900億パラメータ+ビジョン機能 画像も入力可能な最先端マルチモーダルモデル、指示理解力や創造力が高い マルチモーダル
meta.llama-3.3-70b-instruct Meta (LLAMA3) 約700億パラメータ 高性能かつ現実的なコスト・生成精度、指示特化 最新世代
openai.gpt-oss-120b OpenAI 1200億パラメータ OSS/商用利用向けLLMで高精度、広い業務応用 複数言語対応
openai.gpt-oss-20b OpenAI 200億パラメータ 軽量モデル、応答スピード優先 or コスト抑制用途 小型・軽量

コードの表示画面で開発ツールをここではPythonを選択し、コピーします。

image.png

コードをjupyter notebookにペーストします。
その際、「chat_request.message = "{input}"」の部分を以下のようにLLMへの質問文に書き換えます。

image.png

実行すると以下のようにOCI生成AIサービスに問い合わせた結果が取得できました。

image.png

回答文字列だけ取得する場合は以下のようにJSONの階層を指定して取得すればいいですね。

image.png

5. RAG

また、以下の前回記事でADBへの接続もできている場合は、RAGとしての使い方もできます。
https://qiita.com/twakimura/items/e2b145e2460c388ba0cc

以下のテーブルを用意しました。

NAME PRICE
みかん 80
りんご 130

select_db()はテーブル検索結果をdf形式で返却します。
ask_llm()は前述のmessageに設定するtextを引数から渡す形に修正しています。
llmに渡す際はdfをテキスト形式にして渡すようにしています。

# DB内容と質問をプロンプトにしてLLMに聞く
def ask_from_db(question):
    df = select_db()
    table_text = df.to_string(index=False)
    prompt = (
        f"以下は果物のデータベーステーブルです。\n"
        f"{table_text}\n\n"
        f"この情報を参考に、次の質問に日本語で端的に答えてください:\n{question}"
    )
    return ask_llm(prompt)

# 実行例
if __name__ == "__main__":
    question = "りんごとみかんを合わせて10個買います。合計金額を1000円にするには何をいくつ買えばいいですか?"
    answer = ask_from_db(question)
    print(answer)

実行結果は以下になりました。
DBの情報を元に回答を生成してくれていますね。

image.png

以上で、PythonからOCI生成AIサービスの利用、及びADBを活用したRAGの構築ができました。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?