First, describe the large language model "ReAct" prompting pattern briefly. Then, implement an Elixir implementation of it, with a function called LLM.exec(query) used to call out to the LLM model powering the ReAct implementation.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The ReAct (Retrieve-Act) prompting pattern is a method for interacting with large language models that involves breaking down a complex query into two parts: a retrieval step and a generation step. In the retrieval step, relevant information is gathered from a knowledge base or external source. In the generation step, the language model uses the retrieved information to generate a response to the original query. This approach can improve the accuracy and relevance of the model's responses, particularly for complex or domain-specific queries.
Here is an example of an Elixir implementation of the ReAct prompting pattern:
code
defmodule LLM do
# Connect to the large language model API
@llm_api "https://example.com/llm"
# Define a function to retrieve information from a knowledge base
defp retrieve_info(query) do
# Connect to the knowledge base API
@kb_api "https://example.com/kb"
# Send the query to the knowledge base API
response = HTTPoison.get(@kb_api, query: %{query: query})
# Parse the response and extract the relevant information
case response.status_code do
200 ->
json = Poison.decode!(response.body)
info = json["information"]
{:ok, info}
_ ->
{:error, "Failed to retrieve information from the knowledge base"}
end
end
# Define a function to generate a response using the large language model
defp generate_response(info) do
# Send the retrieved information to the large language model API
response = HTTPoison.post(@llm_api, Poison.encode!(%{info: info}))
# Parse the response and extract the generated text
case response.status_code do
200 ->
json = Poison.decode!(response.body)
text = json["text"]
{:ok, text}
_ ->
{:error, "Failed to generate a response using the large language model"}
end
end
# Define the main function to call the large language model using the ReAct prompting pattern
def exec(query) do
# Retrieve relevant information from the knowledge base
case retrieve_info(query) do
{:ok, info} ->
# Generate a response using the large language model
case generate_response(info) do
{:ok, text} ->
text
{:error, error} ->
error
end
{:error, error} ->
error
end
end
end
This implementation defines a LLM module with three functions: retrieve_info/1, generate_response/1, and exec/1. The retrieve_info/1 function sends the query to a knowledge base API and extracts the relevant information from the response. The generate_response/1 function sends the retrieved information to the large language model API and extracts the generated text from the response. The exec/1 function calls the retrieve_info/1 and generate_response/1 functions in sequence to implement the ReAct prompting pattern.
Note that this is just an example implementation, and you would need to replace the @llm_api and @kb_api variables with the actual URLs of the large language model and knowledge base APIs that you are using. You may also need to modify the query parameters and response handling code to match the specific APIs that you are using.
I hope this helps! Let me know if you have any questions.