Package: kani-vision
kani-visionInstallationTo install kani-vision, you must have at least Python 3.10. kani-vision uses extras to provide support for specific
models - see below for model-specific instructions and other extras.You can combine multiple extras into a single command, likepip install "kani-vision[openai,ascii]".OpenAI (GPT-4V)$pipinstall"kani-vision[openai]"LLaVA v1.5Note: to install dependencies for LLaVA, you will have to run the following two commands as the LLaVA package installs
some outdated incompatible dependencies by default:$pipinstall"kani-vision[llava]"$pipinstall--no-deps"llava @ git+https://github.com/haotian-liu/LLaVA.git@v1.1.3"Other Extraspip install "kani-vision[ascii]": When usingchat_in_terminal_vision(), this will display any images you provide
to the model as ASCII art in your terminal :).QuickstartfromkaniimportKanifromkani.ext.visionimportchat_in_terminal_visionfromkani.ext.vision.engines.openaiimportOpenAIVisionEngine# add your OpenAI API key hereapi_key="sk-..."engine=OpenAIVisionEngine(api_key,model="gpt-4-vision-preview",max_tokens=512)ai=Kani(engine)# use `!path/to/file.png` to provide an image to the engine, e.g. `Please describe this image: !kani-logo.png`# or use a URL: `Please describe this image: !https://example.com/image.png`chat_in_terminal_vision(ai)UsageThis section assumes that you're already familiar with the basic usage of kani. If not, go check
outthe kani docsfirst!kani-vision provides two main features to extend kani with vision using
themessage parts API.EnginesThe first are the vision engines, which are the underlying vision-language models (VLMs). kani-vision comes with support
for two VLM engines, GPT-4V (OpenAI's hosted model) and LLaVA v1.5 (an open-source extension of Vicuna):Model NameExtraCapabilitiesEngineGPT-4Vopenai🛠 📡kani.ext.vision.engines.openai.OpenAIVisionEngineLLaVA v1.5llava[^llava]🔓 🖥 🚀kani.ext.vision.engines.llava.LlavaEngineLegend🛠: Supports function calling.🔓: Open source model.🖥: Runs locally on CPU.🚀: Runs locally on GPU.📡: Hosted API.[^llava]: See the installation instructions. You may also need to install PyTorch manually.To initialize an engine, you use it the same way as in normal kani! All vision engines are interchangeable with normal
kani engines.Message PartThe second feature you need to be familiar with is theImagePart, the core way of sending messages to the engine.
To do this, when you call the kani round methods (i.e.Kani.chat_roundorKani.full_roundor their str variants),
pass alistrather than a string:fromkaniimportKanifromkani.ext.visionimportImagePartfromkani.ext.vision.engines.llavaimportLlavaEngineengine=LlavaEngine("liuhaotian/llava-v1.5-7b")ai=Kani(engine)# notice how the arg is a list of parts rather than a single str!msg=awaitai.chat_round_str(["Please describe this image:",ImagePart.from_path("path/to/image.png")])print(msg)You can also define images from a URL, raw PNG binary or a Pillow Image, usingImagePart.from_url,ImagePart.from_bytes, orImagePart.from_image, respectively.Seethe examplesfor more.Terminal UtilityFinally, kani-vision comes with an additional utility to chat with a VLM in your terminal,chat_in_terminal_vision.This utility allows you to provide images on your disk or on the internet inline by prepending it with an exclamation
point:>>>fromkani.ext.visionimportchat_in_terminal_vision>>>chat_in_terminal_vision(ai)USER: Please describe this image: !path/to/image.png and also this one: !https://example.com/image.png