prebuilt

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 the factual accuracy of an LLM response against a knowledge context, a user question, and an optional reference answer.

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.",
    ReferenceAnswer: "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

	// ReferenceAnswer is the ground-truth answer used for comparison.
	// May be left empty if no reference is available; the agent will rely on Context alone.
	ReferenceAnswer 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

	// ReferenceAnswer is the ground-truth answer used for comparison.
	// May be left empty if no reference is available; the agent will rely on Context alone.
	ReferenceAnswer 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