Skip to content

Commit 8dbf5d3

Browse files
committed
LlmEventProcessor to handling streaming of messages
1 parent c399b06 commit 8dbf5d3

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

ChatRPG/API/LlmEventProcessor.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Text;
2+
using System.Threading.Channels;
3+
using LangChain.Providers;
4+
using LangChain.Providers.OpenAI;
5+
6+
namespace ChatRPG.API;
7+
8+
public class LlmEventProcessor
9+
{
10+
private readonly object _lock = new object();
11+
private readonly StringBuilder _buffer = new StringBuilder();
12+
private bool _foundFinalAnswer = false;
13+
private readonly Channel<string> _channel = Channel.CreateUnbounded<string>();
14+
15+
public LlmEventProcessor(OpenAiChatModel model)
16+
{
17+
model.DeltaReceived += OnDeltaReceived;
18+
model.ResponseReceived += OnResponseReceived;
19+
}
20+
21+
public async IAsyncEnumerable<string> GetContentStreamAsync()
22+
{
23+
while (await _channel.Reader.WaitToReadAsync())
24+
{
25+
while (_channel.Reader.TryRead(out var chunk))
26+
{
27+
yield return chunk;
28+
}
29+
}
30+
}
31+
32+
private void OnDeltaReceived(object? sender, ChatResponseDelta delta)
33+
{
34+
lock (_lock)
35+
{
36+
if (_foundFinalAnswer)
37+
{
38+
// Directly output content after "Final answer: " has been detected
39+
_channel.Writer.TryWrite(delta.Content);
40+
}
41+
else
42+
{
43+
// Accumulate the content in the buffer
44+
_buffer.Append(delta.Content);
45+
46+
// Check if the buffer contains "Final answer: "
47+
var bufferString = _buffer.ToString();
48+
int finalAnswerIndex = bufferString.IndexOf("Final Answer: ", StringComparison.Ordinal);
49+
50+
if (finalAnswerIndex != -1)
51+
{
52+
// Output everything after "Final answer: " has been detected
53+
int startOutputIndex = finalAnswerIndex + "Final Answer: ".Length;
54+
55+
// Switch to streaming mode
56+
_foundFinalAnswer = true;
57+
58+
// Output any content after "Final answer: "
59+
_channel.Writer.TryWrite(bufferString[startOutputIndex..]);
60+
61+
// Clear the buffer since it's no longer needed
62+
_buffer.Clear();
63+
}
64+
}
65+
}
66+
}
67+
68+
private void OnResponseReceived(object? sender, ChatResponse response)
69+
{
70+
lock (_lock)
71+
{
72+
// Reset the state so that the process can start over
73+
_foundFinalAnswer = false;
74+
_channel.Writer.TryComplete();
75+
_buffer.Clear(); // Clear buffer to avoid carrying over any previous data
76+
}
77+
}
78+
}

ChatRPG/API/ReActLlmClient.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using LangChain.Chains.HelperChains;
2+
using LangChain.Chains.StackableChains.Agents;
3+
using LangChain.Memory;
4+
using LangChain.Prompts;
5+
using LangChain.Providers.OpenAI;
6+
using LangChain.Providers.OpenAI.Predefined;
7+
using static LangChain.Chains.Chain;
8+
9+
namespace ChatRPG.API;
10+
11+
public class ReActLlmClient : IOpenAiLlmClient
12+
{
13+
private readonly Gpt4Model _llm;
14+
private readonly ConversationBufferMemory _memory;
15+
private readonly PromptTemplate _promptTemplate;
16+
private StackChain _chain;
17+
private readonly ReActAgentExecutorChain _agent;
18+
19+
public ReActLlmClient(IConfiguration configuration)
20+
{
21+
ArgumentException.ThrowIfNullOrEmpty(configuration.GetSection("ApiKeys")?.GetValue<string>("OpenAI"));
22+
var provider = new OpenAiProvider(configuration.GetSection("ApiKeys")?.GetValue<string>("OpenAI")!);
23+
_llm = new Gpt4Model(provider);
24+
_memory = new ConversationBufferMemory(new ChatMessageHistory());
25+
_chain = LoadMemory(_memory, outputKey: "history") | Template("I'm AI, hello") | LLM(_llm) |
26+
UpdateMemory(_memory, requestKey: "input", responseKey: "text");
27+
_promptTemplate = GetTemplate();
28+
_agent = ReActAgentExecutor(_llm);
29+
//agent.UseTool();
30+
_llm.Settings = new OpenAiChatSettings { UseStreaming = true };
31+
32+
}
33+
34+
public async Task<string> GetChatCompletion(IList<OpenAiGptMessage> inputs, string systemPrompt)
35+
{
36+
var chain = Set() | _agent;
37+
return (await chain.RunAsync("text"))!;
38+
}
39+
40+
public IAsyncEnumerable<string> GetStreamedChatCompletion(IList<OpenAiGptMessage> inputs, string systemPrompt)
41+
{
42+
var eventProcessor = new LlmEventProcessor(_llm);
43+
44+
var chain = Set() | _agent;
45+
46+
_ = Task.Run(async () => await chain.RunAsync());
47+
48+
return eventProcessor.GetContentStreamAsync();
49+
}
50+
51+
private PromptTemplate GetTemplate()
52+
{
53+
return new PromptTemplate(new PromptTemplateInput(
54+
template: @"Assistant is a large language model trained by OpenAI.
55+
56+
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
57+
58+
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
59+
60+
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
61+
62+
TOOLS:
63+
------
64+
65+
Assistant has access to the following tools:
66+
67+
{tools}
68+
69+
To use a tool, please use the following format:
70+
71+
```
72+
Thought: Do I need to use a tool? Yes
73+
Action: the action to take, should be one of [{tool_names}]
74+
Action Input: the input to the action
75+
Observation: the result of the action
76+
```
77+
78+
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
79+
80+
```
81+
Thought: Do I need to use a tool? No
82+
Final Answer: [your response here]
83+
```
84+
85+
Begin!
86+
87+
Previous conversation history:
88+
{chat_history}
89+
90+
New input: {input}
91+
{agent_scratchpad}",
92+
inputVariables: new[] { "tools", "tool_names", "chat_history", "input", "agent_scratchpad" }));
93+
}
94+
}

ChatRPG/ChatRPG.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Blazored.Modal" Version="7.1.0" />
12+
<PackageReference Include="LangChain" Version="0.15.2" />
1213
<PackageReference Include="MailKit" Version="4.2.0" />
1314
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.12" />
1415
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.12" />

0 commit comments

Comments
 (0)