LlmKira
novelai-python
Python

✨ NovelAI api python sdk, easy to use, modern and user-friendly.

Last updated Jul 11, 2026
49
Stars
2
Forks
5
Issues
0
Stars/day
Attention Score
29
Language breakdown
Python 100.0%
Files click to expand
README

banner


PyPI version Downloads

>Declaration (2025/06/04) > >I have nothing to do with NovelAI. > >Due to various considerations, such as NovelAI's confusing project architecture and "can't learn anything from it", this project will be in a non-active maintenance state. If you have any questions, please raise an issue. > >This project is designed to be a sample project for me to write Python SDK. It turns out that Pydantic is very powerful. > >I will invest more time in open source projects that are more beneficial to society. >

✨ NovelAI api python sdk with Pydantic, modern and user-friendly.

The goal of this repository is to use Pydantic to build legitimate requests to access the NovelAI API service.

Python >= 3.9 <3.13 is required.

📰 News

📦 Usage

pip -U install novelai-python

All API users must adhere to the NovelAI Terms of Service: https://novelai.net/terms.

**More examples can be found in the playground directory, read code as documentation.**

import asyncio
import os

from dotenv import load_dotenv from pydantic import SecretStr

from novelai_python import GenerateImageInfer, ImageGenerateResp, ApiCredential from novelaipython.sdk.ai.generateimage import Model, Character, Sampler, UCPreset from novelaipython.sdk.ai.generateimage.schema import PositionMap

load_dotenv() session = ApiCredential(apitoken=SecretStr(os.getenv("NOVELAIJWT"))) # pst-*

For security reasons, storing user credentials in plaintext is strongly discouraged.

prompt = "2girls, fisheye, closeup, from above"

async def main(): gen = GenerateImageInfer.build_generate( prompt=prompt, model=Model.NAIDIFFUSION45FULL, character_prompts=[ Character( prompt="1girl, head tilt, short hair, black hair, grey eyes, small breasts, looking at viewer", uc="red hair", center=PositionMap.B2 ), Character( prompt="1girl, fox ears, fox tail, white hair, white tail, white ears", uc="black hair", center=PositionMap.D2 ) ], sampler=Sampler.KEULERANCESTRAL, ucPreset=UCPreset.TYPE0, # Recommended, using preset negative_prompt depends on selected model qualityToggle=True, decrisp_mode=False, variety_boost=True, furry_mode=False, # Checkbox in novelai.net ) cost = gen.calculatecost(isopus=True) print(f"charge: {cost} if you are vip3") resp = await gen.request(session=session) resp: ImageGenerateResp print(resp.meta) file = resp.files[0] with open(file[0], "wb") as f: f.write(file[1])

loop = asyncio.geteventloop() loop.rununtilcomplete(main())

📦 LLM

import asyncio
import os

from dotenv import load_dotenv from pydantic import SecretStr

from novelai_python import APIError, LoginCredential from novelaipython.sdk.ai.generate import TextLLMModel, LLM, getdefault_preset, AdvanceLLMSetting from novelaipython.sdk.ai.generate.enum import getmodelpreset

load_dotenv() username = os.getenv("NOVELAI_USER", None) assert username is not None

credential = JwtCredential(jwt_token=SecretStr(jwt))

login_credential = LoginCredential( username=os.getenv("NOVELAI_USER"), password=SecretStr(os.getenv("NOVELAI_PASS")) )

async def chat(prompt: str): try: model = TextLLMModel.ERATO # llama3 parameters = getdefaultpreset(model).parameters agent = LLM.build( prompt=prompt, model=model, # parameters=None, # Auto Select or get from preset parameters=getmodelpreset(TextLLMModel.ERATO).getallpresets()[0].parameters, # Select from enum preset advanced_setting=AdvanceLLMSetting( min_length=1, max_length=None, # Auto ) ) # NOTE:parameter > advanced_setting, which logic in generate/init.py # If you not pass the parameter, it will use the default preset. # So if you want to set the generation params, you should pass your own params. # Only if you want to use some params not affect the generation, you can use advanced_setting. result = await agent.request(session=login_credential) except APIError as e: raise Exception(f"Error: {e.message}") print(f"Result: \n{result.text}")

loop = asyncio.geteventloop() loop.rununtilcomplete(chat("Hello"))

📦 Random Prompt

from novelaipython.tool.randomprompt import RandomPromptGenerator

generator = RandomPromptGenerator() for i in range(10): print(generator.generatecommontags(nsfw=False)) print(generator.generatescenetags()) print(generator.generatescenecomposition()) print(generator.getholidaythemed_tags())

📦 Run A Server

pip install novelai_python
python3 -m novelai_python.server -h '127.0.0.1' -p 7888

📦 Tokenizer

from novelaipython.enum import gettokenizermodel, TextLLMModel, TextTokenizerGroup
from novelai_python.tokenizer import NaiTokenizer

Through llm model name to get the tokenizer

tokenizerpackage = NaiTokenizer(gettokenizer_model(TextLLMModel.ERATO))

Directly use the tokenizer

clip_tokenizer = NaiTokenizer(TextTokenizerGroup.CLIP)

Tokenize a text

t_text = "a fox jumped over the lazy dog" encodetokens = tokenizerpackage.encode(t_text) print(tokenizerpackage.tokenizetext(t_text)) print(f"Tokenized text: {encode_tokens}") print(tokenizerpackage.decode(tokenizerpackage.encode(t_text)))

🔨 Roadmap

  • [x] tool.random_prompt
  • [x] tool.paint_mask
  • [x] tool.image_metadata
  • [x] tokenizer
  • [x] /ai/generate-image
  • [x] /user/subscription
  • [x] /user/login
  • [x] /user/information
  • [x] /ai/upscale
  • [x] /ai/generate-image/suggest-tags
  • [x] /ai/generate-voice
  • [x] /ai/generate-stream
  • [x] /ai/generate
  • [x] /ai/augment-image
  • [ ] /ai/annotate-image
  • [ ] /ai/classify
  • [ ] /ai/generate-prompt
GenerateImageInfer.calculate_cost is correct in most cases, but please request account information to get accurate
consumption information.
This repo is maintained by me personally now. If you have any questions, please feel free to open an issue.

🚫 About Nsfw

You might need some solutions for identifying NSFW content and adding a mosaic to prevent operational mishaps.

https://dghs-imgutils.deepghs.org/main/api_doc/detect/nudenet.html

https://dghs-imgutils.deepghs.org/main/api_doc/operate/censor.html

🙏 Acknowledgements

novelai-api

NovelAI-API

🔗 More in this category

© 2026 GitRepoTrend · LlmKira/novelai-python · Updated daily from GitHub