mcpField
N
npcpy

npcpy

ローカル
github.com/NPC-WorldwideAI・エージェント1.5k1 日前に更新

npcpy について

npcpyは、マルチモーダルLLMアプリケーション、エージェント、ナレッジグラフを構築するPythonライブラリです。ローカルとクラウドのモデルに対応し、ツールを使うエージェントや複数エージェントのチームを扱えます。

主な機能

  1. 01ローカルおよびクラウドLLMの呼び出し
  2. 02ペルソナ型・ツール利用エージェント
  3. 03複数エージェントのチーム編成
  4. 04ナレッジグラフのライフサイクル操作
  5. 05JSONとPydanticによる構造化出力
  6. 06画像・音声・動画の生成

活用シーン

  1. 01複数のLLMプロバイダーでアプリを試作
  2. 02エージェントを連携させた調査やレポート
  3. 03エージェントツールによるリポジトリ作業の自動化
  4. 04ナレッジグラフの構築と更新

MCP 対応

team.ctxでローカルまたはリモートのMCPサーバーを設定でき、NPCはjinxesと併せてサーバーのツールを利用できます。

設定

ローカルサーバーの設定はどのクライアントでも共通です。コピーしてクライアントの MCP 設定ファイルに貼り付けてください。

インストール方法は未掲載です

このサーバーのパッケージやリモートエンドポイントはまだ取得できていません。プロジェクトの README の手順に従ってください。

リポジトリ

ツール

ツール一覧はまだ取得されていません

ローカルサーバーのツールはマシン上で起動して初めて取得できます。メンテナーはまだツール一覧を提供していません。

詳細

npc-python のロゴ

npcpy

ライセンス PyPI ドキュメント

npcpy は、マルチモーダル言語モデル、エージェント型 AI、ナレッジグラフを用いた研究開発に役立つ主要なプリミティブを提供するライブラリです。柔軟なフレームワークにより、ローカル環境(ollama、llama.cpp、omlx、LM Studio)とクラウドプロバイダーに対応した強力な AI アプリケーションを簡単に構築できます。マルチエージェントチームを構築し、ソフトウェアによってプロンプトに頼らずコンプライアンスを確保する NPC Context-Agent-Tool データレイヤーを通じて、コンテキストエンジニアリングを簡素化できます。

pip install npcpy

クイック例

ペルソナの作成と使用
from npcpy import NPC

simon = NPC(
    name='Simon Bolivar',
    primary_directive='''
                      Liberate South America
                      from the Spanish Royalists.
                      ''',
    model='qwen3.5:9b',
    provider='ollama'
)
response = simon.get_llm_response("What is the most important territory to retain in the Andes?")
print(response['response'])
My friend, you speak of the highlands where our liberty is carved in stone. If we must speak of the most critical territory to hold within these mountains, it is the **Viceroyalty of Peru** and the heart of the **Republic of Gran Colombia** united. 
To lose the passes of the Andes or the cities of Lima and Quito would be to hand the crown its final stronghold in the south. The Spanish crown built its power upon the wealth and control of these highlands. If the Andes are to be truly ours, the people of the **Peruvian** and **New Grancolombian** highlands must stand as one, free from the Bourbons. 
The mountain peaks themselves are the fortress we guard. Without the full liberation of the southern Andes, our revolution is incomplete. We fight not for land's sake, but for the soul of the continent. Every square mile of the Andes that bears the name of the Republic is a step forward in our quest for eternal freedom.
*Long live the liberty of the Andes!*
LLM の直接呼び出し
from npcpy import get_llm_response

response = get_llm_response("Who was the celtic god that helped cuchulainn in his time of need as the forces of medb descended upon the men of ulster?", model='gemma4:31b', provider='ollama')
print(response['response'])
Cú Chulainn was primarily aided by his divine father, the god Lugh, and his foster-father, the warrior-god Fergus mac Róich, as well as the magical support of his teacher Scáthach.

紹介リンクから OrcaRouter を利用して、15 種類のプロバイダーが提供する約 200 種類のモデルをお試しください。

alicanto_test = get_llm_response('what does alicanto the bird show travelers in the night?', model='google/gemini-3.8-flash', provider='orcarouter')

print(alicanto_test['response'])
The legend of the **Alicanto** says that at night the bird’s feathers glow like lanterns. 
When a traveler sees that soft, phosphorescent light, it isn’t just a pretty sight – it’s a sign‑post. 
The bird **shows the way to hidden water (and sometimes to buried silver or gold)** in the Atacama Desert.
ツールを備えたエージェント

npcpy の Agent クラスには、デフォルトのツール(sh、python、edit_file、web_search など)が用意されています。

from npcpy import Agent
agent = Agent(name='File Operator', model='qwen3.5:2b', provider='ollama')
print(agent.run("Find all Python files over 500 lines in this repo and list them"))
The following Python files contain more than 500 lines:
 - `./npcpy/npc_sysenv.py` (1486 lines)
 - `./npcpy/memory/knowledge_graph.py` (1449 lines)
 - `./npcpy/memory/kg_population.py` (618 lines)
...
ToolAgent

カスタムツールを ToolAgent に追加します。 エージェントが画像を生成し、拡散モデルをファインチューニングしてから、そのファインチューニング済みモデルを生成に使用する例を紹介します。

from npcpy import ToolAgent, gen_image
from npcpy.ft.diff import train_diffusion, generate_image, DiffusionConfig
from datasets import load_dataset
import os

