Home

Awesome

文心千帆 GO SDK

Go Reference

本库为文心千帆GO语言SDK,非官方库,目前官方还没有GO语言的SDK 文心千帆

已支持自动刷新 accessToken, 需要通过 ernie.NewDefaultClient("API Key", "Secret Key") 方式初始化客户端

目前支持的模型:

安装

go get github.com/anhao/go-ernie

需要 go 版本为 1.18+

使用示例

package main

import (
	"context"
	"fmt"
	ernie "github.com/anhao/go-ernie"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	completion, err := client.CreateErnieBotChatCompletion(context.Background(), ernie.ErnieBotRequest{
		Messages: []ernie.ChatCompletionMessage{
			{
				Role:    ernie.MessageRoleUser,
				Content: "你好呀",
			},
		},
	})
	if err != nil {
		fmt.Printf("ernie bot error: %v\n", err)
		return
	}
	fmt.Println(completion)
}

获取文心千帆 APIKEY

  1. 在百度云官网进行申请:https://cloud.baidu.com/product/wenxinworkshop
  2. 申请通过后创建应用:https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application
  3. 获取 apikey 和 api secret

其他示例

<details> <summary>ERNIE-Bot 4.0 对话 </summary>
import (
	"context"
	"errors"
	"fmt"
	ernie "github.com/anhao/go-ernie"
	"io"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	request := ernie.ErnieBot4Request{
		Messages: []ChatCompletionMessage{
			{
				Role:    "user",
				Content: "Hello",
			},
		},
		Stream: true,
	}

	stream, err := client.CreateErnieBot4ChatCompletionStream(context.Background(), request)
	if err != nil {
		fmt.Printf("ernie bot stream error: %v\n", err)
		return
	}
	defer stream.Close()
	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			fmt.Println("ernie bot 4 Stream finished")
			return
		}
		if err != nil {
			fmt.Printf("ernie bot 4 stream error: %v\n", err)
			return
		}
		fmt.Println(response.Result)
	}
}
</details> <details> <summary>ERNIE-Bot stream流 对话 </summary>
import (
	"context"
	"errors"
	"fmt"
	ernie "github.com/anhao/go-ernie"
	"io"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	request := ernie.ErnieBotRequest{
		Messages: []ChatCompletionMessage{
			{
				Role:    "user",
				Content: "Hello",
			},
		},
		Stream: true,
	}

	stream, err := client.CreateErnieBotChatCompletionStream(context.Background(), request)
	if err != nil {
		fmt.Printf("ernie bot stream error: %v\n", err)
		return
	}
	defer stream.Close()
	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			fmt.Println("ernie bot Stream finished")
			return
		}
		if err != nil {
			fmt.Printf("ernie bot stream error: %v\n", err)
			return
		}
		fmt.Println(response.Result)
	}
}
</details> <details> <summary>ERNIE-Bot-Turbo 对话 </summary>
package main

import (
	"context"
	"fmt"
	ernie "github.com/anhao/go-ernie"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	completion, err := client.CreateErnieBotTurboChatCompletion(context.Background(), ernie.ErnieBotTurboRequest{
		Messages: []ernie.ChatCompletionMessage{
			{
				Role:    ernie.MessageRoleUser,
				Content: "你好呀",
			},
		},
	})
	if err != nil {
		fmt.Printf("ernie bot turbo error: %v\n", err)
		return
	}
	fmt.Println(completion)
}
</details> <details> <summary>ERNIE-Bot Turbo stream流 对话 </summary>
import (
	"context"
	"errors"
	"fmt"
	ernie "github.com/anhao/go-ernie"
	"io"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	request := ernie.ErnieBotTurboRequest{
		Messages: []ChatCompletionMessage{
			{
				Role:    "user",
				Content: "Hello",
			},
		},
		Stream: true,
	}

	stream, err := client.CreateErnieBotTurboChatCompletionStream(context.Background(), request)
	if err != nil {
		fmt.Printf("ernie bot stream error: %v\n", err)
		return
	}
	defer stream.Close()
	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			fmt.Println("ernie bot turbo Stream finished")
			return
		}
		if err != nil {
			fmt.Printf("ernie bot turbo stream error: %v\n", err)
			return
		}
		fmt.Println(response.Result)
	}
}
</details> <details> <summary>BLOOMZ-7B 对话 </summary>
package main

