commit 3ca2d6417529a436beaf650ffa37bc0d09d9b209
parent 420daad7b9950d4ac745cca783f050681d7b3be3
Author: Minerva-Juppiter <ryouturn@gmail.com>
Date: Sat, 29 Jul 2023 15:31:52 +0900
Diffstat:
| M | TalkEngine.cs | | | 156 | ++++++++++++++++---------------------------------------------------------------- |
1 file changed, 30 insertions(+), 126 deletions(-)
diff --git a/TalkEngine.cs b/TalkEngine.cs
@@ -1,14 +1,16 @@
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http.Headers;
+using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
+using System.Xml;
using static Aida_chiyo_Talk_app.TalkEngine;
-using static Aida_chiyo_Talk_app.TalkEngine.ChatGptResponse;
namespace Aida_chiyo_Talk_app
{
@@ -16,9 +18,11 @@ namespace Aida_chiyo_Talk_app
{
string answer = string.Empty;
string APIkey = "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";
+ string talkAPIkey = "DZZqpz3HVnJL4LGk57u8ZfmgfAIjrB3B";
+ string APIurl = "https://api.a3rt.recruit.co.jp/talk/v1/smalltalk";
public string CallTalkAPI(string callTalkAPI)
{
- var task = ChatGPptAPI.RequestTChatGPptAsync(callTalkAPI);
+ var task = Talkapi(callTalkAPI);
while (task.IsCompletedSuccessfully == false)
{
@@ -27,135 +31,35 @@ namespace Aida_chiyo_Talk_app
return answer;
}
- public class ChatGptResponse
+ public async Task<string> Talkapi(string input)
{
- // Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse);
- public class Choice
+ string output = string.Empty;
+
+ // Web API を呼ぶための HttpClient を作る
+ var client = new HttpClient();
+ // POST メソッドで JSON の Body のリクエストを投げる
+ var response = await client.PostAsJsonAsync(APIurl,new RequestBody { apikey = talkAPIkey , query = input });
+ // レスポンスのステータスコードが成功していたら Answer の値を出力
+ if (response.IsSuccessStatusCode)
{
- [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; }
-
- [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();
- }
+ var responseBody = await response.Content.ReadFromJsonAsync<Response>();
+ Console.WriteLine(responseBody);
}
+ return output;
}
- 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 });
+ }
+ class RequestBody
+ {
+ public string? apikey { get; set; }
+ public string? callback { get; set; }
+ public string? query { get; set; }
+ }
- 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;
- }
- }
- }
+ class Response
+ {
+ public string? status { get; set; }
+ public string? message { get; set; }
+ public string? results { get; set; }
}
}