llm

package
v0.0.0-...-fbd5fee Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptySQL       = &SQLError{Message: "SQL query is empty"}
	ErrNotSelectQuery = &SQLError{Message: "query must be a SELECT statement"}
)

Errors

Functions

func BuildPrompt

func BuildPrompt(schema, question string) string

BuildPrompt builds a complete prompt with schema and question

func CleanSQL

func CleanSQL(sql string) string

CleanSQL cleans and normalizes SQL query

func ExtractSQL

func ExtractSQL(response string) string

ExtractSQL extracts SQL query from LLM response

func FewShotExamples

func FewShotExamples() []struct {
	Question string
	SQL      string
}

FewShotExamples returns example question-SQL pairs

func FormatSchema

func FormatSchema(tables []Table) string

FormatSchema formats database schema for LLM prompt

func ParseResponse

func ParseResponse(response string) (string, error)

ParseResponse parses LLM response and extracts SQL

func SystemPrompt

func SystemPrompt() string

SystemPrompt returns the system prompt for Text2SQL

func ValidateSQL

func ValidateSQL(sql string) error

ValidateSQL performs basic validation on extracted SQL

Types

type ChatMessage

type ChatMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

ChatMessage represents a chat message

func BuildPromptWithContext

func BuildPromptWithContext(schema, question string, history []ChatMessage) []ChatMessage

BuildPromptWithContext builds a prompt with conversation history

func BuildPromptWithExamples

func BuildPromptWithExamples(schema, question string) []ChatMessage

BuildPromptWithExamples builds a prompt with few-shot examples

type ChatRequest

type ChatRequest struct {
	Messages    []ChatMessage
	Stream      bool
	Temperature float32
	MaxTokens   int
}

ChatRequest represents a chat completion request

type ChatResponse

type ChatResponse struct {
	Content      string
	FinishReason string
	Usage        Usage
}

ChatResponse represents a chat completion response

type Client

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

Client wraps OpenAI-compatible API client

func NewClient

func NewClient(cfg Config) *Client

NewClient creates a new LLM client

func (*Client) Chat

func (c *Client) Chat(ctx context.Context, req ChatRequest) (*ChatResponse, error)

Chat sends a chat completion request

func (*Client) ChatStream

func (c *Client) ChatStream(ctx context.Context, req ChatRequest, callback StreamCallback) error

ChatStream sends a streaming chat completion request

func (*Client) ChatWithRetry

func (c *Client) ChatWithRetry(ctx context.Context, req ChatRequest, maxRetries int) (*ChatResponse, error)

ChatWithRetry sends a chat request with exponential backoff retry

type Column

type Column struct {
	Name       string
	Type       string
	Nullable   bool
	PrimaryKey bool
	ForeignKey string
}

Column represents a table column (matches db.Column)

type Config

type Config struct {
	BaseURL     string
	APIKey      string
	Model       string
	Temperature float32
	MaxTokens   int
}

Config holds LLM client configuration

type GenerateSQLRequest

type GenerateSQLRequest struct {
	Question string
	Schema   string
	History  []ChatMessage
}

GenerateSQLRequest represents a SQL generation request

type GenerateSQLResponse

type GenerateSQLResponse struct {
	SQL          string
	RawResponse  string
	TokensUsed   int
	FinishReason string
}

GenerateSQLResponse represents a SQL generation response

type SQLError

type SQLError struct {
	Message string
}

SQLError represents a SQL parsing error

func (*SQLError) Error

func (e *SQLError) Error() string

type Service

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

Service provides high-level LLM operations

func NewService

func NewService(client *Client) *Service

NewService creates a new LLM service

func (*Service) GenerateSQL

func (s *Service) GenerateSQL(ctx context.Context, req GenerateSQLRequest) (*GenerateSQLResponse, error)

GenerateSQL generates SQL from natural language question

func (*Service) GenerateSQLStream

func (s *Service) GenerateSQLStream(ctx context.Context, req GenerateSQLRequest, callback func(chunk string) error) (string, error)

GenerateSQLStream generates SQL with streaming

type StreamCallback

type StreamCallback func(chunk string) error

StreamCallback is called for each chunk in streaming mode

type Table

type Table struct {
	Name    string
	Columns []Column
}

Table represents a database table (matches db.Table)

type Usage

type Usage struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
}

Usage represents token usage

Jump to

Keyboard shortcuts

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