Try   HackMD

生成式 AI 微調與呼叫 (Google AI Studio)

登入 Google AI Studio

取得 API keys

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Create new prompt

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

建立資料集

  • 設定多個 inputoutput
  • 使用一問一答(Q&A)、多輪對話、任務導向型對話等方式
  • 可嘗試將相關檔案上傳到AI Studio透過語言模型協助產生資料集
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

選擇模型及輸出設定

  • 中文自述如何換算tokens
    • 約字數(含標點符號) / 2
  • 模型比較表
    模型 用途 即用即付(美元價格) 免費
    gemini-1.0-pro 自然語言工作、多輪文字和程式碼聊天,以及產生程式碼 $1.50 / 1 million tokens 15 RPM(每分鐘請求數)/ 32,000 TPM(每分鐘令牌)/ 1,500 RPD(每天請求)
    gemini-1.5-pro 複雜的推理工作,例如程式碼和文字生成、文字編輯、問題解決、資料擷取及產生 $21.00 / 1 million tokens 2 RPM(每分鐘請求數) / 32,000 TPM(每分鐘令牌)/ 50 RPD(每天請求)
    gemini-1.5-flash 快速靈活地處理各種工作 $2.10 / 1 million tokens 15 RPM(每分鐘請求數) / 100 萬個 TPM(每分鐘令牌) / 1,500 RPD(每天請求)

image

Tune a model

  • 選擇基底模型

    image

  • 調整微調參數

    • Tuning epochsinfo
    • Learning rate multiplierinfo
    • Batch sizeinfo

    image

查看訓練結果

image

測試模型問答

  • 輸入資料集中所包含的 input ,並確認 output 是否為理想答案
    • 如果 output 文不對題,則重新調整參數訓練
      image

使用 OAuth 進行身份驗證

透過 函數 或 flask 呼叫模型

  • 工作資料夾
    • token.json (運行 load_creds.py 後自動生成)
    • client_secret.json (上一個步驟產生)
    • load_creds.py (將client_secret.json轉換為token.json)
    • show.py (顯示可用模型)
    • main.py (主程式)
    • test.py (需要運行 main.py)
  • 環境安裝
    • pip install Flask
    • pip install google-generativeai
  • load_creds.py
import os.path

from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow

SCOPES = ['https://www.googleapis.com/auth/generative-language.tuning']

def load_creds():
    """Converts `oauth-client-id.json` to a credential object.

    This function caches the generated tokens to minimize the use of the
    consent screen.
    """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'client_secret.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())
    return creds
  • show.py
import google.generativeai as genai

# 印出該帳號下所有可用的模型
print('Available base models:', [m.name for m in genai.list_models()])
  • main.py
import google.generativeai as genai
from load_creds import load_creds

# 憑證
creds = load_creds()
genai.configure(credentials=creds)

# 輸出設定
generation_config = {
    "temperature": 0.9,
    "top_p": 1,
    "top_k": 1,
    "max_output_tokens": 8192,
}
safety_settings = []

def tune_chat(mes:str):
    model = genai.GenerativeModel(model_name="tunedModels/模型編號",
                  generation_config=generation_config,
                  safety_settings=safety_settings)
    prompt_parts = [mes]
    response = model.generate_content(prompt_parts)
    return response.text

def gemini_chat(mes:str):
    model = genai.GenerativeModel(model_name="gemini-pro",
                  generation_config=generation_config,
                  safety_settings=safety_settings)
    prompt_parts = [mes]
    response = model.generate_content(prompt_parts)
    return response.text

from flask import Flask
from flask import request

app = Flask(__name__)

# 微調模型
@app.route('/tune', methods=['POST'])
def tune():
    try:
        data = request.get_json()
        message = data.get('message')
        response = tune_chat(message)
        return response
    except Exception as e:
        return str(e), 500

# 原始模型
@app.route('/gemini', methods=['POST'])
def gemini():
    try:
        data = request.get_json()
        message = data.get('message')
        response = gemini_chat(message)
        return response
    except Exception as e:
        return str(e), 500

if __name__ == '__main__':
    app.run()
  • test.py
import requests

mes = input("請輸入訊息:")
response = requests.post('http://localhost:5000/tune', json={'message':mes})

print(response.text)