-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
llm chat support create assistant and thread
- Loading branch information
Showing
24 changed files
with
626 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
|
||
-- name: InserUser :exec | ||
-- name: InserUser :one | ||
INSERT INTO users (username, telegram, activate_assistant_id, activate_thread_id) | ||
VALUES ($1, $2, $3, $4); | ||
VALUES ($1, $2, $3, $4) | ||
RETURNING *; | ||
|
||
-- name: GetTelegramUser :one | ||
SELECT * FROM users WHERE telegram = $1; | ||
|
||
-- name: UpdateTelegramUser :exec | ||
UPDATE users SET activate_assistant_id = $1, activate_thread_id = $2 WHERE telegram = $3; | ||
-- name: UpdateTelegramUser :one | ||
UPDATE users SET activate_assistant_id = $1, activate_thread_id = $2 WHERE telegram = $3 | ||
RETURNING *; | ||
|
||
-- name: DeleteTelegramUser :exec | ||
DELETE FROM users WHERE telegram = $1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package assistants | ||
|
||
import ( | ||
"vibrain/internal/pkg/llms" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type AssistantMetaData struct{} | ||
|
||
type Assistant struct { | ||
Id uuid.UUID `json:"uuid"` | ||
UserId uuid.UUID `json:"user_id"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
SystemPrompt string `json:"system_prompt"` | ||
Model string `json:"model"` | ||
MetaData AssistantMetaData `json:"metadata"` | ||
} | ||
|
||
type AssistantOption func(*Assistant) | ||
|
||
func NewAssistant(userId uuid.UUID, opts ...AssistantOption) *Assistant { | ||
a := &Assistant{ | ||
UserId: userId, | ||
Model: llms.OpenAIGPT4oMini, | ||
Name: "Assistant" + uuid.New().String(), | ||
Description: "I am an AI assistant. I can help you with a variety of tasks.", | ||
SystemPrompt: "You are a helpful AI assistant.", | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(a) | ||
} | ||
return a | ||
} | ||
|
||
func WithAssistantName(name string) AssistantOption { | ||
return func(a *Assistant) { | ||
a.Name = name | ||
} | ||
} | ||
|
||
func WithAssistantDescription(description string) AssistantOption { | ||
return func(a *Assistant) { | ||
a.Description = description | ||
} | ||
} | ||
|
||
func WithAssistantSystemPrompt(systemPrompt string) AssistantOption { | ||
return func(a *Assistant) { | ||
a.SystemPrompt = systemPrompt | ||
} | ||
} | ||
|
||
func WithAssistantModel(model string) AssistantOption { | ||
return func(a *Assistant) { | ||
a.Model = model | ||
} | ||
} | ||
|
||
func WithAssistantMetaData(metaData AssistantMetaData) AssistantOption { | ||
return func(a *Assistant) { | ||
a.MetaData = metaData | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package assistants | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"vibrain/internal/pkg/db" | ||
|
||
"github.com/google/uuid" | ||
"github.com/jackc/pgx/v5/pgtype" | ||
) | ||
|
||
type Repository interface { | ||
CreateAssistant(ctx context.Context, assistant *Assistant) error | ||
GetAssistant(ctx context.Context, id uuid.UUID) (*Assistant, error) | ||
|
||
CreateThread(ctx context.Context, thread *Thread) error | ||
GetThread(ctx context.Context, id uuid.UUID) (*Thread, error) | ||
} | ||
|
||
type repository struct { | ||
db *db.Queries | ||
} | ||
|
||
func NewRepository(pool *db.Pool) Repository { | ||
return &repository{db: db.New(pool)} | ||
} | ||
|
||
func (r *repository) CreateAssistant(ctx context.Context, assistant *Assistant) error { | ||
ast, err := r.db.CreateAssistant(ctx, db.CreateAssistantParams{ | ||
UserID: pgtype.UUID{Bytes: assistant.UserId, Valid: true}, | ||
Name: assistant.Name, | ||
Description: pgtype.Text{String: assistant.Description, Valid: assistant.Description != ""}, | ||
SystemPrompt: pgtype.Text{String: assistant.SystemPrompt, Valid: assistant.SystemPrompt != ""}, | ||
Model: assistant.Model, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create assistant: %w", err) | ||
} | ||
|
||
assistant.Id = ast.Uuid | ||
return nil | ||
} | ||
|
||
func (r *repository) GetAssistant(ctx context.Context, id uuid.UUID) (*Assistant, error) { | ||
ast, err := r.db.GetAssistant(ctx, id) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get assistant: %w", err) | ||
} | ||
|
||
assistant := &Assistant{ | ||
Id: ast.Uuid, | ||
UserId: ast.UserID.Bytes, | ||
Name: ast.Name, | ||
Description: ast.Description.String, | ||
SystemPrompt: ast.SystemPrompt.String, | ||
Model: ast.Model, | ||
} | ||
return assistant, nil | ||
} | ||
|
||
func (r *repository) CreateThread(ctx context.Context, thread *Thread) error { | ||
th, err := r.db.CreateAssistantThread(ctx, db.CreateAssistantThreadParams{ | ||
UserID: pgtype.UUID{Bytes: thread.UserId, Valid: true}, | ||
AssistantID: pgtype.UUID{Bytes: thread.AssistantId, Valid: true}, | ||
Name: thread.Name, | ||
Description: pgtype.Text{String: thread.Description, Valid: thread.Description != ""}, | ||
Model: thread.Model, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create thread: %w", err) | ||
} | ||
|
||
thread.Id = th.Uuid | ||
|
||
return nil | ||
} | ||
|
||
func (r *repository) GetThread(ctx context.Context, id uuid.UUID) (*Thread, error) { | ||
th, err := r.db.GetAssistantThread(ctx, id) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get thread: %w", err) | ||
} | ||
thread := &Thread{ | ||
Id: th.Uuid, | ||
UserId: th.UserID.Bytes, | ||
AssistantId: th.AssistantID.Bytes, | ||
Name: th.Name, | ||
Description: th.Description.String, | ||
Model: th.Model, | ||
} | ||
return thread, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package assistants | ||
|
||
import ( | ||
"context" | ||
"vibrain/internal/pkg/db" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type Service struct { | ||
db Repository | ||
} | ||
|
||
func NewService(db *db.Pool) (*Service, error) { | ||
s := &Service{ | ||
db: NewRepository(db), | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
func (s *Service) CreateAssistant(ctx context.Context, assistant *Assistant) error { | ||
return s.db.CreateAssistant(ctx, assistant) | ||
} | ||
|
||
func (s *Service) GetAssistant(ctx context.Context, id string) (*Assistant, error) { | ||
return s.db.GetAssistant(ctx, uuid.MustParse(id)) | ||
} | ||
|
||
func (s *Service) CreateThread(ctx context.Context, thread *Thread) error { | ||
return s.db.CreateThread(ctx, thread) | ||
} | ||
|
||
func (s *Service) GetThread(ctx context.Context, id string) (*Thread, error) { | ||
return s.db.GetThread(ctx, uuid.MustParse(id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package assistants | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
) | ||
|
||
type ThreadMetaData struct{} | ||
|
||
type Thread struct { | ||
Id uuid.UUID `json:"uuid"` | ||
UserId uuid.UUID `json:"user_id"` | ||
AssistantId uuid.UUID `json:"assistant_id"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Model string `json:"model"` | ||
MetaData ThreadMetaData `json:"metadata"` | ||
} | ||
|
||
type ThreadOption func(*Thread) | ||
|
||
func NewThread(userId uuid.UUID, assistant Assistant, opts ...ThreadOption) *Thread { | ||
t := &Thread{ | ||
UserId: userId, | ||
AssistantId: assistant.Id, | ||
Model: assistant.Model, | ||
Name: "Thread" + uuid.New().String(), | ||
Description: "I am a conversation thread.", | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(t) | ||
} | ||
return t | ||
} | ||
|
||
func WithThreadName(name string) ThreadOption { | ||
return func(t *Thread) { | ||
t.Name = name | ||
} | ||
} | ||
|
||
func WithThreadDescription(description string) ThreadOption { | ||
return func(t *Thread) { | ||
t.Description = description | ||
} | ||
} | ||
|
||
func WithThreadModel(model string) ThreadOption { | ||
return func(t *Thread) { | ||
t.Model = model | ||
} | ||
} | ||
|
||
func WithThreadMetaData(metaData ThreadMetaData) ThreadOption { | ||
return func(t *Thread) { | ||
t.MetaData = metaData | ||
} | ||
} | ||
|
Oops, something went wrong.