package main
import (
_ "awesomeProject1/docs"
"fmt"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"net/http"
"os"
)
type Metadata struct {
AppId int `json:"appid"`
AppName string `json:"appname"`
}
type RequestPayload struct {
InputTexts []string `json:"input_texts"`
}
type ResponsePayload struct {
OutputEmbeddings [][]float32 `json:"output_embeddings"`
}
type EmbeddingRequest struct {
MetaInfo Metadata `json:"metadata"`
PayloadData RequestPayload `json:"payload"`
}
type EmbeddingResponse struct {
MetaInfo Metadata `json:"metadata"`
PayloadData ResponsePayload `json:"payload"`
}
func main() {
r := gin.Default()
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.POST("/get_embeddings", Embedding)
if err := r.Run(":8080"); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// Embedding
// @Summary Get embeddings for input texts
// @Description Returns embeddings for a list of input texts
// @Tags embeddings
// @Param requestBody body EmbeddingRequest true "Request body for embeddings"
// @Success 200 {object} EmbeddingResponse
// @Router /get_embeddings [post]
func Embedding(c *gin.Context) {
var req EmbeddingRequest
var resp EmbeddingResponse
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
embeddings := generateEmbeddings(req.PayloadData.InputTexts)
resp.MetaInfo = req.MetaInfo
resp.PayloadData.OutputEmbeddings = embeddings
c.JSON(http.StatusOK, resp)
}
func generateEmbeddings(inputTexts []string) [][]float32 {
embeddings := make([][]float32, len(inputTexts))
for i, text := range inputTexts {
fmt.Printf("%d %s", i, text)
embeddings[i] = []float32{0.1, 0.2, 0.3} // Placeholder values
}
return embeddings
}
标签:swag,embeddings,struct,err,示例,json,简单,gin,type
From: https://www.cnblogs.com/tangjicheng/p/17678408.html