/zhipu

A 3rd-Party Golang Client Library for Zhipu AI Platform

Primary LanguageGoMIT LicenseMIT

zhipu

Go Reference go codecov

中文文档

A 3rd-Party Golang Client Library for Zhipu AI Platform

Usage

Install the package

go get -u github.com/yankeguo/zhipu

Create a client

// this will use environment variables ZHIPUAI_API_KEY
client, err := zhipu.NewClient()
// or you can specify the API key
client, err = zhipu.NewClient(zhipu.WithAPIKey("your api key"))

Use the client

ChatCompletion

service := client.ChatCompletion("glm-4-flash").
    AddMessage(zhipu.ChatCompletionMessage{
        Role: "user",
        Content: "你好",
    })

res, err := service.Do(context.Background())

if err != nil {
    zhipu.GetAPIErrorCode(err) // get the API error code
} else {
    println(res.Choices[0].Message.Content)
}

ChatCompletion (Stream)

service := client.ChatCompletion("glm-4-flash").
    AddMessage(zhipu.ChatCompletionMessage{
        Role: "user",
        Content: "你好",
    }).SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {
        println(chunk.Choices[0].Delta.Content)
        return nil
    })

res, err := service.Do(context.Background())

if err != nil {
    zhipu.GetAPIErrorCode(err) // get the API error code
} else {
    // this package will combine the stream chunks and build a final result mimicking the non-streaming API
    println(res.Choices[0].Message.Content)
}

ChatCompletion (Stream with GLM-4-AllTools)

// CodeInterpreter
s := client.ChatCompletion("GLM-4-AllTools")
s.AddMessage(zhipu.ChatCompletionMultiMessage{
    Role: "user",
    Content: []zhipu.ChatCompletionMultiContent{
        {
            Type: "text",
            Text: "计算[5,10,20,700,99,310,978,100]的平均值和方差。",
        },
    },
})
s.AddTool(zhipu.ChatCompletionToolCodeInterpreter{
    Sandbox: zhipu.Ptr(CodeInterpreterSandboxAuto),
})
s.SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {
    for _, c := range chunk.Choices {
        for _, tc := range c.Delta.ToolCalls {
            if tc.Type == ToolTypeCodeInterpreter && tc.CodeInterpreter != nil {
                if tc.CodeInterpreter.Input != "" {
                    // DO SOMETHING
                }
                if len(tc.CodeInterpreter.Outputs) > 0 {
                    // DO SOMETHING
                }
            }
        }
    }
    return nil
})

// WebBrowser
// CAUTION: NOT 'WebSearch'
s := client.ChatCompletion("GLM-4-AllTools")
s.AddMessage(zhipu.ChatCompletionMultiMessage{
    Role: "user",
    Content: []zhipu.ChatCompletionMultiContent{
        {
            Type: "text",
            Text: "搜索下本周深圳天气如何",
        },
    },
})
s.AddTool(zhipu.ChatCompletionToolWebBrowser{})
s.SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {
    for _, c := range chunk.Choices {
        for _, tc := range c.Delta.ToolCalls {
            if tc.Type == ToolTypeWebBrowser && tc.WebBrowser != nil {
                if tc.WebBrowser.Input != "" {
                    // DO SOMETHING
                }
                if len(tc.WebBrowser.Outputs) > 0 {
                    // DO SOMETHING
                }
            }
        }
    }
    return nil
})
s.Do(context.Background())

// DrawingTool
s := client.ChatCompletion("GLM-4-AllTools")
s.AddMessage(zhipu.ChatCompletionMultiMessage{
    Role: "user",
    Content: []zhipu.ChatCompletionMultiContent{
        {
            Type: "text",
            Text: "画一个正弦函数图像",
        },
    },
})
s.AddTool(zhipu.ChatCompletionToolDrawingTool{})
s.SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {
    for _, c := range chunk.Choices {
        for _, tc := range c.Delta.ToolCalls {
            if tc.Type == ToolTypeDrawingTool && tc.DrawingTool != nil {
                if tc.DrawingTool.Input != "" {
                    // DO SOMETHING
                }
                if len(tc.DrawingTool.Outputs) > 0 {
                    // DO SOMETHING
                }
            }
        }
    }
    return nil
})
s.Do(context.Background())

Embedding

service := client.Embedding("embedding-v2").SetInput("你好呀")
service.Do(context.Background())

Image Generation

service := client.ImageGeneration("cogview-3").SetPrompt("一只可爱的小猫咪")
service.Do(context.Background())

Video Generation

service := client.VideoGeneration("cogvideox").SetPrompt("一只可爱的小猫咪")
resp, err := service.Do(context.Background())

for {
    result, err := client.AsyncResult(resp.ID).Do(context.Background())

    if result.TaskStatus == zhipu.VideoGenerationTaskStatusSuccess {
        _ = result.VideoResult[0].URL
        _ = result.VideoResult[0].CoverImageURL
        break
    }

    if result.TaskStatus != zhipu.VideoGenerationTaskStatusProcessing {
        break
    }

    time.Sleep(5 * time.Second)
}

Upload File (Retrieval)

service := client.FileCreate(zhipu.FilePurposeRetrieval)
service.SetLocalFile(filepath.Join("testdata", "test-file.txt"))
service.SetKnowledgeID("your-knowledge-id")

service.Do(context.Background())

Upload File (Fine-Tune)

service := client.FileCreate(zhipu.FilePurposeFineTune)
service.SetLocalFile(filepath.Join("testdata", "test-file.jsonl"))
service.Do(context.Background())

Batch Create

service := client.BatchCreate().
  SetInputFileID("fileid").
  SetCompletionWindow(zhipu.BatchCompletionWindow24h).
  SetEndpoint(BatchEndpointV4ChatCompletions)
service.Do(context.Background())

Knowledge Base

client.KnowledgeCreate("")
client.KnowledgeEdit("")

Fine Tune

client.FineTuneCreate("")

Batch Support

Batch File Writer

f, err := os.OpenFile("batch.jsonl", os.O_CREATE|os.O_WRONLY, 0644)

bw := zhipu.NewBatchFileWriter(f)

bw.Add("action_1", client.ChatCompletion("glm-4-flash").
    AddMessage(zhipu.ChatCompletionMessage{
        Role: "user",
        Content: "你好",
    }))
bw.Add("action_2", client.Embedding("embedding-v2").SetInput("你好呀"))
bw.Add("action_3", client.ImageGeneration("cogview-3").SetPrompt("一只可爱的小猫咪"))

Batch Result Reader

br := zhipu.NewBatchResultReader[zhipu.ChatCompletionResponse](r)

for {
    var res zhipu.BatchResult[zhipu.ChatCompletionResponse]
    err := br.Read(&res)
    if err != nil {
        break
    }
}

Donation

Executing unit tests will actually call the ChatGLM API and consume my quota. Please donate and thank you for your support!

Credits

GUO YANKE, MIT License