# webLLM

This is a configuration section of [Custom MT](/advanced/custom-mt.md). The webLLM method allows interaction with web-based Large Language Models (LLMs) by automating browser interactions and capturing streaming responses.

## Parameters

* **method**: `string` - Set to "web\_llm"
* **callback**: `string` - Callback method for handling responses ("dataSync")
* **url**: `string` - Set to null when using webLLM method
* **initialUrl**: `string` - The initial URL of the web-based LLM interface to load
* **inputEvent**: `array of objects` - Sequence of browser interaction events:
  * **Selector:** Set element property \<selector: string, property: string, value: string>
  * **Selector with Event:** Trigger element event \<selector: string, property: string, newEvent?: string, options?: object>
  * **Sleep:** Wait for specified duration \<sleep: number> (duration in milliseconds)
* **streamAdapter**: `string` - Type of network adapter to intercept:
  * "xhr" - XMLHttpRequest
  * "fetch" - Fetch API
* **streamOverrideDelay**: `number` - Delay in milliseconds before overriding stream responses
* **streamType:** `string` - How to handle streaming data:
  * `append` - Append new data to existing content
  * `replace` - Replace content with new data
* **streamFormat:** `string` - Expected format of streaming data:
  * "json" - JSON format
  * "string" - Plain text format
* **streamFilter:** `object` - Filter criteria for capturing network requests:
  * **url:** `array of strings` - Filter by URL patterns
  * **method:** `array of strings` - Filter by HTTP methods (\["GET", "POST"])
  * **contentType:** `array of strings` - Filter by content type headers
* **streamCompleted:** `object` - Conditions to determine when streaming is complete:
  * **setTimeout:** `number` - Maximum wait time in milliseconds after last data received
  * **requestReadyState:** `number` - XMLHttpRequest ready state value indicating completion
  * **requestException:** `string` - Exception name that indicates stream completion
* **streamParser:** `array of arrays` - Pipeline of parsing operations applied to each stream chunk. Each pipeline is an array of parser objects with the following available operations:
  * **Validation Operations:**
    * `{"act": "isString"}` - Check if value is a string
    * `{"act": "isArray"}` - Check if value is an array
    * `{"act": "isObject"}` - Check if value is an object
    * `{"act": "isNotNull"}` - Check if value is not null
    * `{"act": "isNotEq", "value": string}` - Check if value is not equal to specified value
    * `{"act": "minChar", "value": number}` - Check if string length is at least the specified value
    * `{"act": "maxChar", "value": number}` - Check if string length is at most the specified value
  * **String Operations:**
    * `{"act": "trim"}` - Remove whitespace from both ends
    * `{"act": "split", "separator": string, "limit": number, "index": number}` - Split string and get element at index
    * `{"act": "replace", "find": string, "replace": string}` - Replace first occurrence
    * `{"act": "replaceAll", "find": string, "replace": string}` - Replace all occurrences
    * `{"act": "search", "text": string}` - Check if text exists in string
    * `{"act": "indexOf", "text": string}` - Get index of text in string
    * `{"act": "regexpMatch", "regexp": string, "global": boolean}` - Match using regular expression
    * `{"act": "regexpReplace", "regexp": string, "global": boolean, "replace": string}` - Replace using regular expression
  * **Conversion Operations:**
    * `{"act": "toJSON"}` - Parse string as JSON
    * `{"act": "toString"}` - Convert value to string
  * **Object Operations:**
    * `{"act": "getValue", "key": string}` - Extract value from object by key

```json
{
    "request": {
        "method": "web_llm",
        "callback": "dataSync",
        "initialUrl": "https://chatgpt.com/", 
        "url": null,
        "inputEvent": [
            {"selector": "#textarea", "property": "value", "value": "$SOURCE_TEXT"},
            {"sleep": 100},
            {"selector": "button", "property": "click"},
        ],
        "streamAdapter": "fetch",
        "streamOverrideDelay": 0,
        "streamFilter": {},
        "streamFormat": "string",
        "streamType": "append",
        "streamCompleted": {
            "setTimeout": 2500,
            "requestException": "AbortError"
        },
        "streamParser": [
            [
                {"act": "isString"},
                {"act": "search", "text": "data:"},
                {"act": "replace", "find": "data:", "replace": ""},
                {"act": "toJSON"},
                {"act": "isObject"},
                {"act": "getValue", "key": "v"},
                {"act": "isString"},
                {"act": "isNotNull"}
            ],
            [...],
            [...]
        ]
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.vntranslator.com/advanced/custom-mt/webllm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