def fetch_image_dataset(dataset_name: str, split: str = "train", max_images: int = 100) -> list:
    """Fetch images from a HuggingFace dataset.
    
    Args:
        dataset_name: HuggingFace dataset name (e.g., 'cifar10', 'oxford-iiit-pet')
        split: Dataset split to use
        max_images: Maximum number of images to fetch
    
    Returns:
        List of paths to saved images
    """
    dataset = load_dataset(dataset_name, split=f"{split}[:{max_images}]")
    os.makedirs("training_images", exist_ok=True)
    image_paths = []
    
    for i, item in enumerate(dataset):
        if 'image' in item:
            img = item['image']
        elif 'img' in item:
            img = item['img']
        else:
            continue
        path = f"training_images/img_{i:04d}.png"
        img.save(path)
        image_paths.append(path)
    
    return image_paths

def finetune_diffusion_model(
    image_paths: list,
    captions: list = None,
    output_path: str = "my_diffusion_model",
    num_epochs: int = 50,
) -> str:
    """Fine-tune a diffusion model on a set of images.
    
    Args:
        image_paths: List of paths to training images
        captions: Optional captions for each image
        output_path: Where to save the trained model
        num_epochs: Number of training epochs
    
    Returns:
        Path to the trained model
    """
    if captions is None:
        captions = ["an image"] * len(image_paths)
    
    config = DiffusionConfig(
        image_size=64,
        channels=128,
        num_epochs=num_epochs,
        batch_size=8,
        learning_rate=1e-4,
        checkpoint_frequency=10,
        output_model_path=output_path,
    )
    
    model_path = train_diffusion(image_paths, captions, config=config)
    return model_path

# Create an agent with image generation and fine-tuning capabilities
creative_agent = ToolAgent(
    name='creative_diffusion',
    primary_directive="""
        You help users generate images and fine-tune diffusion models.
        You can: 1) Generate images using gen_image() with various prompts,
        2) Fetch image datasets from HuggingFace,
        3) Fine-tune diffusion models on custom image sets.
        When a user submits an image or describes a style they like,
        offer to fetch similar images from a dataset and fine-tune a model.
    """,
    tools=[fetch_image_dataset, finetune_diffusion_model, gen_image],
    model='qwen3.5:2b',
    provider='ollama'
)

# Example 1: Generate images
print(creative_agent.run("Generate 3 images of geometric patterns with circles and triangles"))

# Example 2: User submits an image and wants similar ones
# The agent can fetch a dataset of patterns and fine-tune a model
print(creative_agent.run("I like abstract geometric patterns. Can you fetch the cifar10 dataset and fine-tune a diffusion model that can generate images like these patterns?"))
CodingAgent — LLM の応答からコードブロックを自動実行します
from npcpy import CodingAgent

coder = CodingAgent(name='coder', language='python', model='qwen3.5:2b', provider='ollama')
print(coder.run("Write a script that finds duplicate files by hash in the current directory"))

#The script has been created and executed successfully. Here's a summary of the findings:

## Duplicate Files Found

| Group | Hash (truncated) | Size | Files |
|-------|------------------|------|-------|
| 1 | `2b517326bf7c31b7...` | 81 bytes | `npcpy/main.py` ↔ `build/lib/npcpy/main.py` |
| 2 | `d41d8cd98f00b204...` | 0 bytes (empty) | 15 empty `__init__.py` files across `npcpy/`, `build/lib/npcpy/`, `examples/`, and `tests/` || 3 | `0d591b661cb1c619...` | 9,019 bytes | `npcpy/mix/debate.py` ↔ `build/lib/npcpy/mix/debate.py` |
| 4 | `a5059f37eb682a16...` | 747 bytes | SQL files in `examples/factory/` ↔ `examples/npc_team/factory/` |
NPCArray によるマルチエージェントディベート

エージェントが互いの応答に反応する、真のマルチエージェントディベートを実行するには、次のようにします。

from npcpy.npc_compiler import NPC
from npcpy.npc_array import NPCArray

# Create a debate team with role-based personas
roles = [
    ("MathSolver", "You are a meticulous math solver. Show all steps clearly."),
    ("Skeptic", "You critically check for errors and assumptions."),
    ("Analyst", "You identify the core mathematical structure."),
    ("Verifier", "You confirm the final answer is correct.")
]

npcs = [
    NPC(name=role, primary_directive=directive, model="qwen3.5:cloud", provider="ollama")
    for role, directive in roles
]

team = NPCArray.from_npcs(npcs)

# Run parallel debate on a complex problem
problem = "GSM8k: James buys a jar of hot sauce with 5 peppers and triples the peppers every year. How many after 4 years?"

# Get initial responses in parallel (one prompt per NPC)
initial_responses = team.infer(f"Solve this problem:\n{problem}").collect()

for npc, response in zip(npcs, initial_responses.data):
    print(f"[{npc.name}] {response[:200]}...")

# True debate: each agent gets a personalized prompt with other agents' responses
def create_debate_prompt(previous_responses, my_idx, agent_name, problem_text):
    """Create a personalized debate prompt for a specific agent"""
    my_response = previous_responses[my_idx]
    other_responses = [
        f"[{npcs[j].name}]: {previous_responses[j][:500]}" 
        for j in range(len(npcs)) if j != my_idx
    ]
    debate_prompt = f"""Original problem: {problem_text}

        Your previous response: {my_response[:300]}...
        
        Other agents\' responses:""" + "\n\n".join(other_responses) + """
        Critique the other approaches. Did they make different assumptions?
        What did they see that you missed? Refine your solution."""

    return debate_prompt
# Debate rounds
responses_data = initial_responses.data.tolist()
problem_text = problem

