!LM Studio: Simple Text-API Samples

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > AI - Artificial Intelligence Commands > !Calling Any API Samples >

!LM Studio: Simple Text-API Samples

 

clip1085

Overview

This script demonstrates how to process an text-Input and use the LM Studio API to do some A.I. work using an AI model wit 128k-Token Contextwindow.
It can generate and work on larger Texts, yet this will take time depending on your hardware.

 

 

The script:

- Uses an Text-Prompt

- Escapes the Prompt bevor inserting it into the JSON

- Sends the JSON Payload it to LM Studio's API

- Returns an AI-generated description

 

About LM Studio

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 Model: Gemma-3-27b-it and gemma-3-27b-it@q8_0

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.

 

 

Script Code to have it generate a Text

 

' specify the model to use.

$$MOD=gemma-3-27b-it@q8_0 

 

' API Setup:

' Defines the base URL for the API endpoint

$$URB=http://localhost:1234/

' Defines the specific API endpoint for chat completions

$$REA=v1/chat/completions 

' Combines the base URL and endpoint to create the full API URL

VAV.$$URL=$$URB$$REA 

 

' Initializes HTTP headers (no initial headers)

HTP.InitHdr|-|- 

' Adds a header specifying that the content type is JSON

HTP.AddHdr|Content-Type|application/json 

' Adds an authorization header (currently empty, may need a token)

HTP.AddHdr|Authorization|Bearer - 

 

' AI Configuration:

' Defines the system prompt – instructions for the AI

$$SYS=You are a helpful assistant. 

' Defines the user prompt – the question to ask the AI

$$USR=please make me a long rhyme that sounds like schiller in german 

 

' Escape the System Prompt

' Escapes special characters in the system prompt for safe JSON inclusion

AIC.ESC|$$SYS|$$ESS 

 

' Escape strings for JSON using AIC.ESC

' Escapes special characters in the user prompt for safe JSON inclusion

AIC.ESC|$$USR|$$ESU 

 

' Combines the escaped user prompt into a variable called $$CNT

VAV.$$CNT=$$ESU 

 

' Constructs the JSON payload for the API request: model name, system message, user message, max tokens, temperature, and streaming flag

VAV.$$JSO={"model": "$$MOD", "messages": [{"role": "system", "content": "$$ESS"},

VAV.$$JSO+ {"role": "user", "content": "$$CNT"}], "max_tokens": 100000, "temperature": 0.7, "stream": false}

 

' HTTP Request:

' Specifies that this is a POST request

$$CMD=POST 

' Disables debug output from the HTTP requests

HTP.Setdebug|0 

' Sets a timeout of 3600 seconds (1 hour) for the HTTP request

HTP.SetTO|3600000 

' Sends POST request with 3600s timeout

HTP.Request|$$CMD|$$URL|$$JSO|0|0|0|0|$$RET 

 

' Response Handling:

' Parses the JSON response from the API into a document object ($$DOC)

PSJ.Parse|-|$$DOC 

' Specifies the path within the JSON response where the AI’s answer is located

$$PAT=choices[0].message.content 

' Extracts the AI’s answer from the parsed JSON and stores it in $$TEX

PSJ.GetStr|$$DOC|$$PAT|$$TEX 

' Displays the extracted text ($$TEX) in a message box

MBX.$$TEX 

' Gets the last result as wide string

PSJ.GetLastResultW|$$TEA 

' Sets the unitext to the AI response

CLB.Set Unitext|$$TEA 

' Frees memory used by the JSON parsing functions

PSJ.FreeAll 

' Ends the script

ENR. 

 

 

 

 

clip1084

Part of the Server output in LM-Studio during the processing of the Script.