Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
efd18779
authored
Jul 08, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: hunyuan
parent
976e1f09
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
30 additions
and
18 deletions
+30
-18
common/constants.go
+1
-1
controller/channel-test.go
+0
-1
relay/channel/tencent/adaptor.go
+5
-3
relay/channel/tencent/dto.go
+7
-3
relay/channel/tencent/relay-tencent.go
+16
-10
relay/common/relay_utils.go
+1
-0
No files found.
common/constants.go
View file @
efd18779
...
...
@@ -241,7 +241,7 @@ var ChannelBaseURLs = []string{
"https://openrouter.ai/api"
,
// 20
"https://api.aiproxy.io"
,
// 21
"https://fastgpt.run/api/openapi"
,
// 22
"https://hunyuan.
cloud.tencent
.com"
,
//23
"https://hunyuan.
tencentcloudapi
.com"
,
//23
"https://generativelanguage.googleapis.com"
,
//24
"https://api.moonshot.cn"
,
//25
"https://open.bigmodel.cn"
,
//26
...
...
controller/channel-test.go
View file @
efd18779
...
...
@@ -121,7 +121,6 @@ func testChannel(channel *model.Channel, testModel string) (err error, openaiErr
return
errors
.
New
(
"usage is nil"
),
nil
}
result
:=
w
.
Result
()
// print result.Body
respBody
,
err
:=
io
.
ReadAll
(
result
.
Body
)
if
err
!=
nil
{
return
err
,
nil
...
...
relay/channel/tencent/adaptor.go
View file @
efd18779
...
...
@@ -17,6 +17,7 @@ import (
type
Adaptor
struct
{
Sign
string
AppID
int64
Action
string
Version
string
Timestamp
int64
...
...
@@ -34,7 +35,7 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo, request dto.GeneralOpenAIReq
}
func
(
a
*
Adaptor
)
GetRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
{
return
fmt
.
Sprintf
(
"%s/
hyllm/v1/chat/completions
"
,
info
.
BaseUrl
),
nil
return
fmt
.
Sprintf
(
"%s/"
,
info
.
BaseUrl
),
nil
}
func
(
a
*
Adaptor
)
SetupRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
RelayInfo
)
error
{
...
...
@@ -52,11 +53,12 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *dto.Gen
}
apiKey
:=
c
.
Request
.
Header
.
Get
(
"Authorization"
)
apiKey
=
strings
.
TrimPrefix
(
apiKey
,
"Bearer "
)
_
,
secretId
,
secretKey
,
err
:=
parseTencentConfig
(
apiKey
)
appId
,
secretId
,
secretKey
,
err
:=
parseTencentConfig
(
apiKey
)
a
.
AppID
=
appId
if
err
!=
nil
{
return
nil
,
err
}
tencentRequest
:=
requestOpenAI2Tencent
(
*
request
)
tencentRequest
:=
requestOpenAI2Tencent
(
a
,
*
request
)
// we have to calculate the sign here
a
.
Sign
=
getTencentSign
(
*
tencentRequest
,
a
,
secretId
,
secretKey
)
return
tencentRequest
,
nil
...
...
relay/channel/tencent/dto.go
View file @
efd18779
...
...
@@ -30,17 +30,17 @@ type TencentChatRequest struct {
//
// 注意:
// 通过 SDK 调用时,流式和非流式调用需用**不同的方式**获取返回值,具体参考 SDK 中的注释或示例(在各语言 SDK 代码仓库的 examples/hunyuan/v20230901/ 目录中)。
Stream
*
bool
`json:"Stream"`
Stream
*
bool
`json:"Stream
,omitempty
"`
// 说明:
// 1. 影响输出文本的多样性,取值越大,生成文本的多样性越强。
// 2. 取值区间为 [0.0, 1.0],未传值时使用各模型推荐值。
// 3. 非必要不建议使用,不合理的取值会影响效果。
TopP
*
float64
`json:"TopP"`
TopP
*
float64
`json:"TopP
,omitempty
"`
// 说明:
// 1. 较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定。
// 2. 取值区间为 [0.0, 2.0],未传值时使用各模型推荐值。
// 3. 非必要不建议使用,不合理的取值会影响效果。
Temperature
*
float64
`json:"Temperature"`
Temperature
*
float64
`json:"Temperature
,omitempty
"`
}
type
TencentError
struct
{
...
...
@@ -69,3 +69,7 @@ type TencentChatResponse struct {
Note
string
`json:"Note,omitempty"`
// 注释
ReqID
string
`json:"Req_id,omitempty"`
// 唯一请求 Id,每次请求都会返回。用于反馈接口入参
}
type
TencentChatResponseSB
struct
{
Response
TencentChatResponse
`json:"Response,omitempty"`
}
relay/channel/tencent/relay-tencent.go
View file @
efd18779
...
...
@@ -22,7 +22,7 @@ import (
// https://cloud.tencent.com/document/product/1729/97732
func
requestOpenAI2Tencent
(
request
dto
.
GeneralOpenAIRequest
)
*
TencentChatRequest
{
func
requestOpenAI2Tencent
(
a
*
Adaptor
,
request
dto
.
GeneralOpenAIRequest
)
*
TencentChatRequest
{
messages
:=
make
([]
*
TencentMessage
,
0
,
len
(
request
.
Messages
))
for
i
:=
0
;
i
<
len
(
request
.
Messages
);
i
++
{
message
:=
request
.
Messages
[
i
]
...
...
@@ -31,17 +31,23 @@ func requestOpenAI2Tencent(request dto.GeneralOpenAIRequest) *TencentChatRequest
Role
:
message
.
Role
,
})
}
return
&
TencentChatRequest
{
Temperature
:
&
request
.
Temperature
,
TopP
:
&
request
.
TopP
,
var
req
=
TencentChatRequest
{
Stream
:
&
request
.
Stream
,
Messages
:
messages
,
Model
:
&
request
.
Model
,
}
if
request
.
TopP
!=
0
{
req
.
TopP
=
&
request
.
TopP
}
if
request
.
Temperature
!=
0
{
req
.
Temperature
=
&
request
.
Temperature
}
return
&
req
}
func
responseTencent2OpenAI
(
response
*
TencentChatResponse
)
*
dto
.
OpenAITextResponse
{
fullTextResponse
:=
dto
.
OpenAITextResponse
{
Id
:
response
.
Id
,
Object
:
"chat.completion"
,
Created
:
common
.
GetTimestamp
(),
Usage
:
dto
.
Usage
{
...
...
@@ -129,7 +135,7 @@ func tencentStreamHandler(c *gin.Context, resp *http.Response) (*dto.OpenAIError
}
func
tencentHandler
(
c
*
gin
.
Context
,
resp
*
http
.
Response
)
(
*
dto
.
OpenAIErrorWithStatusCode
,
*
dto
.
Usage
)
{
var
TencentResponse
TencentChatResponse
var
tencentSb
TencentChatResponseSB
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
service
.
OpenAIErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
),
nil
...
...
@@ -138,20 +144,20 @@ func tencentHandler(c *gin.Context, resp *http.Response) (*dto.OpenAIErrorWithSt
if
err
!=
nil
{
return
service
.
OpenAIErrorWrapper
(
err
,
"close_response_body_failed"
,
http
.
StatusInternalServerError
),
nil
}
err
=
json
.
Unmarshal
(
responseBody
,
&
TencentResponse
)
err
=
json
.
Unmarshal
(
responseBody
,
&
tencentSb
)
if
err
!=
nil
{
return
service
.
OpenAIErrorWrapper
(
err
,
"unmarshal_response_body_failed"
,
http
.
StatusInternalServerError
),
nil
}
if
Tencent
Response
.
Error
.
Code
!=
0
{
if
tencentSb
.
Response
.
Error
.
Code
!=
0
{
return
&
dto
.
OpenAIErrorWithStatusCode
{
Error
:
dto
.
OpenAIError
{
Message
:
Tencent
Response
.
Error
.
Message
,
Code
:
Tencent
Response
.
Error
.
Code
,
Message
:
tencentSb
.
Response
.
Error
.
Message
,
Code
:
tencentSb
.
Response
.
Error
.
Code
,
},
StatusCode
:
resp
.
StatusCode
,
},
nil
}
fullTextResponse
:=
responseTencent2OpenAI
(
&
Tencent
Response
)
fullTextResponse
:=
responseTencent2OpenAI
(
&
tencentSb
.
Response
)
jsonResponse
,
err
:=
json
.
Marshal
(
fullTextResponse
)
if
err
!=
nil
{
return
service
.
OpenAIErrorWrapper
(
err
,
"marshal_response_body_failed"
,
http
.
StatusInternalServerError
),
nil
...
...
relay/common/relay_utils.go
View file @
efd18779
...
...
@@ -38,6 +38,7 @@ func RelayErrorHandler(resp *http.Response) (OpenAIErrorWithStatusCode *dto.Open
var
textResponse
dto
.
TextResponseWithError
err
=
json
.
Unmarshal
(
responseBody
,
&
textResponse
)
if
err
!=
nil
{
OpenAIErrorWithStatusCode
.
Error
.
Message
=
fmt
.
Sprintf
(
"error unmarshalling response body: %s"
,
responseBody
)
return
}
OpenAIErrorWithStatusCode
.
Error
=
textResponse
.
Error
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment