-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bot.cs
162 lines (132 loc) · 4.81 KB
/
Bot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Internal.Catalog.PropertyPages.NetworkDataset;
using OpenAI_API;
using OpenAI_API.Chat;
using PAMChatGPT.Enums;
using PAMChatGPT.Extensions;
using PAMChatGPT.Properties;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PAMChatGPT
{
public class Bot
{
private Conversation chat = null;
public ObservableCollection<Message> Messages { get; } = new ObservableCollection<Message>();
private static Bot instance;
private static readonly object locker = new();
public static Bot GetBot()
{
if (instance == null)
{
lock (locker)
{
instance ??= new Bot();
}
}
return instance;
}
protected Bot()
{
if (Settings.Default.UseAPI.ToLowerInvariant() == UseAPI.OpenAI.GetDescription().ToLowerInvariant())
{
Api = new()
{
Auth = new APIAuthentication(Settings.Default.OpenAI_api_key)
};
if (!string.IsNullOrWhiteSpace(Settings.Default.OpenAI_Organization))
{
Api.Auth.OpenAIOrganization = Settings.Default.OpenAI_Organization;
}
}
else if (Settings.Default.UseAPI.ToLowerInvariant() == UseAPI.OpenAI_Azure.GetDescription().ToLowerInvariant())
{
Api = OpenAIAPI.ForAzure(Settings.Default.OpenAIAzure_ResourceName, Settings.Default.OpenAIAzure_DeploymentId, Settings.Default.OpenAIAzure_api_key);
Api.ApiVersion = Settings.Default.OpenAIAzure_APIVersion;
}
}
public OpenAIAPI Api
{
get;set;
}
public async Task SendActivityAsync(string text, MessageFrom messageFrom = MessageFrom.User, string assistantText = null, string userText = null)
{
try
{
CreateConversationIfNotExistAsync();
if (!string.IsNullOrWhiteSpace(userText))
{
Messages.Add(new Message
{
MessageFrom = MessageFrom.UserCode,
Text = userText,
});
chat.AppendUserInput(userText);
}
else if (!string.IsNullOrWhiteSpace(assistantText))
{
Messages.Add(new Message
{
MessageFrom = MessageFrom.AssistantCode,
Text = assistantText,
});
chat.AppendUserInput(assistantText);
}
Messages.Add(new Message
{
MessageFrom = messageFrom,
Text = text,
});
chat.AppendUserInput(text);
string response = await chat.GetResponseFromChatbotAsync();
Message message = new()
{
MessageFrom = MessageFrom.Bot,
Text = response
};
Messages.Add(message);
//await foreach (var res in chat.StreamResponseEnumerableFromChatbotAsync())
//{
// message.Text += res;
//}
}
catch (System.Security.Authentication.AuthenticationException)
{
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Invalid OpenAI api key set in Options -> ChatGPT!",
"Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
}
catch (Exception ex)
{
Messages.Add(new Message
{
MessageFrom = MessageFrom.System,
Text = ex.Message
});
}
}
public void ClearChat()
{
Messages.Clear();
chat = null;
}
private void CreateConversationIfNotExistAsync()
{
if (chat != null)
{
return;
}
chat = Api.Chat.CreateConversation();
if (Settings.Default.UseAPI.ToLowerInvariant() == UseAPI.OpenAI.GetDescription().ToLowerInvariant())
{
string modelName = Settings.Default.OpenAI_Model;
if (string.IsNullOrWhiteSpace(modelName))
{
modelName = OpenAI_API.Models.Model.ChatGPTTurbo;
}
chat.Model = new OpenAI_API.Models.Model(modelName);
}
}
}
}