DimonSmart.AiUtils
1.25315.1324
Prefix Reserved
dotnet add package DimonSmart.AiUtils --version 1.25315.1324
NuGet\Install-Package DimonSmart.AiUtils -Version 1.25315.1324
<PackageReference Include="DimonSmart.AiUtils" Version="1.25315.1324" />
paket add DimonSmart.AiUtils --version 1.25315.1324
#r "nuget: DimonSmart.AiUtils, 1.25315.1324"
// Install DimonSmart.AiUtils as a Cake Addin #addin nuget:?package=DimonSmart.AiUtils&version=1.25315.1324 // Install DimonSmart.AiUtils as a Cake Tool #tool nuget:?package=DimonSmart.AiUtils&version=1.25315.1324
DimonSmart.AiUtils
DimonSmart.AiUtils is a library designed to extract JSON strings and process structured content from responses generated by Large Language Models (LLMs). It ensures accurate extraction of both JSON data and thought processes from LLM outputs.
Background
LLMs often produce additional text alongside the requested JSON data. This can include explanations, clarifications, or contextual information that may interfere with JSON parsing. DimonSmart.AiUtils addresses this issue by extracting only the valid JSON strings from the response, allowing for seamless integration with JSON parsers.
Features
- ExtractJson: Returns the largest valid JSON object or array as a string from the provided text.
- ExtractAllJsons: Returns all valid JSON objects or arrays as strings from the text.
- ExtractJson (overload): Returns a single JSON fragment as a string, starting from a specific index in the text.
- ExtractThinkAnswer: Extracts content within
<think>
tags and separates it from the main response.
API Overview
JSON Extraction
ExtractJson(string? text)
Extracts and returns the largest valid JSON object or array as a string from the input text.
string json = JsonExtractor.ExtractJson(responseText);
ExtractAllJsons(string? text)
Extracts all valid JSON objects or arrays from the input text.
var jsons = JsonExtractor.ExtractAllJsons(responseText);
ExtractJson(int start, string? text)
Extracts a single valid JSON fragment starting from the specified index in the text.
string json = JsonExtractor.ExtractJson(0, responseText);
Think Tag Parsing
ExtractThinkAnswer(string input)
Extracts and separates content within <think>
tags from the main response text. This is particularly useful when working with LLM responses that include both thought processes and final answers.
var result = ThinkTagParser.ExtractThinkAnswer(responseText);
string thoughts = result.Thoughts; // Combined content from all <think> tags
string answer = result.Answer; // Clean text without the think tags
Example:
var input = "Let me think about this. <think>First consider option A</think> The answer is B <think>Option B is better because...</think>";
var result = ThinkTagParser.ExtractThinkAnswer(input);
// result.Thoughts contains: "First consider option A\nOption B is better because..."
// result.Answer contains: "Let me think about this. The answer is B"
Installation
Install via NuGet:
dotnet add package DimonSmart.AiUtils
Use Cases
- Preprocess LLM responses to extract structured JSON data.
- Handle mixed LLM responses that include additional text explanations or metadata.
- Extract multiple JSON fragments from a single response.
- Clean up LLM outputs to obtain pure JSON strings for parsing.
- Separate LLM thought processes from final answers using think tags.
- Analyze LLM reasoning by extracting intermediate thoughts.
License
This library is licensed under the 0BSD License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.25315.1324 | 53 | 3/15/2025 |
1.25301.2132 | 94 | 3/1/2025 |
1.25110.2049 | 97 | 1/10/2025 |