prebuilt

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccuracyCheckAgent added in v0.0.17

type AccuracyCheckAgent struct {
	// contains filtered or unexported fields
}

AccuracyCheckAgent evaluates how accurately an LLM response matches a ground-truth reference answer, independently of the retrieved knowledge context.

This is distinct from FactCheckAgent (which checks context faithfulness / hallucination) and RelevanceCheckAgent (which checks whether the question was answered). AccuracyCheckAgent answers: "Given we know the correct answer, did the LLM get it right?"

The agent performs a single-shot call using agentloop.Agent with no tools.

Use NewAccuracyCheckAgent to create an instance, then call AccuracyCheckAgent.Check to score a response.

func NewAccuracyCheckAgent added in v0.0.17

func NewAccuracyCheckAgent(chatModel model.ToolCallingChatModel, opts ...AccuracyCheckOption) (*AccuracyCheckAgent, error)

NewAccuracyCheckAgent creates a new AccuracyCheckAgent backed by the given chat model.

Example:

agent, err := prebuilt.NewAccuracyCheckAgent(chatModel)
result, err := agent.Check(rail, prebuilt.AccuracyCheckInput{
    Question:        "What is the capital of France?",
    Output:          "The capital of France is Paris.",
    ReferenceAnswer: "Paris",
})

func (*AccuracyCheckAgent) Check added in v0.0.17

Check evaluates how accurately the LLM response matches the reference answer and returns an AccuracyCheckResult.

The prompt template is substituted with the provided inputs using strutil.NamedSprintfv. The model response is parsed for "Score:" and "Reason:" fields. If either field is missing the call is retried up to [accuracyCheckConfig.RetryCount] additional times.

type AccuracyCheckInput added in v0.0.17

type AccuracyCheckInput struct {
	// Question is the user question that prompted the LLM response.
	Question string

	// Output is the LLM response to be evaluated.
	Output string

	// ReferenceAnswer is the ground-truth answer to compare against.
	// This field is required; if empty the agent will return a low score.
	ReferenceAnswer string
}

AccuracyCheckInput holds all the inputs required for a single accuracy-check call.

type AccuracyCheckOption added in v0.0.17

type AccuracyCheckOption func(o *accuracyCheckConfig)

AccuracyCheckOption configures an AccuracyCheckAgent.

func WithAccuracyCheckLanguage added in v0.0.17

func WithAccuracyCheckLanguage(lang string) AccuracyCheckOption

WithAccuracyCheckLanguage sets the response language for the accuracy-check agent.

func WithAccuracyCheckRetry added in v0.0.17

func WithAccuracyCheckRetry(n int) AccuracyCheckOption

WithAccuracyCheckRetry sets the number of additional retry attempts when the model response is missing a Score or Reason field. The default is 2 (up to 3 total attempts).

func WithAccuracyCheckSystemPrompt added in v0.0.17

func WithAccuracyCheckSystemPrompt(prompt string) AccuracyCheckOption

WithAccuracyCheckSystemPrompt sets an optional system prompt for the accuracy-check agent.

type AccuracyCheckResult added in v0.0.17

type AccuracyCheckResult struct {
	// Score is the answer accuracy score on a 1-5 scale measured against the reference answer:
	//   1 = Completely wrong or contradicts the reference answer
	//   2 = Significant divergence from the reference answer affecting core meaning
	//   3 = Partially matches the reference answer but missing key points
	//   4 = Mostly matches the reference answer with minor omissions
	//   5 = Fully matches the reference answer in all key points
	Score int

	// Reason is a brief textual justification for the score.
	Reason string
}

AccuracyCheckResult holds the numeric score and textual reason returned by the agent.

type CategoryAnalysisResult added in v0.0.16

type CategoryAnalysisResult struct {
	// Reason is the step-by-step reasoning across all subjects.
	Reason string `json:"reason"`

	// CategoryMatched contains existing categories selected from the provided list.
	CategoryMatched []string `json:"categoryMatched"`

	// NewCategory contains new category names invented by the agent for subjects that did not fit any existing category.
	NewCategory []string `json:"newCategory"`
}

