Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
ccf6af4d
Unverified
Commit
ccf6af4d
authored
Mar 08, 2023
by
boojack
Committed by
GitHub
Mar 08, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: request body format in openai api (#1309)
parent
ce7564a9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
15 deletions
+26
-15
chat_completion.go
plugin/openai/chat_completion.go
+11
-6
text_completion.go
plugin/openai/text_completion.go
+15
-9
No files found.
plugin/openai/chat_completion.go
View file @
ccf6af4d
package
openai
import
(
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"
)
type
ChatCompletionMessage
struct
{
...
...
@@ -25,10 +25,6 @@ type ChatCompletionResponse struct {
}
func
PostChatCompletion
(
prompt
string
,
apiKey
string
,
apiHost
string
)
(
string
,
error
)
{
requestBody
:=
strings
.
NewReader
(
`{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "`
+
prompt
+
`"}]
}`
)
if
apiHost
==
""
{
apiHost
=
"https://api.openai.com"
}
...
...
@@ -37,7 +33,16 @@ func PostChatCompletion(prompt string, apiKey string, apiHost string) (string, e
return
""
,
err
}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
requestBody
)
values
:=
map
[
string
]
interface
{}{
"model"
:
"gpt-3.5-turbo"
,
"messages"
:
[]
map
[
string
]
string
{{
"role"
:
"user"
,
"content"
:
prompt
}},
}
jsonValue
,
err
:=
json
.
Marshal
(
values
)
if
err
!=
nil
{
return
""
,
err
}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
bytes
.
NewBuffer
(
jsonValue
))
if
err
!=
nil
{
return
""
,
err
}
...
...
plugin/openai/text_completion.go
View file @
ccf6af4d
package
openai
import
(
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"
)
type
TextCompletionChoice
struct
{
...
...
@@ -20,13 +20,6 @@ type TextCompletionResponse struct {
}
func
PostTextCompletion
(
prompt
string
,
apiKey
string
,
apiHost
string
)
(
string
,
error
)
{
requestBody
:=
strings
.
NewReader
(
`{
"prompt": "`
+
prompt
+
`",
"temperature": 0.5,
"max_tokens": 100,
"n": 1,
"stop": "."
}`
)
if
apiHost
==
""
{
apiHost
=
"https://api.openai.com"
}
...
...
@@ -35,7 +28,20 @@ func PostTextCompletion(prompt string, apiKey string, apiHost string) (string, e
return
""
,
err
}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
requestBody
)
values
:=
map
[
string
]
interface
{}{
"model"
:
"gpt-3.5-turbo"
,
"prompt"
:
prompt
,
"temperature"
:
0.5
,
"max_tokens"
:
100
,
"n"
:
1
,
"stop"
:
"."
,
}
jsonValue
,
err
:=
json
.
Marshal
(
values
)
if
err
!=
nil
{
return
""
,
err
}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
bytes
.
NewBuffer
(
jsonValue
))
if
err
!=
nil
{
return
""
,
err
}
...
...
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