ai

package
v0.5.9-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingAuthToken = errors.New("auth token is required")
	ErrInvalidQuery     = errors.New("query is invalid")
)
View Source
var ServicePromptMap = map[PromptType]string{
	DefaultType: "You are a helpful assistant.",

	Text2sqlType: `You are an AI specialized in writing SQL queries.
    Please convert the text: "%s" to sql.
    If the text is not accurate enough, please output "Error".
    The output tokens only need to give the SQL first, the other thought process please do not give.
    The SQL should begin with "select * from" and end with ";".

    1. The database now only supports one table resources.

    Table resources, columns = [cluster, apiVersion, kind,
    namespace, name, creationTimestamp, deletionTimestamp, ownerReferences,
    resourceVersion, labels.[key], annotations.[key], content]

    2. find the schema_links for generating SQL queries for each question based on the database schema.
       If there are Chinese expressions, please translate them into English.

    Follow are some examples.

    Q: find the kind which is not equal to pod
    A: Let's think step by step. In the question "find the kind column which is not equal to pod", we are asked:
    "find the kind" so we need column = [kind].
    Based on the columns, the set of possible cell values are = [pod].
    So the Schema_links are:
    Schema_links: [kind, pod]

    Q: find the kind Deployment which created before January 1, 2024, at 18:00:00
    A: Let's think step by step. In the question "find the kind Deployment which created before January 1, 2024, at 18:00:00", we are asked:
    "find the kind Deployment" so we need column = [kind].
    "created before" so we need column = [creationTimestamp].
    Based on the columns, the set of possible cell values are = [Deployment, 2024-01-01T18:00:00Z].
    So the Schema_links are:
    Schema_links: [[kind, Deployment], [creationTimestamp, 2024-01-01T18:00:00Z]]

    Q: find the kind Namespace which which created
    A: Let's think step by step. In the question "find the kind", we are asked:
    "find the kind Namespace " so we need column = [kind]
    "created before" so we need column = [creationTimestamp]
    Based on the columns, the set of possible cell values are = [kind, creationTimestamp].
    There is no creationTimestamp corresponding cell values, so the text is not accurate enough.
    So the Schema_links are:
    Schema_links: error

    3. Use the the schema links to generate the SQL queries for each of the questions.

    Follow are some examples.

    Q: find the kind which is not equal to pod
    Schema_links: [kind, pod]
    SQL: select * from resources where kind!='Pod';

    Q: find the kind Deployment which created before January 1, 2024, at 18:00:00
    Schema_links: [[kind, Deployment], [creationTimestamp, 2024-01-01T18:00:00Z]]
    SQL: select * from resources where kind='Deployment' and creationTimestamp < '2024-01-01T18:00:00Z';

    Q: find the namespace which does not contain banan
    Schema_links: [namespace, banan]
    SQL: select * from resources where namespace notlike 'banan_';

    Q: find the kind Namespace which which created
    Schema_links: error
    Error;

    Please convert the text to sql.`,

	SQLFixType: `You are an AI specialized in writing SQL queries.
    Please convert the text: "%s" to sql.
    The SQL should begin with "select * from".

    The database now only supports one table resources.

    Table resources, columns = [cluster, apiVersion, kind,
    namespace, name, creationTimestamp, deletionTimestamp, ownerReferences,
    resourceVersion, labels.[key], annotations.[key], content]

    After we executed SQL: "%s",  we observed the following error "%s".
    Please fix the SQL.`,

	LogDiagnosisType: `You are a Kubernetes log analysis expert. Your task is to analyze pod logs and provide a diagnosis in %s.

Logs to analyze:
%s

Please provide:
1. A brief summary of any errors or issues found
2. The potential root cause of the problems
3. Recommended solutions or next steps
4. Any relevant Kubernetes best practices that could help prevent similar issues

Format your response in markdown with clear sections.`,

	EventDiagnosisType: `You are a Kubernetes expert specialized in diagnosing system and application issues through event analysis.
Please analyze the following Kubernetes events and provide your diagnosis in %s.
Focus on:
1. Identify any issues or potential problems
2. Explain the root causes
3. Suggest specific solutions or preventive measures
4. Prioritize critical issues that need immediate attention

Events:
%s

Please structure your response with clear sections:
1. Summary of Issues
2. Detailed Analysis
3. Recommendations
4. Next Steps

Be specific and include technical details when relevant.`,
}

Functions

func CheckAIManager

func CheckAIManager(aiMgr *AIManager) error

CheckAIManager check if the AI manager is created

func ExtractSelectSQL

func ExtractSelectSQL(sql string) string

ExtractSelectSQL extracts SQL statements that start with "SELECT * FROM"

func IsInvalidQuery

func IsInvalidQuery(sql string) bool

IsInvalidQuery check if the query is invalid

Types

type AIManager

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

func NewAIManager

func NewAIManager(c registry.ExtraConfig) (*AIManager, error)

NewAIManager returns a new AIManager object

func (*AIManager) ConvertTextToSQL

func (a *AIManager) ConvertTextToSQL(query string) (string, error)

ConvertTextToSQL converts natural language text to an SQL query

func (*AIManager) DiagnoseEvents

func (a *AIManager) DiagnoseEvents(ctx context.Context, events []Event, language string, eventChan chan<- *DiagnosisEvent) error

DiagnoseEvents analyzes Kubernetes events using LLM and returns diagnostic information through a streaming channel

func (*AIManager) DiagnoseLogs

func (a *AIManager) DiagnoseLogs(ctx context.Context, logs []string, language string, eventChan chan<- *DiagnosisEvent) error

DiagnoseLogs analyzes pod logs using LLM and returns diagnostic information through a streaming channel

func (*AIManager) DiagnoseLogsHandler

func (a *AIManager) DiagnoseLogsHandler(ctx context.Context, logs []string, language string, w http.ResponseWriter)

DiagnoseLogsHandler handles the HTTP streaming response for log diagnosis

func (*AIManager) FixSQL

func (a *AIManager) FixSQL(sql string, query string, sqlErr string) (string, error)

FixSQL fix the error SQL

type DiagnosisEvent

type DiagnosisEvent struct {
	Type    string `json:"type"`    // Event type: start/chunk/error/complete
	Content string `json:"content"` // Event content
}

DiagnosisEvent represents a diagnosis streaming event

type Event

type Event struct {
	Type           string `json:"type"`
	Reason         string `json:"reason"`
	Message        string `json:"message"`
	Count          int32  `json:"count"`
	LastTimestamp  string `json:"lastTimestamp"`
	FirstTimestamp string `json:"firstTimestamp"`
}

Event represents a Kubernetes event for diagnosis

type PromptType

type PromptType string

PromptType represents the type of prompt to be used

const (
	// DefaultType represents the default prompt type
	DefaultType PromptType = "default"
	// Text2sqlType represents the prompt type for text to SQL conversion
	Text2sqlType PromptType = "text2sql"
	// SQLFixType represents the prompt type for SQL fix
	SQLFixType PromptType = "sqlfix"
	// LogDiagnosisType represents the prompt type for log diagnosis
	LogDiagnosisType PromptType = "log_diagnosis"
	// EventDiagnosisType represents the prompt type for event diagnosis
	EventDiagnosisType PromptType = "event_diagnosis"
)

Jump to

Keyboard shortcuts

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