CategoryAnalysisResult is the aggregated JSON response returned by the category analysis agent.

type CategoryAnalyzeAgent added in v0.0.16

type CategoryAnalyzeAgent struct {
	// contains filtered or unexported fields
}

CategoryAnalyzeAgent identifies which categories a description belongs to. It may select from existing categories or create new ones. Uses a single-shot agentloop.Agent call and parses the JSON response.

Use NewCategoryAnalyzeAgent to create an instance, then call CategoryAnalyzeAgent.Analyze.

func NewCategoryAnalyzeAgent added in v0.0.16

func NewCategoryAnalyzeAgent(chatModel model.ToolCallingChatModel, opts ...CategoryAnalyzeOption) (*CategoryAnalyzeAgent, error)

NewCategoryAnalyzeAgent creates a new CategoryAnalyzeAgent backed by the given chat model.

Example:

agent, err := prebuilt.NewCategoryAnalyzeAgent(chatModel)
result, err := agent.Analyze(rail, prebuilt.CategoryAnalyzeInput{
    TaskExplanation: "Categorize trade activities for customs declaration.",
    Categories:      []string{"Goods trade-Agricultural", "Service trade-Logistics"},
    Descriptions:    []string{"区块链咨询服务"},
})

func (*CategoryAnalyzeAgent) Analyze added in v0.0.16

Analyze runs the category analysis agent and returns the matched or newly created categories with reasoning.

type CategoryAnalyzeInput added in v0.0.16

type CategoryAnalyzeInput struct {
	// TaskExplanation is additional context about the task/domain.
	TaskExplanation string

	// Categories is the list of existing candidate category names.
	Categories []string

	// Subjects is the batch of subjects to analyze.
	Subjects []string
}

CategoryAnalyzeInput holds the inputs for a single category analysis call.

type CategoryAnalyzeOption added in v0.0.16

type CategoryAnalyzeOption func(o *categoryAnalyzeConfig)

CategoryAnalyzeOption configures a CategoryAnalyzeAgent.

func WithCategoryAnalyzeLanguage added in v0.0.16

func WithCategoryAnalyzeLanguage(lang string) CategoryAnalyzeOption

WithCategoryAnalyzeLanguage sets the response language for the category analysis agent.

type ClassificationAgent added in v0.0.16

type ClassificationAgent struct {
	// contains filtered or unexported fields
}

ClassificationAgent classifies a batch of subjects against a given list of categories. It uses a single-shot agentloop.Agent call and parses the JSON response.

Use NewClassificationAgent to create an instance, then call ClassificationAgent.Classify.

func NewClassificationAgent added in v0.0.16

func NewClassificationAgent(chatModel model.ToolCallingChatModel, opts ...ClassificationOption) (*ClassificationAgent, error)

NewClassificationAgent creates a new ClassificationAgent backed by the given chat model.

Example:

agent, err := prebuilt.NewClassificationAgent(chatModel)
result, err := agent.Classify(rail, prebuilt.ClassificationInput{
    Categories: []string{"Goods trade-Alcohol", "Service trade-Logistics"},
    Subjects:   []string{"物流", "进口葡萄酒"},
})

func (*ClassificationAgent) Classify added in v0.0.16

Classify runs the classification agent over all subjects and returns per-subject results.

type ClassificationInput added in v0.0.16

type ClassificationInput struct {
	// Categories is the list of candidate category names to match against.
	Categories []string

	// Subjects is the batch of subjects to classify.
	Subjects []string
}

ClassificationInput holds the inputs for a batch classification call.

type ClassificationOption added in v0.0.16

type ClassificationOption func(o *classificationConfig)

ClassificationOption configures a ClassificationAgent.

func WithClassificationLanguage added in v0.0.16

func WithClassificationLanguage(lang string) ClassificationOption

WithClassificationLanguage sets the response language for the classification agent.

func WithClassificationSystemPrompt added in v0.0.16

func WithClassificationSystemPrompt(prompt string) ClassificationOption

