mirror of
https://github.com/NousResearch/atropos.git
synced 2026-04-22 16:48:57 +00:00
Fix formatting issues in community environments - Applied black, isort, trailing whitespace, and end-of-file fixes - Remaining flake8 issues (unused imports, line length) noted for future cleanup
This commit is contained in:
parent
e85a170c34
commit
7f2e1a4f90
34 changed files with 1560 additions and 821 deletions
|
|
@ -1,55 +1,84 @@
|
|||
import os
|
||||
import logging
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from livekit.agents import mcp
|
||||
from livekit.agents.llm import ChatContext, ChatMessage, LLM # Removed ChatRole as using strings
|
||||
from livekit.agents.llm import ( # Removed ChatRole as using strings
|
||||
LLM,
|
||||
ChatContext,
|
||||
ChatMessage,
|
||||
)
|
||||
from livekit.plugins import openai
|
||||
|
||||
logger = logging.getLogger("text-perplexity-agent")
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logging.basicConfig(
|
||||
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
||||
)
|
||||
|
||||
load_dotenv(os.path.join(os.path.dirname(__file__), "..", "..", ".env"))
|
||||
|
||||
load_dotenv(os.path.join(os.path.dirname(__file__), '..', '..', '.env'))
|
||||
|
||||
# --- Configure Perplexity MCP Server (as a function to allow async context management) ---
|
||||
def get_perplexity_mcp_server():
|
||||
if os.environ.get("PERPLEXITY_API_KEY"):
|
||||
mcp_script_path = os.path.abspath(os.path.join(
|
||||
os.path.dirname(__file__), '..', 'tools', 'mcp', 'perplexity', 'perplexity-ask', 'dist', 'index.js'
|
||||
))
|
||||
mcp_script_path = os.path.abspath(
|
||||
os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
"..",
|
||||
"tools",
|
||||
"mcp",
|
||||
"perplexity",
|
||||
"perplexity-ask",
|
||||
"dist",
|
||||
"index.js",
|
||||
)
|
||||
)
|
||||
|
||||
if not os.path.exists(mcp_script_path):
|
||||
logger.error(f"❌ MCP script not found at {mcp_script_path}. Make sure you\'ve run \'npm install && npm run build\' in the server directory.")
|
||||
logger.error(
|
||||
f"❌ MCP script not found at {mcp_script_path}. Make sure you've run 'npm install && npm run build' in the server directory."
|
||||
)
|
||||
logger.warning("⚠️ Perplexity tools will be unavailable.")
|
||||
return None
|
||||
else:
|
||||
logger.info(f"📂 Configuring Perplexity MCP server with script: {mcp_script_path}")
|
||||
logger.info(
|
||||
f"📂 Configuring Perplexity MCP server with script: {mcp_script_path}"
|
||||
)
|
||||
return mcp.MCPServerStdio(
|
||||
name="PerplexityStdioServer",
|
||||
name="PerplexityStdioServer",
|
||||
params={
|
||||
"command": "node",
|
||||
"args": [mcp_script_path],
|
||||
"cwd": os.path.dirname(mcp_script_path),
|
||||
"env": {"PERPLEXITY_API_KEY": os.environ.get("PERPLEXITY_API_KEY") or ""},
|
||||
"client_session_timeout_seconds": 30
|
||||
"env": {
|
||||
"PERPLEXITY_API_KEY": os.environ.get("PERPLEXITY_API_KEY") or ""
|
||||
},
|
||||
"client_session_timeout_seconds": 30,
|
||||
},
|
||||
client_session_timeout_seconds=30
|
||||
client_session_timeout_seconds=30,
|
||||
)
|
||||
else:
|
||||
logger.warning("⚠️ PERPLEXITY_API_KEY not set. Perplexity tools will be unavailable.")
|
||||
logger.warning(
|
||||
"⚠️ PERPLEXITY_API_KEY not set. Perplexity tools will be unavailable."
|
||||
)
|
||||
return None
|
||||
|
||||
async def run_chat_loop(llm_instance: LLM, p_mcp_server: mcp.MCPServerStdio | None, initial_question: str = None):
|
||||
|
||||
async def run_chat_loop(
|
||||
llm_instance: LLM,
|
||||
p_mcp_server: mcp.MCPServerStdio | None,
|
||||
initial_question: str = None,
|
||||
):
|
||||
"""Runs a text-based chat loop with the LLM and Perplexity tool."""
|
||||
|
||||
|
||||
chat_context = ChatContext()
|
||||
|
||||
system_prompt = \
|
||||
"""
|
||||
You are a specialized assistant for answering general knowledge questions, providing explanations,
|
||||
|
||||
system_prompt = """
|
||||
You are a specialized assistant for answering general knowledge questions, providing explanations,
|
||||
and performing web searches using the 'perplexity_ask' tool.
|
||||
When the user asks for information, facts, or to 'search the web', you are the designated expert.
|
||||
When calling the 'perplexity_ask' tool, ensure the 'messages' argument is an array containing a single object
|
||||
When calling the 'perplexity_ask' tool, ensure the 'messages' argument is an array containing a single object
|
||||
with 'role': 'user' and 'content' set to the user's question.
|
||||
For example: {"messages": [{"role": "user", "content": "What is the capital of France?"}]}
|
||||
You do not have other tools. Do not try to delegate.
|
||||
|
|
@ -59,10 +88,10 @@ async def run_chat_loop(llm_instance: LLM, p_mcp_server: mcp.MCPServerStdio | No
|
|||
async def process_question(question: str):
|
||||
logger.info(f"You: {question}")
|
||||
chat_context.add_message(role="user", content=question)
|
||||
|
||||
|
||||
full_response = ""
|
||||
logger.info("Agent:")
|
||||
|
||||
|
||||
mcp_servers_to_use = []
|
||||
if p_mcp_server:
|
||||
# MCPServerStdio is managed by async with in main, so it should be running
|
||||
|
|
@ -73,7 +102,9 @@ async def run_chat_loop(llm_instance: LLM, p_mcp_server: mcp.MCPServerStdio | No
|
|||
logger.info(f"DEBUG: Type of chat_context: {type(chat_context)}")
|
||||
logger.info(f"DEBUG: Attributes of chat_context: {dir(chat_context)}")
|
||||
# Pass messages from ChatContext and the list of mcp_servers
|
||||
async for chunk in llm_instance.chat(messages=chat_context.messages, mcp_servers=mcp_servers_to_use):
|
||||
async for chunk in llm_instance.chat(
|
||||
messages=chat_context.messages, mcp_servers=mcp_servers_to_use
|
||||
):
|
||||
if chunk.delta.content:
|
||||
print(chunk.delta.content, end="", flush=True)
|
||||
full_response += chunk.delta.content
|
||||
|
|
@ -84,7 +115,7 @@ async def run_chat_loop(llm_instance: LLM, p_mcp_server: mcp.MCPServerStdio | No
|
|||
print(f"Sorry, I encountered an error: {e}")
|
||||
return
|
||||
|
||||
print()
|
||||
print()
|
||||
chat_context.add_message(role="assistant", content=full_response)
|
||||
|
||||
if initial_question:
|
||||
|
|
@ -102,46 +133,67 @@ async def run_chat_loop(llm_instance: LLM, p_mcp_server: mcp.MCPServerStdio | No
|
|||
except KeyboardInterrupt:
|
||||
logger.info("\nExiting chat due to interrupt.")
|
||||
break
|
||||
except EOFError:
|
||||
except EOFError:
|
||||
logger.info("\nExiting chat due to EOF.")
|
||||
break
|
||||
|
||||
|
||||
async def main():
|
||||
"""Main entrypoint for the text-based Perplexity agent."""
|
||||
logger.info("Starting Text-based Perplexity Agent...")
|
||||
|
||||
llm_instance = openai.LLM(model="gpt-4o")
|
||||
|
||||
|
||||
p_mcp_server_instance = get_perplexity_mcp_server()
|
||||
|
||||
test_question = "What is the capital of France?"
|
||||
|
||||
|
||||
if p_mcp_server_instance:
|
||||
try:
|
||||
# await p_mcp_server_instance.connect() # Connect to MCP server -> Removed
|
||||
logger.info("Perplexity MCP Server instance created. Will be used by LLM if needed.")
|
||||
await run_chat_loop(llm_instance, p_mcp_server_instance, initial_question=test_question)
|
||||
logger.info(
|
||||
"Perplexity MCP Server instance created. Will be used by LLM if needed."
|
||||
)
|
||||
await run_chat_loop(
|
||||
llm_instance, p_mcp_server_instance, initial_question=test_question
|
||||
)
|
||||
finally:
|
||||
logger.info("Closing Perplexity MCP server resources.") # Changed log message
|
||||
await p_mcp_server_instance.aclose() # Close MCP server connection
|
||||
logger.info(
|
||||
"Closing Perplexity MCP server resources."
|
||||
) # Changed log message
|
||||
await p_mcp_server_instance.aclose() # Close MCP server connection
|
||||
else:
|
||||
logger.warning("Running chat loop without Perplexity MCP server.")
|
||||
await run_chat_loop(llm_instance, None, initial_question=test_question)
|
||||
|
||||
|
||||
logger.info("Text-based Perplexity Agent finished.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if not os.environ.get("PERPLEXITY_API_KEY"):
|
||||
logger.error("🔴 PERPLEXITY_API_KEY is not set in the environment.")
|
||||
logger.error("🔴 Please set it in your .env file for the agent to function correctly with Perplexity.")
|
||||
|
||||
logger.error(
|
||||
"🔴 Please set it in your .env file for the agent to function correctly with Perplexity."
|
||||
)
|
||||
|
||||
if os.environ.get("PERPLEXITY_API_KEY"):
|
||||
mcp_script_path = os.path.abspath(os.path.join(
|
||||
os.path.dirname(__file__), '..', 'tools', 'mcp', 'perplexity', 'perplexity-ask', 'dist', 'index.js'
|
||||
))
|
||||
mcp_script_path = os.path.abspath(
|
||||
os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
"..",
|
||||
"tools",
|
||||
"mcp",
|
||||
"perplexity",
|
||||
"perplexity-ask",
|
||||
"dist",
|
||||
"index.js",
|
||||
)
|
||||
)
|
||||
if not os.path.exists(mcp_script_path):
|
||||
logger.error(f"❌ Critical: MCP script not found at {mcp_script_path}.")
|
||||
logger.error("❌ The agent cannot use Perplexity tools. Please build the MCP server ('npm install && npm run build' in its directory).")
|
||||
logger.error(
|
||||
"❌ The agent cannot use Perplexity tools. Please build the MCP server ('npm install && npm run build' in its directory)."
|
||||
)
|
||||
exit(1)
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
asyncio.run(main())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue