見出し画像

グーグルの大規模言語モデルPaLM APIを利用したウェブアプリの作成方法

グーグルの大規模言語モデルのPaLM APIを利用することができるようになりましたので、今回は、PaLM APIのページにありましたカテゴリを利用してウェブアプリを作成してみました。

上記ページには、Code、Writing、Editing、Problem solving、Recommend、DataとAgentsとあります。Agentsはうまく利用の仕方を理解できなかったので除外しておりますが、それ以外の項目についてサンプルとして試せるようなウェブアプリを作成してみました。Promptは、上記ページから流用しております。


初期画面


今回のコードは、以下をapp.pyとして保存してください。

#Import the basic library
import os
import streamlit as st

 #PaLM import google.generativeai as palm

#Set the API Key For PaLM
palm.configure(api_key="AIxxxxxxxxxxxxxxxxxxxxx")

#Title
st.title('🤗 Google PaLM API')
prompt = st.text_input('Input the text here')

#select the action
sel_action=st.radio("Action",["none","Code","Writing","Editing","Problem solving","Recommend","Data"])

#set the action in text
if sel_action=="none":
    st.write("Please select the action")

#set the action in text
if sel_action=="Code":
    st.write("""Example: Could you please help me to write code to generate multiples of
                 a number from a given list.""")

#set the action in text
if sel_action=="Writing":
    st.write("""Example: Come up with six taglines making a persuasive case for Red Delicious apples 
                that I can share with Red Delicious apple skeptics.""")

#set the action in text
if sel_action=="Editing":
    st.write("""Example: Rewrite this into a casual email:
            Tis but thy name that is my enemy;
            Thou art thyself, though not a Montague.
            What’s Montague? It is nor hand, nor foot,
            Nor arm, nor face, nor any other part
            Belonging to a man. O, be some other name!
            What’s in a name? That which we call a rose
            By any other name would smell as sweet;
            So Romeo would, were he not Romeo call’d,
            Retain that dear perfection which he owes
            Without that title. Romeo, doff thy name,
            And for that name which is no part of thee
            Take all myself.""")

#set the action in text
if sel_action=="Problem solving":
    st.write("""Example: Six people were eating pickles. Carl finished before Dot, 
                but behind Ed. Fan finished before Gal, but behind Dot. Hans finished first. 
                What was the finishing order?""")

#set the action in text
if sel_action=="Recommend":
    st.write("""Example:My favorite films are Dune, The Dark Knight, and Princess Mononoke. 
            What are four movies that I should watch next? Nothing scary, please.""")

#set the action in text
if sel_action=="Data":
    st.write("""Example: Give me 3 classic Thai dishes, along with a short description. 
                Write the data in the following JSON format:
                { "dish": DISH_NAME, "description": DESCRIPTION }""")

defaults = {
  'model': 'models/text-bison-001',
  'temperature': 0.7,
  'candidate_count': 1,
  'top_k': 40,
  'top_p': 0.95,
  'max_output_tokens': 1024,
  'stop_sequences': [],
  'safety_settings': [{"category":"HARM_CATEGORY_DEROGATORY","threshold":1},{"category":"HARM_CATEGORY_TOXICITY","threshold":1},{"category":"HARM_CATEGORY_VIOLENCE","threshold":2},{"category":"HARM_CATEGORY_SEXUAL","threshold":2},{"category":"HARM_CATEGORY_MEDICAL","threshold":2},{"category":"HARM_CATEGORY_DANGEROUS","threshold":2}],
}

def generate_response(prompt):
    response = palm.generate_text(**defaults, prompt=prompt)
    st.write(response.result)


if st.button('Click'):
    if sel_action=="Code":
        prompt=f"Write code for {prompt}"
        generate_response(prompt)
    elif sel_action=="Writing":
        prompt=f"Write for {prompt}"
        generate_response(prompt)
    elif sel_action=="Editing":
        prompt=f"Rewrite for {prompt}"
        generate_response(prompt)
    elif sel_action=="Problem solving":
        prompt=f"Problem solving for {prompt}"
        generate_response(prompt)
    elif sel_action=="Recommend":
        promt=f"Recommend for {prompt}"
        generate_response(prompt)
    elif sel_action=="Data":
        promt=f"Write the data in the JSON format: {prompt}"
        generate_response(prompt)


PaLM API Keyの取得方法は、以前書いた記事が参考になります。Keyを取得したらAIxxxxxxxxxxxxxxxxに記載ください。



次に、自PCで以下を実施します。

python -m venv venv
venv\Scripts\Activate
pip install google-generativeai
pip install streamlit
streamlit run app.py


実行画面

使い方は、ActionのところのCodeボタンを選択しますと、サンプルテキストが出てきますので、参考にして入力フォームに記載します。


クリックを押します。


コードが作成されます。


他にも、Dataボタンをクリックして、Exampleを参考にテキストに入力してクリックします。


Dataの場合


Exampleを参考に、聞き方を覚えて色々なバリエーションを試してみますと理解が深まると思われます。

この記事が気に入ったらサポートをしてみませんか?