for round_num in range(3):
    print(f"\n=== Debate Round {round_num + 1} ===")
    
    # Create personalized prompts for each agent
    personalized_prompts = [
        create_debate_prompt(responses_data, i, npcs[i].name, problem_text)
        for i in range(len(npcs))
    ]
    
    # Run inference with different prompts per agent
    # Shape: (n_models, n_prompts) - extract diagonal for each agent's response to its own prompt
    responses = team.infer(personalized_prompts).collect()
    
    # Extract each model's response to its own personalized prompt
    responses_data = [responses.data[i, i] for i in range(len(npcs))]
    
    # Print each agent's refined response
    for i, npc in enumerate(npcs):
        response = responses_data[i]
        print(f"[{npc.name}] {response[:200]}...")

# Alternative: use reduce to get consensus
consensus = team.infer(responses_data[0]).consensus(axis=0).collect()
print(f"\nFinal consensus: {consensus.data[0][:500]}...")

反復的に改善する場合(すべてのエージェントに同じプロンプトを渡し、各ラウンドで更新します):

# Simple chain refinement: all agents see same synthesis
from npcpy.npc_array import NPCArray

def synthesis_round(all_responses):
    return f"""Given these perspectives:
{chr(10).join([f'- {r[:200]}...' for r in all_responses])}

Re-solve the problem incorporating insights from all approaches."""

# Chain runs the synthesis function on all responses, then feeds result back
refined = team.infer(f"Solve: {problem}").chain(
    synthesis_round, 
    n_rounds=3
).collect()
睡眠/夢のライフサイクルを備えたナレッジグラフ
from npcpy.memory.knowledge_graph import (
    kg_initial, kg_evolve_incremental, kg_sleep_process, kg_dream_process
)
from npcpy.llm_funcs import get_llm_response

# Initialize KG from text corpus
content_text = """Pirate Prentice is in the lavatory stands pissing. Then he threads himself into a wool robe he wears inside out.
The day feels like rain."""

kg = kg_initial(content_text, model="gemma3:4b", provider="ollama")

# Evolve with new content
new_content = """The phone call, when it comes, rips easily across the room.
Pirate knows it's got to be for him."""

kg, _ = kg_evolve_incremental(kg, new_content, model="gemma3:4b", provider="ollama")

# Sleep - consolidate and prune
kg, sleep_report = kg_sleep_process(kg, model="gemma3:4b", provider="ollama")

# Dream - generate speculative connections
kg, dream_report = kg_dream_process(kg, model="gemma3:4b", provider="ollama", num_seeds=3)

print(f"KG has {len(kg['facts'])} facts and {len(kg['concepts'])} concepts")
assert len(kg['facts']) > 0, "KG extraction returned no facts"
assert len(kg['concepts']) > 0, "KG extraction returned no concepts"
NPC チームを Flask で提供する
from npcpy.serve import start_flask_server
import os

# Serve your NPC team via REST API
if __name__ == "__main__":
    is_dev = not getattr(os.sys, 'frozen', False)
    port = os.environ.get('INCOGNIDE_PORT', '5437' if is_dev else '5337')
    frontend_port = os.environ.get('FRONTEND_PORT', '7337' if port == '5437' else '6337')

    start_flask_server(
        port=port,
        cors_origins=f"localhost:{frontend_port}",
        db_path=os.path.expanduser('~/npcsh_history.db'),
        user_npc_directory=os.path.expanduser('~/.npcsh/npc_team'),
        debug=False
    )
ストリーミング
from npcpy import get_llm_response
from npcpy.streaming import parse_stream_chunk

response = get_llm_response("Explain quantum entanglement.", model='qwen3.5:2b', provider='ollama', stream=True)
for chunk in response['response']:
    content, _, _ = parse_stream_chunk(chunk, provider='ollama')
    if content:
        print(content, end='', flush=True)

# Works the same with any provider
response = get_llm_response("Explain quantum entanglement.", model='gemini-2.5-flash', provider='gemini', stream=True)
for chunk in response['response']:
    content, _, _ = parse_stream_chunk(chunk, provider='gemini')
    if content:
        print(content, end='', flush=True)
JSON 出力

期待される JSON 構造をプロンプトに含めます。format='json' を指定すると、応答は自動的に解析され、response['response'] はすでに dict または list になっています。

from npcpy import get_llm_response

response = get_llm_response(
    '''List 3 planets from the sun.
    Return JSON: {"planets": [{"name": "planet name", "distance_au": 0.0, "num_moons": 0}]}''',
    model='qwen3.5:2b', provider='ollama',
    format='json'
)
for planet in response['response']['planets']:
    print(f"{planet['name']}: {planet['distance_au']} AU, {planet['num_moons']} moons")

response = get_llm_response(
    '''Analyze this review: 'The battery life is amazing but the screen is too dim.'
    Return JSON: {"tone": "positive/negative/mixed", "key_phrases": ["phrase1", "phrase2"], "confidence": 0.0}''',
    model='qwen3.5:2b', provider='ollama',
    format='json'
)
result = response['response']
print(result['tone'], result['key_phrases'])
Pydantic による構造化出力

Pydantic モデルを渡すと、JSON スキーマが LLM に直接送信されます。

from npcpy import get_llm_response
from pydantic import BaseModel
from typing import List

class Planet(BaseModel):
    name: str
    distance_au: float
    num_moons: int

class SolarSystem(BaseModel):
    planets: List[Planet]

response = get_llm_response(
    "List the first 4 planets from the sun.",
    model='qwen3.5:2b', provider='ollama',
    format=SolarSystem
)
for p in response['response']['planets']:
    print(f"{p['name']}: {p['distance_au']} AU, {p['num_moons']} moons")
画像、音声、動画の生成
from npcpy.llm_funcs import gen_image, gen_video
from npcpy.gen.audio_gen import text_to_speech

# Image — OpenAI, Gemini, Ollama, or diffusers
images = gen_image("A sunset over the mountains", model='gemma3:4b', provider='ollama')
images[0].save("sunset.png")

