{"id":24702514,"url":"https://github.com/yankeguo/zhipu","last_synced_at":"2025-10-09T09:30:21.309Z","repository":{"id":246068672,"uuid":"819996037","full_name":"yankeguo/zhipu","owner":"yankeguo","description":"A 3rd-Party Golang Client Library for Zhipu AI Platform","archived":false,"fork":false,"pushed_at":"2025-03-21T22:11:41.000Z","size":179,"stargazers_count":35,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-02T22:51:15.944Z","etag":null,"topics":["ai","artificial-intelligence","chatglm","go","golang","golang-package","llm","openai","zhipuai"],"latest_commit_sha":null,"homepage":"https://bigmodel.cn/dev/api/libraries","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yankeguo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"yankeguo","patreon":"yankeguo","buy_me_a_coffee":"yankeguo"}},"created_at":"2024-06-25T15:34:53.000Z","updated_at":"2025-03-26T09:16:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"1aaf5fd9-1374-4e5c-9147-f2f581a9a2b1","html_url":"https://github.com/yankeguo/zhipu","commit_stats":null,"previous_names":["yankeguo/zhipu"],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/yankeguo/zhipu","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankeguo%2Fzhipu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankeguo%2Fzhipu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankeguo%2Fzhipu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankeguo%2Fzhipu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yankeguo","download_url":"https://codeload.github.com/yankeguo/zhipu/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yankeguo%2Fzhipu/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279001131,"owners_count":26083022,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","artificial-intelligence","chatglm","go","golang","golang-package","llm","openai","zhipuai"],"created_at":"2025-01-27T05:41:28.744Z","updated_at":"2025-10-09T09:30:21.303Z","avatar_url":"https://github.com/yankeguo.png","language":"Go","funding_links":["https://github.com/sponsors/yankeguo","https://patreon.com/yankeguo","https://buymeacoffee.com/yankeguo"],"categories":[],"sub_categories":[],"readme":"# zhipu\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/yankeguo/zhipu.svg)](https://pkg.go.dev/github.com/yankeguo/zhipu)\n[![go](https://github.com/yankeguo/zhipu/actions/workflows/go.yml/badge.svg)](https://github.com/yankeguo/zhipu/actions/workflows/go.yml)\n[![codecov](https://codecov.io/gh/yankeguo/zhipu/graph/badge.svg?token=O08DOWX2TU)](https://codecov.io/gh/yankeguo/zhipu)\n\n[中文文档](README.zh.md)\n\nA 3rd-Party Golang Client Library for Zhipu AI Platform\n\n## Usage\n\n### Install the package\n\n```bash\ngo get -u github.com/yankeguo/zhipu\n```\n\n### Create a client\n\n```go\n// this will use environment variables ZHIPUAI_API_KEY\nclient, err := zhipu.NewClient()\n// or you can specify the API key\nclient, err = zhipu.NewClient(zhipu.WithAPIKey(\"your api key\"))\n```\n\n### Use the client\n\n**ChatCompletion**\n\n```go\nservice := client.ChatCompletion(\"glm-4-flash\").\n    AddMessage(zhipu.ChatCompletionMessage{\n        Role: \"user\",\n        Content: \"你好\",\n    })\n\nres, err := service.Do(context.Background())\n\nif err != nil {\n    zhipu.GetAPIErrorCode(err) // get the API error code\n} else {\n    println(res.Choices[0].Message.Content)\n}\n```\n\n**ChatCompletion (Stream)**\n\n```go\nservice := client.ChatCompletion(\"glm-4-flash\").\n    AddMessage(zhipu.ChatCompletionMessage{\n        Role: \"user\",\n        Content: \"你好\",\n    }).SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {\n        println(chunk.Choices[0].Delta.Content)\n        return nil\n    })\n\nres, err := service.Do(context.Background())\n\nif err != nil {\n    zhipu.GetAPIErrorCode(err) // get the API error code\n} else {\n    // this package will combine the stream chunks and build a final result mimicking the non-streaming API\n    println(res.Choices[0].Message.Content)\n}\n```\n\n**ChatCompletion (Stream with GLM-4-AllTools)**\n\n```go\n// CodeInterpreter\ns := client.ChatCompletion(\"GLM-4-AllTools\")\ns.AddMessage(zhipu.ChatCompletionMultiMessage{\n    Role: \"user\",\n    Content: []zhipu.ChatCompletionMultiContent{\n        {\n            Type: \"text\",\n            Text: \"计算[5,10,20,700,99,310,978,100]的平均值和方差。\",\n        },\n    },\n})\ns.AddTool(zhipu.ChatCompletionToolCodeInterpreter{\n    Sandbox: zhipu.Ptr(CodeInterpreterSandboxAuto),\n})\ns.SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {\n    for _, c := range chunk.Choices {\n        for _, tc := range c.Delta.ToolCalls {\n            if tc.Type == ToolTypeCodeInterpreter \u0026\u0026 tc.CodeInterpreter != nil {\n                if tc.CodeInterpreter.Input != \"\" {\n                    // DO SOMETHING\n                }\n                if len(tc.CodeInterpreter.Outputs) \u003e 0 {\n                    // DO SOMETHING\n                }\n            }\n        }\n    }\n    return nil\n})\n\n// WebBrowser\n// CAUTION: NOT 'WebSearch'\ns := client.ChatCompletion(\"GLM-4-AllTools\")\ns.AddMessage(zhipu.ChatCompletionMultiMessage{\n    Role: \"user\",\n    Content: []zhipu.ChatCompletionMultiContent{\n        {\n            Type: \"text\",\n            Text: \"搜索下本周深圳天气如何\",\n        },\n    },\n})\ns.AddTool(zhipu.ChatCompletionToolWebBrowser{})\ns.SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {\n    for _, c := range chunk.Choices {\n        for _, tc := range c.Delta.ToolCalls {\n            if tc.Type == ToolTypeWebBrowser \u0026\u0026 tc.WebBrowser != nil {\n                if tc.WebBrowser.Input != \"\" {\n                    // DO SOMETHING\n                }\n                if len(tc.WebBrowser.Outputs) \u003e 0 {\n                    // DO SOMETHING\n                }\n            }\n        }\n    }\n    return nil\n})\ns.Do(context.Background())\n\n// DrawingTool\ns := client.ChatCompletion(\"GLM-4-AllTools\")\ns.AddMessage(zhipu.ChatCompletionMultiMessage{\n    Role: \"user\",\n    Content: []zhipu.ChatCompletionMultiContent{\n        {\n            Type: \"text\",\n            Text: \"画一个正弦函数图像\",\n        },\n    },\n})\ns.AddTool(zhipu.ChatCompletionToolDrawingTool{})\ns.SetStreamHandler(func(chunk zhipu.ChatCompletionResponse) error {\n    for _, c := range chunk.Choices {\n        for _, tc := range c.Delta.ToolCalls {\n            if tc.Type == ToolTypeDrawingTool \u0026\u0026 tc.DrawingTool != nil {\n                if tc.DrawingTool.Input != \"\" {\n                    // DO SOMETHING\n                }\n                if len(tc.DrawingTool.Outputs) \u003e 0 {\n                    // DO SOMETHING\n                }\n            }\n        }\n    }\n    return nil\n})\ns.Do(context.Background())\n```\n\n**Embedding**\n\n```go\nservice := client.Embedding(\"embedding-v2\").SetInput(\"你好呀\")\nservice.Do(context.Background())\n```\n\n**Image Generation**\n\n```go\nservice := client.ImageGeneration(\"cogview-3\").SetPrompt(\"一只可爱的小猫咪\")\nservice.Do(context.Background())\n```\n\n**Video Generation**\n\n```go\nservice := client.VideoGeneration(\"cogvideox\").SetPrompt(\"一只可爱的小猫咪\")\nresp, err := service.Do(context.Background())\n\nfor {\n    result, err := client.AsyncResult(resp.ID).Do(context.Background())\n\n    if result.TaskStatus == zhipu.VideoGenerationTaskStatusSuccess {\n        _ = result.VideoResult[0].URL\n        _ = result.VideoResult[0].CoverImageURL\n        break\n    }\n\n    if result.TaskStatus != zhipu.VideoGenerationTaskStatusProcessing {\n        break\n    }\n\n    time.Sleep(5 * time.Second)\n}\n```\n\n**Upload File (Retrieval)**\n\n```go\nservice := client.FileCreate(zhipu.FilePurposeRetrieval)\nservice.SetLocalFile(filepath.Join(\"testdata\", \"test-file.txt\"))\nservice.SetKnowledgeID(\"your-knowledge-id\")\n\nservice.Do(context.Background())\n```\n\n**Upload File (Fine-Tune)**\n\n```go\nservice := client.FileCreate(zhipu.FilePurposeFineTune)\nservice.SetLocalFile(filepath.Join(\"testdata\", \"test-file.jsonl\"))\nservice.Do(context.Background())\n```\n\n**Batch Create**\n\n```go\nservice := client.BatchCreate().\n  SetInputFileID(\"fileid\").\n  SetCompletionWindow(zhipu.BatchCompletionWindow24h).\n  SetEndpoint(BatchEndpointV4ChatCompletions)\nservice.Do(context.Background())\n```\n\n**Knowledge Base**\n\n```go\nclient.KnowledgeCreate(\"\")\nclient.KnowledgeEdit(\"\")\n```\n\n**Fine Tune**\n\n```go\nclient.FineTuneCreate(\"\")\n```\n\n### Batch Support\n\n**Batch File Writer**\n\n```go\nf, err := os.OpenFile(\"batch.jsonl\", os.O_CREATE|os.O_WRONLY, 0644)\n\nbw := zhipu.NewBatchFileWriter(f)\n\nbw.Add(\"action_1\", client.ChatCompletion(\"glm-4-flash\").\n    AddMessage(zhipu.ChatCompletionMessage{\n        Role: \"user\",\n        Content: \"你好\",\n    }))\nbw.Add(\"action_2\", client.Embedding(\"embedding-v2\").SetInput(\"你好呀\"))\nbw.Add(\"action_3\", client.ImageGeneration(\"cogview-3\").SetPrompt(\"一只可爱的小猫咪\"))\n```\n\n**Batch Result Reader**\n\n```go\nbr := zhipu.NewBatchResultReader[zhipu.ChatCompletionResponse](r)\n\nfor {\n    var res zhipu.BatchResult[zhipu.ChatCompletionResponse]\n    err := br.Read(\u0026res)\n    if err != nil {\n        break\n    }\n}\n```\n\n## Donation\n\n**This project is a personal open-source project maintained by GUO YANKE. The following donation channels are not related to Zhipu AI.**\n\n**本项目是个人维护的开源项目，以下赞助渠道与智谱AI官方无关。**\n\nExecuting unit tests will actually call the ChatGLM API and consume my quota. Please donate and thank you for your support!\n\n执行单元测试会真实调用GLM接口，消耗我充值的额度，开发不易，请微信扫码捐赠，感谢您的支持！\n\n\u003cimg src=\"./wechat-donation.png\" width=\"180\"/\u003e\n\n## Credits\n\nGUO YANKE, MIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyankeguo%2Fzhipu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyankeguo%2Fzhipu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyankeguo%2Fzhipu/lists"}