Skip to content

Commit 185b412

Browse files
committed
feature: release v0.5.2
1 parent d6cbffd commit 185b412

1 file changed

Lines changed: 56 additions & 24 deletions

File tree

README.md

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ by [MoonshotAI](https://moonshot.cn).
1919
## 🚀 Installation
2020

2121
```bash
22-
go get github.com/northes/go-moonshot@v0.5.1
22+
go get github.com/northes/go-moonshot@v0.5.2
2323
```
2424

2525
You can find the docs at [go docs](https://pkg.go.dev/github.com/northes/go-moonshot).
2626

2727
## 🤘 Feature
2828

2929
- Easy to use and simple API, chain operation.
30-
- Full API support.
30+
- Full API and builtin functions support.
3131
- Predefined enumeration.
32+
- Messages builder.
3233

3334
## 📄 Supported API
3435

@@ -47,8 +48,14 @@ You can find the docs at [go docs](https://pkg.go.dev/github.com/northes/go-moon
4748
| Tool Use ||
4849
| Context Cache ||
4950

51+
## 🗜 Builtin Functions
52+
53+
- `$web_search`
54+
5055
## 🥪 Usage
5156

57+
For more examples, you can view the test file of the corresponding interface.
58+
5259
### Initialize client
5360

5461
1. Get a MoonshotAI API Key: [https://platform.moonshot.cn](https://platform.moonshot.cn).
@@ -61,23 +68,17 @@ You can find the docs at [go docs](https://pkg.go.dev/github.com/northes/go-moon
6168

6269
```go
6370
key, ok := os.LookupEnv("MOONSHOT_KEY")
64-
if !ok {
65-
return errors.New("missing environment variable: moonshot_key")
66-
}
71+
// do something...
6772

6873
cli, err := moonshot.NewClient(key)
69-
if err != nil {
70-
return err
71-
}
74+
// do something...
7275
```
7376

7477
#### With Config
7578

7679
```go
7780
key, ok := os.LookupEnv("MOONSHOT_KEY")
78-
if !ok {
79-
return errors.New("missing environment variable: moonshot_key")
80-
}
81+
// do something...
8182

8283
cli, err := moonshot.NewClientWithConfig(
8384
moonshot.NewConfig(
@@ -92,9 +93,7 @@ cli, err := moonshot.NewClientWithConfig(
9293

9394
```go
9495
resp, err := cli.Models().List(context.Background())
95-
if err != nil {
96-
return err
97-
}
96+
// do something...
9897
```
9998

10099
#### Chat Completions
@@ -106,10 +105,7 @@ builder.AppendPrompt("你是 Kimi,由 Moonshot AI 提供的人工智能助手
106105
AppendUser("你好,我叫李雷,1+1等于多少?").
107106
WithTemperature(0.3)
108107

109-
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
110-
if err != nil {
111-
return err
112-
}
108+
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
113109
// {"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}}
114110

115111
// do something...
@@ -122,10 +118,9 @@ for _, choice := range resp.Choices {
122118
builder.AppendUser("在这个基础上再加3等于多少")
123119

124120
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
125-
if err != nil {
126-
return err
127-
}
128121
// {"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}}
122+
123+
// do something...
129124
```
130125

131126
#### Chat completions with stream
@@ -143,9 +138,7 @@ resp, err := cli.Chat().CompletionsStream(context.Background(), &moonshot.ChatCo
143138
Temperature: 0.3,
144139
Stream: true,
145140
})
146-
if err != nil {
147-
return err
148-
}
141+
// do something...
149142

150143
for receive := range resp.Receive() {
151144
msg, err := receive.GetMessage()
@@ -164,6 +157,45 @@ for receive := range resp.Receive() {
164157
}
165158
```
166159

160+
#### Web search tool
161+
162+
```go
163+
builder := moonshot.NewChatCompletionsBuilder()
164+
builder.SetModel(moonshot.ModelMoonshotV1128K)
165+
builder.AddUserContent("请搜索 Moonshot AI Context Caching 技术,并告诉我它是什么。")
166+
builder.SetTool(&moonshot.ChatCompletionsTool{
167+
Type: moonshot.ChatCompletionsToolTypeBuiltinFunction,
168+
Function: &moonshot.ChatCompletionsToolFunction{
169+
Name: moonshot.BuiltinFunctionWebSearch,
170+
},
171+
})
172+
173+
resp, err := cli.Chat().Completions(ctx, builder.ToRequest())
174+
// do something...
175+
176+
if len(resp.Choices) != 0 {
177+
choice := resp.Choices[0]
178+
if choice.FinishReason == moonshot.FinishReasonToolCalls {
179+
for _, tool := range choice.Message.ToolCalls {
180+
if tool.Function.Name == moonshot.BuiltinFunctionWebSearch {
181+
// web search
182+
arguments := new(moonshot.ChatCompletionsToolBuiltinFunctionWebSearchArguments)
183+
if err = json.Unmarshal([]byte(tool.Function.Arguments), arguments); err != nil {
184+
continue
185+
}
186+
// do something...
187+
188+
builder.AddMessageFromChoices(resp.Choices)
189+
builder.AddToolContent(tool.Function.Arguments, tool.Function.Name, tool.ID)
190+
}
191+
}
192+
}
193+
}
194+
195+
resp, err = cli.Chat().Completions(ctx, builder.ToRequest())
196+
// do something...
197+
```
198+
167199
## 🤝 Missing a Feature?
168200

169201
Feel free to open a new issue, or contact me.

0 commit comments

Comments
 (0)