WithClassificationSystemPrompt sets an optional system prompt for the classification agent.

type ClassificationOutput added in v0.0.16

type ClassificationOutput struct {
	// Results contains one classification result per subject, in input order.
	Results []ClassificationResult `json:"results"`
}

ClassificationOutput is the JSON response returned by the classification agent.

type ClassificationResult added in v0.0.16

type ClassificationResult struct {
	// Subject is the subject that was classified.
	Subject string `json:"subject"`

	// Reason is the step-by-step reasoning behind the classification result.
	Reason string `json:"reason"`

	// Categories contains the matched category names. Empty if none match.
	Categories []string `json:"categories"`
}

ClassificationResult holds the classification result for a single subject.

type ContextualRetrievalAgent added in v0.0.14

type ContextualRetrievalAgent struct {
	// contains filtered or unexported fields
}

ContextualRetrievalAgent generates a short context snippet for a document chunk, situating it within the overall document to improve RAG retrieval quality.

The agent implements the Contextual Retrieval pattern: given a full document and a specific chunk, it produces a succinct context that describes where the chunk fits in the broader text. This context can then be prepended to the chunk before indexing to significantly improve retrieval recall.

The response language follows the document language automatically — no language configuration is required.

Use NewContextualRetrievalAgent to create an instance, then call ContextualRetrievalAgent.Retrieve to generate context for a chunk.

func NewContextualRetrievalAgent added in v0.0.14

func NewContextualRetrievalAgent(chatModel model.ToolCallingChatModel, opts ...ContextualRetrievalOption) (*ContextualRetrievalAgent, error)

NewContextualRetrievalAgent creates a new ContextualRetrievalAgent backed by the given chat model.

Example:

agent, err := prebuilt.NewContextualRetrievalAgent(chatModel)
result, err := agent.Retrieve(rail, prebuilt.ContextualRetrievalInput{
    Content: "...full document text...",
    Chunk:   "...a specific paragraph or section...",
})

func (*ContextualRetrievalAgent) Retrieve added in v0.0.14

Retrieve generates a short succinct context that situates the given chunk within the overall document. The response language follows the document language automatically.

The model produces chain-of-thought reasoning inside <thinking> tags followed by the final answer inside <final_response> tags. Only the <final_response> content is returned. If the <final_response> tag is absent the call is retried up to [contextualRetrievalConfig.RetryCount] times.

type ContextualRetrievalInput added in v0.0.14

type ContextualRetrievalInput struct {
	// Content is the full document text from which the chunk was extracted.
	Content string

	// Chunk is the specific text chunk to situate within the document.
	Chunk string
}

ContextualRetrievalInput holds the inputs for a single contextual retrieval call.

type ContextualRetrievalOption added in v0.0.14

type ContextualRetrievalOption func(o *contextualRetrievalConfig)

ContextualRetrievalOption configures a ContextualRetrievalAgent.

func WithContextualRetrievalRetry added in v0.0.14

func WithContextualRetrievalRetry(n int) ContextualRetrievalOption

WithContextualRetrievalRetry sets the number of additional retry attempts when the model response is empty. The default is 2 (up to 3 total attempts).

func WithContextualRetrievalSystemPrompt added in v0.0.14

func WithContextualRetrievalSystemPrompt(prompt string) ContextualRetrievalOption

WithContextualRetrievalSystemPrompt sets an optional system prompt for the contextual retrieval agent.

type ContextualRetrievalResult added in v0.0.14

type ContextualRetrievalResult struct {
	// Context is a short succinct description that situates the chunk within the overall document,
	// intended to improve search retrieval quality when prepended to the chunk.
	Context string
}

ContextualRetrievalResult holds the generated context for the chunk.

type CsvFormatAgent

type CsvFormatAgent struct {
	// contains filtered or unexported fields
}

CsvFormatAgent formats a CSV file into a RAG-optimised plain-text representation. Each row (or logical group of rows) is rewritten as an independent, self-contained paragraph suitable for semantic chunking and vector retrieval.

