提供了对gpt-3.5-turbo的支持
This commit is contained in:
parent
f17fc31813
commit
fe5a8ef0c6
@ -14,51 +14,77 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ChatGPTResponseBody 请求体
|
type ChatMessage struct {
|
||||||
|
Role string `json:"role"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChatGPTRequestBody 请求体
|
||||||
|
type ChatGPTRequestBody struct {
|
||||||
|
Model string `json:"model"`
|
||||||
|
Messages []ChatMessage `json:"messages"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResponseChoice struct {
|
||||||
|
Index int `json:"index"`
|
||||||
|
Message ChatMessage `json:"message"`
|
||||||
|
FinishReason string `json:"finish_reason"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResponseUsage struct {
|
||||||
|
PromptTokens int `json:"prompt_tokens"`
|
||||||
|
CompletionTokens int `json:"completion_tokens"`
|
||||||
|
TotalTokens int `json:"total_tokens"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChatGPTResponseBody 响应体
|
||||||
type ChatGPTResponseBody struct {
|
type ChatGPTResponseBody struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Object string `json:"object"`
|
Object string `json:"object"`
|
||||||
Created int `json:"created"`
|
Created int `json:"created"`
|
||||||
Model string `json:"model"`
|
Choices []ResponseChoice `json:"choices"`
|
||||||
Choices []map[string]interface{} `json:"choices"`
|
Usage ResponseUsage `json:"usage"`
|
||||||
Usage map[string]interface{} `json:"usage"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatGPTErrorBody struct {
|
type ChatGPTErrorBody struct {
|
||||||
Error map[string]interface{} `json:"error"`
|
Error map[string]interface{} `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChatGPTRequestBody 响应体
|
/*
|
||||||
type ChatGPTRequestBody struct {
|
curl https://api.openai.com/v1/chat/completions \
|
||||||
Model string `json:"model"`
|
-H 'Content-Type: application/json' \
|
||||||
Prompt string `json:"prompt"`
|
-H 'Authorization: Bearer YOUR_API_KEY' \
|
||||||
MaxTokens int `json:"max_tokens"`
|
-d '{
|
||||||
Temperature float32 `json:"temperature"`
|
"model": "gpt-3.5-turbo",
|
||||||
TopP int `json:"top_p"`
|
"messages": [{"role": "user", "content": "Hello!"}]
|
||||||
FrequencyPenalty int `json:"frequency_penalty"`
|
}'
|
||||||
PresencePenalty int `json:"presence_penalty"`
|
|
||||||
|
{
|
||||||
|
"model": "gpt-3.5-turbo",
|
||||||
|
"messages": [{"role": "user", "content": "Hello!"}]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Completions https://api.openai.com/v1/completions
|
{
|
||||||
// nodejs example
|
"id": "chatcmpl-123",
|
||||||
// const { Configuration, OpenAIApi } = require("openai");
|
"object": "chat.completion",
|
||||||
//
|
"created": 1677652288,
|
||||||
// const configuration = new Configuration({
|
"choices": [{
|
||||||
// apiKey: process.env.OPENAI_API_KEY,
|
"index": 0,
|
||||||
// });
|
"message": {
|
||||||
// const openai = new OpenAIApi(configuration);
|
"role": "assistant",
|
||||||
//
|
"content": "\n\nHello there, how may I assist you today?",
|
||||||
// const response = await openai.createCompletion({
|
},
|
||||||
// model: "text-davinci-003",
|
"finish_reason": "stop"
|
||||||
// prompt: "I am a highly intelligent question answering bot. If you ask me a question that is rooted in truth, I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, I will respond with \"Unknown\".\n\nQ: What is human life expectancy in the United States?\nA: Human life expectancy in the United States is 78 years.\n\nQ: Who was president of the United States in 1955?\nA: Dwight D. Eisenhower was president of the United States in 1955.\n\nQ: Which party did he belong to?\nA: He belonged to the Republican Party.\n\nQ: What is the square root of banana?\nA: Unknown\n\nQ: How does a telescope work?\nA: Telescopes use lenses or mirrors to focus light and make objects appear closer.\n\nQ: Where were the 1992 Olympics held?\nA: The 1992 Olympics were held in Barcelona, Spain.\n\nQ: How many squigs are in a bonk?\nA: Unknown\n\nQ: Where is the Valley of Kings?\nA:",
|
}],
|
||||||
// temperature: 0,
|
"usage": {
|
||||||
// max_tokens: 100,
|
"prompt_tokens": 9,
|
||||||
// top_p: 1,
|
"completion_tokens": 12,
|
||||||
// frequency_penalty: 0.0,
|
"total_tokens": 21
|
||||||
// presence_penalty: 0.0,
|
}
|
||||||
// stop: ["\n"],
|
}
|
||||||
// });
|
|
||||||
//
|
*/
|
||||||
|
|
||||||
// Completions sendMsg
|
// Completions sendMsg
|
||||||
func Completions(msg string) (*string, error) {
|
func Completions(msg string) (*string, error) {
|
||||||
apiKey := config.GetOpenAiApiKey()
|
apiKey := config.GetOpenAiApiKey()
|
||||||
@ -66,14 +92,15 @@ func Completions(msg string) (*string, error) {
|
|||||||
return nil, errors.New("未配置apiKey")
|
return nil, errors.New("未配置apiKey")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var messages []ChatMessage
|
||||||
|
messages = append(messages, ChatMessage{
|
||||||
|
Role: "user",
|
||||||
|
Content: msg,
|
||||||
|
})
|
||||||
|
|
||||||
requestBody := ChatGPTRequestBody{
|
requestBody := ChatGPTRequestBody{
|
||||||
Model: "text-davinci-003",
|
Model: "gpt-3.5-turbo",
|
||||||
Prompt: msg,
|
Messages: messages,
|
||||||
MaxTokens: 4000,
|
|
||||||
Temperature: 0.7,
|
|
||||||
TopP: 1,
|
|
||||||
FrequencyPenalty: 0,
|
|
||||||
PresencePenalty: 0,
|
|
||||||
}
|
}
|
||||||
requestData, err := json.Marshal(requestBody)
|
requestData, err := json.Marshal(requestBody)
|
||||||
|
|
||||||
@ -83,7 +110,7 @@ func Completions(msg string) (*string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("request openai json string : %v", string(requestData))
|
log.Debugf("request openai json string : %v", string(requestData))
|
||||||
req, err := http.NewRequest("POST", "https://api.openai.com/v1/completions", bytes.NewBuffer(requestData))
|
req, err := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", bytes.NewBuffer(requestData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -120,8 +147,8 @@ func Completions(msg string) (*string, error) {
|
|||||||
var reply string
|
var reply string
|
||||||
if len(gptResponseBody.Choices) > 0 {
|
if len(gptResponseBody.Choices) > 0 {
|
||||||
for _, v := range gptResponseBody.Choices {
|
for _, v := range gptResponseBody.Choices {
|
||||||
reply = v["text"].(string)
|
reply += "\n"
|
||||||
break
|
reply += v.Message.Content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,7 +159,9 @@ func Completions(msg string) (*string, error) {
|
|||||||
log.Error(err)
|
log.Error(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
reply = gptErrorBody.Error["message"].(string)
|
|
||||||
|
reply += "Error: "
|
||||||
|
reply += gptErrorBody.Error["message"].(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &reply, nil
|
return &reply, nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user