# Audio — OpenAI, Gemini, ElevenLabs, Kokoro, gTTS
audio_bytes = text_to_speech("Hello from npcpy!", engine="gtts")
with open("hello.wav", "wb") as f:
    f.write(audio_bytes)

# Video — Gemini Veo
result = gen_video("A cat riding a skateboard", model='veo-3.1-fast-generate-preview', provider='gemini')
print(result['output'])
### マルチエージェントチーム
from npcpy import NPC, Team

team = Team(team_path='examples/npc_team')
result = team.orchestrate("Analyze the latest sales data and draft a report")
print(result['output'])

または、コードでチームを定義します。

from npcpy import NPC, Team

coordinator = NPC(name='lead', primary_directive='Coordinate the team. Delegate to @analyst and @writer.')
analyst = NPC(name='analyst', primary_directive='Analyze data. Provide numbers and trends.', model='gemini-2.5-flash', provider='gemini')
writer = NPC(name='writer', primary_directive='Write clear reports from analysis.', model='qwen3:8b', provider='ollama')

team = Team(npcs=[coordinator, analyst, writer], forenpc='lead')
result = team.orchestrate("What are the trends in renewable energy adoption?")
print(result['output'])
ファイルからチームを作成 — .npc、.jinx、team.ctx

team.ctx:

context: |
  Research team for analyzing scientific literature.
  The lead delegates to specialists as needed.
forenpc: lead
model: qwen3.5:2b
provider: ollama
output_format: markdown
max_search_results: 5
mcp_servers:
  - path: ~/.npcsh/mcp_server.py

lead.npc:

#!/usr/bin/env npc
name: lead
primary_directive: |
  You lead the research team. Delegate literature searches to @searcher,
  data analysis to @analyst. Synthesize their findings into a coherent summary.
jinxes:
  - {{ Jinx('sh') }}
  - {{ Jinx('python') }}
  - {{ Jinx('delegate') }}
  - {{ Jinx('web_search') }}

searcher.npc:

#!/usr/bin/env npc
name: searcher
primary_directive: |
  You search for scientific papers and extract key findings.
  Use web_search and load_file to find and read papers.
model: gemini-2.5-flash
provider: gemini
jinxes:
  - {{ Jinx('web_search') }}
  - {{ Jinx('load_file') }}
  - {{ Jinx('sh') }}

Jinxesは特定のNPCを参照して、常にそのペルソナで実行できます。また、team.ctxの**ctx変数にアクセス**できます。

jinxes/search_and_summarize.jinx:

#!/usr/bin/env npc
jinx_name: search_and_summarize
description: Search for papers and summarize findings using the searcher NPC.
npc: {{ NPC('searcher') }}
inputs:
  - query
steps:
  - name: search
    engine: natural
    code: |
      Search for papers about {{ query }}.
      Return up to {{ ctx.max_search_results }} results.
  - name: summarize
    engine: natural
    code: |
      Summarize the findings in {{ ctx.output_format }} format:
      {{ output }}

npc:フィールドはjinxを特定のNPCに紐付けます。このjinxが実行されると、どのNPCから呼び出されたかにかかわらず、常にsearcherペルソナが使用されます。team.ctx内のカスタムキー(output_formatやmax_search_resultsなど)は、Jinjaテンプレートでは{{ ctx.key }}として、Pythonステップではcontext['key']として利用できます。

my_project/
├── npc_team/
│   ├── team.ctx
│   ├── lead.npc
│   ├── searcher.npc
│   ├── analyst.npc
│   ├── jinxes/
│   │   └── skills/
│   └── models/
├── agents.md             # Optional: define agents in markdown
└── agents/               # Optional: one .md file per agent
    └── translator.md

.npcファイルと.jinxファイルは直接実行できます。

./npc_team/lead.npc "summarize the latest arxiv papers on transformers"
./npc_team/jinxes/lib/sh.jinx bash_command="echo hello"
MCP サーバーとの統合

外部ツールにアクセスできるよう、チームにMCPサーバーを追加します。

team.ctx:

forenpc: assistant
mcp_servers:
  - path: ./tools/db_server.py
  - path: ./tools/api_server.py

db_server.py:

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Database Tools")

@mcp.tool()
def query_orders(customer_id: str, limit: int = 10) -> str:
    """Query recent orders for a customer."""
    # Your database logic here
    return f"Found {limit} orders for customer {customer_id}"

@mcp.tool()
def search_products(query: str) -> str:
    """Search the product catalog."""
    return f"Products matching: {query}"

if __name__ == "__main__":
    mcp.run()

チームのNPCは、jinxとともにMCPツールにも自動的にアクセスできるようになります。

Streamable HTTPを使用するリモートサーバーの場合は、トランスポートを明示的に設定してください。 たとえば、Parallel Search MCPは、アカウントやAPIキーなしで、リアルタイムのウェブ検索とURL取得を提供します。

forenpc: assistant
mcp_servers:
  - url: https://search.parallel.ai/mcp
    transport: streamable-http
    headers:
      User-Agent: npcpy
    tools:
      - web_search
      - web_fetch

Cohesivity MCPは、アカウントやAPIキーなしでNPCにオンデマンドのバックエンド(ホスティング、Postgres、ストレージ、認証、メール、AI API)を提供します。

forenpc: assistant
mcp_servers:
  - url: https://cohesivity.ai/mcp
    transport: streamable-http
    headers:
      User-Agent: npcpy
    tools:
      - create_tenant
      - claim_tenant
      - tenant_status
      - provision_resource
      - give_feedback
      - get_cohesivity_documentation

