ai

package
v0.47.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SystemPromptLegal is the system prompt for legal AI assistant
	SystemPromptLegal = `` /* 258-byte string literal not displayed */

	// GenerateContractPromptTemplate is the template for generating contracts
	GenerateContractPromptTemplate = `` /* 398-byte string literal not displayed */

	// ReviewContractPromptTemplate is the template for reviewing contracts
	ReviewContractPromptTemplate = `` /* 516-byte string literal not displayed */

)

Variables

This section is empty.

Functions

func BuildContractPrompt

func BuildContractPrompt(req GenerateContractRequest, context string) string

BuildContractPrompt builds the full prompt for contract generation

func BuildRAGContext

func BuildRAGContext(contracts []SimilarContract) string

BuildRAGContext builds a context string from similar contracts

func BuildReviewPrompt

func BuildReviewPrompt(contractText string) string

BuildReviewPrompt builds the full prompt for contract review

func DecryptAPIKey

func DecryptAPIKey(encrypted string) (string, error)

DecryptAPIKey decrypts an API key using AES-256-GCM

func EncryptAPIKey

func EncryptAPIKey(apiKey string) (string, error)

EncryptAPIKey encrypts an API key using AES-256-GCM

func ExtractTextFromPDF

func ExtractTextFromPDF(r io.Reader) (string, error)

ExtractTextFromPDF extracts plain text from PDF bytes

func SetEncryptionKey

func SetEncryptionKey(key []byte)

SetEncryptionKey sets the key used for AES encryption Key must be 16, 24, or 32 bytes for AES-128, AES-192, or AES-256

func ValidateStartupConfig

func ValidateStartupConfig(db *sql.DB, encryptionKey string) error

ValidateStartupConfig checks AI configuration on startup. Returns nil if OK, or an error warning if misconfigured.

Types

type ContractRetriever

type ContractRetriever struct {
	DB *sql.DB
}

ContractRetriever handles retrieving similar contracts from SQLite

func NewContractRetriever

func NewContractRetriever(db *sql.DB) *ContractRetriever

NewContractRetriever creates a new contract retriever

func (*ContractRetriever) GetSimilarContracts

func (r *ContractRetriever) GetSimilarContracts(companyID int, contractType string, clientID, supplierID int, limit int) ([]SimilarContract, error)

GetSimilarContracts retrieves similar contracts based on type and counterpart, scoped to a company

type GenerateContractRequest

type GenerateContractRequest struct {
	ContractType string  `json:"contract_type"`
	Amount       float64 `json:"amount"`
	StartDate    string  `json:"start_date"`
	EndDate      string  `json:"end_date"`
	ClientID     int     `json:"client_id"`
	SupplierID   int     `json:"supplier_id"`
	Description  string  `json:"description,omitempty"`
	Context      string  `json:"context,omitempty"`
}

GenerateContractRequest is the request body for generating a contract with AI

type GenerateResponse

type GenerateResponse struct {
	Text  string `json:"text"`
	Error string `json:"error,omitempty"`
}

GenerateResponse is the response for AI generation requests

type LLMClient

type LLMClient struct {
	Provider   LLMProvider
	APIKey     string
	Model      string
	Endpoint   string
	HTTPClient *http.Client
}

LLMClient handles communication with LLM providers

func NewLLMClient

func NewLLMClient(provider LLMProvider, apiKey, model, endpoint string) *LLMClient

NewLLMClient creates a new LLM client

func (*LLMClient) Generate

func (c *LLMClient) Generate(ctx context.Context, prompt string, context string) (string, error)

Generate sends a prompt to the LLM and returns the generated text

type LLMProvider

type LLMProvider string

LLM Provider constants

const (
	ProviderOpenAI     LLMProvider = "openai"
	ProviderGroq       LLMProvider = "groq"
	ProviderAnthropic  LLMProvider = "anthropic"
	ProviderOpenRouter LLMProvider = "openrouter"
	ProviderCustom     LLMProvider = "custom"
)

type RateLimiter

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

RateLimiter enforces daily rate limits per company using shared DB storage.

func NewRateLimiter

func NewRateLimiter(db *sql.DB) *RateLimiter

NewRateLimiter creates a RateLimiter backed by the provided DB.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(companyID int) (remaining int, ok bool)

Allow checks if companyID has remaining quota for today. It increments the counter if allowed. Returns (remaining, ok).

func (*RateLimiter) SetLimit

func (rl *RateLimiter) SetLimit(limit int)

SetLimit allows overriding the default daily limit (not persisted).

type ReviewContractRequest

type ReviewContractRequest struct {
	ContractID  int    `json:"contract_id"`
	Text        string `json:"text"`
	DocumentURL string `json:"document_url,omitempty"`
}

ReviewContractRequest is the request body for reviewing a contract with AI

type ReviewResponse

type ReviewResponse struct {
	Summary        string     `json:"summary"`
	Risks          []RiskItem `json:"risks"`
	MissingClauses []string   `json:"missing_clauses"`
	OverallRisk    string     `json:"overall_risk"`
}

ReviewResponse is the structured response for contract review

func ParseReviewResponse

func ParseReviewResponse(raw string) (ReviewResponse, error)

ParseReviewResponse parses LLM output into structured ReviewResponse. It tolerates markdown code fences and common formatting issues.

type RiskItem

type RiskItem struct {
	Clause     string `json:"clause"`
	Risk       string `json:"risk"` // "high", "medium", "low"
	Suggestion string `json:"suggestion"`
}

RiskItem represents a specific risk found in a contract

type SimilarContract

type SimilarContract struct {
	ID      int
	Title   string
	Type    string
	Content string // Extracted text from document
}

SimilarContract represents a contract retrieved for RAG

Jump to

Keyboard shortcuts

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