29 lines
566 B
Go
29 lines
566 B
Go
package llm
|
|
|
|
import (
|
|
"fmt"
|
|
"knowledge-graph-backend/internal/config"
|
|
|
|
"github.com/tmc/langchaingo/llms"
|
|
"github.com/tmc/langchaingo/llms/openai"
|
|
)
|
|
|
|
type Client struct {
|
|
llm llms.Model
|
|
config config.LLMConfig
|
|
}
|
|
|
|
// 创建 LLM 实例
|
|
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 {
|
|
return nil, fmt.Errorf("failed to create llm client: %w", err)
|
|
}
|
|
|
|
return &Client{config: cfg, llm: model}, nil
|
|
} |