-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Merge code of gpu from dev #7982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| package v2 | ||
|
|
||
| import ( | ||
| "github.com/1Panel-dev/1Panel/agent/app/api/v2/helper" | ||
| "github.com/1Panel-dev/1Panel/agent/app/dto" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu/common" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/ai_tools/xpu" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // @Tags AI | ||
| // @Summary Create Ollama model | ||
| // @Accept json | ||
| // @Param request body dto.OllamaModelName true "request" | ||
| // @Success 200 | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/ollama/model [post] | ||
| // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"添加 Ollama 模型 [name]","formatEN":"add Ollama model [name]"} | ||
| func (b *BaseApi) CreateOllamaModel(c *gin.Context) { | ||
| var req dto.OllamaModelName | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if err := aiToolService.Create(req.Name); err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithData(c, nil) | ||
| } | ||
|
|
||
| // @Tags AI | ||
| // @Summary Rereate Ollama model | ||
| // @Accept json | ||
| // @Param request body dto.OllamaModelName true "request" | ||
| // @Success 200 | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/ollama/model/recreate [post] | ||
| // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"添加 Ollama 模型重试 [name]","formatEN":"re-add Ollama model [name]"} | ||
| func (b *BaseApi) RecreateOllamaModel(c *gin.Context) { | ||
| var req dto.OllamaModelName | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if err := aiToolService.Recreate(req.Name); err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithData(c, nil) | ||
| } | ||
|
|
||
| // @Tags AI | ||
| // @Summary Close Ollama model conn | ||
| // @Accept json | ||
| // @Param request body dto.OllamaModelName true "request" | ||
| // @Success 200 | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/ollama/model/close [post] | ||
| // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"关闭 Ollama 模型连接 [name]","formatEN":"close conn for Ollama model [name]"} | ||
| func (b *BaseApi) CloseOllamaModel(c *gin.Context) { | ||
| var req dto.OllamaModelName | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if err := aiToolService.Close(req.Name); err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithData(c, nil) | ||
| } | ||
|
|
||
| // @Tags AI | ||
| // @Summary Sync Ollama model list | ||
| // @Success 200 {array} dto.OllamaModelDropList | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/ollama/model/sync [post] | ||
| // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"同步 Ollama 模型列表","formatEN":"sync Ollama model list"} | ||
| func (b *BaseApi) SyncOllamaModel(c *gin.Context) { | ||
| list, err := aiToolService.Sync() | ||
| if err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithData(c, list) | ||
| } | ||
|
|
||
| // @Tags AI | ||
| // @Summary Page Ollama models | ||
| // @Accept json | ||
| // @Param request body dto.SearchWithPage true "request" | ||
| // @Success 200 {object} dto.PageResult | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/ollama/model/search [post] | ||
| func (b *BaseApi) SearchOllamaModel(c *gin.Context) { | ||
| var req dto.SearchWithPage | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| total, list, err := aiToolService.Search(req) | ||
| if err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
|
|
||
| helper.SuccessWithData(c, dto.PageResult{ | ||
| Items: list, | ||
| Total: total, | ||
| }) | ||
| } | ||
|
|
||
| // @Tags AI | ||
| // @Summary Page Ollama models | ||
| // @Accept json | ||
| // @Param request body dto.OllamaModelName true "request" | ||
| // @Success 200 {string} details | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/ollama/model/load [post] | ||
| func (b *BaseApi) LoadOllamaModelDetail(c *gin.Context) { | ||
| var req dto.OllamaModelName | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| detail, err := aiToolService.LoadDetail(req.Name) | ||
| if err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
|
|
||
| helper.SuccessWithData(c, detail) | ||
| } | ||
|
|
||
| // @Tags AI | ||
| // @Summary Delete Ollama model | ||
| // @Accept json | ||
| // @Param request body dto.ForceDelete true "request" | ||
| // @Success 200 | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/ollama/model/del [post] | ||
| // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"ollama_models","output_column":"name","output_value":"names"}],"formatZH":"删除 Ollama 模型 [names]","formatEN":"remove Ollama model [names]"} | ||
| func (b *BaseApi) DeleteOllamaModel(c *gin.Context) { | ||
| var req dto.ForceDelete | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if err := aiToolService.Delete(req); err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
|
|
||
| helper.SuccessWithOutData(c) | ||
| } | ||
|
|
||
| // @Tags AI | ||
| // @Summary Load gpu / xpu info | ||
| // @Accept json | ||
| // @Success 200 | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/gpu/load [get] | ||
| func (b *BaseApi) LoadGpuInfo(c *gin.Context) { | ||
| ok, client := gpu.New() | ||
| if ok { | ||
| info, err := client.LoadGpuInfo() | ||
| if err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithData(c, info) | ||
| return | ||
| } | ||
| xpuOK, xpuClient := xpu.New() | ||
| if xpuOK { | ||
| info, err := xpuClient.LoadGpuInfo() | ||
| if err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithData(c, info) | ||
| return | ||
| } | ||
| helper.SuccessWithData(c, &common.GpuInfo{}) | ||
| } | ||
|
|
||
| // @Tags AI | ||
| // @Summary Bind domain | ||
| // @Accept json | ||
| // @Param request body dto.OllamaBindDomain true "request" | ||
| // @Success 200 | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/domain/bind [post] | ||
| func (b *BaseApi) BindDomain(c *gin.Context) { | ||
| var req dto.OllamaBindDomain | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
| if err := aiToolService.BindDomain(req); err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithOutData(c) | ||
| } | ||
|
|
||
| // @Tags AI | ||
| // @Summary Get bind domain | ||
| // @Accept json | ||
| // @Param request body dto.OllamaBindDomainReq true "request" | ||
| // @Success 200 {object} dto.OllamaBindDomainRes | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /ai/domain/get [post] | ||
| func (b *BaseApi) GetBindDomain(c *gin.Context) { | ||
| var req dto.OllamaBindDomainReq | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
| res, err := aiToolService.GetBindDomain(req) | ||
| if err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithData(c, res) | ||
| } | ||
|
|
||
| // Tags AI | ||
| // Summary Update bind domain | ||
| // Accept json | ||
| // Param request body dto.OllamaBindDomain true "request" | ||
| // Success 200 | ||
| // Security ApiKeyAuth | ||
| // Security Timestamp | ||
| // Router /ai/domain/update [post] | ||
| func (b *BaseApi) UpdateBindDomain(c *gin.Context) { | ||
| var req dto.OllamaBindDomain | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
| if err := aiToolService.UpdateBindDomain(req); err != nil { | ||
| helper.BadRequest(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithOutData(c) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code you provided is for an API that interacts with a service for creating and managing different types of AI tools like Ollama models. The code looks mostly robust, but here are some potential optimizations:
Here's what could look like after such adjustments: package v2
import (
"database/sql/driver"
"encoding/json"
"github.com/gin-gonic/gin"
)
// ...Note: I made these comments based on their usage in context from your initial snippet. If additional contexts suggest otherwise (like if the commented lines were actually part of a specific use case outside this snippet), those should also be considered during review.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide the original code for comparison. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package dto | ||
|
|
||
| import "time" | ||
|
|
||
| type OllamaModelInfo struct { | ||
| ID uint `json:"id"` | ||
| Name string `json:"name"` | ||
| Size string `json:"size"` | ||
| From string `json:"from"` | ||
| LogFileExist bool `json:"logFileExist"` | ||
|
|
||
| Status string `json:"status"` | ||
| Message string `json:"message"` | ||
| CreatedAt time.Time `json:"createdAt"` | ||
| } | ||
|
|
||
| type OllamaModelDropList struct { | ||
| ID uint `json:"id"` | ||
| Name string `json:"name"` | ||
| } | ||
|
|
||
| type OllamaModelName struct { | ||
| Name string `json:"name"` | ||
| } | ||
|
|
||
| type OllamaBindDomain struct { | ||
| Domain string `json:"domain" validate:"required"` | ||
| AppInstallID uint `json:"appInstallID" validate:"required"` | ||
| SSLID uint `json:"sslID"` | ||
| WebsiteID uint `json:"websiteID"` | ||
| IPList string `json:"ipList"` | ||
| } | ||
|
|
||
| type OllamaBindDomainReq struct { | ||
| AppInstallID uint `json:"appInstallID" validate:"required"` | ||
| } | ||
|
|
||
| type OllamaBindDomainRes struct { | ||
| Domain string `json:"domain"` | ||
| SSLID uint `json:"sslID"` | ||
| AllowIPs []string `json:"allowIPs"` | ||
| WebsiteID uint `json:"websiteID"` | ||
| ConnUrl string `json:"connUrl"` | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file appears to be part of an implementation of a service called "ai/ollama" which is used for operations related to AI models such as creating, recreating, closing models, syncing lists, and more. It's structured in a way that allows it to have dependencies on functions like
CreateOllamaModel,RecreateOllamaModel, etc., with appropriate annotations indicating how these functions interact.There doesn't seem to be immediate discrepancies or obvious issues; however, if you find any inconsistencies, anomalies, errors, or inefficiencies while looking at this codebase or any specific parts of its functionality and logic, please share them so we can analyze them together in order to better understand what might need improvement or fixing.
For optimization suggestions though, I would recommend focusing on efficiency improvements where necessary but maintaining the current structure and design decisions taken into consideration when building such comprehensive API implementations. Any unnecessary complexity should also be considered when making optimizations. If there are certain segments of this code that operate very much similar to each other and could potentially benefit from refactoring underlining their usage patterns, it might be useful. The key point is not changing things just for change sake, rather aiming for high-quality robustness, maintainability, readability, and scalability over time without sacrificing simplicity too much.