2026-04-19 15:12:32 +08:00
|
|
|
package llm
|
|
|
|
|
|
|
|
|
|
import (
|
2026-04-19 15:58:41 +08:00
|
|
|
"context"
|
2026-04-19 15:12:32 +08:00
|
|
|
"fmt"
|
|
|
|
|
"knowledge-graph-backend/internal/config"
|
2026-04-19 15:58:41 +08:00
|
|
|
"log"
|
2026-04-19 15:12:32 +08:00
|
|
|
|
|
|
|
|
"github.com/tmc/langchaingo/llms"
|
|
|
|
|
"github.com/tmc/langchaingo/llms/openai"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
2026-04-20 15:34:40 +08:00
|
|
|
model llms.Model
|
2026-04-19 15:12:32 +08:00
|
|
|
config config.LLMConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewClient(cfg config.LLMConfig) (*Client, error) {
|
|
|
|
|
model, err := openai.New(
|
|
|
|
|
openai.WithToken(cfg.APIKey),
|
|
|
|
|
openai.WithBaseURL(cfg.BaseURL),
|
|
|
|
|
openai.WithModel(cfg.Model),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2026-04-20 15:34:40 +08:00
|
|
|
return nil, fmt.Errorf("failed to create llm client: %w", err)
|
|
|
|
|
}
|
2026-04-19 15:12:32 +08:00
|
|
|
|
2026-04-20 15:34:40 +08:00
|
|
|
return &Client{config: cfg, model: model}, nil
|
2026-04-19 15:58:41 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 15:34:40 +08:00
|
|
|
func (c *Client) Generate(ctx context.Context, messages []llms.MessageContent, opts ...llms.CallOption) (*llms.ContentResponse, error) {
|
|
|
|
|
resp, err := c.model.GenerateContent(ctx, messages, opts...)
|
2026-04-19 15:58:41 +08:00
|
|
|
if err != nil {
|
2026-04-20 15:34:40 +08:00
|
|
|
log.Printf("[ERROR] GenerateContent failed: %v", err)
|
|
|
|
|
return nil, fmt.Errorf("generate content error: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if resp == nil || len(resp.Choices) == 0 {
|
|
|
|
|
log.Printf("[WARN] Response is empty or has no choices")
|
|
|
|
|
return nil, fmt.Errorf("no content choices returned")
|
|
|
|
|
}
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
2026-04-19 15:58:41 +08:00
|
|
|
|
2026-04-20 15:34:40 +08:00
|
|
|
func (c *Client) Chat(ctx context.Context, messages []llms.MessageContent) (string, error) {
|
|
|
|
|
resp, err := c.Generate(ctx, messages)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
log.Printf("[INFO] Chat completed successfully, choice length: %d", len(resp.Choices[0].Content))
|
2026-04-20 14:23:41 +08:00
|
|
|
return resp.Choices[0].Content, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) ChatWithSystemPrompt(ctx context.Context, systemPrompt string, messages []llms.MessageContent) (string, error) {
|
2026-04-20 15:34:40 +08:00
|
|
|
allMessages := NewMessageBuilder().
|
|
|
|
|
WithSystemPrompt(systemPrompt).
|
|
|
|
|
WithMessages(messages).
|
|
|
|
|
Build()
|
2026-04-20 14:23:41 +08:00
|
|
|
|
2026-04-20 15:34:40 +08:00
|
|
|
resp, err := c.Generate(ctx, allMessages)
|
2026-04-20 14:23:41 +08:00
|
|
|
if err != nil {
|
2026-04-20 15:34:40 +08:00
|
|
|
return "", err
|
2026-04-20 14:23:41 +08:00
|
|
|
}
|
|
|
|
|
log.Printf("[INFO] Chat with system prompt completed successfully, choice length: %d", len(resp.Choices[0].Content))
|
2026-04-19 15:58:41 +08:00
|
|
|
return resp.Choices[0].Content, nil
|
2026-04-20 15:34:40 +08:00
|
|
|
}
|