简体中文 | English
为 月之暗面/MoonshotAI 创建的AI Kimi 提供的 Go SDK。
- 人体工学的API,链式操作
- 完全的API支持
- 预定义枚举
API | Done |
---|---|
Chat Completion | ✅ |
Chat Completion(stream) | ✅ |
List Models | ✅ |
List Files | ✅ |
Upload File | ✅ |
Delete File | ✅ |
Get File Info | ✅ |
Get File Contents | ✅ |
Estimate Token Count | ✅ |
- 申请一个 MoonshotAI API Key: https://platform.moonshot.cn
- 使用配置文件或环境变量设置密钥
⚠️ 注意:您的API密钥是敏感信息。 不要和任何人分享。
key, ok := os.LookupEnv("MOONSHOT_KEY")
if !ok {
return errors.New("missing environment variable: moonshot_key")
}
cli, err := moonshot.NewClient(key)
if err != nil {
return err
}
key, ok := os.LookupEnv("MOONSHOT_KEY")
if !ok {
return errors.New("missing environment variable: moonshot_key")
}
cli, err := moonshot.NewClientWithConfig(
moonshot.NewConfig(
moonshot.WithAPIKey("xxxx"),
),
)
// 列出可用模型
resp, err := cli.Models().List(context.Background())
if err != nil {
return err
}
// Chat completions(stream)
resp, err := cli.Chat().CompletionsStream(context.Background(), &moonshot.ChatCompletionsRequest{
Model: moonshot.ModelMoonshotV18K,
Messages: []*moonshot.ChatCompletionsMessage{
{
Role: moonshot.RoleUser,
Content: "你好,我叫李雷,1+1等于多少?",
},
},
Temperature: 0.3,
Stream: true,
})
if err != nil {
return err
}
for receive := range resp.Receive() {
msg, err := receive.GetMessage()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
break
}
switch msg.Role {
case moonshot.RoleSystem,moonshot.RoleUser,moonshot.RoleAssistant:
// do something...
default:
// do something...
}
}
This is open-sourced library licensed under the MIT license.