commit 6d32586a0c88aa43cbd94a3eb4a4327cfa71d86b
parent d9d3c41c2166318e6679f03a5d26ad793cefffc9
Author: Minerva-Juppiter <ryouturn@gmail.com>
Date: Sun, 13 Aug 2023 02:32:24 +0900
Diffstat:
5 files changed, 175 insertions(+), 4 deletions(-)
diff --git a/Aida chiyo Talk app.csproj b/Aida chiyo Talk app.csproj
@@ -9,9 +9,9 @@
</PropertyGroup>
<ItemGroup>
+ <PackageReference Include="IronPython" Version="3.4.1" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
<PackageReference Include="Microsoft.ML" Version="2.0.1" />
- <PackageReference Include="OpenAI" Version="1.7.2" />
</ItemGroup>
</Project>
diff --git a/MainController.cs b/MainController.cs
@@ -1,4 +1,6 @@
-using System;
+using IronPython.Hosting;
+using Microsoft.Scripting.Hosting;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -30,5 +32,62 @@ namespace Aida_chiyo_Talk_app
Console.WriteLine(answer);
}
+
+ public void PythonTest()
+ {
+ Inputs inputs = new Inputs();
+ string input = inputs.InputConsole();
+
+ var script = @"
+ def get_x():
+ import torch
+ return 1";
+
+ var engine = Python.CreateEngine();
+ dynamic scope = engine.CreateScope();
+ engine.Execute(script, scope);
+
+ var x = scope.get_x();
+ Console.WriteLine("x is {0}", x);
+ /*
+ ScriptRuntime py = Python.CreateRuntime();
+ dynamic script = py.UseFile("Python.py");
+
+ string importPy = string.Empty;
+ try
+ {
+ importPy = script.ToString();
+ }catch (Exception ex)
+ {
+ Console.WriteLine(ex.ToString());
+ }
+ if (importPy == "import is true")
+ {
+ Console.WriteLine("import was ended");
+ }
+ else
+ {
+ Console.WriteLine("import was not ended");
+ Console.WriteLine(importPy);
+ }
+
+ string loadmodel = string.Empty;
+ try
+ {
+ loadmodel = script.ToString();
+ }catch(Exception ex)
+ {
+ Console.WriteLine(ex.ToString());
+ }
+ if(loadmodel == "importModel_is_true")
+ {
+ Console.WriteLine("Model was loaded");
+ }
+ else
+ {
+ Console.WriteLine("model was not loaded");
+ }
+ */
+ }
}
}
diff --git a/Program.cs b/Program.cs
@@ -2,4 +2,5 @@
using Aida_chiyo_Talk_app;
MainController mainController = new MainController();
-mainController.MainCon();-
\ No newline at end of file
+// mainController.MainCon();
+mainController.Talkcon();+
\ No newline at end of file
diff --git a/RinnaAIPython.py b/RinnaAIPython.py
@@ -0,0 +1,44 @@
+# coding: shift-jis
+import torch
+from transformers import AutoTokenizer, AutoModelForCausalLM
+
+print("we are loading model")
+
+# モデルのダウンロード。
+tokenizer = AutoTokenizer.from_pretrained("rinna/japanese-gpt-neox-3.6b-instruction-sft", use_fast=False)
+model = AutoModelForCausalLM.from_pretrained("rinna/japanese-gpt-neox-3.6b-instruction-sft")
+
+# GPUが使える状態なら使用する。
+device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
+model = model.to(device)
+
+print("model was loaded")
+
+# 対話の開始
+#print("会話を始めましょう!何か質問して下さい。")
+while True:
+ #user_input = input("ユーザー: ")
+ user_input = input()
+
+ # ユーザーの入力をプロンプトに追加
+ prompt = f"<NL>ユーザー: {user_input}<NL>システム: "
+
+ # プロンプトをrinnaに与えて、回答を生成する。
+ token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(device)
+
+ with torch.no_grad():
+ output_ids = model.generate(
+ token_ids,
+ do_sample=True,
+ max_length=128,
+ temperature=0.7,
+ pad_token_id=tokenizer.pad_token_id,
+ bos_token_id=tokenizer.bos_token_id,
+ eos_token_id=tokenizer.eos_token_id
+ )
+
+ # rinnaからの回答を取得する。
+ output = tokenizer.decode(output_ids.tolist()[0][token_ids.size(1):])
+ output = output.replace("<NL>", "\n")
+ #print("システム:", output)
+ print(output)+
\ No newline at end of file
diff --git a/TalkAI.cs b/TalkAI.cs
@@ -0,0 +1,66 @@
+using IronPython.Hosting;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Aida_chiyo_Talk_app
+{
+ internal class TalkAI
+ {
+ public String FileName { get; set; }
+ public String WorkingDirectory { get; set; }
+ public String Arguments { get; set; }
+ public String InputString { get; set; }
+ public String StandardOutput { get; set; }
+ public int ExitCode { get; set; }
+
+ private StringBuilder standardOutputStringBuilder = new StringBuilder();
+ public string TalkAIcon()
+ {
+ ProcessStartInfo processStartInfo = new ProcessStartInfo();
+ processStartInfo.FileName = FileName;
+ processStartInfo.WorkingDirectory = WorkingDirectory;
+ processStartInfo.Arguments = Arguments;
+
+ processStartInfo.CreateNoWindow = true;
+ processStartInfo.UseShellExecute = false;
+ processStartInfo.RedirectStandardInput = true;
+ processStartInfo.RedirectStandardOutput = true;
+ processStartInfo.RedirectStandardError = true;
+
+ Process process = new System.Diagnostics.Process();
+ process.StartInfo = processStartInfo;
+ process.OutputDataReceived += Process_OutputDataReceived;
+ process.ErrorDataReceived += Process_ErrorDataReceived;
+
+ process.Start();
+
+ using(StreamWriter streamWriter = process.StandardInput)
+ {
+ streamWriter.Write(InputString);
+ }
+
+ process.BeginOutputReadLine();
+ process.BeginErrorReadLine();
+
+ process.WaitForExit();
+ this.ExitCode = process.ExitCode;
+ this.StandardOutput = standardOutputStringBuilder.ToString();
+
+
+ }
+
+ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
+ {
+ throw new NotImplementedException();
+ }
+
+ private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
+ {
+ throw new NotImplementedException();
+ }
+ }
+}