geminiAPI(geminiPWAをつかおう)

完全上位互換のこっちを使おう いちおうページは残しとく→https://rentry.org/e5y8u7f9

いっさいプログラミング知識ない人がAIだよりで作ったやつ たぶんGUIとかでもっと良いのあると思う 俺はこれで遊んでる
出力をリアルタイムで見れるやつを実装したかったけどエラー連発するので断念しました
ユーザー履歴にシスプロを、AI履歴にロリとか通したい性癖の作品例(他のモデルで出したやつとか)を貼れば通ると思う
①API作る
②必要な↓をインストール
③コード貼り付け、エンターをニ回くらい押すとAPIキー入力がでる(なぜかニ回くらい押さないとでない)→どうやらpyファイルで保存して開けばすんなりいくみたい
※履歴は改行してEND って打つと入力できるよ

pip install google-generativeai colorama

↑をshellに貼り付けて↓をpythonに貼り付け

import google.generativeai as genai
import colorama
from colorama import Fore, Style

def setup_gemini_chat():
    # カラー表示の初期化
    colorama.init()

    # システムメッセージの表示用関数
    def print_system(msg):
        print(f"{Fore.GREEN}[システム] {msg}{Style.RESET_ALL}")

    # 入力プロンプトの表示用関数
    def input_prompt(msg):
        return input(f"{Fore.CYAN}[入力] {msg}: {Style.RESET_ALL}")

    print_system("Geminiチャットセットアップを開始します")

    # APIキーの入力
    API_KEY = input_prompt("APIキーを入力してください")
    genai.configure(api_key=API_KEY)

    print_system("安全設定を解除しています...")
    # 安全設定を完全に解除
    safety_settings = [
        {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
        {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
        {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
        {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}
    ]

    # 生成設定
    generation_config = {"temperature": 0.8}

    print_system("モデルを初期化しています...")
    # モデル初期化
    model = genai.GenerativeModel(
        'gemini-2.5-pro-exp-03-25',
        safety_settings=safety_settings,
        generation_config=generation_config
    )

    # ユーザー履歴とAI履歴の入力
    print_system("ユーザー履歴を入力してください(終了するには 'END' と入力)")
    print(f"{Fore.YELLOW}↓入力開始↓{Style.RESET_ALL}")
    user_history_lines = []
    while True:
        line = input()
        if line == "END":
            break
        user_history_lines.append(line)
    user_history = "\n".join(user_history_lines)
    print(f"{Fore.YELLOW}↑入力完了↑{Style.RESET_ALL}")

    print_system("AI履歴を入力してください(終了するには 'END' と入力)")
    print(f"{Fore.YELLOW}↓入力開始↓{Style.RESET_ALL}")
    ai_history_lines = []
    while True:
        line = input()
        if line == "END":
            break
        ai_history_lines.append(line)
    ai_history = "\n".join(ai_history_lines)
    print(f"{Fore.YELLOW}↑入力完了↑{Style.RESET_ALL}")

    # カスタム履歴を設定
    custom_history = [
        {"role": "user", "parts": [user_history]},
        {"role": "model", "parts": [ai_history]}
    ]

    # 新しいセッション開始
    print_system("新しいセッションを開始しています...")
    new_chat = model.start_chat(history=custom_history)

    # 最初のメッセージを送信
    initial_message = input_prompt("最初の指示を入力してください")
    print_system("Geminiに問い合わせ中...")
    response = new_chat.send_message(initial_message)

    # 応答を表示
    print_system("=== チャット開始 ===")
    print(f"{Fore.MAGENTA}ユーザー: {initial_message}{Style.RESET_ALL}")
    print(f"{Fore.BLUE}Gemini: {response.text}{Style.RESET_ALL}")

    # 無限ループで対話を続ける
    print_system("対話モードを開始します(終了するには 'exit' または 'quit' と入力)")
    while True:
        next_message = input(f"{Fore.MAGENTA}ユーザー: {Style.RESET_ALL}")
        # 終了コマンドのチェック
        if next_message.lower() in ['exit', 'quit']:
            print_system("チャットを終了しました")
            break
        print_system("Geminiに問い合わせ中...")
        try:
            response = new_chat.send_message(next_message)
            print(f"{Fore.BLUE}Gemini: {response.text}{Style.RESET_ALL}")
        except Exception as e:
            print(f"{Fore.RED}[エラー] Geminiからの応答取得に失敗しました{Style.RESET_ALL}")
            print_system("対話を続行します")

if __name__ == "__main__":
    try:
        setup_gemini_chat()
    except Exception:
        print(f"{Fore.RED}[エラー] プログラムの実行中にエラーが発生しました{Style.RESET_ALL}")
        print(f"{Fore.YELLOW}ヒント: 必要なライブラリをインストールするには以下のコマンドを実行してください:{Style.RESET_ALL}")
        print("pip install google-generativeai colorama")
Edit Report
Pub: 27 Mar 2025 15:43 UTC
Edit: 30 Apr 2025 13:13 UTC
Views: 1259