Skip to content

Commit

Permalink
update golang unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
userpj committed Aug 8, 2024
1 parent fa5af9f commit 0c1f4a4
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go/appbuilder/app_builder_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (t *AppBuilderClient) Run(conversationID string, query string, fileIDS []st
return &AppBuilderClientOnceIterator{body: resp.Body}, nil
}

func (t *AppBuilderClient) RunWithFunction(req AppBuilderClientRunRequest) (AppBuilderClientIterator, error) {
func (t *AppBuilderClient) RunWithFunctionCall(req AppBuilderClientRunRequest) (AppBuilderClientIterator, error) {
if len(req.ConversationID) == 0 {
return nil, errors.New("conversationID mustn't be empty")
}
Expand Down
106 changes: 106 additions & 0 deletions go/appbuilder/app_builder_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,109 @@ func TestNewAppBuilderClient(t *testing.T) {
fmt.Println("----------------answer-------------------")
fmt.Println(totalAnswer)
}

func TestAppBuilderClientRunWithFunctionCall(t *testing.T) {
os.Setenv("APPBUILDER_LOGLEVEL", "DEBUG")
os.Setenv("APPBUILDER_LOGFILE", "")
config, err := NewSDKConfig("", "")
if err != nil {
t.Fatalf("new http client config failed: %v", err)
}

appID := ""
client, err := NewAppBuilderClient(appID, config)
if err != nil {
t.Fatalf("new AgentBuidler instance failed")
}

conversationID, err := client.CreateConversation()
if err != nil {
t.Fatalf("create conversation failed: %v", err)
}

parameters := make(map[string]interface{})

location := make(map[string]interface{})
location["type"] = "string"
location["description"] = "省,市名,例如:河北省"

unit := make(map[string]interface{})
unit["type"] = "string"
unit["enum"] = []string{"摄氏度", "华氏度"}

properties := make(map[string]interface{})
properties["location"] = location
properties["unit"] = unit

parameters["type"] = "object"
parameters["properties"] = properties
parameters["required"] = []string{"location"}

i, err := client.RunWithFunctionCall(AppBuilderClientRunRequest{
AppID: appID,
Query: "今天北京的天气怎么样?",
ConversationID: conversationID,
Stream: false,
Tools: []Tool{
{
Type: "function",
Function: Function{
Name: "get_cur_whether",
Description: "这是一个获得指定地点天气的工具",
Parameters: parameters,
},
},
},
})
if err != nil {
t.Fatalf("run failed:%v", err)
}
totalAnswer := ""
for answer, err := i.Next(); err == nil; answer, err = i.Next() {
totalAnswer = totalAnswer + answer.Answer
for _, ev := range answer.Events {
fmt.Println("------------usage------------")
usageJson, _ := json.Marshal(ev.Usage)
fmt.Printf("%s\n", usageJson)
if ev.ContentType == TextContentType {
detail := ev.Detail.(TextDetail)
fmt.Println("---------------TextDetail------------")
fmt.Println(detail)
} else if ev.ContentType == CodeContentType {
detail := ev.Detail.(CodeDetail)
fmt.Println("---------------CodeDetail------------")
fmt.Println(detail)
} else if ev.ContentType == ImageContentType {
detail := ev.Detail.(ImageDetail)
fmt.Println("---------------ImageDetail------------")
fmt.Println(detail)
} else if ev.ContentType == RAGContentType {
detail := ev.Detail.(RAGDetail)
fmt.Println("---------------RAGDetail------------")
fmt.Println(detail)
} else if ev.ContentType == FunctionCallContentType {
detail := ev.Detail.(FunctionCallDetail)
fmt.Println("---------------FunctionCallDetail------------")
fmt.Println(detail)
} else if ev.ContentType == AudioContentType {
fmt.Println("---------------AudioDetail------------")
detail := ev.Detail.(AudioDetail)
fmt.Println(detail.Audio)
} else if ev.ContentType == VideoContentType {
fmt.Println("---------------VideoDetail------------")
detail := ev.Detail.(VideoDetail)
fmt.Println(detail)
} else if ev.ContentType == StatusContentType {
} else { // 默认是json.RawMessage
detail, ok := ev.Detail.(json.RawMessage)
if !ok {
t.Fatalf("unknown detail type")
}
fmt.Println("---------------rawMessage------------")
fmt.Println(string(detail))
}
}
}
fmt.Println("----------------answer-------------------")
fmt.Println(totalAnswer)
}

0 comments on commit 0c1f4a4

Please sign in to comment.