From 30e0bf85aa5989abf8d27d69721fa7aaa8df96d0 Mon Sep 17 00:00:00 2001 From: northes Date: Tue, 2 Apr 2024 14:35:31 +0800 Subject: [PATCH] feat(README): update --- README.md | 65 ++++++++++++++++++++++++++++++++++++++++----- README_zh.md | 74 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 123 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e555fd1..7a42553 100644 --- a/README.md +++ b/README.md @@ -40,14 +40,33 @@ by [MoonshotAI](https://moonshot.cn). > :warning: Note: Your API key is sensitive information. Do not share it with anyone. +#### With Only Key + +```go +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 +} +``` + +#### With Config + ```go -key, ok := os.LookupEnv("moonshot_key") +key, ok := os.LookupEnv("MOONSHOT_KEY") if !ok { -return nil, errors.New("missing environment variable: moonshot_key") + return errors.New("missing environment variable: moonshot_key") } -return moonshot.NewClient(moonshot.NewConfig( -moonshot.SetAPIKey(key), -)) + +cli, err := moonshot.NewClientWithConfig( + moonshot.NewConfig( + moonshot.WithAPIKey("xxxx"), + ), +) ``` ### Call API @@ -56,7 +75,41 @@ moonshot.SetAPIKey(key), // List Models resp, err := cli.Models().List(context.Background()) if err != nil { -return err + return err +} +``` + +```go +// 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... + } } ``` diff --git a/README_zh.md b/README_zh.md index addc8bc..7be8ea2 100644 --- a/README_zh.md +++ b/README_zh.md @@ -34,28 +34,82 @@ ### 初始化 -1. Get a MoonshotAI API Key: [https://platform.moonshot.cn](https://platform.moonshot.cn). -2. Set up key using a configuration file or environment variable. +1. 申请一个 MoonshotAI API Key: [https://platform.moonshot.cn](https://platform.moonshot.cn) +2. 使用配置文件或环境变量设置密钥 -> :warning: Note: Your API key is sensitive information. Do not share it with anyone. +> :warning: 注意:您的API密钥是敏感信息。 +不要和任何人分享。 + +#### 仅使用Key + +```go +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 +} +``` + +#### 使用配置 ```go -key, ok := os.LookupEnv("moonshot_key") +key, ok := os.LookupEnv("MOONSHOT_KEY") if !ok { -return nil, errors.New("missing environment variable: moonshot_key") + return errors.New("missing environment variable: moonshot_key") } -return moonshot.NewClient(moonshot.NewConfig( -moonshot.SetAPIKey(key), -)) + +cli, err := moonshot.NewClientWithConfig( + moonshot.NewConfig( + moonshot.WithAPIKey("xxxx"), + ), +) ``` ### 调用 API ```go -// List Models +// 列出可用模型 resp, err := cli.Models().List(context.Background()) if err != nil { -return err + return err +} +``` + +```go +// 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... + } } ```