llmutils

package
v1.29.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package llmutils provides utilities for processing LLM prompt templates with support for comment stripping and metadata extraction.

This package combines comment stripping from the API repository with metadata extraction from both the web and API repositories to provide a unified interface for prompt processing.

Comment Format: - Lines starting with /// are treated as comments - Inline comments after /// are supported - Comments are stripped before sending to LLM

Special Directives: - /// param: value - Extracts metadata for BigQuery tracking - /// key: value - Generic metadata extraction

Example:

/// This is a comment that will be removed
/// param: temperature=0.7
/// flow: checkout-process
You are a helpful assistant /// inline comment removed

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractParams

func ExtractParams(content string) map[string]string

ExtractParams extracts only the params from a prompt template without cleaning. This is useful if you want to inspect metadata before processing.

func StripComments

func StripComments(content string) string

StripComments is a convenience function that only removes comments without extracting params. Use Process() instead if you need param extraction.

This function removes blank lines after comment removal and handles URLs correctly.

func StripCommentsFromMessages

func StripCommentsFromMessages(messages []interface{}) []interface{}

StripCommentsFromMessages strips /// comments from all messages in a chat completion request.

This function is designed to integrate with LLM APIs (OpenAI, Groq, etc.). It processes both system and user messages, removing comments while preserving the message structure.

Parameters:

  • messages: Array of message maps with "role" and "content" fields

Returns:

  • Modified messages array with comments stripped from all content fields

Example:

messages := []map[string]interface{}{
    {"role": "system", "content": "You are helpful /// be nice"},
    {"role": "user", "content": "/// Debug note\nTell me a joke"},
}
cleaned := StripCommentsFromMessages(messages)
// Result:
// [
//   {"role": "system", "content": "You are helpful"},
//   {"role": "user", "content": "Tell me a joke"},
// ]

Types

type ProcessedPrompt

type ProcessedPrompt struct {
	// CleanedPrompt is the prompt with all /// comments removed
	CleanedPrompt string

	// Params contains extracted metadata from /// param: directives
	// Multiple params can be combined: temperature=0.7, max_tokens=1000
	Params map[string]string

	// Metadata contains all extracted key-value pairs from /// directives
	Metadata map[string]string

	// Flow is the application flow name (from /// flow: directive)
	Flow string

	// Node is the node/step name within the flow (from /// node: directive)
	Node string

	// Tags contains additional tags extracted from directives
	Tags []string
}

ProcessedPrompt contains the cleaned prompt and extracted metadata.

func ExtractMetadata

func ExtractMetadata(content string) ProcessedPrompt

ExtractMetadata extracts all metadata (params, flow, node, tags) from a prompt template. Returns a ProcessedPrompt with only metadata fields populated.

func Process

func Process(content string) ProcessedPrompt

Process removes /// comments from a prompt template and extracts metadata.

Comment Processing:

  • Full-line comments: Lines containing only /// and comment text are removed
  • Inline comments: Content after /// on a line is removed
  • Whitespace handling: Trailing whitespace is trimmed from lines with inline comments
  • URL protection: URLs like http://example.com are NOT treated as comments

Metadata Extraction:

  • /// param: key=value - Extracts key-value pairs for BigQuery metadata
  • /// param: temperature=0.7, max_tokens=1000 - Supports comma-separated values
  • /// flow: name - Extracts application flow name
  • /// node: name - Extracts node/step name
  • /// tag: value - Extracts tags (comma-separated)
  • /// key: value - Generic metadata extraction

Example:

input := `
/// This is a full-line comment - will be removed
/// param: model=gpt-4, temperature=0.7
/// flow: checkout-process
/// node: payment-validation
You are a helpful assistant /// inline comment
Visit http://example.com for more /// URL preserved
`

result := Process(input)
// result.CleanedPrompt = "You are a helpful assistant\nVisit http://example.com for more"
// result.Params = {"model": "gpt-4", "temperature": "0.7"}
// result.Flow = "checkout-process"
// result.Node = "payment-validation"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL