geminiAPI(geminiPWAをつかおう)
完全上位互換のこっちを使おう いちおうページは残しとく→https://rentry.org/e5y8u7f9
いっさいプログラミング知識ない人がAIだよりで作ったやつ たぶんGUIとかでもっと良いのあると思う 俺はこれで遊んでる
出力をリアルタイムで見れるやつを実装したかったけどエラー連発するので断念しました
ユーザー履歴にシスプロを、AI履歴にロリとか通したい性癖の作品例(他のモデルで出したやつとか)を貼れば通ると思う
①API作る
②必要な↓をインストール
③コード貼り付け、エンターをニ回くらい押すとAPIキー入力がでる(なぜかニ回くらい押さないとでない)→どうやらpyファイルで保存して開けばすんなりいくみたい
※履歴は改行してEND って打つと入力できるよ
↑をshellに貼り付けて↓をpythonに貼り付け
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 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")
|