forked from lobehub/lobe-chat
-
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.
feat: supper whisper now (close lobehub#197)
- Loading branch information
1 parent
1c4409a
commit d09d317
Showing
6 changed files
with
177 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package controller | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"one-api/common" | ||
"one-api/model" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { | ||
audioModel := "whisper-1" | ||
|
||
tokenId := c.GetInt("token_id") | ||
channelType := c.GetInt("channel") | ||
userId := c.GetInt("id") | ||
group := c.GetString("group") | ||
|
||
preConsumedTokens := common.PreConsumedQuota | ||
modelRatio := common.GetModelRatio(audioModel) | ||
groupRatio := common.GetGroupRatio(group) | ||
ratio := modelRatio * groupRatio | ||
preConsumedQuota := int(float64(preConsumedTokens) * ratio) | ||
userQuota, err := model.CacheGetUserQuota(userId) | ||
if err != nil { | ||
return errorWrapper(err, "get_user_quota_failed", http.StatusInternalServerError) | ||
} | ||
err = model.CacheDecreaseUserQuota(userId, preConsumedQuota) | ||
if err != nil { | ||
return errorWrapper(err, "decrease_user_quota_failed", http.StatusInternalServerError) | ||
} | ||
if userQuota > 100*preConsumedQuota { | ||
// in this case, we do not pre-consume quota | ||
// because the user has enough quota | ||
preConsumedQuota = 0 | ||
} | ||
if preConsumedQuota > 0 { | ||
err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota) | ||
if err != nil { | ||
return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusForbidden) | ||
} | ||
} | ||
|
||
// map model name | ||
modelMapping := c.GetString("model_mapping") | ||
if modelMapping != "" { | ||
modelMap := make(map[string]string) | ||
err := json.Unmarshal([]byte(modelMapping), &modelMap) | ||
if err != nil { | ||
return errorWrapper(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError) | ||
} | ||
if modelMap[audioModel] != "" { | ||
audioModel = modelMap[audioModel] | ||
} | ||
} | ||
|
||
baseURL := common.ChannelBaseURLs[channelType] | ||
requestURL := c.Request.URL.String() | ||
|
||
if c.GetString("base_url") != "" { | ||
baseURL = c.GetString("base_url") | ||
} | ||
|
||
fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL) | ||
requestBody := c.Request.Body | ||
|
||
req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody) | ||
if err != nil { | ||
return errorWrapper(err, "new_request_failed", http.StatusInternalServerError) | ||
} | ||
req.Header.Set("Authorization", c.Request.Header.Get("Authorization")) | ||
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type")) | ||
req.Header.Set("Accept", c.Request.Header.Get("Accept")) | ||
|
||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return errorWrapper(err, "do_request_failed", http.StatusInternalServerError) | ||
} | ||
|
||
err = req.Body.Close() | ||
if err != nil { | ||
return errorWrapper(err, "close_request_body_failed", http.StatusInternalServerError) | ||
} | ||
err = c.Request.Body.Close() | ||
if err != nil { | ||
return errorWrapper(err, "close_request_body_failed", http.StatusInternalServerError) | ||
} | ||
var audioResponse AudioResponse | ||
|
||
defer func() { | ||
go func() { | ||
quota := countTokenText(audioResponse.Text, audioModel) | ||
quotaDelta := quota - preConsumedQuota | ||
err := model.PostConsumeTokenQuota(tokenId, quotaDelta) | ||
if err != nil { | ||
common.SysError("error consuming token remain quota: " + err.Error()) | ||
} | ||
err = model.CacheUpdateUserQuota(userId) | ||
if err != nil { | ||
common.SysError("error update user quota cache: " + err.Error()) | ||
} | ||
if quota != 0 { | ||
tokenName := c.GetString("token_name") | ||
logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio) | ||
model.RecordConsumeLog(userId, 0, 0, audioModel, tokenName, quota, logContent) | ||
model.UpdateUserUsedQuotaAndRequestCount(userId, quota) | ||
channelId := c.GetInt("channel_id") | ||
model.UpdateChannelUsedQuota(channelId, quota) | ||
} | ||
}() | ||
}() | ||
|
||
responseBody, err := io.ReadAll(resp.Body) | ||
|
||
if err != nil { | ||
return errorWrapper(err, "read_response_body_failed", http.StatusInternalServerError) | ||
} | ||
err = resp.Body.Close() | ||
if err != nil { | ||
return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError) | ||
} | ||
err = json.Unmarshal(responseBody, &audioResponse) | ||
if err != nil { | ||
return errorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError) | ||
} | ||
|
||
resp.Body = io.NopCloser(bytes.NewBuffer(responseBody)) | ||
|
||
for k, v := range resp.Header { | ||
c.Writer.Header().Set(k, v[0]) | ||
} | ||
c.Writer.WriteHeader(resp.StatusCode) | ||
|
||
_, err = io.Copy(c.Writer, resp.Body) | ||
if err != nil { | ||
return errorWrapper(err, "copy_response_body_failed", http.StatusInternalServerError) | ||
} | ||
err = resp.Body.Close() | ||
if err != nil { | ||
return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError) | ||
} | ||
return 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
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