|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > AI - Artificial Intelligence Commands > !Calling Any API Samples > !LM-Studio - Sample Script with Image |
This script demonstrates how to process an image and use the LM Studio API to analyze and describe it using an AI model.
The script:
1.- Loads an input image
2.- Resizes it to 256x256 pixels
3.- Encodes it to BASE64
4.- Sends it to LM Studio's API
5.- Returns an AI
6.- generated description
LM Studio is a free, open-source desktop application that allows users to discover, download, and run large language models (LLMs) locally on Windows, macOS, or Linux. It provides a user-friendly GUI to interact with models from Hugging Face, ensuring data privacy by keeping everything offline.
The script uses 'gemma-3-27b-it@q8_0' as the AI model. Gemma is a family of lightweight, open-source LLMs developed by Google, designed for research and efficient local deployment. The '3' indicates the third generation, '27b' refers to 27 billion parameters, and 'it' denotes an instruction-tuned variant optimized for tasks like image analysis. '@q8_0' specifies 8-bit quantization, reducing memory usage while maintaining performance, ideal for local execution on consumer hardware.
' Variable Definitions:
$$IMX=?path\T2.jpg
$$IMK=?path\T5.jpg
$$MOD=gemma-3-27b-it@q8_0
' Input image path, output image path, and AI model specification
' Image Processing:
ANA.Load|1|$$IMX
ANA.Resize To|1|2|256|256
ANA.Save|2|$$IMK
'ANA.Show|2!
' Loads input image, resizes to 256x256, saves processed image (display commented out)
' API Setup:
$$URB=http://localhost:1234/
$$REA=v1/chat/completions
VAV.$$URL=$$URB$$REA
HTP.InitHdr|-|-
HTP.AddHdr|Content-Type|application/json
HTP.AddHdr|Authorization|Bearer -
' Sets up base URL, endpoint, and HTTP headers
' AI Configuration:
$$SYS=You are a helpful assistant that can analyze images and provide click coordinates.
$$USR=Describe the picture.
' System and user prompts for AI
' Image Encoding:
CFF.$$IMK|$$IMZ
AIC.Encode to BASE64|$$IMZ|$$B64
$$PRE=data:image/jpeg;base64,
VAV.$$IMG=$$PRE$$B64
' Encodes image to BASE64 with MIME prefix
' JSON Preparation:
' Escape the System Prompt
AIC.ESC|$$SYS|$$ESS
' Escape strings for JSON using AIC.ESC
AIC.ESC|$$USR|$$ESU
AIC.ESC|$$IMG|$$ESI
VAV.$$CNT=$$ESU $$ESI
VAV.$$JSO={"model": "$$MOD", "messages": [{"role": "system", "content": "$$ESS"},
VAV.$$JSO+ {"role": "user", "content": "$$CNT"}], "max_tokens": 100, "temperature": 0.7, "stream": false}
' Prepares JSON payload with escaped strings
' HTTP Request:
$$CMD=POST
HTP.Setdebug|0
' Must be large because the Model may need >10 Minutes to generate an answer
' Sends POST request with 3600s timeout
HTP.SetTO|3600000
HTP.Request|$$CMD|$$URL|$$JSO|0|0|0|0|$$RET
' Response Handling:
' Parses response, shows description, and cleans up
PSJ.Parse|-|$$DOC
$$PAT=choices[0].message.content
PSJ.GetStr|$$DOC|$$PAT|$$TEX
MBX.$$TEX
PSJ.GetLastResultW|$$TEA
CLB.Set Unitext|$$TEA
PSJ.FreeAll
ENR.
$$IMK=?path\T5.jpg
$$MOD=gemma-3-27b-it@q8_0
STW.ct|Chrome_WidgetWin_1|X - Brave
'SWP.67,60|1714,1840|T
ANA.Snap|1
ANA.Save|1|$$IMK
'ANA.Load|1|$$IMX
'ANA.Resize To|1|2|256|256
'ANA.Save|2|$$IMK
$$URB=http://localhost:1234/
$$REA=v1/chat/completions
VAV.$$URL=$$URB$$REA
HTP.InitHdr|-|-
HTP.AddHdr|Content-Type|application/json
HTP.AddHdr|Authorization|Bearer -
$$SYS=You are a helpful assistant that can analyze and describe
$$SYS+images and provide click coordinates.
$$USR=Describe the picture.
CFF.$$IMK|$$IMZ
AIC.Encode to BASE64|$$IMZ|$$B64
$$PRE=data:image/jpeg;base64,
VAV.$$IMG=$$PRE$$B64
' Escape strings for JSON using AIC.ESC
AIC.ESC|$$USR|$$ESU
AIC.ESC|$$SYS|$$ESS
AIC.ESC|$$IMG|$$ESI
VAV.$$CNT=$$ESU $$ESI
VAR.$$JSO={"model": "$$MOD", "messages":
VAR.$$JSO+[{"role": "system", "content": "$$ESS"},
VAR.$$JSO+ {"role": "user", "content": "$$CNT"}],
VAR.$$JSO+ "max_tokens": 91000, "temperature": 0.7,
VAR.$$JSO+ "stream": false}
'LEN.$$JSO|$$JSL
$$CMD=POST
HTP.Setdebug|0
HTP.SetTO|3600000
HTP.Request|$$CMD|$$URL|$$JSO|0|0|0|0|$$RET
PSJ.Parse|-|$$DOC
$$PAT=choices[0].message.content
PSJ.GetStr|$$DOC|$$PAT|$$TEX
MBX.$$TEX
PSJ.GetLastResultW|$$TEA
CLB.Set Unitext|$$TEA
PSJ.FreeAll
ENR.
Requirements:- LM Studio running on localhost:1234- Input image at specified path- Gemma-3-27b-it model available
Download LM Studio from these official sources (as of March 21, 2025):
- Official Website: https://lmstudio.ai (Windows, macOS, Linux versions)
- TechSpot: https://www.techspot.com/downloads/7798-lm-studio.html (Latest version download)
System Requirements:- 16GB+ RAM recommended- 6GB+ VRAM for GPU support (NVIDIA/AMD)- AVX2 processor support- macOS 13.6+ for Apple Silicon (M1/M2/M3)