Use NewCsvFormatAgent to create an instance, then call CsvFormatAgent.Format to process a file.

func NewCsvFormatAgent

func NewCsvFormatAgent(chatModel model.ToolCallingChatModel, opts ...CsvFormatOption) (*CsvFormatAgent, error)

NewCsvFormatAgent compiles and returns a new CsvFormatAgent.

Example:

agent, err := prebuilt.NewCsvFormatAgent(chatModel,
    prebuilt.WithCsvFormatMaxRunSteps(30),
    prebuilt.WithCsvFormatLanguage("Chinese"),
)

func (*CsvFormatAgent) Format

func (a *CsvFormatAgent) Format(rail flow.Rail, srcPath string, dstPath string) error

Format reads the CSV file at srcPath, runs the formatting agent, and writes the resulting plain-text output to dstPath.

The agent reads /input/data.csv from the virtual backend, transforms the content into RAG-optimised paragraphs, and writes the result to /output/context.txt. The output file is registered as an artifact and then written to dstPath.

Returns an error if the file cannot be read, the agent fails, or no artifact is produced.

type CsvFormatOption

type CsvFormatOption func(o *csvFormatConfig)

CsvFormatOption configures a CsvFormatAgent.

func WithCsvFormatLanguage

func WithCsvFormatLanguage(lang string) CsvFormatOption

WithCsvFormatLanguage sets the language for agent responses and selects the matching task prompt. Supported values: "Chinese" (default), any other value selects the English prompt.

func WithCsvFormatMaxRunSteps

func WithCsvFormatMaxRunSteps(n int) CsvFormatOption

WithCsvFormatMaxRunSteps sets the maximum number of graph steps before the agent terminates. If 0, no limit is applied.

type FactCheckAgent

type FactCheckAgent struct {
	// contains filtered or unexported fields
}

FactCheckAgent evaluates whether an LLM response is grounded in the retrieved knowledge context — detecting hallucinations and incorrect abstentions.

This is distinct from AccuracyCheckAgent (which compares against a reference answer) and RelevanceCheckAgent (which checks whether the question was answered). FactCheckAgent answers: "Did the LLM stay faithful to what the context actually says?"

The agent performs a single-shot call using agentloop.Agent with no tools.

Use NewFactCheckAgent to create an instance, then call FactCheckAgent.Check to score a response.

func NewFactCheckAgent

func NewFactCheckAgent(chatModel model.ToolCallingChatModel, opts ...FactCheckOption) (*FactCheckAgent, error)

NewFactCheckAgent creates a new FactCheckAgent backed by the given chat model.

Example:

agent, err := prebuilt.NewFactCheckAgent(chatModel)
result, err := agent.Check(rail, prebuilt.FactCheckInput{
    Question: "What is the capital of France?",
    Context:  "France is a country in Western Europe. Its capital is Paris.",
    Output:   "The capital of France is Paris.",
})

func (*FactCheckAgent) Check

func (a *FactCheckAgent) Check(rail flow.Rail, input FactCheckInput) (FactCheckResult, error)

Check evaluates the factual accuracy of an LLM response and returns a FactCheckResult.

The prompt template is substituted with the provided inputs using strutil.NamedSprintfv. The model response is parsed for "Score:" and "Reason:" fields. If either field is missing the call is retried up to [factCheckConfig.RetryCount] additional times.

type FactCheckInput

type FactCheckInput struct {
	// Question is the user question that prompted the LLM response.
	Question string

	// Context is the knowledge context retrieved for the question.
	Context string

	// Output is the LLM response to be evaluated.
	Output string
}

FactCheckInput holds all the inputs required for a single fact-checking call.

type FactCheckOption

type FactCheckOption func(o *factCheckConfig)

FactCheckOption configures a FactCheckAgent.

func WithFactCheckLanguage

func WithFactCheckLanguage(lang string) FactCheckOption

WithFactCheckLanguage sets the response language for the fact-check agent.

func WithFactCheckRetry

func WithFactCheckRetry(n int) FactCheckOption

