20230115-Manjaro 與 VSCode 綜合練習2 (類別庫)
新增類別庫專案
新增類別庫參考語法
1
dotnet new classlib -o StringLibrary
Tutorial: Create a .NET class library using Visual Studio Code
建置置專案
1
dotnet build /home/hcc/DotNetCore/HankService/ChatGPT/ChatGPT.csproj
專案配置成果
- 建置類別庫
- 建置主程式專案
- 執行主程式
1
sudo dotnet run
HankService.ChatGPT
- /home/hcc/DotNetCore/HankService/ChatGPT/ChatGPT.csproj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
</ItemGroup>
</Project> - /home/hcc/DotNetCore/HankService/ChatGPT/ChatGPTService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace HankService.ChatGPT
{
public class ChatGPTService
{
IConfigurationRoot _config;
public ChatGPTService(IConfigurationRoot config)
{
this._config = config;
}
public CompletionsResponse GetResponseByWebClient(string prompt)
{
WebClient cli;
using (cli = new WebClient())
{
cli.Encoding = Encoding.GetEncoding(this._config["ChatGPT:Encoding"].ToString());
cli.Headers[HttpRequestHeader.ContentType] = this._config["ChatGPT:HttpRequestHeader:ContentType"];
cli.Headers[HttpRequestHeader.Authorization] = this._config["ChatGPT:HttpRequestHeader:Authorization"];
string payload = GetRequestBodyJson(prompt, 0, 999);
string responseBody = cli.UploadString(this._config["ChatGPT:Host"], payload);
return JsonSerializer.Deserialize<CompletionsResponse>(responseBody);
}
}
public string GetResponseTextByWebClient(string prompt)
{
return this.GetResponseByWebClient(prompt).Choices[0].Text;
}
private static string GetRequestBodyJson(string prompt, decimal temperature, int maxTokens)
{
// Set up the request body
var requestBody = new CompletionsRequestBody
{
Model = "text-davinci-003",
Prompt = prompt,
Temperature = temperature,
MaxTokens = maxTokens,
TopP = 1.0m,
FrequencyPenalty = 0.0m,
PresencePenalty = 0.0m,
N = 1,
Stop = "[END]",
};
// Create a new JsonSerializerOptions object with the IgnoreNullValues and IgnoreReadOnlyProperties properties set to true
var serializerOptions = new JsonSerializerOptions
{
IgnoreNullValues = true,
IgnoreReadOnlyProperties = true,
};
// Serialize the request body to JSON using the JsonSerializer.Serialize method overload that takes a JsonSerializerOptions parameter
return JsonSerializer.Serialize(requestBody, serializerOptions);
}
}
} - /home/hcc/DotNetCore/HankService/ChatGPT/CompletionsRequestBody.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
//https://github.com/yuzd/maui_chatgpt/blob/master/chatgpt/Services/ChatGpt/CompletionsRequestBody.cs
namespace HankService.ChatGPT
{
public class CompletionsRequestBody
{
[ ]
public string Model { get; set; }
[ ]
public string Prompt { get; set; } = "";
[ ]
public string Suffix { get; set; } = null;
[ ]
public int MaxTokens { get; set; } = 16;
[ ]
public decimal Temperature { get; set; } = 1;
[ ]
public decimal TopP { get; set; } = 1;
[ ]
public int N { get; set; } = 1;
[ ]
public bool Stream { get; set; } = false;
[ ]
public int? Logprobs { get; set; } = null;
[ ]
public bool Echo { get; set; } = false;
[ ]
public string Stop { get; set; } = null;
[ ]
public decimal PresencePenalty { get; set; } = 0;
[ ]
public decimal FrequencyPenalty { get; set; } = 0;
[ ]
public int BestOf { get; set; } = 1;
[ ]
public Dictionary<string, decimal> LogitBias { get; set; } = null;
[ ]
public string User { get; set; }
}
public class CompletionsResponse
{
[ ]
public string Id { get; set; }
[ ]
public string Object { get; set; } // Escaped with @ symbol
[ ]
public int Created { get; set; }
[ ]
public string Model { get; set; }
[ ]
public CompletionsChoice[] Choices { get; set; }
[ ]
public CompletionsUsage Usage { get; set; }
}
public class CompletionsChoice
{
[ ]
public string Text { get; set; }
[ ]
public int Index { get; set; }
[ ]
public object Logprobs { get; set; }
[ ]
public string FinishReason { get; set; }
}
public class CompletionsUsage
{
[ ]
public int PromptTokens { get; set; }
[ ]
public int CompletionTokens { get; set; }
[ ]
public int TotalTokens { get; set; }
}
}
主程式 Program.cs
1 | using System.Text; |
執行結果
20230118 新增 HttpClient
類別庫新增的程式碼
1 | public async Task<CompletionsResponse> GetResponseByHttpClient(string prompt) |
主程式
1 | using System.Text; |