-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPrompt.cs
More file actions
56 lines (46 loc) · 2.11 KB
/
Copy pathPrompt.cs
File metadata and controls
56 lines (46 loc) · 2.11 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
using System;
namespace ReasoningAI;
public class Prompt
{
public static Action<string>? TitleChanged { get; set; }
public string UserPrompt { get; set; } = "";
public List<string> Reasonings { get; set; } = new List<string>();
public string FinalResponse { get; set; } = "";
public static async Task<Prompt> Send(string prompt, int maxIterations = 15)
{
Prompt p = new Prompt();
p.UserPrompt = prompt;
OllamaRequest request = new OllamaRequest();
request.messages.Add(new OllamaMessage() { role = "user", content = prompt });
OllamaResponse response = await request.Send();
int i = 0;
while (!p.AddResponse(response.message!.content) && ++i < maxIterations)
{
string continueToken = "<CONTINUE>";
if (i == maxIterations - 1)
continueToken = "<FINAL CONTINUE>";
request.messages.Add(new OllamaMessage() { role = "assistant", content = response.message.content });
request.messages.Add(new OllamaMessage() { role = "user", content = "<ORIGINAL PROMPT>" + prompt + "</ORIGINAL PROMPT>" + continueToken });
response = await request.Send();
}
request.messages.Add(new OllamaMessage() { role = "assistant", content = response.message.content });
request.messages.Add(new OllamaMessage() { role = "user", content = "<ORIGINAL PROMPT>" + prompt + "</ORIGINAL PROMPT><GENERATE RESPONSE>" });
p.FinalResponse = (await request.Send()).message!.content;
return p;
}
public bool AddResponse(string response)
{
if (response.Contains("<TITLE>") && response.Contains("</TITLE>"))
{
int index = response.IndexOf("<TITLE>");
int endIndex = response.IndexOf("</TITLE>");
string title = response.Substring(index + "<TITLE>".Length, endIndex - index - "<TITLE>".Length);
if (TitleChanged != null)
TitleChanged.Invoke(title);
}
Reasonings.Add(response);
if (response.Contains("<FINISHED>"))
return true;
return false;
}
}