Your subscription plan will change at the end of your current billing period. You’ll continue to have access to your current plan until then.
Welcome back!
Hi ,
We'd like to know you better so we can create more relevant courses. What do you do for work?
You've achieved today's streak!
Complete one lesson every day to keep the streak going.
Su
Mo
Tu
We
Th
Fr
Sa
You earned a Free Pass!
Free Passes help protect your daily streak. Complete more lessons to earn up to 3 Free Passes.
Elevate Your Career with Full Learning Experience
Unlock Plus AI learning and gain exclusive insights from industry leaders
Access exclusive features like graded notebooks and quizzes
Earn unlimited certificates to enhance your resume
Starting at $1 USD/mo after a free trial – cancel anytime
You'll now set up the second agent for your system, the file suggestion agent. This agent will help you choose the relevant files from your structured data to build the domain graph. Let's go. In the workflow that you're working through, you're building the Structured Data Agent workflow. In Lesson 4, you created the User Intent Agent. Here in Lesson 5, you will create the File Suggestion Agent. It will have a similar pattern to what we did with the User Intent Agent in Lesson 4. The agent itself has one primary goal, creating this approved files list. This will be the list of files that the agent itself has looked at, decided these look relevant for what the user's trying to do, and then ask the user, hey, do you approve these? And if so, the output will be the approved_files. To do that, the File Suggestion Agent has a bunch of tools to actually take a look at the files that are available. So that's the list_import_files. For any of those files, it can sample_file. That's like doing a little bit of a read from the file. It can only read about 100 lines. And then based on that, it can make a recommendation based using this tool called the set_suggested_files tool. And then if the user approves, it'll call the approved suggested files tool, and that will update the memory so that we have the approved files in context rather than just the suggested files. All of this of course informed by the work of the previous agent. All this is based on the overall direction of what the user is trying to accomplish. This agent gets that from memory rather than from conversational history. So rather than relying on the transcript of what's happened before this tool was called or before this agent was called. This agent's going to use the get_approved_user_goal tool to find out what it is that the user's trying to accomplish. Then with that, take a look at the files, figure out which ones are relevant and make a suggestion to the user. Let's take a look at the code. You'll start with the usual setup, importing the libraries that we need and any helper functions that we defined previously. With the libraries all ready, we'll go ahead and create a connection to OpenAI and make sure that it's working. Oh, it's very consistent today. Yes, I'm ready. How can I assist you? You'll start by constructing the instructions for the agent itself. Now we're going to do this a little bit at a time. First you want to define the agent's role and also its goal. So for this agent that is in charge of making suggestions for files that are relevant, we're going to say that it's a constructive critic reviewing a list of files. And its goal is to suggest relevant files that can be used for constructing a knowledge graph. You can then give it some hints about how to go about achieving that task. The particular task of course is to review the list of files and see from within those files, which ones are relevant for the kind of graph and the description that is based on the approved user goal. For each of those files, there's a tool available called sample file that will let the agent actually see the content of that file to confirm whether the file contains information that's useful for achieving the goal. And we're giving it also the hint that it should only take a look at structured data files like CSV or JSON. You can close that out with the chain of thought directions. And here the chain of thought really has two pieces. There's going to be a preparation part of it, which is go ahead and call the get_approved_user_goal to get what the user goal is from session memory. We're going to call the tool for that rather than passing that in directly to the prompt. So encourage the agent to use the tool. And then with that in hand, we're going to give it the chain of thought. As always, think carefully, repeating these steps until you're finished. And here are the steps we want the agent to follow. Get a list of all the files that are available. Then for that list of files, evaluate each one of them for their relevance, and then use the set_suggested_files tool to record the suggested files into memory. This step here, step 3, encouraging the agent to use the get_suggested_files tool to get the list of suggested files is really quite important. It's possible for the agent to realize within its own reasoning and within conversational history what the list of files might be and what it thinks the suggested files are. rather than relying on that, we really want to encourage it to only use the files that have been saved to the suggested files memory. Here the get_suggested_files tool is what accesses that memory, completing that loop actually encourages the LLM to really focus on what is in memory, not what it remembers from conversational history. With that then, the result that you got back from the tool call is what's going to be presented to the user for approval. If the user approves, then go ahead and say that's great. User approved suggested files. But if the user has feedback, go back to step 1 and repeat the process with the feedback in mind. You can now define the tools that are used by the agent. The first tool is get_approved_user_goal. This function is defined in the tools module that is provided to you. It just returns the approved user goal that has been stored in the state. We're also going to import a load helper function here that these tools that we're about to define will use. Each of these tools interact with the file system. But it's not the entire file system, they're constrained to just looking at the files that are available for import that Neo4j itself can access. So Neo4j has the notion of an import directory, so this helper is going to let us know where is that import directory, all the tools then can use that import directory for deciding what files are available, and of those files, which ones to use. You can then define the tool to list the import files, and it's going to be saved to this key ALL_AVAILABLE_FILES. The tool call needs the tool_context because there are some helper utilities we're going to use for actually getting the directory on disk where these files are stored, and so that the files that are listed are only relative paths rather than absolute paths. So you can see down here in the implementation, we're going to get this helper function, get_neo4j_import_dir. that's the import directory that Neo4J itself has access to. Neo4J does not have access to the full disk space, so we can't use absolute files. It has to be relative to this import directory. So we get here. And then we list all of the files. This is just some string using the import directory. We then go ahead and do a glob listing of all the files recursively down from that directory and do a bit of list comprehension here to turn that into a relative list of files. We save that to a current state as ALL_AVAILABLE_FILES and then return that as the result of this tool call. You can then define the sample_file tool call. Now, the sample_file tool is again going to use the tool_context, and it's also going to use a file_path. This is going to be a relative path to the file that the LLM has decided it wants to take a look at. This tool could be instead a call out to the operating system to actually use cat or something. Here we're going to do this all in Python. The result of calling this tool is going to be just up to 100 lines of text. We're going to assume it's a text file. Get 100 lines of text, return that as the value of this tool call. Now we have a couple of guards in place here because the LLM can invent paths, it can invent lots of different things. When it calls this, we're going to do some sanity checking on the file_path that was passed in. We're going to make sure that it's not absolute, so that if it is absolute, we're going to return an error because it must be only relative files and we expect them to be relative to the import directory. And the last part here, we also encourage the agent to remember that the file path being passed in here should appear in the list of available files. So if it's not sure, could actually check that. And based on the import directory, we go ahead and get the full path to that file. We make sure that the file exists. Again, return a helpful error message if it doesn't exist so that the agent can take corrective measures. And if it is existing, then we go ahead and read 100 lines and save that into the content, passing that back as a successful tool call. If any errors occur here, we're going to pass that error back as a response to the LLM as well. And the agent can decide whether or not it's capable of doing anything or it just needs to complain to the user that something is just wrong. You've given the agent the ability to list all the files, take a look at those files. So now it's able to actually make a suggestion based on those files and the file content about what it thinks are relevant files for what the user is trying to get done. Here we've got a pair of tools that actually does that. The first is to set the list of suggested files. It takes a list of strings that should be file paths. And you can add some extra checking here if you wanted to to make sure that the list passed in here are all actually existing files. Here we're actually going to be a bit trustful, a bit of YOLO in this tool here. Of course, for an improvement, you can add that check. And then the flip side, of course, we're going to have a get_suggested_files that just looks into memory for that list of files that have been suggested. Once the agent has successfully figured out what files they think are relevant for the user's goal and made that suggestion to the user. If the user has approved, of course, it will call the approve_suggested_files tool to make that transfer from the state of suggested files into approved files. As in the previous lesson, we're going to double check that before this call is made that the SUGGESTED_FILES actually exist in state, and if not, suggest to the agent that it needs to go and figure that out before it calls this tool. If it has been set, it will go ahead and make a copy of the SUGGESTED_FILES into the APPROVED_FILES and return that as the success from this tool call. You can then go ahead and take all of those tool definitions, put those into a list, and save that as the file_suggestion_agent_tools. We'll use that for the agent definition in just a moment. You can now define your agent. The agent definition is pretty straightforward. We did all the hard work in setting up the tools and defining the prompt earlier. We're just going to pass those in here and create the agent. With the agent in hand, you can now go ahead and interact with the agent. Import the make_agent_caller helper that we defined earlier, and then create a caller using that. Alright, just really just one thing that's a little bit different here. Previously when we used the make_agent_caller, we would just call it directly with the agent we want to execute. Notice here that we're initializing the state with the approved_user_goal. Now, as we had said at the beginning of the workflow, the approved_user_goal is just the overall direction for all the different agents and what they're trying to accomplish. So it's important to set that here. In the final production system, in the multi-agent system by the time we get to this part of the workflow, the state will already have the user goal because we'd gone through the previous part of the workflow. Here, we haven't gone through that part of the workflow, so we simulate it by setting the initial state as if we had gone through and approved a user goal that wants to do a supply chain analysis, with a description about doing a multi-level bill of materials for manufactured products. You can then go ahead and run a very short conversation where the user asks what files can we use for import. And then the file suggestion agent will go ahead and do all of its processing to find out which files are available and which ones are relevant. We should see in the end that the available files list contains all files in the directory and the suggested files are a subset of that, hopefully just the CSV files. Now this may take a few moments to work because the LLM is of course looking through all the files that it finds, trying to understand whether or not those are relevant. We can see in this response that it's done a pretty good job. It's figured out these CSV files for the assemblies, some components, a mapping from parts to suppliers, some products themselves, the suppliers themselves, and also the parts. That's perfect. If you look at the available files, it's got a lot of extra ones, particularly all these markdown files. It has ignored those and correctly just suggested these assembly files, the component files, all the CSV files. Awesome. Since the agent has made those excellent suggestions, you can go ahead and send another message to the agent to say, Yes, let's do it! You can rephrase this perhaps, depending on how the agent responds, that may not be a good way for approving. Let's try this out. The agent was very happy about that and should have made a tool call so that the approved files here we can see from the session state that the approved files match what we had earlier as the suggested files. Success. You've now built two agents within the workflow for constructing a knowledge graph out of structured data. The first that would establish the user's intent, the next one building on the user's intent and looking for data files that are available that support that user's intent. In the next lesson you'll take the next step which is given the user's intent, the files that are available, what could a graph actually look like?