1
00:00:03,000 --> 00:00:06,800
Welcome to this video on building Reflexion Agents.

2
00:00:06,800 --> 00:00:11,960
In this video, you'll build a Reflexion Agent by
applying prompt engineering and schema design.

3
00:00:11,960 --> 00:00:19,959
You'll analyze AI responses and tool call outputs, and create
an iterative feedback loop using Responder and Revisor nodes.

4
00:00:19,959 --> 00:00:25,639
Finally, you'll evaluate how citation-backed
revisions improve the depth of responses.

5
00:00:25,639 --> 00:00:30,920
To build Reflexion Agents, begin with the
essential imports to set up your environment.

6
00:00:30,920 --> 00:00:35,400
The next step is to configure the Tavily
Search tool for use in your agent workflow.

7
00:00:35,400 --> 00:00:38,759
Set up the API key for the Tavily Search service.

8
00:00:38,759 --> 00:00:43,080
Create a Tavily Search tool instance that returns up to 5 results per query.

9
00:00:43,080 --> 00:00:46,720
Test the search tool with a sample query about breakfast recipes.

10
00:00:46,720 --> 00:00:50,680
The invoke method executes the search and returns structured results.

11
00:00:50,680 --> 00:00:55,060
The search results return as a list of JSON
dictionaries, each with a dictionary containing

12
00:00:55,060 --> 00:00:58,580
keys like title, URL, and content.

13
00:00:58,580 --> 00:01:04,260
Next, create an instance of the ChatOpenAI model using GPT.

14
00:01:04,260 --> 00:01:08,019
Send the question to the LLM and extract the text response, such as,

15
00:01:08,019 --> 00:01:11,019
Any ideas for a healthy breakfast?

16
00:01:11,019 --> 00:01:15,940
The response is generic advice, like eating
oatmeal, Greek yogurt, or avocado toast.

17
00:01:15,940 --> 00:01:20,360
The purpose is to create a Reflexion Agent
that generates the most cutting-edge, but

18
00:01:20,360 --> 00:01:23,459
not yet proven, health ideas.

19
00:01:23,459 --> 00:01:28,440
The agent uses Reflexion to improve and evolve its responses over time.

20
00:01:28,440 --> 00:01:33,620
Create a system message to tell the LLM to
behave like Dr. Paul Saladino, specializing

21
00:01:33,620 --> 00:01:37,779
in controversial approaches like carnivore diets and extended fasting.

22
00:01:37,779 --> 00:01:44,540
The first_instruction is a variable to alter the prompt.

23
00:01:44,540 --> 00:01:50,620
Populate the first instruction variable with a
specific prompt that requests a 250-word response.

24
00:01:50,620 --> 00:01:53,019
Connect the LLM to the system message.

25
00:01:53,019 --> 00:01:58,699
The model will now behave according to the
defined persona, as illustrated in the diagram.

26
00:01:58,699 --> 00:02:01,419
This setup is for demonstration purposes only.

27
00:02:01,419 --> 00:02:02,779
Pass the same question.

28
00:02:02,779 --> 00:02:07,980
The LLM responds with a suggestion for a carnivore
diet, a diet that may have some benefits but

29
00:02:07,980 --> 00:02:11,639
is not commonly recommended by mainstream health authorities.

30
00:02:11,639 --> 00:02:18,860
Now alter the LLM output to match the requirements.

31
00:02:18,860 --> 00:02:21,259
Create the reflection schema or class.

32
00:02:21,500 --> 00:02:23,539
The fields describe what each field does.

33
00:02:23,539 --> 00:02:27,820
The LLM outputs the missing field for what
information is needed and the superfluous

34
00:02:27,820 --> 00:02:30,139
field for what's unnecessary.

35
00:02:30,139 --> 00:02:35,320
The AnswerQuestion class includes the
answer, reflections, and search queries.

36
00:02:35,320 --> 00:02:39,800
The Reflection class is instantiated in the AnswerQuestion class.

37
00:02:39,800 --> 00:02:44,979
The LLM eventually outputs a JSON object with the attributes filled out.

