-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
206 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// RequestOpenAiImages 请求OpenAi 图片模型 | ||
func RequestOpenAiImages(prompt string) string { | ||
url := "https://api.openai.com/v1/images/generations" | ||
fmt.Println("prompt:", prompt) | ||
reqData := requestImage{ | ||
Prompt: prompt, | ||
Size: "1024x1024", | ||
N: 1, | ||
ResponseFormat: "url", | ||
} | ||
reqBody, _ := json.Marshal(reqData) | ||
resp := httpRequest(http.MethodPost, url, bytes.NewReader(reqBody)) | ||
|
||
defer func() { | ||
if nil != resp { | ||
_ = resp.Body.Close() | ||
} | ||
}() | ||
decoder := json.NewDecoder(resp.Body) | ||
|
||
resBody := &responseImage{} | ||
|
||
_ = decoder.Decode(resBody) | ||
return resBody.Data[0].Url | ||
} | ||
|
||
// RequestOpenAiImageEdit 请求OpenAi 图片编辑模型 | ||
func RequestOpenAiImageEdit(prompt string) string { | ||
url := "https://api.openai.com/v1/images/edits" | ||
reqData := ImageEdit{ | ||
Image: "", | ||
Mask: "", | ||
Prompt: prompt, | ||
Size: "1024x1024", | ||
N: 1, | ||
ResponseFormat: "url", | ||
} | ||
reqBody, _ := json.Marshal(reqData) | ||
resp := httpRequest(http.MethodPost, url, bytes.NewReader(reqBody)) | ||
defer func() { | ||
if nil != resp { | ||
_ = resp.Body.Close() | ||
} | ||
}() | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package api | ||
|
||
type requestChat struct { | ||
Model string `json:"model"` | ||
Message []message `json:"messages"` | ||
MaxTokens int `json:"-"` | ||
Temperature float32 `json:"temperature"` | ||
TopP float32 `json:"top_p"` | ||
N int `json:"n"` | ||
Stream bool `json:"stream"` | ||
Stop string `json:"-"` | ||
} | ||
|
||
type message struct { | ||
Role string `json:"role"` | ||
Content string `json:"content"` | ||
} | ||
|
||
type requestImage struct { | ||
Prompt string `json:"prompt"` // 描述信息 | ||
N int `json:"n" default:"1"` | ||
Size string `json:"size" default:"1024x1024"` | ||
ResponseFormat string `json:"response_format" default:"url"` // b64_json | ||
} | ||
|
||
type ImageEdit struct { | ||
Image string `json:"image"` // 修改的头像 | ||
Mask string `json:"mask"` // 附加头像,需要编辑的位置 | ||
Prompt string `json:"prompt"` | ||
Size string `json:"size"` | ||
N int `json:"n"` | ||
ResponseFormat string `json:"response_format"` // 响应格式 url 或 b64_json | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package api | ||
|
||
type responseImage struct { | ||
Created int `json:"created"` | ||
Data []struct { | ||
B64Json string `json:"b64_json"` | ||
Url string `json:"url"` | ||
} `json:"data"` | ||
} | ||
|
||
type responseChat struct { | ||
Id string `json:"id"` | ||
Model string `json:"model"` | ||
Choices []struct { | ||
Delta struct { | ||
Role string `json:"role"` | ||
Content string `json:"content"` | ||
} `json:"delta"` | ||
FinishReason string `json:"finish_reason"` | ||
Index string `json:"index"` | ||
} `json:"choices"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,8 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
import "lucky-ai/src/com/lucky/ai/server" | ||
|
||
func main() { | ||
// 通过代理请求 curl https://www.google.com -x http://127.0.0.1:7890 | ||
//server.Server() | ||
str := "data: {\"id\":\"chatcmpl-6yOiFpOOqOOsfkj2285j6p1CHZr9Z\",\"object\":\"chat.completion.chunk\",\"created\":1679852695,\"model\":\"gpt-3.5-turbo-0301\",\"choices\":[{\"delta\":{\"content\":\"我\"},\"index\":0,\"finish_reason\":null}]}" | ||
//fmt.Println(str) | ||
bytes := []byte(str) | ||
for { | ||
var responseJSON map[string]interface{} | ||
err := json.Unmarshal(bytes, &responseJSON) | ||
if err != nil { | ||
break | ||
} | ||
choices := responseJSON["choices"].([]interface{}) | ||
if len(choices) > 0 { | ||
text := choices[0].(map[string]interface{})["delta"].(map[string]interface{})["content"].(string) | ||
fmt.Print(text) | ||
} | ||
} | ||
server.Server() | ||
} |