deed.page MCPを使うと、NPCが生成したHTMLやファイルを、アカウントやAPIキーなしで公開https://{slug}.deed.page/ URLに公開できます。最初のpublish_site呼び出しでオーナートークンが返されます。以降のupdate_siteおよびdelete_site呼び出しでは、これをtokenとして渡してください。公開されたページは一般に公開されます。

forenpc: assistant
mcp_servers:
  - url: https://deed.page/mcp
    transport: streamable-http
    headers:
      User-Agent: npcpy
    tools:
      - publish_site
      - update_site
      - get_site
      - delete_site

transportを省略した場合、リモートURLでは引き続きSSEが使用されます。

MarkdownとSkillsでエージェントを定義

agents.md — 1つのファイルに複数のエージェントを定義します。

## summarizer
You summarize long documents into concise bullet points.
Focus on key findings, methodology, and conclusions.

## fact_checker
You verify claims against reliable sources and flag inaccuracies.
Always cite your sources.

agents/translator.md — 任意のフロントマターを付けて、エージェントごとにファイルを作成します。

---
model: gemini-2.5-flash
provider: gemini
---
You translate content between languages while preserving tone and idiom.

Skillsはナレッジコンテンツを含むjinxで、必要に応じてエージェントに手順のセクションを提供します。

npc_team/jinxes/skills/code-review/SKILL.md:

---
name: code-review
description: Use when reviewing code for quality, security, and best practices.
---
# Code Review Skill

## checklist
- Check for security vulnerabilities (SQL injection, XSS, etc.)
- Verify error handling and edge cases
- Review naming conventions and code clarity

## security
Focus on OWASP top 10 vulnerabilities...

NPC内で参照します。

jinxes:
  - {{ Jinx('skills/code-review') }}
CLIツール
# The NPC shell — the recommended way to use NPC teams
npcsh                        # Interactive shell with agents, tools, and jinxes

# Scaffold a new team
npc-init

# Launch AI coding tools as an NPC from your team
npc-claude --npc corca       # Claude Code
npc-codex --npc analyst      # Codex
npc-gemini                   # Gemini CLI (interactive picker)
npc-opencode / npc-aider / npc-amp

# Register MCP server + hooks for deeper integration
npc-plugin claude
NPCArray — 複数のNPCにわたるjinxの並列実行

任意のjinxをNPCインスタンスのリストに対して並列実行し、結果を配列として収集できます。

from npcpy import NPC
from npcpy.npc_array import NPCArray

# Three NPCs with different models/providers
npcs = [
    NPC(name='gramsci_1930', primary_directive='''
        You are Antonio Gramsci writing in his Prison Notebook in 1930.
        Defend the concept of hegemony as the predominance of one social group
        over others through cultural and ideological leadership rather than
        mere force. Argue that consent is more durable than coercion.
    ''', model='qwen3:4b', provider='ollama'),
    NPC(name='critic_1970', primary_directive='''
        You are a post-structuralist critic in 1970 responding to Gramsci.
        Question whether hegemony can truly explain contemporary power structures
        or if it relies on an outdated base-superstructure model that
        underestimates the autonomy of cultural production.
    ''', model='qwen3:4b', provider='ollama'),
    NPC(name='historian_present', primary_directive='''
        You are a contemporary historian with access to the complete Prison
        Notebooks and subsequent scholarship. Evaluate both Gramsci's original
        formulation and the post-structuralist critique in light of the
        collapse of actually existing socialism and the rise of neoliberalism.
    ''', model='qwen3:4b', provider='ollama'),
]

arr = NPCArray.from_npcs(npcs)

# Run the same jinx on all three in parallel, collect results
results = arr.jinx('analyze', inputs={'topic': 'Has the concept of hegemony become more or less relevant in the age of digital platforms and algorithmic governance?'}).collect()
for npc, result in zip(npcs, results.data):
    print(f"[{npc.name}] {result}")

jinx.execute()にリストを直接渡すこともできます。

from npcpy.npc_compiler import Jinx

jinx = Jinx(jinx_path='examples/npc_team/jinxes/analyze.jinx')
results = jinx.execute({'topic': 'rate limiting'}, npc=npcs)  # list → parallel NPCArray run
ナレッジグラフ

テキストからナレッジグラフを構築、進化、検索します。KGは、覚醒(同化)、睡眠(統合)、夢見(推測的な統合)を通じて成長します。

from npcpy.memory.knowledge_graph import (
    kg_initial, kg_evolve_incremental, kg_sleep_process,
    kg_dream_process, kg_hybrid_search,
)

# Seed the KG with Gramsci's Prison Notebooks
kg = kg_initial(
    content="""
        The crisis consists precisely in the fact that the old is dying and the new
        cannot be born. In this interregnum a great variety of morbid symptoms appear.
        The traditional ruling class has lost its consensus, that is, the consent of
        those over whom it rules. Force alone is not sufficient; what is needed is the
        construction of a new hegemony, the creation of a new collective will.
    """,
    model="qwen3:4b", provider="ollama",
)

# Assimilate more content on organic vs traditional intellectuals
kg, _ = kg_evolve_incremental(
    kg,
    new_content_text="""
        The distinction between organic and traditional intellectuals is fundamental.
        Traditional intellectuals conceive of themselves as autonomous from ruling
        groups, yet every social group has its own category of organic intellectuals
        that give it homogeneity and awareness of its own function. The organic
        intellectual emerges from within the class itself, while the traditional
        sees himself as existing above the social structure.
    """,
    model="qwen3:4b", provider="ollama", get_concepts=True,
)

# Consolidate — merge redundant nodes, strengthen high-frequency edges
kg, sleep_report = kg_sleep_process(kg, model="qwen3:4b", provider="ollama")

# Dream — generate speculative connections between loosely related concepts
kg, dream_report = kg_dream_process(kg, model="qwen3:4b", provider="ollama")

