简体中文 | English
A Go SDK for Kimi which created by MoonshotAI.
Warning
This project is still actively developing, and the API may change with the release of the version. Please pay attention when you upgrade the version.
go get github.com/northes/go-moonshot@v0.5.2
You can find the docs at go docs.
- Easy to use and simple API, chain operation.
- Full API and builtin functions support.
- Predefined enumeration.
- Messages builder.
API | Done |
---|---|
Chat Completion | ✅ |
Chat Completion(stream) | ✅ |
List Models | ✅ |
List Files | ✅ |
Upload File | ✅ |
Delete File | ✅ |
Get File Info | ✅ |
Get File Contents | ✅ |
Estimate Token Count | ✅ |
User Balance | ✅ |
Tool Use | ✅ |
Context Cache | ✅ |
$web_search
For more examples, you can view the test file of the corresponding interface.
- Get a MoonshotAI API Key: https://platform.moonshot.cn.
- Set up key using a configuration file or environment variable.
Tip
Your API key is sensitive information. Do not share it with anyone.
key, ok := os.LookupEnv("MOONSHOT_KEY")
// do something...
cli, err := moonshot.NewClient(key)
// do something...
key, ok := os.LookupEnv("MOONSHOT_KEY")
// do something...
cli, err := moonshot.NewClientWithConfig(
moonshot.NewConfig(
moonshot.WithAPIKey(key),
),
)
resp, err := cli.Models().List(context.Background())
// do something...
// Use builder to build a request more conveniently
builder := moonshot.NewChatCompletionsBuilder()
builder.AppendPrompt("你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。").
AppendUser("你好,我叫李雷,1+1等于多少?").
WithTemperature(0.3)
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
// {"id":"cmpl-eb8e8474fbae4e42bea9f6bbf38d56ed","object":"chat.completion","created":2647921,"model":"moonshot-v1-8k","choices":[{"index":0,"message":{"role":"assistant","content":"你好,李雷!1+1等于2。这是一个基本的数学加法运算。如果你有任何其他问题或需要帮助,请随时告诉我。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":87,"completion_tokens":31,"total_tokens":118}}
// do something...
// append context
for _, choice := range resp.Choices {
builder.AppendMessage(choice.Message)
}
builder.AppendUser("在这个基础上再加3等于多少")
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
// {"id":"cmpl-a7b938eaddc04fbf85fe578a980040ac","object":"chat.completion","created":5455796,"model":"moonshot-v1-8k","choices":[{"index":0,"message":{"role":"assistant","content":"在这个基础上,即1+1=2的结果上再加3,等于5。所以,2+3=5。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":131,"completion_tokens":26,"total_tokens":157}}
// do something...
// use struct
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,
})
// do something...
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...
}
}
builder := moonshot.NewChatCompletionsBuilder()
builder.SetModel(moonshot.ModelMoonshotV1128K)
builder.AddUserContent("请搜索 Moonshot AI Context Caching 技术,并告诉我它是什么。")
builder.SetTool(&moonshot.ChatCompletionsTool{
Type: moonshot.ChatCompletionsToolTypeBuiltinFunction,
Function: &moonshot.ChatCompletionsToolFunction{
Name: moonshot.BuiltinFunctionWebSearch,
},
})
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
// do something...
if len(resp.Choices) != 0 {
choice := resp.Choices[0]
if choice.FinishReason == moonshot.FinishReasonToolCalls {
for _, tool := range choice.Message.ToolCalls {
if tool.Function.Name == moonshot.BuiltinFunctionWebSearch {
// web search
arguments := new(moonshot.ChatCompletionsToolBuiltinFunctionWebSearchArguments)
if err = json.Unmarshal([]byte(tool.Function.Arguments), arguments); err != nil {
continue
}
// do something...
builder.AddMessageFromChoices(resp.Choices)
builder.AddToolContent(tool.Function.Arguments, tool.Function.Name, tool.ID)
}
}
}
}
resp, err = cli.Chat().Completions(ctx, builder.ToRequest())
// do something...
Feel free to open a new issue, or contact me.
Made with contrib.rocks.
This is open-sourced library licensed under the MIT license.