Skip to content

Commit

Permalink
update llm api
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrie-eng committed Oct 31, 2024
1 parent 6d9b751 commit 4e19f93
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 184 deletions.
26 changes: 26 additions & 0 deletions common/genAIDTO.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,29 @@ type ReqGenImgFlux struct {
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
Prompt string `json:"prompt" example:"a flying airplane:"` //prompt, not empty
}

// ReqTxt2Motion TextToMotion request parameters
type ReqTxt2Motion struct {
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
Prompt string `json:"prompt"` //prompt, not empty
NegativePromt string `json:"negative_prompt"` //tokens, not empty
}

// ReqImg2Motion ImgToMotion request parameters
type ReqImg2Motion struct {
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
ImagePath string `json:"image_path"` //image path, not empty
Prompt string `json:"prompt"` //prompt, not empty
}

// ReqCogVideo text to motion request parameters
type ReqCogVideo struct {
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
Prompt string `json:"prompt"` //prompt, not empty
}

// ReqPyramid pyramid request parameters
type ReqPyramid struct {
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
Prompt string `json:"prompt"` //prompt, not empty
}
53 changes: 7 additions & 46 deletions common/llmReq.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package common

// ReqBuyLlmToken the request parameters of buying LLM token
type ReqBuyLlmToken struct {
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
JobName string `json:"jobName" example:"Buy LLM Token"` //job name
TokensNum int64 `json:"tokensNum"` //token num
}

// ReqLLM LLM request parameters
type ReqLLM struct {
Prompt string `json:"prompt" example:"Funniest joke ever:"` //prompt, not empty
Expand All @@ -14,49 +21,3 @@ type ReqLLM3 struct {
MaxNewTokens int `json:"max_new_tokens" example:"200"` //tokens, not empty
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
}

type ReqCreateLlmJob struct {
JobName string `json:"jobName" example:"Millionaires' Problem"` //job name
TokensNum int64 `json:"tokensNum"` //本次购买llm对话次数
}

// ReqTxt2Motion TextToMotion request parameters
type ReqTxt2Motion struct {
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
Prompt string `json:"prompt"` //prompt, not empty
NegativePromt string `json:"negative_prompt"` //tokens, not empty
}

// ReqImg2Motion ImgToMotion request parameters
type ReqImg2Motion struct {
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
ImagePath string `json:"image_path"` //image path, not empty
Prompt string `json:"prompt"` //prompt, not empty
}

// ReqGenSDXLParam gen SDXL by parameters
type ReqGenSDXLParam struct {
UidName string `json:"uidname" example:"[email protected]"`
Prompt string `json:"prompt" example:"a blue dog"` //prompt, not empty
NegativePromt string `json:"negative_prompt"` //tokens, not empty
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
NSteps int `json:"n_steps" example:"20"`
GuidanceScale float64 `json:"guidance_scale" example:"8"` // float, 0 to 10
Seed int `json:"seed" example:"1"` // int, >0
PipeName string `json:"pipe_name" example:"txt2img"`
LoraPathList string `json:"lora_path_list" example:"https://xxxx"`
LoraWeightList string `json:"lora_weight_list" example:"0.8"`
TextualInversionPath string `json:"textual_inversion_path" example:"without"`
LoraScale float64 `json:"lora_scale" example:"0.8"`
Size string `json:"size" example:"1024^*^1024"`
ClipSkip int `json:"clip_skip" example:"1"`
InitImage string `json:"init_image" example:"https://xxx.jpg"`
MaskImage string `json:"mask_image" example:"https://xxx.jpg"`
}

// ReqImgToPrompt ImgToPrompt parameters
type ReqImgToPrompt struct {
UserToken string `json:"user_token" example:"xxxxxxxxxxxx"` //user_token, not empty
ImagePath string `json:"image_path" example:"https://xxx.jpg"` //image path, not empty
Prompt string `json:"prompt" example:"Funniest joke ever:"` //prompt, not empty
}
33 changes: 32 additions & 1 deletion controllers/genAIcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
genImgFluxMethod = "genImgFlux"
txt2MotionMethod = "txt2Motion"
img2MotionMethod = "img2Motion"
cogVideoMethod = "cogVideo"
pyramidMethod = "pyramid"
)

// QueryTask query task by task id
Expand Down Expand Up @@ -94,7 +96,36 @@ func Img2Motion(reqJSON common.ReqImg2Motion) (json.RawMessage, error) {
if err != nil {
return nil, err
}
result, err := util.SendHTTPPostForLLM(ImgToMotionURL, reqBody, reqJSON.UserToken)
rawURL := fmt.Sprint(apiEndpoint, genAIEndPoint, img2MotionMethod)
result, err := util.SendHTTPPostForLLM(rawURL, reqBody, reqJSON.UserToken)
if err != nil {
return nil, err
}
return result, nil
}