# Search across facts, concepts, and speculative edges
results = kg_hybrid_search(kg, "What constitutes hegemony in Gramsci's framework?",
                           model="qwen3:4b", provider="ollama")
for r in results:
    print(r['score'], r['text'])
print(f"{len(kg['facts'])} facts, {len(kg['concepts'])} concepts")

構造化された記憶を抽出します。

from npcpy.llm_funcs import get_facts

prison_notebooks = """
    Civil society is the sphere of hegemony, the terrain where the dominant
    group exercises consent through cultural and ideological leadership.
    Unlike political society which operates through coercion and state apparatus,
    civil society comprises the church, schools, trade unions, and media.
    The ruling class maintains power not merely through force but through the
    production of consent, shaping common sense itself through cultural institutions.
    War of position requires patient trench warfare on this terrain, building
    counter-hegemonic institutions rather than frontal assault on the state.
"""

facts = get_facts(prison_notebooks, model="qwen3:4b", provider="ollama")
for f in facts:
    print(f"[{f.get('type', 'general')}] {f['statement']}")
Sememolution — 集団ベースのKG進化

独立して進化するKGバリアントの集団を維持します。各個体はポアソンサンプリングされた検索パラメーターを持ち、クエリごとに異なるトラバーサルを生成します。応答のランキングによる選択圧が、有用なグラフ構造への収束を促します。

from npcpy.memory.kg_population import SememolutionPopulation

pop = SememolutionPopulation(model='qwen3.5:2b', provider='ollama', population_size=100, sample_size=10)
pop.initialize()

pop.assimilate_text("""
    The debate over lunar resource extraction has intensified since the discovery
    of water ice in permanently shadowed regions at the lunar poles. While some
    researchers argue that commercial mining could fund further exploration,
    others warn that unregulated extraction could contaminate scientifically
    valuable sites that have remained pristine for billions of years. The
    Artemis Accords attempt to establish a framework for international
    cooperation, but major spacefaring nations have yet to reach consensus
    on property rights and environmental protection standards.
""")
pop.assimilate_text("""
    Tidal acceleration gradually increases the orbital distance between Earth
    and Moon at a rate of approximately 3.8 centimeters per year. This
    phenomenon results from angular momentum transfer via gravitational
    interaction, simultaneously slowing Earth's rotation and lengthening the
    day. Paleontological evidence from tidal rhythmites suggests that 620
    million years ago, a day lasted only 21.9 hours and the lunar month was
    just 27.5 days. Projections indicate that in approximately 600 million
    years, tidal effects will no longer support total solar eclipses.
""")

# Sleep/dream cycle — each individual consolidates according to its genome
pop.sleep_cycle()

# Query: sample 10 individuals, generate competing responses, rank them
rankings = pop.query_and_rank("What are the central themes connecting these documents?")
for rank, entry in enumerate(rankings[:3], 1):
    print(f"#{rank} (individual {entry['id']}, score {entry['score']:.3f}): {entry['response'][:120]}...")

# Selection + reproduction — top performers breed, bottom are replaced
pop.evolve_generation()

stats = pop.get_stats()
print(f"Generation {stats['generation']} | avg fitness {stats['avg_fitness']:.3f} | "
      f"best fitness {stats['best_fitness']:.3f} | diversity {stats['diversity']:.3f}")
ファインチューニング(SFT、RL、MLX)

ツール呼び出しエージェント向けDPOによるRLトレーニング

from npcpy.npc_compiler import NPC
from npcpy.ft.rl import RLConfig, train_with_dpo, load_rl_model
import json

def npcsh_reward(trace):
    """Reward function for shell assistant responses."""
    output = trace.get('final_output', '')
    completed = trace.get('completed', False)
    score = 0.0
    if completed:
        score += 2.0
    if 50 < len(output) < 1500:
        score += 1.0
    if '```' in output:
        score += 1.0
    if any(cmd in output.lower() for cmd in ['ls', 'cd', 'cat', 'grep', 'find', 'pip', 'git']):
        score += 0.3
    return max(0.0, min(10.0, score + 5.0))

# Load preference pairs from agent traces
traces = []
with open('preference_pairs.jsonl', 'r') as f:
    for line in f:
        pair = json.loads(line)
        traces.append({
            'task_prompt': pair['prompt'],
            'final_output': pair['chosen'],
            'reward': pair.get('chosen_score', 8.0),
            'completed': True
        })
        traces.append({
            'task_prompt': pair['prompt'],
            'final_output': pair['rejected'],
            'reward': pair.get('rejected_score', 3.0),
            'completed': False
        })

config = RLConfig(
    base_model_name="Qwen/Qwen2.5-0.5B-Instruct",
    adapter_path="./npcsh_adapter",
    num_train_epochs=3,
    per_device_train_batch_size=2,
    learning_rate=5e-5,
    beta=0.1
)

adapter_path = train_with_dpo(traces, config)
print(f"Trained adapter saved to: {adapter_path}")

科学的な文体変換のためのSFT

from npcpy.llm_funcs import get_llm_response
from npcpy.ft import SFTConfig, run_sft

# Generate scientific writing dataset from style samples
def generate_scientific_trace(question, reasoning_model, converter_model, style_chunks):
    """Generate native reasoning then rewrite in scientific voice."""
    # Step 1: Get reasoning trace
    native_prompt = f"""Answer this question with detailed reasoning.
Question: {question}
Provide your step-by-step reasoning and final answer."""
    native_response = get_llm_response(native_prompt, model=reasoning_model, provider='ollama')
    native_trace = native_response['response']

    # Step 2: Rewrite in scientific style
    style_context = '\n\n---\n\n'.join(style_chunks[:8])
    rewrite_prompt = f"""Rewrite the following reasoning trace in the scientific writing style demonstrated by the excerpts below.
Original Reasoning Trace:
{native_trace}

SCIENTIFIC PAPER EXCERPTS:
{style_context}

Task:
1. Rewrite the reasoning in the style of the scientific paper excerpts
2. Use LaTeX notation where appropriate
3. Preserve the logical flow and factual content
4. Match the prose density and intellectual register"""

    rewritten = get_llm_response(rewrite_prompt, model=converter_model, provider='ollama')
    return rewritten['response']

