- Notifications
You must be signed in to change notification settings - Fork5
🚗 ERNIE wrapper for Go 文心一言 Go语言SDK
License
NotificationsYou must be signed in to change notification settings
ConnectAI-E/Go-Wenxin
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
百度文心一言 golang sdk
- 全接口字段注释
- ChatCompletions 文心一言云服务
- ChatEbInstant ErnieBot-Lite
- 无缝对接官方文档:单轮问答、历史记忆问答、流返回
- 支持Swagger文档 和 ApiFox 文档

- 访问https://console.bce.baidu.com/ai/#/ai/wenxinworkshop/app/list 并创建应用。
- 取 API Key 作为 ClientId,Secret Key 作为 ClientSecret。
- 使用 New 方法并根据传参说明生成 client。
package mainimport ("context""fmt""github.com/ConnectAI-E/go-wenxin/baidubce"ai_customv1"github.com/ConnectAI-E/go-wenxin/gen/go/baidubce/ai_custom/v1"baidubcev1"github.com/ConnectAI-E/go-wenxin/gen/go/baidubce/v1")//init clientfuncmain() {ctx:=context.Background()varopts []baidubce.Optionopts=append(opts,baidubce.WithTokenRequest(&baidubcev1.TokenRequest{GrantType:"client_credentials",ClientId:"YOUR BAIDU_API Key",ClientSecret:"YOUR BAIDU_SECRET Key",}))client,_:=baidubce.New(opts...)//chatreq:=&ai_customv1.ChatCompletionsRequest{User:"feishu-user",Messages: []*ai_customv1.Message{{Role:"user",Content:"嗨"},},}res,_:=client.ChatCompletions(ctx,req)fmt.Println(res.Result)// output: 嗨!有什么我可以帮助你的吗?}
ErnieBot completion
package mainimport ("context""fmt""github.com/ConnectAI-E/go-wenxin/baidubce"ai_customv1"github.com/ConnectAI-E/go-wenxin/gen/go/baidubce/ai_custom/v1"baidubcev1"github.com/ConnectAI-E/go-wenxin/gen/go/baidubce/v1")//init clientfuncmain() {ctx:=context.Background()varopts []baidubce.Optionopts=append(opts,baidubce.WithTokenRequest(&baidubcev1.TokenRequest{GrantType:"client_credentials",ClientId:"YOUR BAIDU_API Key",ClientSecret:"YOUR BAIDU_SECRET Key",}))client,_:=baidubce.New(opts...)//chatreq:=&ai_customv1.ChatCompletionsRequest{User:"feishu-user",Messages: []*ai_customv1.Message{{Role:"user",Content:"嗨"},},}res,_:=client.ChatCompletions(ctx,req)fmt.Println(res.Result)// output: 嗨!有什么我可以帮助你的吗?}
ErnieBot stream completion
package mainimport ("context""errors""fmt""github.com/ConnectAI-E/go-wenxin/baidubce"ai_customv1"github.com/ConnectAI-E/go-wenxin/gen/go/baidubce/ai_custom/v1"baidubcev1"github.com/ConnectAI-E/go-wenxin/gen/go/baidubce/v1""io")//init clientfuncmain() {ctx:=context.Background()varopts []baidubce.Optionopts=append(opts,baidubce.WithTokenRequest(&baidubcev1.TokenRequest{GrantType:"client_credentials",ClientId:"YOUR BAIDU_API Key",ClientSecret:"YOUR BAIDU_SECRET Key",}))client,_:=baidubce.New(opts...)//chatreq:=&ai_customv1.ChatCompletionsRequest{Stream:true,Messages: []*ai_customv1.Message{{Role:"user",Content:"用鲁迅的口气写一封道歉信,给领导说对不起,我不该在会议上睡觉。200字左右"},},}stream,_:=client.ChatCompletionsStream(ctx,req)deferstream.CloseSend()for {response,err:=stream.Recv()iferrors.Is(err,io.EOF) {break}iferr!=nil {fmt.Println(err)break}fmt.Printf(response.Result)}}
ErnieBot history stream completion
package mainimport ("context""errors""fmt""github.com/ConnectAI-E/go-wenxin/baidubce"ai_customv1"github.com/ConnectAI-E/go-wenxin/gen/go/baidubce/ai_custom/v1"baidubcev1"github.com/ConnectAI-E/go-wenxin/gen/go/baidubce/v1""io")//init clientfuncmain() {ctx:=context.Background()varopts []baidubce.Optionopts=append(opts,baidubce.WithTokenRequest(&baidubcev1.TokenRequest{GrantType:"client_credentials",ClientId:"YOUR BAIDU_API Key",ClientSecret:"YOUR BAIDU_SECRET Key",}))client,_:=baidubce.New(opts...)//chatreq:=&ai_customv1.ChatCompletionsRequest{Stream:true,Messages: []*ai_customv1.Message{{Role:"user",Content:"推荐三本书"},{Role:"assistant",Content:"1. 《活着》、《许三观卖血记》和《被掩埋的巨人》\n2. 《小王子》、《浮生六记》和《人类简史》\n3. 《百年孤独》、《查泰莱夫人的情人》和《1984》"},{Role:"user",Content:"从中选一本适合小朋友看的"},},}stream,_:=client.ChatCompletionsStream(ctx,req)deferstream.CloseSend()for {response,err:=stream.Recv()iferrors.Is(err,io.EOF) {break}iferr!=nil {fmt.Println(err)break}fmt.Printf(response.Result)//建议选择《小王子》一书,这本书的故事简单易懂,讲述了一个外星王子来到地球上的经历,以及他与一朵玫瑰、一只狐狸之间的奇遇。这本书的插图也很精美,有助于小朋友理解故事情节。此外,《小王子》一书中的寓言和哲理也对小朋友有一定启示作用,帮助他们理解人性}}
About
🚗 ERNIE wrapper for Go 文心一言 Go语言SDK
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published