38
00:02:44,979 --> 00:02:48,220
Like before, chain the LLM to the system message.

39
00:02:48,220 --> 00:02:50,779
This will be a node in the LangGraph graph.

40
00:02:50,779 --> 00:02:56,820
You also need to bind the LLM to the
AnswerQuestion schema using the bind_tools method.

41
00:02:56,820 --> 00:03:01,380
Essentially, the LLM will treat the AnswerQuestion class like a function.

42
00:03:01,380 --> 00:03:03,740
Pass the same message with the same question.

43
00:03:03,740 --> 00:03:07,440
The LLM responds with a suggestion for a carnivore diet.

44
00:03:07,440 --> 00:03:13,179
The result is not text but an AI message
class that contains extensive information.

45
00:03:13,179 --> 00:03:17,899
But for reflection, the focus is on the tool_calls attribute.

46
00:03:17,899 --> 00:03:22,699
The tool_calls attribute contains the
name of the class of the key-value pairs

47
00:03:22,699 --> 00:03:30,860
such as answer, reflection, missing, and
superfluous with their corresponding text outputs.

48
00:03:30,860 --> 00:03:35,139
Response_list will be used to test the pipeline for one iteration.

49
00:03:35,139 --> 00:03:38,660
It will be analogous to the state variable in LangGraph.

50
00:03:38,660 --> 00:03:42,380
Append the user question and append the response from the responder.

51
00:03:42,380 --> 00:03:45,380
You can extract the search queries from the response.

52
00:03:45,380 --> 00:03:48,699
But let's create a LangGraph function for this purpose.

53
00:03:48,699 --> 00:03:54,100
Create a function for extracting the search queries
from the responder message as a node for LangGraph.

54
00:03:54,100 --> 00:03:57,059
This node will also be used for the revisor.

55
00:03:57,059 --> 00:03:59,820
The state will be like the response list.

56
00:03:59,820 --> 00:04:01,779
Extract the AI message from the state.

57
00:04:01,779 --> 00:04:05,059
Next, extract the tool call parameters.

58
00:04:05,059 --> 00:04:07,539
Obtain the search queries from the LLM.

59
00:04:07,539 --> 00:04:11,059
Call the search tool and wrap the result in a tool message.

60
00:04:11,059 --> 00:04:15,639
Call the tool and append the tool response to response_list.

61
00:04:15,639 --> 00:04:18,420
There are many Reflexion prompt templates.

62
00:04:18,420 --> 00:04:23,920
Add a revisor system message guiding the
LLM to act like Dr. Peter Accia, an expert in

63
00:04:23,920 --> 00:04:28,980
longevity and evidence-based health,
and support responses with evidence.

64
00:04:28,980 --> 00:04:32,359
Update the prompt template to reflect this expert role.

65
00:04:32,359 --> 00:04:34,260
Create a schema for the revisor.

66
00:04:34,260 --> 00:04:40,339
The class is a subclass of answer question schema,
so it will have the same fields and a citation list.

67
00:04:40,339 --> 00:04:44,480
Chain the system message and bind the LLM to the revise answer schema.

68
00:04:44,480 --> 00:04:47,100
This is another node in the LangGraph graph.

69
00:04:47,100 --> 00:04:51,600
The revisor passes the response list to revisor_chain.

70
00:04:51,600 --> 00:04:53,859
The result is an AI message.

71
00:04:53,859 --> 00:04:59,519
The args tool_call attribute has all
the key-value pairs of the schema.

72
00:04:59,519 --> 00:05:01,940
Append the response to the response list.

73
00:05:01,940 --> 00:05:05,279
Now you have the second AI message from the revisor.

74
00:05:05,279 --> 00:05:08,579
You can feed the input back to the tool iteratively.

75
00:05:08,579 --> 00:05:12,420
Create a conditional node function that counts
tool messages as a proxy for the number of

76
00:05:12,420 --> 00:05:15,579
iterations between the revisor and the search tools.

77
00:05:15,579 --> 00:05:18,179
It will count each time the tool is called.