import (
	"context"
	"fmt"
	ernie "github.com/anhao/go-ernie"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	completion, err := client.CreateBloomz7b1ChatCompletion(context.Background(), ernie.Bloomz7b1Request{
		Messages: []ernie.ChatCompletionMessage{
			{
				Role:    ernie.MessageRoleUser,
				Content: "你好呀",
			},
		},
	})
	if err != nil {
		fmt.Printf("BLOOMZ-7B error: %v\n", err)
		return
	}
	fmt.Println(completion)
}
</details> <details> <summary>BLOOMZ-7B stream流 对话 </summary>
import (
	"context"
	"errors"
	"fmt"
	ernie "github.com/anhao/go-ernie"
	"io"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	request := ernie.Bloomz7b1Request{
		Messages: []ChatCompletionMessage{
			{
				Role:    "user",
				Content: "Hello",
			},
		},
		Stream: true,
	}

	stream, err := client.CreateBloomz7b1ChatCompletionStream(context.Background(), request)
	if err != nil {
		fmt.Printf("BLOOMZ-7B error: %v\n", err)
		return
	}
	defer stream.Close()
	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			fmt.Println("BLOOMZ-7B  Stream finished")
			return
		}
		if err != nil {
			fmt.Printf("BLOOMZ-7B stream error: %v\n", err)
			return
		}
		fmt.Println(response.Result)
	}
}
</details> <details> <summary>Llama2 对话 </summary>
package main

import (
	"context"
	"fmt"
	ernie "github.com/anhao/go-ernie"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	completion, err := client.CreateLlamaChatCompletion(context.Background(), ernie.LlamaChatRequest{
		Messages: []ernie.ChatCompletionMessage{
			{
				Role:    ernie.MessageRoleUser,
				Content: "你好呀",
			},
		},
		Model: "", //申请发布时填写的API名称
	})
	if err != nil {
		fmt.Printf("llama2 error: %v\n", err)
		return
	}
	fmt.Println(completion)
}
</details> <details> <summary>Llama2 stream流 对话 </summary>
import (
	"context"
	"errors"
	"fmt"
	ernie "github.com/anhao/go-ernie"
	"io"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	request := ernie.LlamaChatRequest{
		Messages: []ChatCompletionMessage{
			{
				Role:    "user",
				Content: "Hello",
			},
		},
		Stream: true,
		Model: "", //申请发布时填写的API名称
	}

	stream, err := client.CreateLlamaChatCompletionStream(context.Background(), request)
	if err != nil {
		fmt.Printf("llama2 error: %v\n", err)
		return
	}
	defer stream.Close()
	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			fmt.Println("llama2  Stream finished")
			return
		}
		if err != nil {
			fmt.Printf("llama2 stream error: %v\n", err)
			return
		}
		fmt.Println(response.Result)
	}
}
</details> <details> <summary>Embedding向量</summary>
package main

import (
	"context"
	"fmt"
	ernie "github.com/anhao/go-ernie"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	request := ernie.EmbeddingRequest{
		Input: []string{
			"Hello",
		},
	}
	embeddings, err := client.CreateEmbeddings(context.Background(), request)
	if err != nil {
		fmt.Sprintf("embeddings err: %v", err)
		return
	}
	fmt.Println(embeddings)
}

</details> <details> <summary>自定义 accessToken</summary>
client :=ernie.NewClient("accessToken")
</details> <details> <summary>百度云其他Chat模型</summary> 百度云的其他模型需要自己部署,部署需要填写一个API地址
import (
	"context"
	"errors"
	"fmt"
	ernie "github.com/anhao/go-ernie"
	"io"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	request := ernie.BaiduChatRequest{
		Messages: []ChatCompletionMessage{
			{
				Role:    "user",
				Content: "Hello",
			},
		},
		Stream: true,
		Model: "", //申请发布时填写的API名称
	}

	stream, err := client.CreateBaiduChatCompletionStream(context.Background(), request)
	if err != nil {
		fmt.Printf("baidu chat error: %v\n", err)
		return
	}
	defer stream.Close()
	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			fmt.Println("baidu chat  Stream finished")
			return
		}
		if err != nil {
			fmt.Printf("baidu chat stream error: %v\n", err)
			return
		}
		fmt.Println(response.Result)
	}
}
</details> <details> <summary>文本生成图片</summary> 百度云的其他模型需要自己部署,部署需要填写一个API地址
import (
	"context"
	"errors"
	"fmt"
	ernie "github.com/anhao/go-ernie"
	"io"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	request := ernie.BaiduTxt2ImgRequest{
		Model: "", //申请发布时填写的API名称
		Prompt: "",//提示词
	}

    response, err := client.CreateBaiduTxt2Img(context.Background(), request)
	if err != nil {
		fmt.Printf("baidu txt2img error: %v\n", err)
		return
	}
	fmt.Println(response)
}
</details> <details> <summary>插件应用</summary>
import (
	"context"
	"errors"
	"fmt"
	ernie "github.com/anhao/go-ernie"
	"io"
)

func main() {

	client := ernie.NewDefaultClient("API Key", "Secret Key")
	request := ernie.ErnieCustomPluginRequest{
		PluginName: "xxx", // 插件名称
		Query:      "你好",// 查询信息
		Stream:     false,
	}

    response, err := client.CreateErnieCustomPluginChatCompletion(context.Background(), request)
	if err != nil {
		fmt.Printf("ernie custom plugins error: %v\n", err)
		return
	}
	fmt.Println(response)
}
</details>