Skip to content

Commit 9d47ea5

Browse files
committed
Looking at embedded docs for discord now.
1 parent 2ca2b73 commit 9d47ea5

File tree

6 files changed

+33
-15
lines changed

6 files changed

+33
-15
lines changed

Chat/ChatBot.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text.Json.Serialization;
2+
using Discord;
23
using OpenAI.GPT3.ObjectModels;
34
using OpenAI.GPT3.ObjectModels.RequestModels;
45
using OpenAI.GPT3.ObjectModels.ResponseModels;
@@ -75,6 +76,9 @@ public void AddInstruction(ChatMessage message)
7576

7677
public async IAsyncEnumerable<ChatCompletionCreateResponse> GetResponseAsync()
7778
{
79+
//await Console.Out.WriteLineAsync(string.Join("\r\n\r\n",
80+
// State.PrimeDirectives.Concat(State.Instructions).Concat(State.Messages).Select(s => s.Content)));
81+
7882
await foreach (var response in OpenAILogic.CreateChatCompletionAsyncEnumerable(
7983
await ParameterMapping.MapCommon(
8084
State.Parameters,

Chat/Discord/DiscordBot.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,25 @@ private async Task HandleMessageReceivedAsync(SocketMessage message)
283283
return;
284284
}
285285

286+
287+
288+
if (_documents.GetOrAdd(message.Channel.Id, new List<Document>()) is {Count: > 0} documents)
289+
{
290+
// Search for the closest few documents and add those if they aren't used yet
291+
var closestDocuments =
292+
Document.FindMostSimilarDocuments(documents, await _openAILogic.GetEmbeddingForPrompt(message.Content), channel.Chat.State.Parameters.ClosestMatchLimit).ToList();
293+
if (closestDocuments.Any(cd => cd.Similarity > 0.80))
294+
{
295+
channel.Chat.AddMessage(new(StaticValues.ChatMessageRoles.User,
296+
$"Context for the next {closestDocuments.Count} message(s). Use this to answer:"));
297+
foreach (var closestDocument in closestDocuments)
298+
{
299+
channel.Chat.AddMessage(new(StaticValues.ChatMessageRoles.User,
300+
$"---context---\r\n{closestDocument.Document.Text}\r\n--end context---"));
301+
}
302+
}
303+
}
304+
286305
// Add this message as a chat log
287306
channel.Chat.AddMessage(new ChatMessage(StaticValues.ChatMessageRoles.User, $"<{message.Author.Username}> {message.Content}"));
288307

@@ -355,9 +374,7 @@ private MessageComponent BuildStandardComponents()
355374
(StaticValues.ChatMessageRoles.System,
356375
"This is the Prime Directive: This is a chat bot running in [GPT-CLI](https://github.com/kainazzzo/GPT-CLI). Answer questions and" +
357376
" provide responses in Discord message formatting. Encourage users to add instructions with /gptcli or by using the :up_arrow:" +
358-
" emoji reaction on any message. Instructions are like 'sticky' chat messages that provide upfront context to the bot. The the 📌 emoji reaction is for pinning a message to instructions. The 🔄 emoji reaction is for replaying a message as a new prompt."),
359-
new ChatMessage(StaticValues.ChatMessageRoles.Assistant,
360-
"Got it. My name is GPT-CLI. I'm a chat bot running on discord that uses [GPT-CLI](https://github.com/kainazzzo/GPT-CLI). I'm still learning, so please be patient with me. I'm also still in development, so please report any bugs you find. You can find the source code on github here: https://github.com/kainazzzo/GPT-CLI")
377+
" emoji reaction on any message. Instructions are like 'sticky' chat messages that provide upfront context to the bot. The the 📌 emoji reaction is for pinning a message to instructions. The 🔄 emoji reaction is for replaying a message as a new prompt.")
361378
};
362379

363380
private IEnumerable<ChatMessage> PrimeDirective => _defaultPrimeDirective;

Dockerfile.discord

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ RUN yum install -y libicu
99
COPY --from=build /app/publish .
1010
COPY --from=build /src/appSettings.json .
1111
ENV MODEL=""
12-
ENTRYPOINT ./gpt discord --max-chat-history-length=4096 --chunk-size=1536 --max-tokens=4096
12+
ENTRYPOINT ./gpt discord --chunk-size=1536 --max-tokens=4096 --model=gpt-4

Embeddings/Document.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static List<Document> LoadEmbeddings(Stream input)
2424
}
2525
}
2626

27-
public static List<Document> FindMostSimilarDocuments(List<Document> documents, List<double> queryEmbedding, int numResults)
27+
public static IEnumerable<(Document Document, double Similarity)> FindMostSimilarDocuments(List<Document> documents, List<double> queryEmbedding, int numResults)
2828
{
2929
var similarities = new List<(Document Document, double Similarity)>();
3030

@@ -39,7 +39,7 @@ public static List<Document> FindMostSimilarDocuments(List<Document> documents,
3939
similarities.Sort((x, y) => y.Similarity.CompareTo(x.Similarity));
4040

4141
// Return the top numResults
42-
return similarities.Take(numResults).Select(x => x.Document).ToList();
42+
return similarities.Take(numResults);
4343
}
4444

4545

ParameterMapping.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static async Task<ChatCompletionCreateRequest> MapCommon(GPTParameters pa
3030
foreach (var closestDocument in closestDocuments)
3131
{
3232
request.Messages.Add(new(StaticValues.ChatMessageRoles.User,
33-
$"Context for the next message: {closestDocument.Text}"));
33+
$"Context for the next message: {closestDocument.Document.Text}"));
3434
}
3535
}
3636
}

Program.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,16 +409,13 @@ async Task PrintLogo()
409409
{
410410
// Search for the closest few documents and add those if they aren't used yet
411411
var closestDocuments =
412-
Document.FindMostSimilarDocuments(documents, await openAILogic.GetEmbeddingForPrompt(chatInput), gptParameters.ClosestMatchLimit);
413-
if (closestDocuments != null)
412+
Document.FindMostSimilarDocuments(documents, await openAILogic.GetEmbeddingForPrompt(chatInput), gptParameters.ClosestMatchLimit).ToList();
413+
chatBot.AddMessage(new(StaticValues.ChatMessageRoles.User,
414+
$"Embedding context for the next {closestDocuments.Count} message(s). Please use this information to answer the next prompt"));
415+
foreach (var closestDocument in closestDocuments)
414416
{
415417
chatBot.AddMessage(new(StaticValues.ChatMessageRoles.User,
416-
$"Embedding context for the next {closestDocuments.Count} message(s). Please use this information to answer the next prompt"));
417-
foreach (var closestDocument in closestDocuments)
418-
{
419-
chatBot.AddMessage(new(StaticValues.ChatMessageRoles.User,
420-
$"---context---\r\n{closestDocument.Text}\r\n--end context---"));
421-
}
418+
$"---context---\r\n{closestDocument.Document.Text}\r\n--end context---"));
422419
}
423420
}
424421

0 commit comments

Comments
 (0)