Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PandasAIにおけるLLMを用いた自然言語クエリの仕組み

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 PandasAIにおけるLLMを用いた自然言語クエリの仕組み

Avatar for Takatomo Torigoe

Takatomo Torigoe

October 20, 2023
Tweet

More Decks by Takatomo Torigoe

Other Decks in Programming

Transcript

  1. PandasAI とは • Pandas に LLM を組み合わせて、自然言語でデータ分析を行うライブラリ • データ探索・分析用途のため production

    では使わないで、とのお断り • LLMは OpenAI GPT, Google PaLM ほか LangChain 経由で諸々使える • 似た立ち位置のライブラリとして pyspark-ai がある 補足 • 本スライドは v1.3.3 (2023/10) 準拠です • 以降のプロンプトやコードは、改行&インデントの整形を行っています
  2. PandasAI - chat() の仕組み 1. テンプレート生成したプロンプトを LLM に投げて、コード生成してもらう ◦ 生成済みコードのキャッシュが既にあれば、それを利用

    2. 生成されたコードを実行する ◦ エラーが発生したら、データフレーム・自然言語クエリ・生成コード、エ ラー結果を LLM に投げて、コード修正してもらったうえでリトライ ◦ 実行結果の型が不整合であれば Warning 3. 実行結果の型に応じて整形
  3. PandasAI - コード生成プロンプト例 1/5 You are provided with the following

    pandas DataFrames: <dataframe> Dataframe dfs[0], with 10 rows and 3 columns. This is the metadata of the dataframe dfs[0]: country,gdp,happiness_index United Kingdom,1194260612,7.16 United States,5382530119,6.94 Germany,7427242378,7.07 France,3889391010,6.66 Italy,5937009073,6.38 </dataframe>  データフレームのサンプルデータ  ただし enforce_privacy 設定が有効なら省略 参照:generate_python_code.tmpl, _generate_dataframes(), _get_sample_head()  データフレームの行列数と  ヘッダを埋め込み  複数データフレームの場合は上記を繰り返す
  4. PandasAI - コード生成プロンプト例 2/5 <conversation> User 1: Return the top

    5 countries by GDP </conversation> This is the initial python code to be updated: ```python # TODO import all the dependencies required import pandas as pd 参照:generate_python_code.tmpl  ユーザーが chat() の引数として  与えた自然言語クエリを埋め込み  Pandas 以外のライブラリ使う  コードを生成するなら  import 増やしてね、という気持ち
  5. PandasAI - コード生成プロンプト例 3/5 def analyze_data(dfs: list[pd.DataFrame]) -> dict: """

    Analyze the data 1. Prepare: Preprocessing and cleaning data if necessary 2. Process: Manipulating data for analysis (grouping, filtering, aggregating, etc.) 3. Analyze: Conducting the actual analysis (if the user asks to plot a chart save it to an image in temp_chart.png and do not show the chart.) 参照:generate_python_code.tmpl, GeneratePythonCodePrompt()  ask, prepare, process,  analyze, share, act  というデータ分析の  6段階フレームワーク中の  3段階に対応してね  という気持ち  ユーザーが  Custom Instructions  を指定していなければ  そちらにも同文を設定
  6. PandasAI - コード生成プロンプト例 4/5 Examples: { "type": "string", "value": "The

    highest salary is $9,000." } or { "type": "number", "value": 125 } or { "type": "dataframe", "value": pd.DataFrame({...}) } or { "type": "plot", "value": "temp_chart.png" } """ 生成箇所:generate_python_code.tmpl  返り値の型情報も  合わせて返してね  という気持ち  
  7. Pandas AI - コード生成プロンプト例 5/5 Using the provided dataframes (`dfs`),

    update the python code based on the last question in the conversation. Updated code:  ということで  うまいことコード生成してね  という気持ち • 上記は SmartDataframe の last_prompt で確認可能 生成箇所:generate_python_code.tmpl
  8. PandasAI - 生成されたコード例 def analyze_data(dfs: list[pd.DataFrame]) ->dict: """ (プロンプト通りのコメントのため省略) """

    sorted_df = dfs[0].sort_values(by='gdp', ascending=False) top_5_countries = sorted_df.head(5) top_5_countries_str = top_5_countries.to_string(index=False) return {'type': 'string', 'value': top_5_countries_str} • 上記は SmartDataframe の last_code_{generated | executed} で確認可能 Return the top 5 countries by GDP に対する 過不足ないコード