Skip to content

Commit

Permalink
feat: 修改content上传
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Nov 20, 2023
1 parent 4bb5cb0 commit c1b4936
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
17 changes: 17 additions & 0 deletions code/handlers/event_msg_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ func setDefaultPrompt(msg []openai.Messages) []openai.Messages {
return msg
}

//func setDefaultVisionPrompt(msg []openai.VisionMessages) []openai.VisionMessages {
// if !hasSystemRole(msg) {
// msg = append(msg, openai.VisionMessages{
// Role: "system", Content: []openai.ContentType{
// {Type: "text", Text: "You are ChatGPT4V, " +
// "You are ChatGPT4V, " +
// "a large language and picture model trained by" +
// " OpenAI. " +
// "Answer in user's language as concisely as" +
// " possible. Knowledge cutoff: 20230601 " +
// "Current date" + time.Now().Format("20060102"),
// }},
// })
// }
// return msg
//}

type MessageAction struct { /*消息*/
}

Expand Down
23 changes: 23 additions & 0 deletions code/handlers/event_vision_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ func (*VisionAction) Execute(a *ActionInfo) bool {
a.info.msgId)
return false
}
//
var msg []openai.VisionMessages
detail := a.handler.sessionCache.GetVisionDetail(*a.info.sessionId)
// 如果没有提示词,默认模拟ChatGPT
msg = append(msg, openai.VisionMessages{
Role: "user", Content: []openai.ContentType{
{
Type: "image", ImageURL: openai.
ImageURL{URL: base64, Detail: detail},
},
},
})
// get ai mode as temperature
fmt.Println("msg: ", msg)
completions, err := a.handler.gpt.GetVisionInfo(msg)
if err != nil {
replyMsg(*a.ctx, fmt.Sprintf(
"🤖️:消息机器人摆烂了,请稍后再试~\n错误信息: %v", err), a.info.msgId)
return false
}
msg = append(msg, completions)
a.handler.sessionCache.SetMsg(*a.info.sessionId, msg)

////图片校验
//err = openai.VerifyPngs([]string{f})
//if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions code/services/openai/gpt3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ func TestCompletions(t *testing.T) {
func TestVisionOnePic(t *testing.T) {
config := initialization.LoadConfig("../../config.yaml")
content := []ContentType{
{Type: "text", Text: "What’s in this image?"},
{Type: "image_url", URL: ImageURL{
{Type: "text", Text: "What’s in this image?", ImageURL: nil},
{Type: "image_url", ImageURL: &ImageURL{
URL: "https://resource.liaobots." +
"com/1849d492904448a0ac17f975f0b7ca8b.jpg",
Detail: "low",
Detail: "high",
}},
}
//turn content-json to str
//contentStr, err2 := json.Marshal(content)
//if err2 != nil {
// return
//}
msgs := []VisionMessages{
{Role: "user", Content: content},
{Role: "assistant", Content: content},
}
gpt := NewChatGPT(*config)
resp, err := gpt.GetVisionInfo(msgs)
Expand Down
14 changes: 6 additions & 8 deletions code/services/openai/vision.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import (
)

type ImageURL struct {
URL string `json:"url"`
URL string `json:"url,omitempty"`
Detail string `json:"detail,omitempty"`
}

type ContentType struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
URL ImageURL `json:"image_url,omitempty"`
// Add other fields as needed for different content types
Type string `json:"type"`
Text string `json:"text,omitempty"`
ImageURL *ImageURL `json:"image_url,omitempty"`
}

type VisionMessages struct {
Role string `json:"role"`
Content []ContentType `json:"content"`
Role string `json:"role"`
Content interface{} `json:"content"`
}

type VisionRequestBody struct {
Expand Down

0 comments on commit c1b4936

Please sign in to comment.