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
0ba5ff9d
authored
May 21, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: PaLM support is WIP (#105)
parent
38ecd5cd
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
2 deletions
+79
-2
common/constants.go
+2
-0
controller/relay-palm.go
+59
-0
controller/relay.go
+18
-2
No files found.
common/constants.go
View file @
0ba5ff9d
...
...
@@ -129,6 +129,7 @@ const (
ChannelTypeCustom
=
8
ChannelTypeAILS
=
9
ChannelTypeAIProxy
=
10
ChannelTypePaLM
=
11
)
var
ChannelBaseURLs
=
[]
string
{
...
...
@@ -143,4 +144,5 @@ var ChannelBaseURLs = []string{
""
,
// 8
"https://api.caipacity.com"
,
// 9
"https://api.aiproxy.io"
,
// 10
""
,
// 11
}
controller/relay-palm.go
0 → 100644
View file @
0ba5ff9d
package
controller
import
(
"fmt"
"github.com/gin-gonic/gin"
)
type
PaLMChatMessage
struct
{
Author
string
`json:"author"`
Content
string
`json:"content"`
}
type
PaLMFilter
struct
{
Reason
string
`json:"reason"`
Message
string
`json:"message"`
}
// https://developers.generativeai.google/api/rest/generativelanguage/models/generateMessage#request-body
type
PaLMChatRequest
struct
{
Prompt
[]
Message
`json:"prompt"`
Temperature
float64
`json:"temperature"`
CandidateCount
int
`json:"candidateCount"`
TopP
float64
`json:"topP"`
TopK
int
`json:"topK"`
}
// https://developers.generativeai.google/api/rest/generativelanguage/models/generateMessage#response-body
type
PaLMChatResponse
struct
{
Candidates
[]
Message
`json:"candidates"`
Messages
[]
Message
`json:"messages"`
Filters
[]
PaLMFilter
`json:"filters"`
}
func
relayPaLM
(
openAIRequest
GeneralOpenAIRequest
,
c
*
gin
.
Context
)
*
OpenAIErrorWithStatusCode
{
// https://developers.generativeai.google/api/rest/generativelanguage/models/generateMessage
messages
:=
make
([]
PaLMChatMessage
,
0
,
len
(
openAIRequest
.
Messages
))
for
_
,
message
:=
range
openAIRequest
.
Messages
{
var
author
string
if
message
.
Role
==
"user"
{
author
=
"0"
}
else
{
author
=
"1"
}
messages
=
append
(
messages
,
PaLMChatMessage
{
Author
:
author
,
Content
:
message
.
Content
,
})
}
request
:=
PaLMChatRequest
{
Prompt
:
nil
,
Temperature
:
openAIRequest
.
Temperature
,
CandidateCount
:
openAIRequest
.
N
,
TopP
:
openAIRequest
.
TopP
,
TopK
:
openAIRequest
.
MaxTokens
,
}
// TODO: forward request to PaLM & convert response
fmt
.
Print
(
request
)
return
nil
}
controller/relay.go
View file @
0ba5ff9d
...
...
@@ -19,6 +19,19 @@ type Message struct {
Name
*
string
`json:"name,omitempty"`
}
// https://platform.openai.com/docs/api-reference/chat
type
GeneralOpenAIRequest
struct
{
Model
string
`json:"model"`
Messages
[]
Message
`json:"messages"`
Prompt
string
`json:"prompt"`
Stream
bool
`json:"stream"`
MaxTokens
int
`json:"max_tokens"`
Temperature
float64
`json:"temperature"`
TopP
float64
`json:"top_p"`
N
int
`json:"n"`
}
type
ChatRequest
struct
{
Model
string
`json:"model"`
Messages
[]
Message
`json:"messages"`
...
...
@@ -101,8 +114,8 @@ func relayHelper(c *gin.Context) *OpenAIErrorWithStatusCode {
channelType
:=
c
.
GetInt
(
"channel"
)
tokenId
:=
c
.
GetInt
(
"token_id"
)
consumeQuota
:=
c
.
GetBool
(
"consume_quota"
)
var
textRequest
Text
Request
if
consumeQuota
||
channelType
==
common
.
ChannelTypeAzure
{
var
textRequest
GeneralOpenAI
Request
if
consumeQuota
||
channelType
==
common
.
ChannelTypeAzure
||
channelType
==
common
.
ChannelTypePaLM
{
requestBody
,
err
:=
io
.
ReadAll
(
c
.
Request
.
Body
)
if
err
!=
nil
{
return
errorWrapper
(
err
,
"read_request_body_failed"
,
http
.
StatusBadRequest
)
...
...
@@ -141,6 +154,9 @@ func relayHelper(c *gin.Context) *OpenAIErrorWithStatusCode {
model_
=
strings
.
TrimSuffix
(
model_
,
"-0301"
)
model_
=
strings
.
TrimSuffix
(
model_
,
"-0314"
)
fullRequestURL
=
fmt
.
Sprintf
(
"%s/openai/deployments/%s/%s"
,
baseURL
,
model_
,
task
)
}
else
if
channelType
==
common
.
ChannelTypePaLM
{
err
:=
relayPaLM
(
textRequest
,
c
)
return
err
}
promptTokens
:=
countTokenMessages
(
textRequest
.
Messages
,
textRequest
.
Model
)
...
...
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