// CogVideo Generating a gif by CogVideo mode
func CogVideo(reqJSON common.ReqCogVideo) (json.RawMessage, error) {
reqBody, err := json.Marshal(reqJSON)
if err != nil {
return nil, err
}
rawURL := fmt.Sprint(apiEndpoint, genAIEndPoint, cogVideoMethod)
result, err := util.SendHTTPPostForLLM(rawURL, reqBody, reqJSON.UserToken)
if err != nil {
return nil, err
}
return result, nil
}

// Pyramid Generating a gif by Pyramid mode
func Pyramid(reqJSON common.ReqPyramid) (json.RawMessage, error) {
reqBody, err := json.Marshal(reqJSON)
if err != nil {
return nil, err
}
rawURL := fmt.Sprint(apiEndpoint, genAIEndPoint, pyramidMethod)
result, err := util.SendHTTPPostForLLM(rawURL, reqBody, reqJSON.UserToken)
if err != nil {
return nil, err
}
Expand Down
141 changes: 27 additions & 114 deletions controllers/llmController.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,154 +2,67 @@ package controllers

import (
"encoding/json"
"fmt"

"github.com/PhoenixGlobal/Phoenix-Computation-SDK/util"

"github.com/PhoenixGlobal/Phoenix-Computation-SDK/common"
)

// const CallLLMURL string = "https://www.phoenix.global/sdk/computation/LLM/callLLM"
const CallLLMURL3 string = "https://www.phoenix.global/sdk/computation/LLM/callLLM3"
const VerticalLLMURL string = "https://www.phoenix.global/sdk/computation/LLM/verticalLLM"
const GenImageURL string = "https://www.phoenix.global/sdk/computation/LLM/callGenBaseParam"
const GenSDXLImageURL string = "https://www.phoenix.global/sdk/computation/LLM/callGenSDXLImage"
const GenSDXLParamURL string = "https://www.phoenix.global/sdk/computation/LLM/callGenSDXLParam"
const TextToMotionURL string = "https://www.phoenix.global/sdk/computation/LLM/callTextToMotion"
const ImgToMotionURL string = "https://genapi.phoenix.global/sdk/computation/LLM/callImgToMotion"
const ImgToPromptURL string = "https://www.phoenix.global/sdk/computation/LLM/imgToPrompt"
const CreateLLMJobURL string = "https://www.phoenix.global/sdk/computation/LLM/createLLMJob"
const QueryLLMPriceURL string = "https://www.phoenix.global/sdk/computation/LLM/queryLLMPrice"
const QueryLLMCountURL string = "https://www.phoenix.global/sdk/computation/LLM/queryLLMActualCount"
const QueryLLMTokensBalanceURL string = "https://www.phoenix.global/sdk/computation/LLM/queryLLMTokensBalance"
const QueryLLMFreeTokensBalanceURL string = "https://www.phoenix.global/sdk/computation/LLM/queryLLMFreeTokensBalance"

// CallLLM3 call LLM api
func CallLLM3(reqBody common.ReqLLM3) (json.RawMessage, error) {
reqJson, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}
result, err := util.SendHTTPPostForLLM(CallLLMURL3, reqJson, reqBody.UserToken)
if err != nil {
return nil, err
}
return result, nil
}
const (
llmEndPoint = "LLM/"
buyLLMTokenMehod = "buyLLMToken"
llmPriceMethod = "queryLLMPrice"
llm3Method = "callLLM3"
verticalMethod = "verticalLLM"
)

