Skip to content

Commit

Permalink
兼容run方法
Browse files Browse the repository at this point in the history
  • Loading branch information
userpj committed Nov 14, 2024
1 parent d1b216f commit 1e2b115
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
92 changes: 50 additions & 42 deletions go/appbuilder/app_builder_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"strconv"
"time"
)

type AppbuilderClient interface {
CreateConversation() (string, error)
Run(conversationID string, query string, fileIDS []string, stream bool)
}

// Deprecated: 将废弃,请使用DescribeApps替代
func GetAppList(req GetAppListRequest, config *SDKConfig) ([]App, error) {
request := http.Request{}
Expand Down Expand Up @@ -250,52 +256,22 @@ func (t *AppBuilderClient) UploadLocalFile(conversationID string, filePath strin
return val.(string), nil
}

func (t *AppBuilderClient) Run(conversationID string, query string, fileIDS []string, stream bool) (AppBuilderClientIterator, error) {
if len(conversationID) == 0 {
return nil, errors.New("conversationID mustn't be empty")
}
m := map[string]any{
"app_id": t.appID,
"conversation_id": conversationID,
"query": query,
"file_ids": fileIDS,
"stream": stream,
}
request := http.Request{}

serviceURL, err := t.sdkConfig.ServiceURLV2("/app/conversation/runs")
if err != nil {
return nil, err
func (t *AppBuilderClient) Run(param ...interface{}) (AppBuilderClientIterator, error) {
if len(param) == 0 {
return nil, errors.New("no arguments provided")
}
var err error
var req AppBuilderClientRunRequest

request.URL = serviceURL
request.Method = "POST"
header := t.sdkConfig.AuthHeaderV2()
header.Set("Content-Type", "application/json")
request.Header = header
data, _ := json.Marshal(m)
request.Body = NopCloser(bytes.NewReader(data))
request.ContentLength = int64(len(data)) // 手动设置长度

t.sdkConfig.BuildCurlCommand(&request)

resp, err := t.client.Do(&request)
if err != nil {
return nil, err
}

requestID, err := checkHTTPResponse(resp)
if err != nil {
return nil, fmt.Errorf("requestID=%s, err=%v", requestID, err)
}
r := NewSSEReader(1024*1024, bufio.NewReader(resp.Body))
if stream {
return &AppBuilderClientStreamIterator{requestID: requestID, r: r, body: resp.Body}, nil
if reflect.TypeOf(param[0]) == reflect.TypeOf(AppBuilderClientRunRequest{}) {
req = param[0].(AppBuilderClientRunRequest)
} else {
req, err = t.buildAppBuilderClientRunRequest(param...)
if err != nil {
return nil, err
}
}
return &AppBuilderClientOnceIterator{body: resp.Body}, nil
}

func (t *AppBuilderClient) RunV2(req AppBuilderClientRunRequest) (AppBuilderClientIterator, error) {
if len(req.ConversationID) == 0 {
return nil, errors.New("conversationID mustn't be empty")
}
Expand Down Expand Up @@ -332,6 +308,38 @@ func (t *AppBuilderClient) RunV2(req AppBuilderClientRunRequest) (AppBuilderClie
return &AppBuilderClientOnceIterator{body: resp.Body}, nil
}

func (t *AppBuilderClient) buildAppBuilderClientRunRequest(param ...interface{}) (AppBuilderClientRunRequest, error) {
conversationID, ok := param[0].(string)
if !ok {
return AppBuilderClientRunRequest{}, errors.New("conversationID must be string type")
}
query, ok := param[1].(string)
if !ok {
return AppBuilderClientRunRequest{}, errors.New("query must be string type")
}

var fileIDS []string
if param[2] != nil {
fileIDS, ok = param[2].([]string)
if !ok {
fileIDS = nil
}
}

stream, ok := param[3].(bool)
if !ok {
stream = false
}

return AppBuilderClientRunRequest{
AppID: t.appID,
ConversationID: conversationID,
Query: query,
Stream: stream,
FileIDs: fileIDS,
}, nil
}

func (t *AppBuilderClient) RunWithToolCall(req AppBuilderClientRunRequest) (AppBuilderClientIterator, error) {
if len(req.ConversationID) == 0 {
return nil, errors.New("conversationID mustn't be empty")
Expand Down
1 change: 1 addition & 0 deletions go/appbuilder/app_builder_client_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type AppBuilderClientRunRequest struct {
Stream bool `json:"stream"`
EndUserID *string `json:"end_user_id"`
ConversationID string `json:"conversation_id"`
FileIDs []string `json:"file_ids"`
Tools []Tool `json:"tools"`
ToolOutputs []ToolOutput `json:"tool_outputs"`
ToolChoice *ToolChoice `json:"tool_choice"`
Expand Down
6 changes: 3 additions & 3 deletions go/appbuilder/app_builder_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func TestAppBuilderClientRunToolChoice(t *testing.T) {
input := make(map[string]any)
input["city"] = "北京"
end_user_id := "go_user_id_0"
i, err := client.RunV2(AppBuilderClientRunRequest{
i, err := client.Run(AppBuilderClientRunRequest{
ConversationID: conversationID,
AppID: appID,
Query: "你能干什么",
Expand Down Expand Up @@ -635,7 +635,7 @@ func TestAppBuilderClientRunChatflow(t *testing.T) {
t.Fatalf("create conversation failed: %v", err)
}

i, err := client.RunV2(AppBuilderClientRunRequest{
i, err := client.Run(AppBuilderClientRunRequest{
ConversationID: conversationID,
AppID: appID,
Query: "查天气",
Expand Down Expand Up @@ -668,7 +668,7 @@ func TestAppBuilderClientRunChatflow(t *testing.T) {
t.Fatalf("interrupt id is empty")
}

i2, err := client.RunV2(AppBuilderClientRunRequest{
i2, err := client.Run(AppBuilderClientRunRequest{
ConversationID: conversationID,
AppID: appID,
Query: "我先查个航班动态",
Expand Down

0 comments on commit 1e2b115

Please sign in to comment.