Aida-chiyo-Talk-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 65c66aab6ae41fa2cd1e1d61fde0710896fc0913
parent 1e4025e9d4fcf2635c0e2b2ead76822bb8388b64
Author: Minerva-Juppiter <ryouturn@gmail.com>
Date:   Sat, 29 Jul 2023 00:38:43 +0900


Diffstat:
MAida chiyo Talk app.csproj | 5+++++
MInputs.cs | 64++++++++++++++++++++++++++++++++++++++++++++++++----------------
MProgram.cs | 8+++++---
MTalkEngine.cs | 145+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
4 files changed, 200 insertions(+), 22 deletions(-)

diff --git a/Aida chiyo Talk app.csproj b/Aida chiyo Talk app.csproj @@ -8,4 +8,9 @@ <Nullable>enable</Nullable> </PropertyGroup> + <ItemGroup> + <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" /> + <PackageReference Include="OpenAI" Version="1.7.2" /> + </ItemGroup> + </Project> diff --git a/Inputs.cs b/Inputs.cs @@ -6,6 +6,7 @@ using System.Net.Http.Json; using System.Net; using System.Text; using System.Threading.Tasks; +using static System.Net.WebRequestMethods; namespace Aida_chiyo_Talk_app { @@ -14,34 +15,62 @@ namespace Aida_chiyo_Talk_app public string InputConsole() { string inputString = Console.ReadLine(); - while (inputString == null) + while (inputString == "") { Console.WriteLine("Write something"); inputString = Console.ReadLine(); } return inputString; } - public async Task<string> Proofreading(string unproofread) + public string Proofreading(string unproofread) { string APIurl = "https://api.a3rt.recruit.co.jp/proofreading/v2/typo"; string APIkey = "DZZr8Yho8FWTYiQoXDXOHOOp96S2cEsN"; - string sensitivity = "medium"; - //string APIsecret = APIurl + "?" + APIkey; - // Web API を呼ぶための HttpClient を作る - var client = new HttpClient(); - // POST メソッドで JSON の Body のリクエストを投げる - var response = await client.PostAsJsonAsync(APIurl, new RequestBody { apikey = APIkey , sentence = unproofread , sensitivity = sensitivity }); - // レスポンスのステータスコードが成功していたら Answer の値を出力 - if (response.IsSuccessStatusCode) + + string proofreaded = string.Empty; + + Task task = ProofreadingAPI(); + + int waitcount = 0; + while(task.IsCompletedSuccessfully == false) { - var responseBody = await response.Content.ReadFromJsonAsync<Response>(); - //Console.WriteLine(responseBody?.Answer); - return responseBody?.Answer.ToString(); + waitcount++; } - else + + + async Task ProofreadingAPI() { - return string.Empty; + Console.WriteLine("we are calling proofreadingAPI"); + // Web API を呼ぶための HttpClient を作る + var client = new HttpClient(); + // POST メソッドで JSON の Body のリクエストを投げる + try + { + var response = await client.PostAsJsonAsync(APIurl, new RequestBody { apikey = APIkey, sentence = unproofread }); + Console.WriteLine("API was called."); + // レスポンスのステータスコードが成功していたら Answer の値を出力 + Console.WriteLine(response.ToString()); + if (response.IsSuccessStatusCode) + { + Console.WriteLine("API was successed!"); + var responseBody = await response.Content.ReadFromJsonAsync<Response>(); + Console.WriteLine(responseBody); + proofreaded = responseBody?.checkedSentence; + } + else + { + Console.WriteLine("API was not answered"); + } + } + catch (Exception ex) + { + Console.WriteLine("something happen in APIs"); + Console.WriteLine(ex); + } } + + return proofreaded; + } // リクエストとレスポンス @@ -55,7 +84,10 @@ namespace Aida_chiyo_Talk_app class Response { - public string? Answer { get; set; } + public string? resultID { get; set; } + public string? status { get; set; } + public string? checkedSentence { get; set; } + public string? alerts { get; set; } } } } diff --git a/Program.cs b/Program.cs @@ -9,5 +9,7 @@ Console.WriteLine("Input someting you like."); Inputs inputs = new Inputs(); string input = inputs.InputConsole(); -string proofreaded = inputs.Proofreading(input).ToString(); -Console.WriteLine(proofreaded);- \ No newline at end of file +TalkEngine talkEngine = new TalkEngine(); +string answer = talkEngine.CallTalkAPI(input); + +Console.WriteLine(answer);+ \ No newline at end of file diff --git a/TalkEngine.cs b/TalkEngine.cs @@ -1,22 +1,161 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; +using System.Net.Http.Headers; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; +using static Aida_chiyo_Talk_app.TalkEngine; +using static Aida_chiyo_Talk_app.TalkEngine.ChatGptResponse; namespace Aida_chiyo_Talk_app { internal class TalkEngine { + string answer = string.Empty; + string APIkey = "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"; public string CallTalkAPI(string callTalkAPI) { - string answer; - answer = string.Empty; + ChatGPptAPI chat = new ChatGPptAPI(); + Task task = ChatGPptAPI.RequestTChatGPptAsync(callTalkAPI); + while (task.IsCompletedSuccessfully == false) + { + + } + return answer; + } + + public class ChatGptResponse + { + // Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse); + public class Choice + { + [JsonPropertyName("finish_reason")] + public string FinishReason { get; set; } + [JsonPropertyName("index")] + public int Index { get; set; } + [JsonPropertyName("message")] + public Message Message { get; set; } + } + public class Message + { + [JsonPropertyName("content")] + public string Content { get; set; } - return answer; + [JsonPropertyName("role")] + public string Role { get; set; } + } + + public class Root + { + [JsonPropertyName("choices")] + public List<Choice> Choices { get; set; } + + [JsonPropertyName("created")] + public int Created { get; set; } + + [JsonPropertyName("id")] + public string Id { get; set; } + + [JsonPropertyName("model")] + public string Model { get; set; } + + [JsonPropertyName("object")] + public string Object { get; set; } + + [JsonPropertyName("usage")] + public Usage Usage { get; set; } + } + + public class Usage + { + [JsonPropertyName("completion_tokens")] + public int CompletionTokens { get; set; } + + [JsonPropertyName("prompt_tokens")] + public int PromptTokens { get; set; } + + [JsonPropertyName("total_tokens")] + public int TotalTokens { get; set; } + } + public class Messages + { + private static List<Message> _messages; + + static Messages() + { + _messages = new List<Message>(); + } + + public static List<Message> GetMessages() + { + return new List<Message>(_messages); + } + + public static void AddMessage(Message message) + { + _messages.Add(message); + } + + public static void ClearMessages() + { + _messages.Clear(); + } + } + } + public class ChatGPptAPI + { + // エンドポイント + const string _url = "https://api.openai.com/v1/chat/completions"; + // APIキー + const string _token = "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"; + // モデル + const string _model = "gpt-3.5-turbo"; + + public static async Task<string> RequestTChatGPptAsync(string text) + { + // 文脈に追加 + Messages.AddMessage(new Message() { Role = "user", Content = text }); + + using var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token); + var requestData = new + { + model = _model, + messages = Messages.GetMessages(), + }; + var content = new StringContent( + JsonSerializer.Serialize(requestData), + Encoding.UTF8, + "application/json"); + try + { + var response = await httpClient.PostAsync(_url, content); + if (response.IsSuccessStatusCode) + { + // レスポンスをJSONとしてパースし、必要な情報を取得 + var responseContent = await response.Content.ReadAsStringAsync(); + var chatGptResponse = JsonSerializer.Deserialize<ChatGptResponse.Root>(responseContent); + // 文脈に追加 + Messages.AddMessage(new Message() { Role = "assistant", Content = chatGptResponse.Choices[0].Message.Content }); + Console.WriteLine(chatGptResponse.Choices[0].Message.Content); + return chatGptResponse.Choices[0].Message.Content; + } + else + { + return ""; + } + } + catch (Exception e) + { + return e.Message; + } + } } } }