From b3c189a9fa64eaffa1cd0d2815bd8263e71c926c Mon Sep 17 00:00:00 2001 From: marko-kraemer Date: Sat, 23 Nov 2024 00:33:24 -0500 Subject: [PATCH] llm keys helper function in example agent && llm non-required api keys --- agentpress/agents/simple_web_dev/agent.py | 14 ++++++++++++++ agentpress/llm.py | 17 ++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/agentpress/agents/simple_web_dev/agent.py b/agentpress/agents/simple_web_dev/agent.py index f3f2634c..606923a4 100644 --- a/agentpress/agents/simple_web_dev/agent.py +++ b/agentpress/agents/simple_web_dev/agent.py @@ -8,6 +8,7 @@ This agent can: - Use either XML or Standard tool calling patterns """ +import os import asyncio import json from agentpress.thread_manager import ThreadManager @@ -88,6 +89,17 @@ file contents here """ +def get_anthropic_api_key(): + """Get Anthropic API key from environment or prompt user.""" + api_key = os.getenv("ANTHROPIC_API_KEY") + if not api_key: + api_key = input("\nšŸ”‘ Please enter your Anthropic API key: ").strip() + if not api_key: + print("āŒ No API key provided. Please set ANTHROPIC_API_KEY environment variable or enter a key.") + sys.exit(1) + os.environ["ANTHROPIC_API_KEY"] = api_key + return api_key + async def run_agent(thread_id: str, use_xml: bool = True, max_iterations: int = 5): """Run the development agent with specified configuration.""" thread_manager = ThreadManager() @@ -181,6 +193,8 @@ def main(): """Main entry point with synchronous setup.""" print("\nšŸš€ Welcome to AgentPress Web Developer Example!") + get_anthropic_api_key() + project_description = input("What would you like to build? (default: Create a modern, responsive landing page)\n> ") if not project_description.strip(): project_description = "Create a modern, responsive landing page" diff --git a/agentpress/llm.py b/agentpress/llm.py index ab42c2b4..0ad44322 100644 --- a/agentpress/llm.py +++ b/agentpress/llm.py @@ -7,14 +7,17 @@ from openai import OpenAIError import asyncio import logging -OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') -ANTHROPIC_API_KEY = os.environ.get('ANTHROPIC_API_KEY') -GROQ_API_KEY = os.environ.get('GROQ_API_KEY') -AGENTOPS_API_KEY = os.environ.get('AGENTOPS_API_KEY') +OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', None) +ANTHROPIC_API_KEY = os.environ.get('ANTHROPIC_API_KEY', None) +GROQ_API_KEY = os.environ.get('GROQ_API_KEY', None) +AGENTOPS_API_KEY = os.environ.get('AGENTOPS_API_KEY', None) -os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY -os.environ['ANTHROPIC_API_KEY'] = ANTHROPIC_API_KEY -os.environ['GROQ_API_KEY'] = GROQ_API_KEY +if OPENAI_API_KEY: + os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY +if ANTHROPIC_API_KEY: + os.environ['ANTHROPIC_API_KEY'] = ANTHROPIC_API_KEY +if GROQ_API_KEY: + os.environ['GROQ_API_KEY'] = GROQ_API_KEY async def make_llm_api_call( messages: list,