Repository navigation
Expand file tree
/
Copy pathPrompt.cpp
More file actions
117 lines (94 loc) · 2.46 KB
/
Copy pathPrompt.cpp
File metadata and controls
117 lines (94 loc) · 2.46 KB
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
#include "Prompt.h"
#include "resource.h"
#include "ResourceUtils.h"
#include "StringUtils.h"
#include <cwctype>
#include <string_view>
#include <utility>
namespace
{
constexpr std::size_t kAnchorWordCount = 3;
std::wstring LoadSystemPromptResource()
{
std::string_view prompt = ResourceUtils::LoadRcDataResource(IDR_SYSTEM_PROMPT);
prompt = StringUtils::StripTrailingLineBreaks(StringUtils::StripUtf8Bom(prompt));
return StringUtils::ReplaceWhitespaceWithSpaces(StringUtils::Utf8ToWide(prompt));
}
}
bool PromptRequest::IsAnchorSeparator(wchar_t ch)
{
return std::iswspace(static_cast<std::wint_t>(ch)) != 0;
}
std::wstring PromptRequest::BuildAnchor(std::wstring_view userInput)
{
std::size_t position = userInput.size();
std::size_t anchorBegin = userInput.size();
std::size_t wordsFound = 0;
while (position > 0 && wordsFound < kAnchorWordCount)
{
while (position > 0 && IsAnchorSeparator(userInput[position - 1]))
{
--position;
}
if (position == 0)
{
break;
}
while (position > 0 && !IsAnchorSeparator(userInput[position - 1]))
{
--position;
}
anchorBegin = position;
++wordsFound;
}
if (wordsFound == 0)
{
return L"";
}
return std::wstring(userInput.substr(anchorBegin));
}
PromptRequest::PromptRequest(std::wstring systemInstruction, std::wstring userInput)
: m_SystemInstruction(std::move(systemInstruction)),
m_UserInput(std::move(userInput)),
m_Anchor(BuildAnchor(m_UserInput))
{
}
std::wstring_view PromptRequest::SystemInstruction() const
{
return m_SystemInstruction;
}
std::wstring_view PromptRequest::UserInput() const
{
return m_UserInput;
}
std::wstring_view PromptRequest::Anchor() const
{
return m_Anchor;
}
bool PromptRequest::IsPromptValid() const
{
for (wchar_t ch : m_UserInput)
{
if (!std::iswspace(static_cast<std::wint_t>(ch)))
{
return true;
}
}
return false;
}
Prompt::Prompt()
: m_SystemInstruction(DefaultSystemInstruction())
{
}
Prompt::Prompt(std::wstring systemInstruction)
: m_SystemInstruction(std::move(systemInstruction))
{
}
PromptRequest Prompt::BuildRequest(const BufferState& snapshot) const
{
return PromptRequest(m_SystemInstruction, snapshot.Text);
}
std::wstring Prompt::DefaultSystemInstruction()
{
return LoadSystemPromptResource();
}