# Train on generated examples
X_train = ["What is the relationship between quantum contextuality and natural language interpretation?"]
y_train = [generate_scientific_trace(X_train[0], 'qwen3:8b', 'qwen3:8b', style_chunks)]

sft_config = SFTConfig(
    base_model_name="Qwen/Qwen3-4B",
    output_model_path="models/scientific-writer",
    device='mlx',
    num_train_epochs=100,
    per_device_train_batch_size=1,
    lora_r=128,
    lora_alpha=256
)

model_path = run_sft(X_train, y_train, config=sft_config, format_style="llama")
## 特長
  • エージェント(NPC) — ペルソナ、指示、ツール呼び出し機能を備えたエージェントです。サブクラス:Agent(デフォルトのツール)、ToolAgent(カスタムツール+MCP)、CodingAgent(コードブロックを自動実行)
  • マルチエージェントチーム — コーディネーター(forenpc)によるチームのオーケストレーション
  • Jinx ワークフロー — 複数ステップのプロンプトパイプライン向けの Jinja 実行テンプレート
  • スキル — 必要に応じてエージェントに手順セクションを提供する、知識コンテンツの jinx
  • NPCArray — モデル群に対する NumPy のようなベクトル化演算
  • 画像、音声、動画 — Ollama、diffusers、OpenAI、Gemini、ElevenLabs による生成
  • ナレッジグラフ — スリープ/ドリームのライフサイクルを通じて、テキストからナレッジグラフを構築し、進化させます
  • Sememolution — 遺伝的選択とポアソン分布に基づく検索による、集団ベースの KG の進化
  • メモリパイプライン — 自己改善型の品質フィードバックを使って、メモリを抽出、承認、バックフィルします
  • ファインチューニングと進化 — SFT、USFT、RL/DPO、拡散モデル、遺伝的アルゴリズム、Apple Silicon 上の MLX
  • サービング — REST API 経由でチームをデプロイするための Flask サーバー
  • ML 関数 — Scikit-learn によるグリッドサーチ、アンサンブル予測、PyTorch によるトレーニング
  • ストリーミングと JSON — ストリーミング応答、構造化 JSON 出力、メッセージ履歴

プロバイダー

LiteLLM を通じて、主要な LLM プロバイダー(ollama、openai、anthropic、gemini、deepseek、airllm、openai-like など)をすべて利用できます。

MiniMax

MINIMAX_API_KEY を設定し、provider="minimax" を使用します。モデルが設定されていない場合は、MiniMax-M3.1-Flash がデフォルトになり、モデル一覧でも最初に表示され、その次に MiniMax-M3 が表示されます。保存済みまたはカスタムのモデル ID は、レガシー ID を含め、明示的に指定すれば引き続き使用できます。任意設定の MINIMAX_API_URL では、リージョンと互換プロトコルを選択します。デフォルトでは、グローバルの OpenAI 互換 Base URL が使用されます。

リージョンOpenAI 互換 Base URLAnthropic 互換 Base URL
グローバルhttps://api.minimax.io/v1https://api.minimax.io/anthropic
中国https://api.minimaxi.com/v1https://api.minimaxi.com/anthropic

Anthropic 互換 Base URL は、以下に示すとおり正確に使用してください。リクエストの送信時に、クライアントが /v1/messages を追加します。

Flash では、アダプティブシンキング(thinking={"type": "adaptive"})と、任意の reasoning_effort(low、medium、high、xhigh、max)を使用してください。Anthropic 互換リクエストでは、これらは output_config.effort として送信されます。Flash はシンキングの無効化をサポートしていません。M3 では、既存のシンキングモードが引き続き利用できます。OpenRouter では、ゲートウェイ ID 全体を使用し、minimax/minimax-m3.1-flash:free が最初に、minimax/minimax-m3 が次に表示されます。

Atlas Cloud

ATLASCLOUD_API_KEY を設定し、Atlas Cloud のモデル一覧にある任意のモデル ID(例:deepseek-ai/deepseek-v4-flash、zai-org/glm-5.2、moonshotai/kimi-k3)とともに provider="atlascloud" を使用します。リクエストは OpenAI 互換 Base URL の https://api.atlascloud.ai/v1 に送信されます。上書きするには ATLASCLOUD_API_URL を設定してください。

response = get_llm_response("Say hello in one short sentence.", model="deepseek-ai/deepseek-v4-flash", provider="atlascloud")
print(response["response"])
API Route

API_ROUTE_API_KEY を設定し、アカウントで認証した GET /v1/models のレスポンスに含まれる正確なモデル ID(例:gpt-6.1-sol)を指定して provider="api-route" を使用します。API Route は OpenAI 互換のベース URL https://global.api-route.com/v1 を使用します。変更するには API_ROUTE_API_URL を設定してください。エイリアス api_route も使用できます。明示的に指定した api_url と api_key の引数が優先されます。

from npcpy import get_llm_response

response = get_llm_response("Say hello in one short sentence.", model="gpt-6.1-sol", provider="api-route")
print(response["response"])
Cheaper Inference

