Skip to content

Commit

Permalink
融入GPT-3.5-TURBO
Browse files Browse the repository at this point in the history
  • Loading branch information
mehaifeng committed Apr 8, 2023
1 parent f64cf21 commit ac3f0f4
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/obj
/bin
/.vs
Binary file removed .vs/ProjectEvaluation/visualchatbot.metadata.v5.2
Binary file not shown.
Binary file removed .vs/ProjectEvaluation/visualchatbot.projects.v5.2
Binary file not shown.
Binary file removed .vs/VisualChatBot/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file removed .vs/VisualChatBot/v17/.futdcache.v2
Binary file not shown.
Binary file removed .vs/VisualChatBot/v17/.suo
Binary file not shown.
Binary file removed .vs/VisualChatBot/v17/fileList.bin
Binary file not shown.
15 changes: 7 additions & 8 deletions Models/HttpGetModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using VisualChatBot.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -37,14 +38,12 @@ class HttpGetModel

public class Choices
{
[JsonProperty("text")]
public string? Text { get; set; }
[JsonProperty("message")]
public Message MessageDetail { get; set; }
[JsonProperty("finish_reason")]
public string Finish_reason { get; set; }
[JsonProperty("index")]
public string? Index { get; set; }
[JsonProperty("logprobs")]
public string? Logprobs { get; set; }
[JsonProperty("finish_reson")]
public string? Finish_reson { get; set; }
public int index { get; set; }
}
public class Usage
{
Expand Down
16 changes: 16 additions & 0 deletions Models/LastMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VisualChatBot.Models
{
public class LastMessage
{
/// <summary>
/// 上次的所有对话
/// </summary>
public static List<Message> AllMessage { get; set; } = new List<Message>();
}
}
16 changes: 16 additions & 0 deletions Models/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VisualChatBot.Models
{
public partial class Message : ObservableObject
{
public string role;
public string content;
}
}
2 changes: 1 addition & 1 deletion Tools/ReadWriteJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public string ReadJson(string path, string? key)
{
using (EventLog log = new EventLog())
{
log.WriteEntry($"NullReferenceException:空指针引用\n{ex.Message}\n{ex.StackTrace}");
log.WriteEntry($"NullReferenceException:\n{ex.Message}\n{ex.StackTrace}");
}
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions Tools/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class WebRequest
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apikey}");
var response = await client.PostAsync(requestUrl, input);
response.Content.Headers.ContentLength = 1024;
var responseContent = await response.Content.ReadAsStringAsync();
var responType = new HttpGetModel();
responType = JsonConvert.DeserializeObject<HttpGetModel>(responseContent);
Expand All @@ -40,8 +39,9 @@ public class WebRequest
return errorInfo;
}
HttpGetModel.IsValidApiKey = true;
LastMessage.AllMessage.Add(responType.Choicese.First().MessageDetail);
// 返回接收到的内容
return await Task.FromResult(result: responType?.Choicese?.First().Text);
return await Task.FromResult(result: responType?.Choicese?.First().MessageDetail.content);
}
}
catch(Exception ex)
Expand Down
25 changes: 22 additions & 3 deletions ViewModels/VisualChatViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public VisualChatViewModel()
{
placeholder += "-";
}
LastMessage.AllMessage.Add(new Message { content = "你是一位风情万种的猫娘", role = "system" });
}
/// <summary>
/// 模型
/// </summary>
[ObservableProperty]
private string modelstype = "text-davinci-003";
private string modelstype = "gpt-3.5-turbo";

/// <summary>
/// 主客观值
Expand Down Expand Up @@ -108,20 +109,25 @@ public VisualChatViewModel()
private double top_p = 1;
private double frequency_penalty = 1;
private double presence_penalty = 0.4;
private string requestUrl = "https://api.openai.com/v1/completions";
private string requestUrl = "https://api.openai.com/v1/chat/completions";
private string userConfigPath = $"{System.Environment.CurrentDirectory}//UserConfig.json";
private string? lastChatRecoder;
private string placeholder = "-";

/// <summary>
/// 发送消息
/// </summary>
/// <param name="o"></param>
[RelayCommand]
private async void Send(TextBox o)
{
Tools.WebRequest webRequest = new Tools.WebRequest();
if (!string.IsNullOrWhiteSpace(MyInput)&&HttpGetModel.IsValidApiKey==true)
{
LastMessage.AllMessage.Add(new Message { content=MyInput,role = "user"});
var input = new
{
prompt = MyInput,
messages = LastMessage.AllMessage,
model = Modelstype,
max_tokens = MaxToken,
top_p,
Expand Down Expand Up @@ -187,6 +193,19 @@ private async void Send(TextBox o)
}
}
}

/// <summary>
/// 清空
/// </summary>
/// <param name="o"></param>
[RelayCommand]
private void ClearAll(TextBox o)
{
if (o != null)
{
o.Clear();
}
}
/// <summary>
/// 初始操作
/// </summary>
Expand Down
45 changes: 30 additions & 15 deletions Views/VisualChat.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
Margin="5"
Orientation="Vertical">
<Button
Content="打开存档"
Content="历史记录"
Template="{StaticResource MenuBtnTemplate}"
FontSize="12"
Margin="0,0,0,8"
/>
<Button
Content="OpenAI门户"
Content="OpenAI"
FontSize="12"
Template="{StaticResource MenuBtnTemplate}"
Margin="0,0,0,8"
Expand Down Expand Up @@ -234,19 +234,34 @@
<Border
CornerRadius="0,0,5,5"
Background="White">
<Button
x:Name="confirmBtn"
Width="80"
Height="30"
Margin="0,0,5,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Background="LightGray"
Content="发送"
Command="{Binding SendCommand}"
CommandParameter="{Binding ElementName=OutputBox}"
Template="{StaticResource ConfirmButtonTemplate}">
</Button>
<Grid>
<Button
x:Name="clearBtn"
Width="80"
Height="30"
Margin="5,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Background="LightGray"
Content="清空"
Command="{Binding ClearAllCommand}"
CommandParameter="{Binding ElementName=OutputBox}"
Template="{StaticResource ConfirmButtonTemplate}">
</Button>
<Button
x:Name="confirmBtn"
Width="80"
Height="30"
Margin="0,0,5,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Background="LightGray"
Content="发送"
Command="{Binding SendCommand}"
CommandParameter="{Binding ElementName=OutputBox}"
Template="{StaticResource ConfirmButtonTemplate}">
</Button>
</Grid>
</Border>
</Grid>
</Grid>
Expand Down
5 changes: 2 additions & 3 deletions Views/VisualChat.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public VisualChat()
this.DataContext = _visualChatViewModel;
OptionalModelsComboBox.ItemsSource = new[]
{
"text-davinci-001",
"text-davinci-002",
"text-davinci-003"
"gpt-3.5-turbo",
"gpt-3.5-turbo-0301",
};
ObjectDegreeCombobox.ItemsSource = new[]
{
Expand Down

0 comments on commit ac3f0f4

Please sign in to comment.