diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..3f16698 --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +OPENAI_API_KEY=your_openai_api_key_here +ELEVENLABS_API_KEY=your_elevenlabs_api_key_here +TWITTER_CONSUMER_KEY=your_twitter_consumer_key_here +TWITTER_CONSUMER_SECRET=your_twitter_consumer_secret_here +TWITTER_ACCESS_TOKEN=your_twitter_access_token_here +TWITTER_ACCESS_TOKEN_SECRET=your_twitter_access_token_secret_here +ZAPIER_NLA_API_KEY=your_zapier_api_key_here \ No newline at end of file diff --git a/README.md b/README.md index bcb805c..b9b0a70 100644 --- a/README.md +++ b/README.md @@ -25,28 +25,33 @@ You need to obtain API keys for the following services: 1. Twitter Developer Account (for Tweepy) 2. OpenAI API key 3. Zapier NLA API key +4. ElevenLabs API key -Replace the placeholders in the code with the respective API keys: +Copy the `.env.example` file to `.env` and fill in your API keys: -```python -consumer_key = "" -consumer_secret = "" -access_token = "" -access_token_secret = "" +```bash +OPENAI_API_KEY=your_openai_api_key_here +ELEVENLABS_API_KEY=your_elevenlabs_api_key_here +TWITTER_CONSUMER_KEY=your_twitter_consumer_key_here +TWITTER_CONSUMER_SECRET=your_twitter_consumer_secret_here +TWITTER_ACCESS_TOKEN=your_twitter_access_token_here +TWITTER_ACCESS_TOKEN_SECRET=your_twitter_access_token_secret_here +ZAPIER_NLA_API_KEY=your_zapier_api_key_here ``` -```python -set_api_key("<11LABS_API_KEY>") -openai.api_key = "" -``` +Replace `your_*_api_key_here` with your actual API keys. -```python -zapier = ZapierNLAWrapper(zapier_nla_api_key="") -``` +**Important**: +1. Never commit your API keys to version control. The `.env` file is already in `.gitignore` to prevent accidental commits. +2. If you see an error like "Did not find openai_api_key", it means your environment variables are not properly set. Make sure you've created the `.env` file and filled in all the required API keys. ## Running the program -1. Save the provided code in a file named `main.py`. +1. Copy the example environment file and fill in your API keys: +```bash +cp .env.example .env +# Edit .env with your actual API keys +``` 2. Open a terminal or command prompt and navigate to the folder containing `main.py`. 3. Run the program using the following command: diff --git a/main.py b/main.py index 65f0405..5a452d5 100644 --- a/main.py +++ b/main.py @@ -11,14 +11,23 @@ from elevenlabs import generate, play, set_api_key from langchain.agents import initialize_agent, load_tools from langchain.agents.agent_toolkits import ZapierToolkit -from langchain.llms import OpenAI +from langchain_openai import OpenAI from langchain.memory import ConversationBufferMemory from langchain.tools import BaseTool from langchain.utilities.zapier import ZapierNLAWrapper -set_api_key("<11LABS_API_KEY>") -openai.api_key = "" +from dotenv import load_dotenv +load_dotenv() + +elevenlabs_api_key = os.getenv('ELEVENLABS_API_KEY') +if not elevenlabs_api_key: + raise ValueError("Please set ELEVENLABS_API_KEY environment variable") +set_api_key(elevenlabs_api_key) + +openai.api_key = os.getenv('OPENAI_API_KEY') +if not openai.api_key: + raise ValueError("Please set OPENAI_API_KEY environment variable") # Set recording parameters duration = 5 # duration of each recording in seconds @@ -49,16 +58,19 @@ def play_generated_audio(text, voice="Bella", model="eleven_monolingual_v1"): play(audio) -# Replace with your API keys -consumer_key = "" -consumer_secret = "" -access_token = "" -access_token_secret = "" +# Get Twitter API credentials from environment variables +consumer_key = os.getenv('TWITTER_CONSUMER_KEY') +consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET') +access_token = os.getenv('TWITTER_ACCESS_TOKEN') +access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET') -client = tweepy.Client( - consumer_key=consumer_key, consumer_secret=consumer_secret, - access_token=access_token, access_token_secret=access_token_secret -) +if all([consumer_key, consumer_secret, access_token, access_token_secret]): + client = tweepy.Client( + consumer_key=consumer_key, consumer_secret=consumer_secret, + access_token=access_token, access_token_secret=access_token_secret + ) +else: + print("Warning: Twitter API credentials not fully configured. Twitter posting will not be available.") class TweeterPostTool(BaseTool): @@ -79,12 +91,21 @@ async def _arun(self, query: str) -> str: llm = OpenAI(temperature=0) memory = ConversationBufferMemory(memory_key="chat_history") - zapier = ZapierNLAWrapper(zapier_nla_api_key="") - toolkit = ZapierToolkit.from_zapier_nla_wrapper(zapier) - - # tools = [TweeterPostTool()] + toolkit.get_tools() + load_tools(["human"]) - - tools = toolkit.get_tools() + load_tools(["human"]) + zapier_api_key = os.getenv('ZAPIER_NLA_API_KEY') + if zapier_api_key: + zapier = ZapierNLAWrapper(zapier_nla_api_key=zapier_api_key) + toolkit = ZapierToolkit.from_zapier_nla_wrapper(zapier) + tools = toolkit.get_tools() + else: + print("Warning: Zapier NLA API key not configured. Zapier integration will not be available.") + tools = [] + + # Add Twitter tool if credentials are configured + if all([consumer_key, consumer_secret, access_token, access_token_secret]): + tools.append(TweeterPostTool()) + + # Always add human tools + tools.extend(load_tools(["human"])) agent = initialize_agent(tools, llm, memory=memory, agent="conversational-react-description", verbose=True) diff --git a/test.py b/test.py new file mode 100644 index 0000000..8b549a8 --- /dev/null +++ b/test.py @@ -0,0 +1,16 @@ +from dotenv import load_dotenv +from langchain_openai import OpenAI +import os + +load_dotenv() + +# Verify API key is loaded +api_key = os.getenv('OPENAI_API_KEY') +if not api_key: + raise ValueError("OPENAI_API_KEY not found in environment variables") +print(f"API key found: {api_key[:5]}...") + +# Test OpenAI integration +llm = OpenAI(temperature=0) +result = llm.invoke("Say hello!") +print(f"OpenAI response: {result}") \ No newline at end of file