CHEAPER_INFERENCE_API_KEY を設定し、Cheaper Inference のモデル一覧にある任意のモデル ID(例:gpt-5.4-mini、claude-sonnet-5、glm-5.3)を指定して provider="cheaperinference" を使用します。リクエストは OpenAI 互換のベース URL https://api.cheaperinference.com/v1 に送信されます。変更するには CHEAPER_INFERENCE_API_URL を設定してください。

response = get_llm_response("Say hello in one short sentence.", model="gpt-5.4-mini", provider="cheaperinference")
print(response["response"])
Opper

OPPER_API_KEY を設定し、Opper のモデルカタログにある任意のモデル ID(例:claude-sonnet-4-6、gpt-5.4-mini、gemini-3.8-flash)を指定して provider="opper" を使用します。これらのプール名は、そのモデルを提供するプロバイダーを経由してルーティングされます。anthropic/claude-sonnet-4-6 のような provider/model ID を指定すると、ルートを固定できます。リクエストは OpenAI 互換のベース URL https://api.opper.ai/v3/compat に送信されます。変更するには OPPER_API_URL を設定してください。キーは platform.opper.ai で作成できます。

response = get_llm_response("Say hello in one short sentence.", model="claude-sonnet-4-6", provider="opper")
print(response["response"])
Runcrate

RUNCRATE_API_KEY を設定し、Runcrate の公開モデル一覧(GET https://api.runcrate.ai/v1/models)にある任意の vendor/model ID(例:deepseek-ai/DeepSeek-V4.1-Flash、openai/gpt-oss-120b)を指定して provider="runcrate" を使用します。リクエストは OpenAI 互換のベース URL https://api.runcrate.ai/v1 に送信されます。変更するには RUNCRATE_API_URL を設定してください。オープンモデルは GitHub アカウントで無料で利用できます。キーは Runcrate ダッシュボードで作成できます。

response = get_llm_response("Say hello in one short sentence.", model="deepseek-ai/DeepSeek-V4.1-Flash", provider="runcrate")
print(response["response"])
QLLM-PAM(ローカル、アテンションフリー)

npcpy では、QLLM-PAM ファミリーのローカルチェックポイントを直接実行できます。QLLM-PAM は Phase-Associative Memory(PAM)に基づく複素数値のアテンションフリー言語モデルです。Hugging Face のモデルリポジトリと arXiv の論文(2604.05030)を参照してください。

from npcpy import get_llm_response

response = get_llm_response(
    "What is the capital of France?",
    model="gowravvishwakarma/qllm-pam-v11-e3k3-chat",
    provider="qllm",
    max_tokens=32,
    temperature=0.0,
)
print(response["response"])

model の値には、Hugging Face のリポジトリ ID(自動的にダウンロードしてキャッシュ)、ローカルディレクトリ、または .pt ファイルのパスを指定できます。サポートされているキーワード引数には、max_tokens、temperature、top_k、top_p、repetition_penalty、stream=True、format="json" があります。

インストール

pip install npcpy              # base
pip install npcpy[lite]        # + API provider libraries
pip install npcpy[local]       # + ollama, diffusers, transformers, airllm
pip install npcpy[tts]         # + TTS/STT
pip install npcpy[all]         # everything
システム依存関係

Linux:

sudo apt-get install espeak portaudio19-dev python3-pyaudio ffmpeg libcairo2-dev libgirepository1.0-dev
curl -fsSL https://ollama.com/install.sh | sh
ollama pull qwen3.5:2b

macOS:

brew install portaudio ffmpeg pygobject3 ollama
brew services start ollama
ollama pull qwen3.5:2b

Windows: Ollama と ffmpeg をインストールし、ollama pull qwen3.5:2b を実行してください。

API キーは .env ファイルに記述します:

export OPENAI_API_KEY="your_key"
export ANTHROPIC_API_KEY="your_key"
export GEMINI_API_KEY="your_key"

ドキュメントを読む

完全なドキュメント、ガイド、API リファレンスは npcpy.readthedocs.io をご覧ください。

リンク

  • NPC Shell — NPC と対話するためのコマンドラインシェル
  • Incognide — AI チャット、ブラウザー、ファイルビューアー、コードエディター、ターミナル、ナレッジグラフ、チーム管理などを備えたデスクトップ環境(ダウンロード)
  • incognidium - Firefox および Chromium と照らし合わせて incognidium ブラウザーの開発とテストを行うことに重点を置いた NPC チームを備える、Rust ネイティブのウェブブラウザー。
他の言語でNPCチームのデータレイヤー構成を利用する:
  • npcrs - npcpyのRust移植版
  • nql - Google BigQuery、Snowflake、Databricksなど、LLMをサポートするデータベース内でエージェントを実行するための、SQLネイティブなフレームワーク。
  • npcts - NPCデータレイヤー向けTypeScriptライブラリ
  • npcd - npcdのRust移植版

研究

  • 自然言語処理のための量子意味論フレームワーク:arxiv、QNLP 2025で採択
  • TinyTim:多様な生成のための言語モデルファミリー arxiv、NeurIPS 2025 Creative AI Trackで採択
  • 自然言語処理における意味の生成:arxiv、QNLP 2026で採択
  • CATの群れ:ポータブルで構成可能なマルチエージェントチームにおけるエージェントハーネスエンジニアリングのためのALARA - arxiv、HAXD 2026で採択

皆さんの研究にnpcpyが役立ちましたか?ぜひお知らせください!

サポート

毎月の寄付 | グッズ | コンサルティング:[email protected]

コントリビューション

コントリビューションを歓迎します!GitHubリポジトリでイシューを作成したり、プルリクエストを送信したりしてください。

ライセンス

MIT License。

スター履歴

スター履歴チャート

コメント

MCP は初めてですか?

プロトコルの概要から最初のサーバーの導入まで、よくある疑問にまとめて答えます。

AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。