Extract JSON from llm response
Function: Extract JSON from LLM Response
This function helps you automatically find and pull out a piece of JSON (JavaScript Object Notation) text from a longer response generated by a Large Language Model (LLM). This is useful when an LLM provides information embedded within other text, and you need to work specifically with the structured data.
Input
- LLM Response (STRING, Required): The full text response you received from an LLM. This is the text that might contain the JSON data you want to extract.
Output
- result (STRING): A variable that will store the extracted JSON text. If no JSON is found, this variable might be empty or contain the original response depending on the LLM's output and the extraction logic.
Execution Flow
Real-Life Examples
Here are some examples of how you can use the "Extract JSON from LLM Response" function:
Example 1: Extracting a Clean JSON Object
Imagine an LLM is asked to provide user details and it responds with just the JSON.
- Inputs:
- LLM Response:
\{"user_id": "U12345", "name": "Jane Doe", "email": "jane.doe@example.com", "status": "active"\}
- LLM Response:
- Result:
The
resultvariable will contain:\{"user_id": "U12345", "name": "Jane Doe", "email": "jane.doe@example.com", "status": "active"\}.
Example 2: Extracting JSON from a Code Block
Suppose an LLM provides a detailed explanation along with a JSON configuration wrapped in a code block.
- Inputs:
- LLM Response: "Here is the recommended configuration for your new service:\n
json\n\{\n \"serviceName\": \"AnalyticsProcessor\",\n \"version\": \"1.0\",\n \"enabled\": true,\n \"endpoints\": [\"/api/data\", \"/api/logs\"]\n\}\n\nPlease deploy this configuration."
- LLM Response: "Here is the recommended configuration for your new service:\n
- Result:
The
resultvariable will contain: ```json { "serviceName": "AnalyticsProcessor", "version": "1.0", "enabled": true, "endpoints": ["/api/data", "/api/logs"] }
#### Example 3: Extracting JSON Embedded in Text
An LLM might provide a summary and then include a JSON object directly within the text without a code block.
* **Inputs:**
* **LLM Response**: "The recent sales data shows a positive trend. Specifically, for Q3, the summary is: \{\"quarter\": \"Q3\", \"year\": 2023, \"revenue\": 1500000, \"growth\": 0.12\}. We should focus on these areas."
* **Result:**
The `result` variable will contain: `\{"quarter": "Q3", "year": 2023, "revenue": 1500000, "growth": 0.12\}`.