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
b27004da
Commit
b27004da
authored
Apr 13, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: retire unused plugin
parent
75359854
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
0 additions
and
171 deletions
+0
-171
chat_completion.go
plugin/openai/chat_completion.go
+0
-88
text_completion.go
plugin/openai/text_completion.go
+0
-83
No files found.
plugin/openai/chat_completion.go
deleted
100644 → 0
View file @
75359854
package
openai
import
(
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
)
type
ChatCompletionMessage
struct
{
Role
string
`json:"role"`
Content
string
`json:"content"`
}
type
ChatCompletionChoice
struct
{
Message
*
ChatCompletionMessage
`json:"message"`
}
type
ChatCompletionResponse
struct
{
Error
any
`json:"error"`
Model
string
`json:"model"`
Choices
[]
ChatCompletionChoice
`json:"choices"`
}
func
PostChatCompletion
(
messages
[]
ChatCompletionMessage
,
apiKey
string
,
apiHost
string
)
(
string
,
error
)
{
if
apiHost
==
""
{
apiHost
=
"https://api.openai.com"
}
url
,
err
:=
url
.
JoinPath
(
apiHost
,
"/v1/chat/completions"
)
if
err
!=
nil
{
return
""
,
err
}
values
:=
map
[
string
]
any
{
"model"
:
"gpt-3.5-turbo"
,
"messages"
:
messages
,
"max_tokens"
:
2000
,
"temperature"
:
0
,
"frequency_penalty"
:
0.0
,
"presence_penalty"
:
0.0
,
}
jsonValue
,
err
:=
json
.
Marshal
(
values
)
if
err
!=
nil
{
return
""
,
err
}
req
,
err
:=
http
.
NewRequest
(
"POST"
,
url
,
bytes
.
NewBuffer
(
jsonValue
))
if
err
!=
nil
{
return
""
,
err
}
// Set the API key in the request header
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
apiKey
)
// Send the request to OpenAI's API
client
:=
http
.
Client
{}
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
return
""
,
err
}
defer
resp
.
Body
.
Close
()
// Read the response body
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
""
,
err
}
chatCompletionResponse
:=
ChatCompletionResponse
{}
err
=
json
.
Unmarshal
(
responseBody
,
&
chatCompletionResponse
)
if
err
!=
nil
{
return
""
,
err
}
if
chatCompletionResponse
.
Error
!=
nil
{
errorBytes
,
err
:=
json
.
Marshal
(
chatCompletionResponse
.
Error
)
if
err
!=
nil
{
return
""
,
err
}
return
""
,
errors
.
New
(
string
(
errorBytes
))
}
if
len
(
chatCompletionResponse
.
Choices
)
==
0
{
return
""
,
nil
}
return
chatCompletionResponse
.
Choices
[
0
]
.
Message
.
Content
,
nil
}
plugin/openai/text_completion.go
deleted
100644 → 0
View file @
75359854
package
openai
import
(
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
)
type
TextCompletionChoice
struct
{
Text
string
`json:"text"`
}
type
TextCompletionResponse
struct
{
Error
any
`json:"error"`
Model
string
`json:"model"`
Choices
[]
TextCompletionChoice
`json:"choices"`
}
func
PostTextCompletion
(
prompt
string
,
apiKey
string
,
apiHost
string
)
(
string
,
error
)
{
if
apiHost
==
""
{
apiHost
=
"https://api.openai.com"
}
url
,
err
:=
url
.
JoinPath
(
apiHost
,
"/v1/chat/completions"
)
if
err
!=
nil
{
return
""
,
err
}
values
:=
map
[
string
]
any
{
"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
}
// Set the API key in the request header
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
apiKey
)
// Send the request to OpenAI's API
client
:=
http
.
Client
{}
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
return
""
,
err
}
defer
resp
.
Body
.
Close
()
// Read the response body
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
""
,
err
}
textCompletionResponse
:=
TextCompletionResponse
{}
err
=
json
.
Unmarshal
(
responseBody
,
&
textCompletionResponse
)
if
err
!=
nil
{
return
""
,
err
}
if
textCompletionResponse
.
Error
!=
nil
{
errorBytes
,
err
:=
json
.
Marshal
(
textCompletionResponse
.
Error
)
if
err
!=
nil
{
return
""
,
err
}
return
""
,
errors
.
New
(
string
(
errorBytes
))
}
if
len
(
textCompletionResponse
.
Choices
)
==
0
{
return
""
,
nil
}
return
textCompletionResponse
.
Choices
[
0
]
.
Text
,
nil
}
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