A ReACT AI Agent combines the reasoning power of large language models with and external tools before taking action. This allows it to understand information, make decisions, take actions, and communicate effectively, making it a more sophisticated system.
#Reason+Act agent that iteratively reasons and gathers information from wikipedia before providing an answer.
import re
import httpx
import ollama
from openai import OpenAI
client = OpenAI(
base_url = 'http://localhost:11434/v1',
api_key='ollama', # required, but unused
)
#Define Prompt for our usecase
#The prompt tells the LLM how to approach a problem. This prompt is a simple example and most definitely for demonstration purposes, only.
prompt = """
You run in a loop of Thought, Action, Observation, Answer.
At the end of the loop you output an Answer
Use Thought to describe your thoughts about the question you have been asked.
Use Action to run one of the actions available to you.
Observation will be the result of running those actions.
Answer will be the result of analysing the Observation
Your available actions are:
calculate:
e.g. calculate: 4 * 7 / 3
Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary
wikipedia:
e.g. wikipedia: Django
Returns a summary from searching Wikipedia
Always look things up on Wikipedia if you have the opportunity to do so.
Example session:
Question: What is the capital of France?
Thought: I should look up France on Wikipedia
Action: wikipedia: France
You should then call the appropriate action and determine the answer from the result
You then output:
Answer: The capital of France is Paris
""".strip()
#Implement a Chatbot
#creating a client and then defining a Python class that will implement a chatbot
class ChatBot:
def __init__(self, system=""):
self.system = system
self.messages = []
def __call__(self, message):
self.messages.append({"role": "user", "content": message})
result = self.execute()
self.messages.append({"role": "assistant", "content": result})
return result
def execute(self):
conversation = self.system + "\n".join([f"{msg['role']}: {msg['content']}" for msg in self.messages])
print("In execute after conversation " )
response = response = client.chat.completions.create(
model="phi3",
messages=[
{"role": "system", "content": self.system},
*self.messages
],
temperature=0
)
result = response.choices[0].message.content
print ("result ", result)
return result
#Define a Query
#The next step is to define a query() function that uses an instantiation of the chatbot. The query function implements a loop that continues until there are no more actions (or we've reached the maximum number of iterations).
action_re = re.compile(r'^Action: (\w+): (.*)$')
print ("action_re ", action_re)
def query(question, max_turns=5, prompt=prompt):
i = 0
bot = ChatBot(system=prompt)
next_prompt = question
print("\n\n this the question ", question)
while i < max_turns:
i += 1
print("\n\n before bot")
result = bot(next_prompt)
print("\n\n after bot")
print("result ", result)
actions = [action_re.match(a) for a in result.split('\n') if action_re.match(a)]
if actions:
# There is an action to run
action, action_input = actions[0].groups()
if action not in known_actions:
raise Exception("Unknown action: {}: {}".format(action, action_input))
print(" -- running {} {}".format(action, action_input))
observation = known_actions[action](action_input)
print("Observation:", observation)
next_prompt = "Observation: {}".format(observation)
else:
return bot.messages
#Define the Action Functions (tools)
#wikipedia and eval and we store references to those in a dictionary.
def wikipedia(q):
return httpx.get("https://en.wikipedia.org/w/api.php", params={
"action": "query",
"list": "search",
"srsearch": q,
"format": "json"
}).json()["query"]["search"][0]["snippet"]
def calculate(what):
return eval(what)
known_actions = {
"wikipedia": wikipedia,
"calculate": calculate
}
#Next is a utility function that prints out the last message to have been generated by the bot.
def get_last_message(bot):
for m in bot.messages[-1]['content'][0].text.split('\n'):
print("\n\n in get last message ", m)
#Test the Agent
bot_instance = ChatBot(system=prompt)
query("Who won the latest US election")