├── .gitignore
├── Models
├── ResponseModels
│ ├── ImageGenerationModels
│ │ ├── ImageResponseDataItem.cs
│ │ └── ImageResponseBase.cs
│ ├── ToolModels
│ │ ├── FunctionDescriptor.cs
│ │ └── ToolCallItem.cs
│ ├── EmbeddingModels
│ │ ├── EmbeddingDataItem.cs
│ │ └── EmbeddingResponseBase.cs
│ ├── ResponseChoiceDelta.cs
│ ├── ResponseChoiceItem.cs
│ └── ResponseBase.cs
└── RequestModels
│ ├── ImageToTextModels
│ ├── ImageUrlType.cs
│ ├── ContentType.cs
│ └── ImageToTextMessageItem.cs
│ ├── MessageItem.cs
│ ├── EmbeddingRequestBase.cs
│ ├── FunctionModels
│ ├── FunctionParameterDescriptor.cs
│ ├── FunctionTool.cs
│ └── FunctionParameters.cs
│ ├── ImageRequestBase.cs
│ └── TextRequestBase.cs
├── ZhipuApi.csproj
├── ClientV4.cs
├── Utils
├── JsonResolver
│ └── JsonResolver.cs
└── AuthenticationUtils.cs
├── Modules
├── Images.cs
├── Embeddings.cs
└── Chat.cs
├── README.md
└── Test
└── Test.cs
/.gitignore:
--------------------------------------------------------------------------------
1 | /obj/*
2 | /bin/*
--------------------------------------------------------------------------------
/Models/ResponseModels/ImageGenerationModels/ImageResponseDataItem.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.ResponseModels.ImageGenerationModels
2 | {
3 | public class ImageResponseDataItem
4 | {
5 | public string url { get; set; }
6 | }
7 | }
--------------------------------------------------------------------------------
/Models/ResponseModels/ToolModels/FunctionDescriptor.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.ResponseModels.ToolModels
2 | {
3 | public class FunctionDescriptor
4 | {
5 | public string name { get; set; }
6 | public string arguments { get; set; }
7 | }
8 | }
--------------------------------------------------------------------------------
/Models/ResponseModels/EmbeddingModels/EmbeddingDataItem.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.ResponseModels.EmbeddingModels
2 | {
3 | public class EmbeddingDataItem
4 | {
5 | public int index { get; set; }
6 | public string _object { get; set; }
7 | public double[] embedding { get; set; }
8 | }
9 | }
--------------------------------------------------------------------------------
/Models/RequestModels/ImageToTextModels/ImageUrlType.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.RequestModels.ImageToTextModels
2 | {
3 | public class ImageUrlType
4 | {
5 | public string url { get; set; }
6 |
7 | public ImageUrlType(string url)
8 | {
9 | this.url = url;
10 | }
11 | }
12 | }
--------------------------------------------------------------------------------
/Models/ResponseModels/ToolModels/ToolCallItem.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.ResponseModels.ToolModels
2 | {
3 | public class ToolCallItem
4 | {
5 | public string id { get; set; }
6 | public FunctionDescriptor function { get; set; }
7 | public int index { get; set; }
8 | public string type { get; set; }
9 | }
10 | }
--------------------------------------------------------------------------------
/Models/ResponseModels/ResponseChoiceDelta.cs:
--------------------------------------------------------------------------------
1 | using ZhipuApi.Models.ResponseModels.ToolModels;
2 |
3 | namespace ZhipuApi.Models.ResponseModels
4 | {
5 | public class ResponseChoiceDelta
6 | {
7 | public string role { get; set; }
8 | public string content { get; set; }
9 | public ToolCallItem[] tool_calls { get; set; }
10 | }
11 | }
--------------------------------------------------------------------------------
/Models/RequestModels/MessageItem.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.RequestModels
2 | {
3 | public class MessageItem
4 | {
5 | public string role { get; set; }
6 | public virtual string content { get; set; }
7 |
8 | public MessageItem(string role, string content)
9 | {
10 | this.role = role;
11 | this.content = content;
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/ZhipuApi.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/Models/ResponseModels/ResponseChoiceItem.cs:
--------------------------------------------------------------------------------
1 | using System.Text.Json;
2 | using ZhipuApi.Models.ResponseModels.ToolModels;
3 |
4 | namespace ZhipuApi.Models.ResponseModels
5 | {
6 | public class ResponseChoiceItem
7 | {
8 | public string finish_reason { get; set; }
9 | public int index { get; set; }
10 | public ResponseChoiceDelta message { get; set; }
11 | public ResponseChoiceDelta delta { get; set; }
12 | }
13 | }
--------------------------------------------------------------------------------
/Models/ResponseModels/ImageGenerationModels/ImageResponseBase.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Text.Json;
3 |
4 | namespace ZhipuApi.Models.ResponseModels.ImageGenerationModels
5 | {
6 | public class ImageResponseBase
7 | {
8 | public long created { get; set; }
9 | public List data { get; set; }
10 | public Dictionary error { get; set; }
11 |
12 | public static ImageResponseBase FromJson(string json)
13 | {
14 | return JsonSerializer.Deserialize(json);
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/ClientV4.cs:
--------------------------------------------------------------------------------
1 | using ZhipuApi.Modules;
2 |
3 | namespace ZhipuApi
4 | {
5 | public class ClientV4
6 | {
7 | private string _apiKey;
8 | public Chat chat { get; private set; }
9 |
10 | public Images images { get; private set; }
11 |
12 | public Embeddings embeddings { get; private set; }
13 |
14 | public ClientV4(string apiKey)
15 | {
16 | this._apiKey = apiKey;
17 | this.chat = new Chat(apiKey);
18 | this.images = new Images(apiKey);
19 | this.embeddings = new Embeddings(apiKey);
20 | }
21 |
22 |
23 | }
24 | }
--------------------------------------------------------------------------------
/Models/RequestModels/EmbeddingRequestBase.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.RequestModels
2 | {
3 | public class EmbeddingRequestBase
4 | {
5 | // "input": input,
6 | // "model": model,
7 | // "encoding_format": encoding_format,
8 | // "user": user,
9 | public string model { get; private set; }
10 | public string input { get; private set; }
11 |
12 | public EmbeddingRequestBase SetModel(string model)
13 | {
14 | this.model = model;
15 | return this;
16 | }
17 | public EmbeddingRequestBase SetInput(string input)
18 | {
19 | this.input = input;
20 | return this;
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/Models/ResponseModels/EmbeddingModels/EmbeddingResponseBase.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Text.Json;
3 |
4 | namespace ZhipuApi.Models.ResponseModels.EmbeddingModels
5 | {
6 | public class EmbeddingResponseBase
7 | {
8 | public string model { set; get; }
9 | public string _object { set; get; }
10 | public Dictionary usage { set; get; }
11 | public EmbeddingDataItem[] data { get; set; }
12 | public Dictionary error { get; set; }
13 |
14 | public static EmbeddingResponseBase FromJson(string json)
15 | {
16 | return JsonSerializer.Deserialize(json);
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/Models/RequestModels/ImageToTextModels/ContentType.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.RequestModels.ImageToTextModels
2 | {
3 |
4 | public class ContentType
5 | {
6 | public string type { get; set; }
7 | public string text { set; get; }
8 | public ImageUrlType image_url { set; get; }
9 |
10 | public ContentType setType(string type)
11 | {
12 | this.type = type;
13 | return this;
14 | }
15 |
16 | public ContentType setText(string text)
17 | {
18 | this.text = text;
19 | return this;
20 | }
21 |
22 | public ContentType setImageUrl(string image_url)
23 | {
24 | this.image_url = new ImageUrlType(image_url);
25 | return this;
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/Models/RequestModels/ImageToTextModels/ImageToTextMessageItem.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.RequestModels.ImageToTextModels
2 | {
3 |
4 | public class ImageToTextMessageItem : MessageItem
5 | {
6 | public ContentType[] content { get; set; }
7 |
8 | public ImageToTextMessageItem(string role) : base(role, null)
9 | {
10 | this.content = new ContentType[2];
11 | }
12 |
13 | public ImageToTextMessageItem setText(string text)
14 | {
15 | this.content[0] = new ContentType().setType("text").setText(text);
16 | return this;
17 | }
18 |
19 | public ImageToTextMessageItem setImageUrl(string image_url)
20 | {
21 | this.content[1] = new ContentType().setType("Image_url").setImageUrl(image_url);
22 | return this;
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/Models/RequestModels/FunctionModels/FunctionParameterDescriptor.cs:
--------------------------------------------------------------------------------
1 | namespace ZhipuApi.Models.RequestModels.FunctionModels
2 | {
3 | public enum ParameterType
4 | {
5 | String,
6 | Integer,
7 | }
8 |
9 | public class FunctionParameterDescriptor
10 | {
11 | public string type { get; set; }
12 | public string description { get; set; }
13 |
14 | private static string ToTypeString(ParameterType type)
15 | {
16 | return type switch
17 | {
18 | ParameterType.String => "string",
19 | ParameterType.Integer => "int",
20 | _ => null
21 | };
22 | }
23 |
24 | public FunctionParameterDescriptor(ParameterType type, string description)
25 | {
26 | this.type = ToTypeString(type);
27 | this.description = description;
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/Models/ResponseModels/ResponseBase.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Text.Json;
3 |
4 | namespace ZhipuApi.Models.ResponseModels
5 | {
6 | public class ResponseBase
7 | {
8 | public string id { get; set; }
9 | public string request_id { get; set; }
10 | public long created { get; set; }
11 | public string model { get; set; }
12 | public Dictionary usage { get; set; }
13 | public ResponseChoiceItem[] choices { get; set; }
14 | public Dictionary error { get; set; }
15 |
16 | public static ResponseBase FromJson(string json)
17 | {
18 | try
19 | {
20 | return JsonSerializer.Deserialize(json);
21 | }
22 | catch (JsonException)
23 | {
24 | return null;
25 | }
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/Models/RequestModels/FunctionModels/FunctionTool.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ZhipuApi.Models.RequestModels.FunctionModels
4 | {
5 | public class FunctionTool
6 | {
7 | public string type { get; set; }
8 | public Dictionary function { get; set; }
9 |
10 | public FunctionTool()
11 | {
12 | this.type = "function";
13 | this.function = new Dictionary();
14 | }
15 |
16 | public FunctionTool SetName(string name)
17 | {
18 | this.function["name"] = name;
19 | return this;
20 | }
21 |
22 | public FunctionTool SetDescription(string desc)
23 | {
24 | this.function["description"] = desc;
25 | return this;
26 | }
27 |
28 | public FunctionTool SetParameters(FunctionParameters param)
29 | {
30 | this.function["parameters"] = param;
31 | return this;
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/Models/RequestModels/FunctionModels/FunctionParameters.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ZhipuApi.Models.RequestModels.FunctionModels
4 | {
5 | public class FunctionParameters
6 | {
7 | public string type { get; set; }
8 | public Dictionary properties { get; }
9 | public string[] required { get; set; }
10 |
11 | public FunctionParameters()
12 | {
13 | this.type = "object";
14 | this.properties = new Dictionary();
15 | }
16 |
17 | public FunctionParameters AddParameter(string name, ParameterType type, string description)
18 | {
19 | properties[name] = new FunctionParameterDescriptor(type, description);
20 | return this;
21 | }
22 |
23 | public FunctionParameters SetRequiredParameter(string[] required)
24 | {
25 | this.required = required;
26 | return this;
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------
/Utils/JsonResolver/JsonResolver.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 | using Newtonsoft.Json;
4 | using Newtonsoft.Json.Serialization;
5 | using ZhipuApi.Models;
6 | using ZhipuApi.Models.RequestModels;
7 | using ZhipuApi.Models.RequestModels.ImageToTextModels;
8 |
9 | namespace ZhipuApi.Utils.JsonResolver
10 | {
11 | public class JsonResolver : DefaultContractResolver
12 | {
13 | protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
14 | {
15 | JsonProperty property = base.CreateProperty(member, memberSerialization);
16 |
17 | if (property.DeclaringType == typeof(MessageItem) && property.PropertyName == "content")
18 | {
19 | Predicate