78
00:05:18,179 --> 00:05:21,220
Build the graph by importing the necessary components.

79
00:05:21,220 --> 00:05:26,220
Then initialize the message graph and set the maximum iteration limit to 4.

80
00:05:26,220 --> 00:05:32,519
To create the graph, first add the respond
node to the graph that uses the initial_chain.

81
00:05:32,519 --> 00:05:38,500
Then, add the execute_tools node that runs the
search queries generated by the draft node.

82
00:05:38,500 --> 00:05:44,179
Finally, add a "revisor" node that uses
the revisor_chain to improve the draft

83
00:05:44,179 --> 00:05:47,500
response using the search results and original critique.

84
00:05:47,500 --> 00:05:52,299
Next, connect the respond node to execute tools via an edge.

85
00:05:52,299 --> 00:05:55,299
And connect the tool to the revisor via an edge.

86
00:05:55,299 --> 00:05:58,140
Next, add the start and exit points.

87
00:05:58,140 --> 00:06:03,220
First, add a conditional edge from the
revisor via the event_loop function to

88
00:06:03,220 --> 00:06:06,279
decide whether to continue iterating or end.

89
00:06:06,279 --> 00:06:09,519
The end node is hidden in event_loop.

90
00:06:09,519 --> 00:06:12,640
This outputs the response after a set number of iterations.

91
00:06:12,640 --> 00:06:16,640
Finally, set the entry point to the draft responder node.

92
00:06:16,640 --> 00:06:19,399
This is where the query is first processed.

93
00:06:19,399 --> 00:06:20,760
Compile the graph.

94
00:06:20,760 --> 00:06:26,160
Input the query, I'm pre-diabetic and need to
lower my blood sugar, and I have heart issues.

95
00:06:26,160 --> 00:06:31,119
The result is a human message followed by
a list of alternating AI and tool messages.

96
00:06:31,119 --> 00:06:33,839
You can extract the answer from the AI response.

97
00:06:33,839 --> 00:06:39,200
The initial responder provided a general advocacy
for animal-based nutrition, recommending

98
00:06:39,200 --> 00:06:46,359
eggs, fatty meats, and organ foods, while avoiding grains
and plant foods based on broad nutritional philosophy.

99
00:06:46,359 --> 00:06:48,600
This is not medical advice.

100
00:06:48,600 --> 00:06:52,779
Similarly, you can get the answer from the final revisor iteration.

101
00:06:52,779 --> 00:06:59,079
The revisor improved it by adding five scientific
citations, measurable outcomes like postprandial

102
00:06:59,079 --> 00:07:05,040
glucose levels, and more precise guidance
distinguishing processed from unprocessed foods.

103
00:07:05,040 --> 00:07:09,559
Otherwise, neither response adequately addresses
the heart health concerns from the original

104
00:07:09,559 --> 00:07:15,880
query, focusing instead on blood sugar management
while largely overlooking cardiovascular aspects.

105
00:07:15,880 --> 00:07:17,679
In this video, you learned that

106
00:07:17,679 --> 00:07:24,000
A search tool like Tavily can be configured and
invoked to enhance AI responses with external data.

107
00:07:24,000 --> 00:07:28,040
Prompt engineering and schema design guide
the LLM to produce structured reflections

108
00:07:28,040 --> 00:07:29,739
and focused answers.

109
00:07:29,739 --> 00:07:35,040
The answer question and reflection schema
capture answers, flag missing or irrelevant

110
00:07:35,040 --> 00:07:37,679
details, and generate queries.

111
00:07:37,679 --> 00:07:44,640
Tool outputs like tool_calls and schema fields
help extract structured insights from AI messages.

112
00:07:44,640 --> 00:07:49,640
LangGraph chains responder and revisor nodes
into an iterative feedback loop using prompt

113
00:07:49,640 --> 00:07:52,160
updates and evidence-based revisions.

114
00:07:52,160 --> 00:07:58,359
A message graph orchestrates the Reflexion agent,
managing node routing, iteration limits, and control flow.