Skip to content

DimonSmart/AI.Utils

Repository files navigation

AI.Utils

This repository contains multiple packages:

  • DimonSmart.AiUtils
  • DimonSmart.AiUtils.SemanticKernel

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
var segments = result.ThoughtSegments;        // Individual thought segments
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.ThoughtSegments == ["First consider option A", "Option 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.

About

Utils, helpers to AI related code

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages