1 line
3.5 KiB
JSON
1 line
3.5 KiB
JSON
{"repo": "bwang106/aria", "files": {"mem.py": {"2a465bd1fc522f8861ef8a892174b6d111ef1d41": {"t": 1785674529.1476219, "n": 3042, "h": "31f7ad3a73344113", "c": "from config import API_KEY\nimport asyncio\nfrom localStoragePy import localStoragePy\nfrom mistralai import Mistral\nfrom aiohttp import ClientSession\n\n# 初始化 Mistral 客户端\nclient = Mistral(api_key=API_KEY)\nmodel = \"open-mistral-nemo\"\n\n# 初始化本地存储\nlocal_storage = localStoragePy('example.mistral_app', 'json')\n\n## 从本地存储获取或设置 API 密钥\n#api_key = local_storage.getItem('API_KEY')\n#if not api_key:\n# api_key = API_KEY\n# local_storage.setItem('API_KEY', api_key)\n\n# 保存物品信息到本地存储\n#def save_item(what, where, status, timestamp):\n# item = {\n# \"what\": what,\n# \"where\": where,\n# \"status\": status,\n# \"timestamp\": timestamp\n# }\n# local_storage.setItem(what, item) # 使用物品名称作为键\n#\n#def get_item(what):\n# return local_storage.getItem(what)\n\n# 代理 ID 字典\nagents = {\n 'memory': \"ag:13e82541:20241025:untitlerecord-model-agent-promptd-agent:ccc270fb\",\n 'expert': \"ag:13e82541:20241025:expert-agent:338569a8\",\n 'analysis': \"ag:13e82541:20241025:analysis-model-agent-prompt:c1d46050\",\n 'workflow': \"ag:13e82541:20241025:master-workflow:7bcaf579\"\n}\n\n# 异步函数,用于根据任务类型选择合适的代理\nasync def select_agent(task_type):\n return agents.get(task_type, \"default_agent_id\")\n\n# 异步函数,用于执行任务并获取响应\nasync def perform_task(task_type, question):\n agent_id = await select_agent(task_type)\n\n# item = get_item(what)\n# if item:\n# print(f\"current status of {what}: {item['status']}\")\n# if item['status'] == 'available':\n# save_item(what, where, 'unavailable', timestamp)\n# return f\"{what} is now unavailable.\"\n# else:\n# return f\"{what} is already unavailable.\"\n \n async with ClientSession() as session:\n url = \"https://api.mistral.ai/v1/agents/completions\"\n headers = {'Authorization' : f'Bearer {API_KEY}'}#添加请求头\n params = {\n #\"model\": model, #这里不需要设置model \"msg\":\"Assertion failed, model can't be set when using an agent\"\n \"agent_id\": agent_id,\n \"messages\": [{\"role\": \"user\", \"content\": question}]\n }\n async with session.post(url, json=params, headers=headers) as response:\n if response.status == 200:\n data = await response.json()\n if 'choices' in data:\n # 保存物品信息到本地存储\n# save_item(what, where, 'unavailable', timestamp)\n return data['choices'][0]['message']['content']\n else:\n return \"No choices available in response.\"\n else:\n error_data = await response.text()#获取错误信息\n print(f\"Failed to fetch data: {response.status}{error_data}:\")#打印错误信息\n return f\"Failed to fetch data: {response.status}\"\n\n# 示例用法,定义主异步函数\nasync def main():\n task_type = 'workflow'\n question = \"I have put the drilling machine in the storage room on the desk1 after use. remember it.\"\n response_content = await perform_task(task_type, question)\n print(response_content)\n\n\n\n# 运行主函数\nif __name__ == \"__main__\":\n asyncio.run(main())"}}}} |