.net网站内容管理系统,网站开发语言数据库有几种,wordpress小说主体,百度不收录的网站准备本案例使用deepseek#xff0c;登录deepseek官网#xff0c;登录账号#xff0c;充值几块钱#xff0c;然后创建Api key可以创建虚拟环境#xff0c;python版本最好是3.12#xff0c;以下是文件目录。test文件夹中#xff0c;放一些txt文件做测试#xff0c;main.p…准备本案例使用deepseek登录deepseek官网登录账号充值几块钱然后创建Api key 可以创建虚拟环境python版本最好是3.12以下是文件目录。test文件夹中放一些txt文件做测试main.py就是充当agenttools.py是自定义工具函数大模型就是通过调用这些工具帮你完成需求。 大致流程就是我写好工具创建一个agent一段程序在agent中注册好我创建的工具同时调用deepseek作为我的私人程序员。之后我向agent发出需求agent将需求传递给deepseekdeepseek分析出完成这个需求需要使用哪些工具返回给agentagent根据指示再去调用工具工具执行完将结果返回给agentagent再将结果返回给deepseek然后deepseek给出最终答案返回给agentagent返回给用户。工具 这里我们就指定了只对 test文件夹进行操作有 列出文件夹中所有文件名称、读取文件内容、重命名文件、创建文件的四个工具。
import ostest_path os.path.join(os.path.dirname(os.path.abspath(__file__)),test)print(test_path)
def list_files() - list[str]:列出 test 文件夹中所有文件名称返回列表相对路径file_list []for root, dirs, files in os.walk(test_path):for file in files:# 获取相对路径rel_path os.path.relpath(os.path.join(root, file), test_path)file_list.append(rel_path)return file_listprint(list_files())def read_file(name: str) - str:读取test文件夹下某一文件内容print(f(read_file {name}))try:with open(os.path.join(test_path,name), r) as f:content f.read()return contentexcept Exception as e:return fAn error occurred: {e}def rename_file(name: str, new_name: str) - str:重命名 test 文件夹下的文件print(f(rename_file {name} - {new_name}))try:old_path os.path.join(test_path, name)new_path os.path.join(test_path, new_name)# 检查 new_path 是否在 test_path 内if not os.path.abspath(new_path).startswith(os.path.abspath(test_path)):return Error: new_name is outside test_path.# 创建父目录os.makedirs(os.path.dirname(new_path), exist_okTrue)# 执行重命名os.rename(old_path, new_path)return fFile {name} successfully renamed to {new_name}.except Exception as e:return fAn error occurred: {e}def create_file(name: str, content: str ) - str:在 test 文件夹下创建文件并写入内容print(f(create_file {name}))try:file_path os.path.join(test_path, name)# 检查文件是否在 test_path 内if not os.path.abspath(file_path).startswith(os.path.abspath(test_path)):return Error: file path is outside test_path.# 创建父目录如果有子目录的话os.makedirs(os.path.dirname(file_path), exist_okTrue)# 写入内容with open(file_path, w, encodingutf-8) as f:f.write(content)return fFile {name} successfully created.except Exception as e:return fAn error occurred: {e}
agent
pip install pydantic-ai如果安装完成后出现 pydantic-core 不存在 的错误
pip install --force-reinstall --no-cache-dir --only-binary:all: pydantic-core -i https://pypi.org/simple创建一个.env文件里面写入DEEPSEEK_API_KEY你的deepseek api key 等号两边不要有空格不要有引号
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
import tools
from dotenv import load_dotenv
import os
load_dotenv()#读取env文件中的变量到环境变量中#使用deepseek模型
model OpenAIModel(deepseek-chat,providerOpenAIProvider(api_keyos.getenv(DEEPSEEK_API_KEY), # 从环境变量加载API密钥base_urlhttps://api.deepseek.com))
agent Agent(model,system_prompt你是我的windows系统管理大师 ,#设定大模型其角色tools[ #给agent注册工具tools.list_files,tools.rename_file,tools.read_file,tools.create_file,] )def main():history []while True:user_input input(Input: )resp agent.run_sync(user_input,message_historyhistory)#记住上下文history list(resp.all_messages())print(resp.output)if __name__ __main__:main()执行结果此时的agent就像一个你自己雇佣的程序员一样可以对test文件夹执行你指定工具的所有功能