# 生成式 AI 微調與呼叫 (Google AI Studio)
:::info
請參考以下網站
- [Google AI Studio](https://aistudio.google.com)
- [Google AI for Developers](https://ai.google.dev/?hl=zh-tw)
:::
### 登入 Google AI Studio
- [傳送門](https://aistudio.google.com)
### 取得 API keys

### Create new prompt

### 建立資料集
- 設定多個 **input** 及 **output**
- 使用一問一答(Q&A)、多輪對話、任務導向型對話...等方式
- 可嘗試將相關檔案上傳到AI Studio透過語言模型協助產生資料集

### 選擇模型及輸出設定
- 中文自述如何換算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(每天請求)|

### Tune a model
- 選擇基底模型

- 調整微調參數
- Tuning epochsinfo
- Learning rate multiplierinfo
- Batch sizeinfo

### 查看訓練結果

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

### 使用 OAuth 進行身份驗證
- 透過身分驗證才能讀取到已訓練的模型
- 請準備以下
- [Google Cloud 專案](https://developers.google.com/workspace/guides/create-project)
- [gcloud CLI 的本機安裝](https://cloud.google.com/sdk/docs/install)
- 在 Google Cloud 控制台中,啟用 Google 產生語言 API。
- https://console.cloud.google.com/flows/enableapi?apiid=generativelanguage.googleapis.com
- 在 Google Cloud 控制台中,前往**選單**> **API 和服務**> **OAuth 同意畫面**。
- https://console.cloud.google.com/apis/credentials/consent
- **外部** > **測試使用者**
- 授權桌面應用程式的憑證
- https://console.cloud.google.com/apis/credentials
- 建立**憑證** > **OAuth 客戶端 ID** > **應用程式類型**> **桌面應用程式** > 下載 JSON 檔案 > 重新命名為 `client_secret.json` > 放到工作目錄 (與 .py 檔同一個目錄)
### 透過 函數 或 flask 呼叫模型
:::success
- 工作資料夾
- `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`
```python!
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`
```python!
import google.generativeai as genai
# 印出該帳號下所有可用的模型
print('Available base models:', [m.name for m in genai.list_models()])
```
- `main.py`
```python!
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`
```python!
import requests
mes = input("請輸入訊息:")
response = requests.post('http://localhost:5000/tune', json={'message':mes})
print(response.text)
```