Package: aichar
AicharPython library for creating/editing/transporting AI characters between different frontends (TavernAI,SillyTavern,TextGenerationWebUI,AI-companion, Pygmalion)This library allows you to read JSON, Yaml and character card files, edit their data, create your characters from scratch and export them as JSON, Yaml or character cards compatible with the frontends mentioned aboveInstallationpipinstallaicharUsageCreating a CharacterTo create a new character, you can use the create_character function. This function takes several parameters to initialize the character's attributes and returns a CharacterClass object.importaicharcharacter=aichar.create_character(name="Character Name",summary="Character Summary",personality="Character Personality",scenario="Character Scenario",greeting_message="Character Greeting Message",example_messages="Character Example Messages",image_path="Character Image Path")Loading a Character data from a PNG Character Card Filecharacter=aichar.load_character_card_file("character_card.png")Loading a Character data from a PNG Character Card Bytescharacter=aichar.load_character_card(data_bytes)Wheredata_bytescan be e.g. bytes of the opened png file of the character cardwithopen("character_card.png",'rb')asfile:data_bytes=file.read()Loading a Character data from a JSON Filecharacter=aichar.load_character_json_file("character.json")Loading a Character data from a JSON Stringcharacter=aichar.load_character_json('{"char_name": "Character Name", "char_persona": "Character Personality", "world_scenario": "Character Scenario", "char_greeting": "Character Greeting Message", "example_dialogue": "Character Example Messages", "name": "Character Name", "description": "Character Summary", "personality": "Character Personality", "scenario": "Character Scenario", "first_mes": "Character Greeting Message", "mes_example": "Character Example Messages"}')Loading a Character data from a Yaml Filecharacter=aichar.load_character_yaml_file("character.yaml")Loading a Character data from a Yaml Stringcharacter=aichar.load_character_yaml('char_name: Character Name\nchar_persona: Character Personality\nworld_scenario: Character Scenario\nchar_greeting: Character Greeting Message\nexample_dialogue: Character Example Messages\nname: Character Name\ndescription: Character Summary\npersonality: Character Personality\nscenario: Character Scenario\nfirst_mes: Character Greeting Message\nmes_example: Character Example Messages\nmetadata:\nversion: 1\ncreated: 1696945481977\nmodified: 1696945481977\nsource: null\ntool:\nname: aichar Python library\nversion: 0.5.0\nurl: https://github.com/Hukasx0/aichar\n')Modifying Character AttributesYou can modify the attributes of a character. Here are some examples:# Load a character card from a JSON filecharacter=aichar.load_character_json_file("character_data.json")# Change character namecharacter.name="New Name"# Change character summarycharacter.summary="New Summary"# Change character personalitycharacter.personality="New Personality"# Change character scenariocharacter.scenario="New Scenario"# Change character greeting messagecharacter.greeting_message="New Greeting Message"# Change character example messagescharacter.example_messages="New Example Messages"# Change character image path (needed if you want to export character as character png card)character.image_path="New Image Path"Printing Character Information SummaryYou can get character's information summary by using the data_summary attribute:print(character.data_summary)Accessing Character AttributesYou can access character's attributes using the provided getter methods. For example:print("Character Name: ",character.name)print("Character Summary: ",character.summary)print("Character Personality: ",character.personality)image_path=character.image_pathExporting Character DataYou can export the character's data in different formats using the export_card_file, export_json, export_json_file, export_yaml and export_yaml_file function. Supported export formats include "tavernai" (or "sillytavern"), "textgenerationwebui" (or "pygmalion"), and "aicompanion".exporting data as character card png:# Export character card in "tavernai" formatcharacter.export_card_file("tavernai","tavernai_character_card.png")# Export character card in "sillytavern" formatcharacter.export_card_file("sillytavern","sillytavern_character_card.png")# Export character card in "textgenerationwebui" formatcharacter.export_card_file("textgenerationwebui","textgenerationwebui_character_card.png")# Export character card in "pygmalion" formatcharacter.export_card_file("pygmalion","pygmalion_character_card.png")# Export character card in "aicompanion" formatcharacter.export_card_file("aicompanion","aicompanion_character_card.png")exporting data as json string or file:# Export character data in "tavernai" formattavernai_json_string=character.export_json("tavernai")# or to filecharacter.export_json_file("tavernai","tavernai_character_data.json")# Export character data in "sillytavern" formatsillytavern_json_string=character.export_json("sillytavern")# or to filecharacter.export_json_file("sillytavern","sillytavern_character_data.json")# Export character data in "textgenerationwebui" formattextgenerationwebui_json_string=character.export_json("textgenerationwebui")# or to filecharacter.export_json_file("textgenerationwebui","textgenerationwebui_character_data.json")# Export character data in "pygmalion" formatpygmalion_json_string=character.export_json("pygmalion")# or to filecharacter.export_json_file("pygmalion","pygmalion_character_data.json")# Export character data in "aicompanion" formataicompanion_json_string=character.export_json("aicompanion")# or to filecharacter.export_json_file("aicompanion","companion_character_data.json")exporting data as yaml string or file:# Export character data in "tavernai" formattavernai_yaml_string=character.export_yaml("tavernai")# or to filecharacter.export_yaml_file("tavernai","tavernai_character_data.yml")# Export character data in "sillytavern" formatsillytavern_yaml_string=character.export_yaml("sillytavern")# or to filecharacter.export_yaml_file("sillytavern","sillytavern_character_data.yml")# Export character data in "textgenerationwebui" formattextgenerationwebui_yaml_string=character.export_yaml("textgenerationwebui")# or to filecharacter.export_yaml_file("textgenerationwebui","textgenerationwebui_character_data.yml")# Export character data in "pygmalion" formatpygmalion_yaml_string=character.export_yaml("pygmalion")# or to filecharacter.export_yaml_file("pygmalion","pygmalion_character_data.yml")# Export character data in "aicompanion" formataicompanion_yaml_string=character.export_yaml("aicompanion")# or to filecharacter.export_yaml_file("aicompanion","companion_character_data.yml")Or you can export it in neutral format for those frontends:neutral_json_string=character.export_neutral_json()neutral_yaml_string=character.export_neutral_yaml()character.export_neutral_json_file("neutral_character_data.json")character.export_neutral_yaml_file("neutral_character_data.yml")character.export_neutral_card_file("neutral_card_name.png")Exporting character cards as bytescharacter_neutral_bytes_list=character.export_neutral_card()# you can also export in any format you choosecharacter_sillytavern_bytes_list=character.export_card("sillytavern")Why bytes_list and not just bytes?Both .export_neutral_card() and .export_card() methods return 'bytes': 'list', if you need bytes then you can use the python function bytes() to convert the data to 'PyBytes'.
For example, you will get an error like this:TypeError: argument 'bytes': 'list' object cannot be converted to 'PyBytes'Example of a solution to a problem:character_neutral_bytes=bytes(character.export_neutral_card())# then you can perform the same operations on it as you would on bytesnew_character=aichar.load_character_card(character_neutral_bytes)License2023-2024 Hubert KasperekAt any time when using the library, you can read the content of the license by calling the.license()methodprint(aichar.license())This library is distributed under theMIT License.