参考文章: https://learnwithhasan.com/create-ai-agents-with-python/
code: https://github.com/hassancs91/AI-Agents-Course
这代码展示最简单的agent原理。我们可以稍作修改,就得到自己的调用多工具能够流水线一样工作的agent智能体。对于微型SaaS来说,够用了,不要太复杂。核心原理就是吴恩达的loop思考:
turn_count = 1 max_turns = 5 while turn_count < max_turns: print (f"Loop: {turn_count}") print("----------------------") turn_count += 1 agent_response = llm_instance.generate_response(messages=messages) messages.append({"role": "assistant", "content": agent_response}) print(agent_response) #extract action JSON From Text Response. action_json = extract_json_from_text(agent_response) if action_json: function_name = action_json[0]['function_name'] function_parms = action_json[0]['function_parms'] if function_name not in available_actions: raise Exception(f"Unknown action: {function_name}: {function_parms}") print(f" -- running {function_name} {function_parms}") action_function = available_actions[function_name] action_function(**function_parms) #call the function result = action_function(**function_parms) print("Observation:", result) function_result_messge = f"Observation: {result}" messages.append({"role": "user", "content": function_result_messge}) print("----------------------") else: break
参考: https://new.qq.com/rain/a/20240329A041XC00
介绍 phidata
https://github.com/phidatahq/phidata
它通过以下几个关键组件增强LLMs的能力:记忆:通过将聊天记录存储在数据库中,使 LLMs 能够进行长期对话。知识:通过将信息存储在向量数据库中,为LLMs 提供商业上下文。工具:使LLMs能够执行如从 API 提取数据、发送电子邮件或查询数据库等操作。代码示例以下是使用 phidata 创建一个简单助手的代码示例:
from phi.assistant import Assistant
from phi.tools.duckduckgo import DuckDuckGo
assistant = Assistant(tools=[DuckDuckGo()], show_tool_calls=True)
assistant.print_response("Whats happening in France?", markdown=True)