|
| 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 | +} |
0 commit comments