// VerticalLLM VerticalLLM api
func VerticalLLM(reqBody common.ReqLLM3) (json.RawMessage, error) {
reqJson, err := json.Marshal(reqBody)
// BuyLLMToken bug LLM token
func BuyLLMToken(reqJSON common.ReqBuyLlmToken) (json.RawMessage, error) {
reqBody, err := json.Marshal(reqJSON)
if err != nil {
return nil, err
}
result, err := util.SendHTTPPostForLLM(VerticalLLMURL, reqJson, reqBody.UserToken)
rawURL := fmt.Sprint(apiEndpoint, llmEndPoint, buyLLMTokenMehod)
result, err := util.SendHTTPPostForLLM(rawURL, reqBody, reqJSON.UserToken)
if err != nil {
return nil, err
}
return result, nil
}

func CreateLLMJob(reqBody common.ReqCreateLlmJob, token string) (json.RawMessage, error) {
reqJson, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}
result, err := util.SendHttpPost(CreateLLMJobURL, reqJson, token)
// QueryLLMPrice query LLM price
func QueryLLMPrice(token string) (json.RawMessage, error) {
rawURL := fmt.Sprint(apiEndpoint, llmEndPoint, llmPriceMethod)
result, err := util.SendHttpGet(rawURL, nil, token)
if err != nil {
return nil, err
}
return result, nil
}

func QueryLLMPrice(token string) (json.RawMessage, error) {
result, err := util.SendHttpGet(QueryLLMPriceURL, nil, token)
// CallLLM3 call LLM api
func CallLLM3(reqJSON common.ReqLLM3) (json.RawMessage, error) {
reqBody, err := json.Marshal(reqJSON)
if err != nil {
return nil, err
}
return result, nil
}

func QueryLLMBuyCount(token string) (json.RawMessage, error) {
result, err := util.SendHttpPost(QueryLLMCountURL, nil, token)
rawURL := fmt.Sprint(apiEndpoint, llmEndPoint, llm3Method)
result, err := util.SendHTTPPostForLLM(rawURL, reqBody, reqJSON.UserToken)
if err != nil {
return nil, err
}
return result, nil
}

func GetLLMBuyCount(token string) (count float64, e error) {
res, err := QueryLLMBuyCount(token)
if err != nil {
e = err
return
}
resultMap := make(map[string]interface{})
e = json.Unmarshal(res, &resultMap)
if e != nil {
return
}
dataMap, ok := resultMap["data"].(map[string]interface{})
if !ok {
return
}
count, ok = dataMap["count"].(float64)
if !ok {
return
}
return
}

func QueryLLMTokensBalance(token string) (llmTokens float64, e error) {
result, err := util.SendHttpGet(QueryLLMTokensBalanceURL, nil, token)
if err != nil {
e = err
return
}
resultMap := make(map[string]interface{})
e = json.Unmarshal(result, &resultMap)
if e != nil {
return
}
dataMap, ok := resultMap["data"].(map[string]interface{})
if !ok {
return
}
llmTokens, ok = dataMap["llmTokens"].(float64)
if !ok {
return
}
return
}

func QueryLLMFreeTokensBalance(token string) (llmTokens float64, e error) {
result, err := util.SendHttpGet(QueryLLMFreeTokensBalanceURL, nil, token)
if err != nil {
e = err
return
}
resultMap := make(map[string]interface{})
e = json.Unmarshal(result, &resultMap)
if e != nil {
return
}
dataMap, ok := resultMap["data"].(map[string]interface{})
if !ok {
return
}
llmTokens, ok = dataMap["llmTokens"].(float64)
if !ok {
return
}
return
}

// ImgToPrompt call img to prompt api
func ImgToPrompt(reqBody common.ReqImgToPrompt) (json.RawMessage, error) {
reqJSON, err := json.Marshal(reqBody)
// VerticalLLM VerticalLLM api
func VerticalLLM(reqJSON common.ReqLLM3) (json.RawMessage, error) {
reqBody, err := json.Marshal(reqJSON)
if err != nil {
return nil, err
}
result, err := util.SendHTTPPostForLLM(ImgToPromptURL, reqJSON, reqBody.UserToken)
rawURL := fmt.Sprint(apiEndpoint, llmEndPoint, verticalMethod)
result, err := util.SendHTTPPostForLLM(rawURL, reqBody, reqJSON.UserToken)
if err != nil {
return nil, err
}
Expand Down
24 changes: 1 addition & 23 deletions controllers/llmController_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestCreateLLMJob(t *testing.T) {
}
tokenStr := tokenMap["token"].(string)

res, _ := CreateLLMJob(reqBody, tokenStr)
res, _ := BuyLLMToken(reqBody, tokenStr)
fmt.Println("res", string(res))
}

Expand All @@ -66,18 +66,6 @@ func TestQueryLLMPrice(t *testing.T) {
fmt.Println("price=", priceStr)
}

func TestQueryLLMBuyCount(t *testing.T) {
tokenStr := "XXXXXXXXXXXXX"

res, err := QueryLLMBuyCount(tokenStr)
resultMap := make(map[string]interface{})
fmt.Println(11111, resultMap, err)
err = json.Unmarshal(res, &resultMap)
dataMap := resultMap["data"].(map[string]interface{})
buyCount := dataMap["count"].(float64)
fmt.Println(22222, buyCount, err)
}

type ReqGenVideo struct {
Uidname string `json:"uidname"`
Prompt string `json:"prompt"`
Expand All @@ -103,13 +91,3 @@ func TestGenSDXl(t *testing.T) {
result, err := GenImgSDXL(reqBody)
fmt.Println(result, err)
}

func TestImgToPrompt(t *testing.T) {
reqBody := common.ReqImgToPrompt{
Prompt: "",
ImagePath: "https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/a08dca98-b98f-4c0c-9475-d9719d4aae1f/original=true/best.jpeg",
UserToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFbWFpbCI6IjExMUBnbWFpbC5jb20iLCJleHAiOjE3MTI2NDY3MjR9.K7i3aaWMmfcPzaBUam5nrIhiVaAMfubFdjdaYcOb8M0",
}
result, err := ImgToPrompt(reqBody)
fmt.Println(result, err)
}

0 comments on commit 4e19f93

Please sign in to comment.