gestion des providers
This commit is contained in:
1 parent
357bf9b41c
commit
4d72952b2c
46 files changed
+995
-150
No files matched your search
@@ -12,6 +12,7 @@ type Database interface {
|
||||
SettingsRepository() domain.SettingsRepository
|
||||
UserRepository() domain.UserRepository
|
||||
FileRepository() domain.FileRepository
|
||||
ProviderRepository() domain.ProviderRepository
|
||||
Migrate() error
|
||||
Close()
|
||||
}
|
||||
|
||||
@@ -67,3 +67,7 @@ func (db *TursoDB) SettingsRepository() domain.SettingsRepository {
|
||||
func (db *TursoDB) FileRepository() domain.FileRepository {
|
||||
return NewTursoFileRepository(db.DB)
|
||||
}
|
||||
|
||||
func (db *TursoDB) ProviderRepository() domain.ProviderRepository {
|
||||
return NewTursoModelRepository(db.DB)
|
||||
}
|
||||
@@ -11,4 +11,3 @@ drop table tools;
|
||||
drop table tasks;
|
||||
drop table history;
|
||||
drop table files;
|
||||
drop table share;
|
||||
@@ -1,25 +1,16 @@
|
||||
create table providers (
|
||||
id text not null primary key,
|
||||
team_id text not null,
|
||||
type text not null,
|
||||
name text not null,
|
||||
key text,
|
||||
url text,
|
||||
models text,
|
||||
_date_created numeric not null default current_timestamp,
|
||||
_date_updated numeric,
|
||||
_version text not null
|
||||
);
|
||||
|
||||
create table models (
|
||||
id text not null primary key,
|
||||
team_id text not null,
|
||||
provider_id text not null,
|
||||
name text not null default '',
|
||||
modelname text not null default '',
|
||||
configuration text not null default '{}',
|
||||
_date_created numeric not null default current_timestamp,
|
||||
_date_updated numeric,
|
||||
_version text not null
|
||||
);
|
||||
|
||||
create table settings (
|
||||
id text not null primary key,
|
||||
@@ -150,14 +141,3 @@ create table files (
|
||||
_date_updated numeric ,
|
||||
_version text not null
|
||||
);
|
||||
|
||||
create table share (
|
||||
id text not null primary key,
|
||||
object_id text not null,
|
||||
object_type text not null,
|
||||
team_id text not null,
|
||||
enabled numeric not null default 0,
|
||||
_date_created numeric not null default current_timestamp,
|
||||
_date_updated numeric,
|
||||
_version text not null
|
||||
);
|
||||
@@ -0,0 +1,61 @@
|
||||
package turso
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
"gitea.trankilou.fr/fabien/lasebuche"
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
)
|
||||
|
||||
type TursoModelRepository struct {
|
||||
db *sql.DB
|
||||
providerTable lasebuche.Table[domain.Provider]
|
||||
}
|
||||
|
||||
func NewTursoModelRepository(db *sql.DB) *TursoModelRepository {
|
||||
dialect := lasebuche.NewSqliteDialect()
|
||||
providerTable, err := lasebuche.NewTable[domain.Provider](db, dialect, "providers")
|
||||
if err != nil {
|
||||
log.Fatalf("error creating lasebuche team table")
|
||||
}
|
||||
|
||||
return &TursoModelRepository{
|
||||
db: db,
|
||||
providerTable: providerTable,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *TursoModelRepository) ListProviders(userID string, teamID string) ([]*domain.Provider, error) {
|
||||
return r.providerTable.SelectWhere(
|
||||
"team_id=$1 and team_id in (select team_id from user_teams where user_id=$2)",
|
||||
teamID,
|
||||
userID,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *TursoModelRepository) GetProvider(userID string, teamID string, id string) (*domain.Provider, error) {
|
||||
return r.providerTable.SelectOne(
|
||||
"id=$1 and team_id=$2 and team_id in (select team_id from user_teams where user_id=$3)",
|
||||
id,
|
||||
teamID,
|
||||
userID,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *TursoModelRepository) CreateProvider(userID string, provider *domain.Provider) (*domain.Provider, error) {
|
||||
return r.providerTable.Insert(provider)
|
||||
}
|
||||
|
||||
func (r *TursoModelRepository) UpdateProvider(userID string, provider *domain.Provider) (*domain.Provider, error) {
|
||||
return r.providerTable.Update(provider)
|
||||
}
|
||||
|
||||
func (r *TursoModelRepository) DeleteProvider(userID string, teamID string, id string) error {
|
||||
return r.providerTable.DeleteWhere(
|
||||
"id=$1 and team_id=$2 and team_id in (select team_id from user_teams where user_id=$3)",
|
||||
id,
|
||||
teamID,
|
||||
userID,
|
||||
)
|
||||
}
|
||||
@@ -73,7 +73,7 @@ func (ur *TursoUserRepository) FindTeam(userid string, teamid string) (*domain.T
|
||||
}
|
||||
|
||||
func (ur *TursoUserRepository) ListTeams(userid string) ([]*domain.Team, error) {
|
||||
return ur.TeamTable.SelectWhere("user_id=$1", userid)
|
||||
return ur.TeamTable.SelectWhere("id in (select team_id from user_teams where user_id=$1)", userid)
|
||||
}
|
||||
|
||||
func (ur *TursoUserRepository) CreateTeam(userid string, team *domain.Team) (*domain.Team, error) {
|
||||
@@ -123,10 +123,6 @@ func (ur *TursoUserRepository) DeleteUserTeam(userid string, teamid string) erro
|
||||
return ur.UserTeamTable.Delete(teamid)
|
||||
}
|
||||
|
||||
func (ur *TursoUserRepository) PopulateUserWithTeams(user *domain.User) (*domain.UserWithTeams, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (ur *TursoUserRepository) ListUserAddresses(id string) ([]*domain.UserAddress, error) {
|
||||
return ur.AddressTable.SelectWhere("user_id=$1", id)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
anyllm "github.com/mozilla-ai/any-llm-go"
|
||||
"github.com/mozilla-ai/any-llm-go/providers/anthropic"
|
||||
"github.com/mozilla-ai/any-llm-go/providers/ollama"
|
||||
"github.com/mozilla-ai/any-llm-go/providers/openai"
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
)
|
||||
|
||||
var providerTypes = []domain.Item{
|
||||
{ID: "anthropic", Text: "Anthropic"},
|
||||
{ID: "openai", Text: "OpenAI"},
|
||||
{ID: "ollama", Text: "Ollama"},
|
||||
{ID: "openaicomp", Text: "OpenAI compatible"},
|
||||
{ID: "openrouter", Text: "Openrouter"},
|
||||
}
|
||||
|
||||
type AnyLLMEngine struct {
|
||||
}
|
||||
|
||||
func NewAnyLLMEngine() *AnyLLMEngine {
|
||||
return &AnyLLMEngine{}
|
||||
}
|
||||
|
||||
func (e *AnyLLMEngine) ListProviderTypes() []domain.Item {
|
||||
return providerTypes
|
||||
}
|
||||
|
||||
func providerFactory(provider *domain.Provider) (anyllm.Provider, error) {
|
||||
switch provider.Type {
|
||||
case "ollama":
|
||||
return ollama.New(
|
||||
anyllm.WithBaseURL(provider.URL),
|
||||
)
|
||||
case "openai":
|
||||
return openai.New(
|
||||
anyllm.WithAPIKey(provider.APIKey),
|
||||
)
|
||||
case "openaicomp":
|
||||
return openai.New(
|
||||
anyllm.WithBaseURL(provider.URL),
|
||||
anyllm.WithAPIKey(provider.APIKey),
|
||||
)
|
||||
case "anthropic":
|
||||
return anthropic.New(
|
||||
anyllm.WithAPIKey(provider.APIKey),
|
||||
)
|
||||
}
|
||||
return nil, fmt.Errorf("unknown provider type: %s", provider.Type)
|
||||
}
|
||||
|
||||
func (e *AnyLLMEngine) ListModelsFromProvider(ctx context.Context, provider *domain.Provider) ([]string, error) {
|
||||
|
||||
models := make([]string, 0)
|
||||
|
||||
prov, err := providerFactory(provider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if lister, ok := prov.(anyllm.ModelLister); ok {
|
||||
response, err := lister.ListModels(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, m := range response.Data {
|
||||
models = append(models, m.ID)
|
||||
}
|
||||
} else {
|
||||
models = append(models, "Default")
|
||||
}
|
||||
return models, nil
|
||||
}
|
||||
Reference in new issue
Block a user