0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ColabでAI作家に続きを書かせてみたら~ Josiefied-Qwen3-8Bで物語を“最後まで”紡ぐ~

Posted at

Josieとの出会い

Hugging Faceで見つけた Josiefied-Qwen3-8B-abliterated-v1。
Qwen3-8Bをベースに検閲を取り払った“超自由型”AIで、拒否も回りくどい説明もなく、ただ創作に集中してくれる。
今回、このJosieをColabで動かし、小説プロローグを生成→途中で途切れた部分を続き生成して完成させるまでを実演する。

物語の続き製作

1 Colab準備

!pip -q install "transformers>=4.44.0" accelerate bitsandbytes "torch>=2.3"

2 モデル読み込み

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig

MODEL_ID = "Goekdeniz-Guelmez/Josiefied-Qwen3-8B-abliterated-v1"

use_4bit = True
bnb_config = None
if use_4bit:
    bnb_config = BitsAndBytesConfig(
        load_in_4bit=True,
        bnb_4bit_quant_type="nf4",
        bnb_4bit_compute_dtype=torch.bfloat16,
        bnb_4bit_use_double_quant=True,
    )

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.bfloat16 if not use_4bit else torch.float16,
    device_map="auto",
    quantization_config=bnb_config,
    trust_remote_code=True,
)

3 生成関数(出力量変更ポイントあり)

def generate_with_josie(system, user, 
    max_new_tokens=1000,  # ★長文生成はここを増やす
    temperature=0.95, 
    top_p=0.9, 
    repetition_penalty=1.05):
    
    messages = [
        {"role": "system", "content": system},
        {"role": "user", "content": user},
    ]
    input_ids = tokenizer.apply_chat_template(
        messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
    ).to(model.device)

    out_ids = model.generate(
        input_ids=input_ids,
        max_new_tokens=max_new_tokens,
        do_sample=True,
        temperature=temperature,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        eos_token_id=tokenizer.eos_token_id,
        pad_token_id=tokenizer.eos_token_id,
    )
    text = tokenizer.decode(out_ids[0], skip_special_tokens=True)
    if "assistant\n" in text:
        text = text.split("assistant\n")[-1].strip()
    return text

4 プロンプト例

SYSTEM_PROMPT = """You are J.O.S.I.E., a poetic, atmospheric novelist.
Write in cinematic Japanese prose, rich in sensory details, with a slow build of tension."""

USER_PROMPT = """小説のプロローグを書いて。
ジャンルはダークファンタジー。
舞台は常夜の港町「ヴァリス」。
風や匂いなど触覚的ディテールを入れ、
主要人物(密輸業者の姉、記憶を失った少年、港湾税吏)の影だけを示唆して、
これから始まる陰謀の気配で締めて。800〜1200字。タイトルも1行目に。"""

5 途中で終わった場合の「続き生成」

# 1回目生成
prologue_part1 = generate_with_josie(SYSTEM_PROMPT, USER_PROMPT)

# 続きを書かせる
USER_PROMPT_CONT = f"{prologue_part1}\n\n---\n\nこの続きから、同じ文体で物語を完結させて。"
prologue_full = generate_with_josie(SYSTEM_PROMPT, USER_PROMPT_CONT)

print(prologue_full)

制作された物語の全編がこちら
前回
スクリーンショット (111).png
今回
スクリーンショット (110).png

使いこなしポイント

max_new_tokens … 長文は1000〜1200で設定

temperature/top_p … 文体や展開の大胆さを調整

システムプロンプト … 詩的、硬派、軽妙など方向性を固定

続き生成 … 前回の出力を入力に貼って「この続きから」と指示

まとめ

今回の体験で分かったのは、Josieは「未完」こそ楽しいということ。
最初から最後まで一発で生成するのもいいが、
あえて途中で切って続き生成すると、物語の起伏や意外性が倍増する。


フリーランスエンジニアです。
お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。
Twitter
Medium

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?