adaptor.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package vertex
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "regexp"
  10. "strings"
  11. "github.com/gin-gonic/gin"
  12. "one-api/common"
  13. "one-api/constant"
  14. "one-api/dto"
  15. "one-api/relay/channel"
  16. vertexcore "one-api/relay/channel/vertex"
  17. relaycommon "one-api/relay/common"
  18. "one-api/service"
  19. )
  20. type requestPayload struct {
  21. Instances []map[string]any `json:"instances"`
  22. Parameters map[string]any `json:"parameters,omitempty"`
  23. }
  24. type submitResponse struct {
  25. Name string `json:"name"`
  26. }
  27. type operationVideo struct {
  28. MimeType string `json:"mimeType"`
  29. BytesBase64Encoded string `json:"bytesBase64Encoded"`
  30. Encoding string `json:"encoding"`
  31. }
  32. type operationResponse struct {
  33. Name string `json:"name"`
  34. Done bool `json:"done"`
  35. Response struct {
  36. Type string `json:"@type"`
  37. RaiMediaFilteredCount int `json:"raiMediaFilteredCount"`
  38. Videos []operationVideo `json:"videos"`
  39. BytesBase64Encoded string `json:"bytesBase64Encoded"`
  40. Encoding string `json:"encoding"`
  41. Video string `json:"video"`
  42. } `json:"response"`
  43. Error struct {
  44. Message string `json:"message"`
  45. } `json:"error"`
  46. }
  47. type TaskAdaptor struct{}
  48. func (a *TaskAdaptor) Init(info *relaycommon.TaskRelayInfo) {}
  49. func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.TaskRelayInfo) (taskErr *dto.TaskError) {
  50. info.Action = constant.TaskActionTextGenerate
  51. req := relaycommon.TaskSubmitReq{}
  52. if err := common.UnmarshalBodyReusable(c, &req); err != nil {
  53. return service.TaskErrorWrapperLocal(err, "invalid_request", http.StatusBadRequest)
  54. }
  55. if strings.TrimSpace(req.Prompt) == "" {
  56. return service.TaskErrorWrapperLocal(fmt.Errorf("prompt is required"), "invalid_request", http.StatusBadRequest)
  57. }
  58. c.Set("task_request", req)
  59. return nil
  60. }
  61. func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.TaskRelayInfo) (string, error) {
  62. adc := &vertexcore.Credentials{}
  63. if err := json.Unmarshal([]byte(info.ApiKey), adc); err != nil {
  64. return "", fmt.Errorf("failed to decode credentials: %w", err)
  65. }
  66. modelName := info.OriginModelName
  67. if v, ok := getRequestModelFromContext(info); ok {
  68. modelName = v
  69. }
  70. if modelName == "" {
  71. modelName = "veo-3.0-generate-001"
  72. }
  73. region := vertexcore.GetModelRegion(info.ApiVersion, modelName)
  74. if strings.TrimSpace(region) == "" {
  75. region = "global"
  76. }
  77. if region == "global" {
  78. return fmt.Sprintf(
  79. "https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:predictLongRunning",
  80. adc.ProjectID,
  81. modelName,
  82. ), nil
  83. }
  84. return fmt.Sprintf(
  85. "https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:predictLongRunning",
  86. region,
  87. adc.ProjectID,
  88. region,
  89. modelName,
  90. ), nil
  91. }
  92. func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.TaskRelayInfo) error {
  93. req.Header.Set("Content-Type", "application/json")
  94. req.Header.Set("Accept", "application/json")
  95. adc := &vertexcore.Credentials{}
  96. if err := json.Unmarshal([]byte(info.ApiKey), adc); err != nil {
  97. return fmt.Errorf("failed to decode credentials: %w", err)
  98. }
  99. token, err := vertexcore.AcquireAccessToken(*adc, info.ChannelSetting.Proxy)
  100. if err != nil {
  101. return fmt.Errorf("failed to acquire access token: %w", err)
  102. }
  103. req.Header.Set("Authorization", "Bearer "+token)
  104. req.Header.Set("x-goog-user-project", adc.ProjectID)
  105. return nil
  106. }
  107. func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, _ *relaycommon.TaskRelayInfo) (io.Reader, error) {
  108. v, ok := c.Get("task_request")
  109. if !ok {
  110. return nil, fmt.Errorf("request not found in context")
  111. }
  112. req := v.(relaycommon.TaskSubmitReq)
  113. body := requestPayload{
  114. Instances: []map[string]any{{"prompt": req.Prompt}},
  115. Parameters: map[string]any{},
  116. }
  117. if req.Metadata != nil {
  118. if v, ok := req.Metadata["storageUri"]; ok {
  119. body.Parameters["storageUri"] = v
  120. }
  121. if v, ok := req.Metadata["sampleCount"]; ok {
  122. body.Parameters["sampleCount"] = v
  123. }
  124. }
  125. if _, ok := body.Parameters["sampleCount"]; !ok {
  126. body.Parameters["sampleCount"] = 1
  127. }
  128. data, err := json.Marshal(body)
  129. if err != nil {
  130. return nil, err
  131. }
  132. return bytes.NewReader(data), nil
  133. }
  134. func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.TaskRelayInfo, requestBody io.Reader) (*http.Response, error) {
  135. return channel.DoTaskApiRequest(a, c, info, requestBody)
  136. }
  137. func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, _ *relaycommon.TaskRelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) {
  138. responseBody, err := io.ReadAll(resp.Body)
  139. if err != nil {
  140. return "", nil, service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError)
  141. }
  142. _ = resp.Body.Close()
  143. var s submitResponse
  144. if err := json.Unmarshal(responseBody, &s); err != nil {
  145. return "", nil, service.TaskErrorWrapper(err, "unmarshal_response_failed", http.StatusInternalServerError)
  146. }
  147. if strings.TrimSpace(s.Name) == "" {
  148. return "", nil, service.TaskErrorWrapper(fmt.Errorf("missing operation name"), "invalid_response", http.StatusInternalServerError)
  149. }
  150. localID := encodeLocalTaskID(s.Name)
  151. c.JSON(http.StatusOK, gin.H{"task_id": localID})
  152. return localID, responseBody, nil
  153. }
  154. func (a *TaskAdaptor) GetModelList() []string { return []string{"veo-3.0-generate-001"} }
  155. func (a *TaskAdaptor) GetChannelName() string { return "vertex" }
  156. func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http.Response, error) {
  157. taskID, ok := body["task_id"].(string)
  158. if !ok {
  159. return nil, fmt.Errorf("invalid task_id")
  160. }
  161. upstreamName, err := decodeLocalTaskID(taskID)
  162. if err != nil {
  163. return nil, fmt.Errorf("decode task_id failed: %w", err)
  164. }
  165. region := extractRegionFromOperationName(upstreamName)
  166. if region == "" {
  167. region = "us-central1"
  168. }
  169. project := extractProjectFromOperationName(upstreamName)
  170. model := extractModelFromOperationName(upstreamName)
  171. if project == "" || model == "" {
  172. return nil, fmt.Errorf("cannot extract project/model from operation name")
  173. }
  174. var url string
  175. if region == "global" {
  176. url = fmt.Sprintf("https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:fetchPredictOperation", project, model)
  177. } else {
  178. url = fmt.Sprintf("https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:fetchPredictOperation", region, project, region, model)
  179. }
  180. payload := map[string]string{"operationName": upstreamName}
  181. data, err := json.Marshal(payload)
  182. if err != nil {
  183. return nil, err
  184. }
  185. adc := &vertexcore.Credentials{}
  186. if err := json.Unmarshal([]byte(key), adc); err != nil {
  187. return nil, fmt.Errorf("failed to decode credentials: %w", err)
  188. }
  189. token, err := vertexcore.AcquireAccessToken(*adc, "")
  190. if err != nil {
  191. return nil, fmt.Errorf("failed to acquire access token: %w", err)
  192. }
  193. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
  194. if err != nil {
  195. return nil, err
  196. }
  197. req.Header.Set("Content-Type", "application/json")
  198. req.Header.Set("Accept", "application/json")
  199. req.Header.Set("Authorization", "Bearer "+token)
  200. req.Header.Set("x-goog-user-project", adc.ProjectID)
  201. return service.GetHttpClient().Do(req)
  202. }
  203. func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, error) {
  204. var op operationResponse
  205. if err := json.Unmarshal(respBody, &op); err != nil {
  206. return nil, fmt.Errorf("unmarshal operation response failed: %w", err)
  207. }
  208. ti := &relaycommon.TaskInfo{}
  209. if op.Error.Message != "" {
  210. ti.Status = "FAILURE"
  211. ti.Reason = op.Error.Message
  212. ti.Progress = "100%"
  213. return ti, nil
  214. }
  215. if !op.Done {
  216. ti.Status = "IN_PROGRESS"
  217. ti.Progress = "50%"
  218. return ti, nil
  219. }
  220. ti.Status = "SUCCESS"
  221. ti.Progress = "100%"
  222. if len(op.Response.Videos) > 0 {
  223. v0 := op.Response.Videos[0]
  224. if v0.BytesBase64Encoded != "" {
  225. mime := strings.TrimSpace(v0.MimeType)
  226. if mime == "" {
  227. enc := strings.TrimSpace(v0.Encoding)
  228. if enc == "" {
  229. enc = "mp4"
  230. }
  231. if strings.Contains(enc, "/") {
  232. mime = enc
  233. } else {
  234. mime = "video/" + enc
  235. }
  236. }
  237. ti.Url = "data:" + mime + ";base64," + v0.BytesBase64Encoded
  238. return ti, nil
  239. }
  240. }
  241. if op.Response.BytesBase64Encoded != "" {
  242. enc := strings.TrimSpace(op.Response.Encoding)
  243. if enc == "" {
  244. enc = "mp4"
  245. }
  246. mime := enc
  247. if !strings.Contains(enc, "/") {
  248. mime = "video/" + enc
  249. }
  250. ti.Url = "data:" + mime + ";base64," + op.Response.BytesBase64Encoded
  251. return ti, nil
  252. }
  253. if op.Response.Video != "" { // some variants use `video` as base64
  254. enc := strings.TrimSpace(op.Response.Encoding)
  255. if enc == "" {
  256. enc = "mp4"
  257. }
  258. mime := enc
  259. if !strings.Contains(enc, "/") {
  260. mime = "video/" + enc
  261. }
  262. ti.Url = "data:" + mime + ";base64," + op.Response.Video
  263. return ti, nil
  264. }
  265. return ti, nil
  266. }
  267. func getRequestModelFromContext(info *relaycommon.TaskRelayInfo) (string, bool) {
  268. return info.OriginModelName, info.OriginModelName != ""
  269. }
  270. func encodeLocalTaskID(name string) string {
  271. return base64.RawURLEncoding.EncodeToString([]byte(name))
  272. }
  273. func decodeLocalTaskID(local string) (string, error) {
  274. b, err := base64.RawURLEncoding.DecodeString(local)
  275. if err != nil {
  276. return "", err
  277. }
  278. return string(b), nil
  279. }
  280. var regionRe = regexp.MustCompile(`locations/([a-z0-9-]+)/`)
  281. func extractRegionFromOperationName(name string) string {
  282. m := regionRe.FindStringSubmatch(name)
  283. if len(m) == 2 {
  284. return m[1]
  285. }
  286. return ""
  287. }
  288. var modelRe = regexp.MustCompile(`models/([^/]+)/operations/`)
  289. func extractModelFromOperationName(name string) string {
  290. m := modelRe.FindStringSubmatch(name)
  291. if len(m) == 2 {
  292. return m[1]
  293. }
  294. idx := strings.Index(name, "models/")
  295. if idx >= 0 {
  296. s := name[idx+len("models/"):]
  297. if p := strings.Index(s, "/operations/"); p > 0 {
  298. return s[:p]
  299. }
  300. }
  301. return ""
  302. }
  303. var projectRe = regexp.MustCompile(`projects/([^/]+)/locations/`)
  304. func extractProjectFromOperationName(name string) string {
  305. m := projectRe.FindStringSubmatch(name)
  306. if len(m) == 2 {
  307. return m[1]
  308. }
  309. return ""
  310. }