2023-07-02 13:41:38 +00:00
|
|
|
|
using LinePutScript.Localization.WPF;
|
|
|
|
|
using System;
|
2023-08-22 09:07:51 +00:00
|
|
|
|
using System.Linq;
|
2023-03-13 15:40:04 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using VPet_Simulator.Core;
|
|
|
|
|
|
|
|
|
|
namespace VPet_Simulator.Windows
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// MessageBar.xaml 的交互逻辑
|
|
|
|
|
/// </summary>
|
2023-04-01 19:31:28 +00:00
|
|
|
|
public partial class TalkBoxAPI : UserControl
|
2023-03-13 15:40:04 +00:00
|
|
|
|
{
|
|
|
|
|
Main m;
|
|
|
|
|
MainWindow mw;
|
|
|
|
|
public TalkBoxAPI(MainWindow mw)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
this.m = mw.Main;
|
|
|
|
|
this.mw = mw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendMessage_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(tbTalk.Text))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var cont = tbTalk.Text;
|
|
|
|
|
tbTalk.Text = "";
|
|
|
|
|
Task.Run(() => OPENAI(cont));
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 使用OPENAI API进行回复
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="content">内容 说话内容</param>
|
|
|
|
|
public void OPENAI(string content)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(content))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (mw.CGPTClient == null)
|
|
|
|
|
{
|
2023-07-17 15:38:06 +00:00
|
|
|
|
m.SayRnd("请先前往设置中设置 ChatGPT API".Translate());
|
2023-03-13 15:40:04 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Dispatcher.Invoke(() => this.IsEnabled = false);
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-22 09:07:51 +00:00
|
|
|
|
if (mw.CGPTClient.Completions.TryGetValue("vpet", out var vpetapi))
|
|
|
|
|
{
|
|
|
|
|
var last = vpetapi.messages.LastOrDefault();
|
|
|
|
|
if (last != null)
|
|
|
|
|
{
|
|
|
|
|
if(last.role == ChatGPT.API.Framework.Message.RoleType.user)
|
|
|
|
|
{
|
|
|
|
|
vpetapi.messages.Remove(last);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 04:04:18 +00:00
|
|
|
|
var resp = mw.CGPTClient.Ask("vpet", content);
|
|
|
|
|
var reply = resp.GetMessageContent();
|
|
|
|
|
if (resp.choices[0].finish_reason == "length")
|
|
|
|
|
{
|
|
|
|
|
reply += " ...";
|
|
|
|
|
}
|
|
|
|
|
var showtxt = "当前Token使用".Translate() + ": " + resp.usage.total_tokens;
|
|
|
|
|
Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
m.MsgBar.MessageBoxContent.Children.Add(new TextBlock() { Text = showtxt, FontSize = 20, ToolTip = showtxt, HorizontalAlignment = HorizontalAlignment.Right });
|
|
|
|
|
});
|
|
|
|
|
m.SayRnd(reply);
|
2023-03-13 15:40:04 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception exp)
|
|
|
|
|
{
|
2023-04-04 09:32:29 +00:00
|
|
|
|
var e = exp.ToString();
|
2023-07-02 13:41:38 +00:00
|
|
|
|
string str = "请检查设置和网络连接".Translate();
|
2023-04-04 09:32:29 +00:00
|
|
|
|
if (e.Contains("401"))
|
|
|
|
|
{
|
2023-07-02 13:41:38 +00:00
|
|
|
|
str = "请检查API token设置".Translate();
|
2023-04-04 09:32:29 +00:00
|
|
|
|
}
|
2023-07-17 15:38:06 +00:00
|
|
|
|
m.SayRnd("API调用失败".Translate() + $",{str}\n{e}");//, GraphCore.Helper.SayType.Serious);
|
2023-03-13 15:40:04 +00:00
|
|
|
|
}
|
|
|
|
|
Dispatcher.Invoke(() => this.IsEnabled = true);
|
|
|
|
|
}
|
2023-04-04 09:32:29 +00:00
|
|
|
|
private void tbTalk_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Key == Key.Enter && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control))
|
|
|
|
|
{
|
|
|
|
|
SendMessage_Click(sender, e);
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-16 06:55:36 +00:00
|
|
|
|
|
|
|
|
|
private void tbTalk_TextChanged(object sender, TextChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (tbTalk.Text.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
mw.Main.ToolBar.MenuPanel_MouseEnter();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mw.Main.ToolBar.MenuPanel_MouseLeave();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-13 15:40:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|