WithFactCheckRetry sets the number of additional retry attempts when the model response is missing a Score or Reason field. The default is 2 (up to 3 total attempts).

func WithFactCheckSystemPrompt

func WithFactCheckSystemPrompt(prompt string) FactCheckOption

WithFactCheckSystemPrompt sets an optional system prompt for the fact-check agent.

type FactCheckResult

type FactCheckResult struct {
	// Score is the factual accuracy score on a 1-5 scale:
	//   1 = Major factual errors or hallucinations
	//   2 = Significant inaccuracies affecting core meaning
	//   3 = Partially correct but with key mistakes
	//   4 = Minor inaccuracies in non-critical details
	//   5 = Fully factually correct with no errors
	Score int

	// Reason is a brief textual justification for the score.
	Reason string
}

FactCheckResult holds the numeric score and textual reason returned by the agent.

type RelevanceCheckAgent

type RelevanceCheckAgent struct {
	// contains filtered or unexported fields
}

RelevanceCheckAgent evaluates how relevant an LLM response is to the user question, knowledge context, and optional reference answer.

The agent performs a single-shot call using agentloop.Agent with no tools.

Use NewRelevanceCheckAgent to create an instance, then call RelevanceCheckAgent.Check to score a response.

func NewRelevanceCheckAgent

func NewRelevanceCheckAgent(chatModel model.ToolCallingChatModel, opts ...RelevanceCheckOption) (*RelevanceCheckAgent, error)

NewRelevanceCheckAgent creates a new RelevanceCheckAgent backed by the given chat model.

Example:

agent, err := prebuilt.NewRelevanceCheckAgent(chatModel)
result, err := agent.Check(rail, prebuilt.RelevanceCheckInput{
    Question: "What is the capital of France?",
    Context:  "France is a country in Western Europe. Its capital is Paris.",
    Output:   "The capital of France is Paris.",
})

func (*RelevanceCheckAgent) Check

Check evaluates the relevance of an LLM response and returns a RelevanceCheckResult.

The prompt template is substituted with the provided inputs using strutil.NamedSprintfv. The model response is parsed for "Score:" and "Reason:" fields. If either field is missing the call is retried up to [relevanceCheckConfig.RetryCount] additional times.

type RelevanceCheckInput

type RelevanceCheckInput struct {
	// Question is the user question that prompted the LLM response.
	Question string

	// Context is the knowledge context retrieved for the question.
	Context string

	// Output is the LLM response to be evaluated.
	Output string
}

RelevanceCheckInput holds all the inputs required for a single relevance-check call.

type RelevanceCheckOption

type RelevanceCheckOption func(o *relevanceCheckConfig)

RelevanceCheckOption configures a RelevanceCheckAgent.

func WithRelevanceCheckLanguage

func WithRelevanceCheckLanguage(lang string) RelevanceCheckOption

WithRelevanceCheckLanguage sets the response language for the relevance-check agent.

func WithRelevanceCheckRetry

func WithRelevanceCheckRetry(n int) RelevanceCheckOption

WithRelevanceCheckRetry sets the number of additional retry attempts when the model response is missing a Score or Reason field. The default is 2 (up to 3 total attempts).

func WithRelevanceCheckSystemPrompt

func WithRelevanceCheckSystemPrompt(prompt string) RelevanceCheckOption

WithRelevanceCheckSystemPrompt sets an optional system prompt for the relevance-check agent.

type RelevanceCheckResult

type RelevanceCheckResult struct {
	// Score is the relevance score on a 1-5 scale:
	//   1 = Completely off-topic — addresses a different subject than what was asked
	//   2 = Mostly irrelevant — on the right topic but misses the core ask or ignores available context
	//   3 = Somewhat relevant — on-topic but with noticeable gaps (e.g. correctly abstains when context has no info)
	//   4 = Mostly relevant with minor omissions or issues
	//   5 = Fully relevant — directly and completely answers the question
	Score int

	// Reason is a brief textual justification for the score.
	Reason string
}

RelevanceCheckResult holds the numeric relevance score and textual reason returned by the agent.

Jump to

Keyboard shortcuts

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