|
| 1 | +from os import getenv |
| 2 | + |
| 3 | +import plotly.express as px |
| 4 | +import streamlit as st |
| 5 | +from dotenv import load_dotenv |
| 6 | +from openai import AzureOpenAI |
| 7 | +from pandas import DataFrame |
| 8 | +from plotly.graph_objs import Figure |
| 9 | + |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | + |
| 13 | +with st.sidebar: |
| 14 | + azure_openai_endpoint = st.text_input( |
| 15 | + label="AZURE_OPENAI_ENDPOINT", |
| 16 | + value=getenv("AZURE_OPENAI_ENDPOINT"), |
| 17 | + key="AZURE_OPENAI_ENDPOINT", |
| 18 | + type="default", |
| 19 | + ) |
| 20 | + azure_openai_api_key = st.text_input( |
| 21 | + label="AZURE_OPENAI_API_KEY", |
| 22 | + key="AZURE_OPENAI_API_KEY", |
| 23 | + type="password", |
| 24 | + ) |
| 25 | + azure_openai_api_version = st.text_input( |
| 26 | + label="AZURE_OPENAI_API_VERSION", |
| 27 | + value=getenv("AZURE_OPENAI_API_VERSION"), |
| 28 | + key="AZURE_OPENAI_API_VERSION", |
| 29 | + type="default", |
| 30 | + ) |
| 31 | + azure_openai_gpt_model = st.text_input( |
| 32 | + label="AZURE_OPENAI_GPT_MODEL", |
| 33 | + value=getenv("AZURE_OPENAI_GPT_MODEL"), |
| 34 | + key="AZURE_OPENAI_GPT_MODEL", |
| 35 | + type="default", |
| 36 | + ) |
| 37 | + "[Go to Azure Portal to get an Azure OpenAI API key](https://portal.azure.com/)" |
| 38 | + "[Go to Azure OpenAI Studio](https://oai.azure.com/resource/overview)" |
| 39 | + "[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_llm_examples/pages/5_Explain_data.py)" |
| 40 | + "[Streamlit > st.plotly_chart](https://docs.streamlit.io/develop/api-reference/charts/st.plotly_chart)" |
| 41 | + "[Plotly > Time Series and Date Axes in Python](https://plotly.com/python/time-series/)" |
| 42 | + |
| 43 | +st.title("Explain data") |
| 44 | + |
| 45 | +if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_gpt_model: |
| 46 | + st.warning("サイドバーに Azure OpenAI の設定を入力してください") |
| 47 | + st.stop() |
| 48 | + |
| 49 | +st.info("データを説明するサンプルです") |
| 50 | + |
| 51 | + |
| 52 | +def get_dataset() -> DataFrame: |
| 53 | + return px.data.stocks() |
| 54 | + |
| 55 | + |
| 56 | +def get_figure(df: DataFrame) -> Figure: |
| 57 | + fig = px.line( |
| 58 | + df, |
| 59 | + x="date", |
| 60 | + y=df.columns, |
| 61 | + hover_data={ |
| 62 | + "date": "|%B %d, %Y", |
| 63 | + }, |
| 64 | + title="2018 年の株価を基準とした株価の変化", |
| 65 | + ) |
| 66 | + fig.update_xaxes( |
| 67 | + dtick="M1", |
| 68 | + tickformat="%b\n%Y", |
| 69 | + ) |
| 70 | + return fig |
| 71 | + |
| 72 | + |
| 73 | +def analyze_data(df: DataFrame) -> str: |
| 74 | + return df.describe(include="all").__str__() |
| 75 | + |
| 76 | + |
| 77 | +def explain_data(input: str) -> str: |
| 78 | + client = AzureOpenAI( |
| 79 | + api_key=azure_openai_api_key, |
| 80 | + api_version=azure_openai_api_version, |
| 81 | + azure_endpoint=azure_openai_endpoint, |
| 82 | + ) |
| 83 | + |
| 84 | + response = client.chat.completions.create( |
| 85 | + model=azure_openai_gpt_model, |
| 86 | + messages=[ |
| 87 | + { |
| 88 | + "role": "system", |
| 89 | + "content": f"""あなたはプロのデータ分析者です。与えられたデータについて説明します。 |
| 90 | + --- |
| 91 | + {input} |
| 92 | + ---""", |
| 93 | + }, |
| 94 | + ], |
| 95 | + ) |
| 96 | + return response.choices[0].message.content |
| 97 | + |
| 98 | + |
| 99 | +df = get_dataset() |
| 100 | + |
| 101 | +fig = get_figure(df) |
| 102 | + |
| 103 | +st.plotly_chart( |
| 104 | + figure_or_data=fig, |
| 105 | + theme="streamlit", |
| 106 | + use_container_width=True, |
| 107 | +) |
| 108 | + |
| 109 | +explain_button = st.button("説明") |
| 110 | + |
| 111 | +if explain_button: |
| 112 | + with st.spinner("数値解析中..."): |
| 113 | + analyze_result = analyze_data(df) |
| 114 | + with st.spinner("LLM による解析中..."): |
| 115 | + explain_result = explain_data(input=analyze_result) |
| 116 | + # st.write(analyze_result) |
| 117 | + st.write(explain_result) |
0 commit comments