Refactor: modularize and streamline LLM providers and utility functions
- Migrate `compress_image.go` to `internal/imageutil` for better encapsulation. - Reorganize LLM provider implementations into distinct packages (`google`, `openai`, and `anthropic`). - Replace `go_llm` package name with `llm`. - Refactor internal APIs for improved clarity, including renaming `anthropic` to `anthropicImpl` and `google` to `googleImpl`. - Add helper methods and restructure message handling for better separation of concerns.
This commit is contained in:
16
google.go
16
google.go
@@ -1,4 +1,4 @@
|
||||
package go_llm
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -11,22 +11,24 @@ import (
|
||||
"google.golang.org/genai"
|
||||
)
|
||||
|
||||
type google struct {
|
||||
type googleImpl struct {
|
||||
key string
|
||||
model string
|
||||
}
|
||||
|
||||
func (g google) ModelVersion(modelVersion string) (ChatCompletion, error) {
|
||||
var _ LLM = googleImpl{}
|
||||
|
||||
func (g googleImpl) ModelVersion(modelVersion string) (ChatCompletion, error) {
|
||||
g.model = modelVersion
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
||||
func (g google) requestToContents(in Request) ([]*genai.Content, *genai.GenerateContentConfig) {
|
||||
func (g googleImpl) requestToContents(in Request) ([]*genai.Content, *genai.GenerateContentConfig) {
|
||||
var contents []*genai.Content
|
||||
var cfg genai.GenerateContentConfig
|
||||
|
||||
for _, tool := range in.Toolbox.functions {
|
||||
for _, tool := range in.Toolbox.Functions() {
|
||||
cfg.Tools = append(cfg.Tools, &genai.Tool{
|
||||
FunctionDeclarations: []*genai.FunctionDeclaration{
|
||||
{
|
||||
@@ -101,7 +103,7 @@ func (g google) requestToContents(in Request) ([]*genai.Content, *genai.Generate
|
||||
return contents, &cfg
|
||||
}
|
||||
|
||||
func (g google) responseToLLMResponse(in *genai.GenerateContentResponse) (Response, error) {
|
||||
func (g googleImpl) responseToLLMResponse(in *genai.GenerateContentResponse) (Response, error) {
|
||||
res := Response{}
|
||||
|
||||
for _, c := range in.Candidates {
|
||||
@@ -142,7 +144,7 @@ func (g google) responseToLLMResponse(in *genai.GenerateContentResponse) (Respon
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (g google) ChatComplete(ctx context.Context, req Request) (Response, error) {
|
||||
func (g googleImpl) ChatComplete(ctx context.Context, req Request) (Response, error) {
|
||||
cl, err := genai.NewClient(ctx, &genai.ClientConfig{
|
||||
APIKey: g.key,
|
||||
Backend: genai.BackendGeminiAPI,
|
||||
|
||||
Reference in New Issue
Block a user