Exercises
This chapter built the
HelloAgentsframework and explained "why we need to build our own Agent framework." Please analyze:- Section 7.1.1 mentioned four main limitations of current mainstream frameworks. Combined with your actual experience using a framework in Chapter 6 exercises or actual projects, explain how these problems affect development efficiency.
HelloAgentsproposes the design philosophy of "everything is a tool," abstracting modules likeMemory,RAG, andMCPas tools. What are the advantages of this design? Are there any limitations? Please provide examples.- Comparing the agent code implemented from scratch in Chapter 4 with the framework implementation in this chapter, what specific improvements does the framework bring? If you were to design a framework, what design principles would you prioritize?
In Section 7.2, we extended
HelloAgentsLLMto support multiple model providers and local model invocation.<strong>Hint</strong>: This is a practical exercise, hands-on operation is recommended
- Referring to the example in Section 7.2.1, try adding support for a new model provider to
HelloAgentsLLM(such asGemini,Anthropic,Kim). Implement it through inheritance and enable automatic detection of that provider's environment variables. - Section 7.2.3 introduced three priorities of the automatic detection mechanism. Please analyze: If both
OPENAI_API_KEYandLLM_BASE_URL="http://localhost:11434/v1"are set, which provider will the framework ultimately choose? Is this priority design reasonable? - Besides
VLLMandOllamaintroduced in this chapter, there are other local model deployment solutions likeSGLang. Please first search for and understand the basic information and characteristics ofSGLang, then compareVLLM,SGLang, andOllamain terms of ease of use, resource consumption, inference speed, and inference accuracy.
- Referring to the example in Section 7.2.1, try adding support for a new model provider to
In Section 7.3, we implemented the
Messageclass,Configclass, andAgentbase class. Please analyze:- The
Messageclass usesPydantic'sBaseModelfor data validation. What are the advantages of this design in practical applications? - The
Agentbase class defines two methods:runand_execute, whererunis the public interface and_executeis an abstract method. What is this design pattern called? What are its benefits? - In the
Configclass, we used the singleton pattern. Please explain what the singleton pattern is, why configuration management needs to use the singleton pattern, and what problems would arise if the singleton pattern is not used.
- The
In Section 7.4, we implemented four
Agentparadigms in a framework manner.<strong>Hint</strong>: This is a practical exercise, hands-on operation is recommended
- Comparing the
ReActAgentimplemented from scratch in Chapter 4 with the framework-basedReActAgentin this chapter, list 3 specific improvements and explain how these improvements enhance code maintainability and extensibility. ReflectionAgentimplements an "execute-reflect-optimize" loop. Please extend this implementation by adding a "quality scoring" mechanism: After each reflection, have theLLMscore the current version's output, and only continue optimization if the score is below a threshold; otherwise, terminate early.- Please design and implement a new
Agentparadigm calledTree-of-Thought Agent, which should inherit from theAgentbase class and be able to generate multiple possible thinking paths at each step, then select the optimal path to continue.
- Comparing the
In Section 7.5, we built the tool system. Please consider the following questions:
- The
BaseToolclass defines anexecuteabstract method that all tools must implement. Please explain why all tools should be forced to implement a unified interface. If a tool needs to return multiple values (such as a search tool returning title, summary, and link), how should it be designed? - Section 7.5.3 implemented tool chains (
ToolChain). Please design a practical application scenario that requires chaining at least 3 tools and draw the execution flow diagram of the tool chain. - The asynchronous tool executor (
AsyncToolExecutor) uses a thread pool to execute tools in parallel. Please analyze: Under what circumstances can parallel tool execution bring performance improvements?
- The
Framework extensibility is one of the important considerations in design. You now need to extend the
HelloAgentsframework to implement some interesting new features and characteristics.- First, add a "streaming output" feature to
HelloAgentsso that theAgentcan return intermediate results in real-time when generating responses (similar to the typing effect in theChatGPTuser interface). Please design the implementation plan for this feature and explain which classes and methods need to be modified. - Then add a "multi-turn conversation management" feature to the framework that can automatically manage conversation history, support conversation branching and backtracking. How would you design this? What new classes are needed? How to integrate with the existing
Messagesystem? - Finally, please design a "plugin system" for
HelloAgentsthat allows third-party developers to extend framework functionality through plugins (such as adding newAgenttypes, new tool types, etc.) without modifying the framework's core code. Draw the architecture diagram of the plugin system and explain the key interfaces.
- First, add a "streaming output" feature to