├── .gitignore ├── env-sample ├── images └── user-input.jpg ├── Llama2ModelFile ├── crewai-create-llamafile.sh ├── llamafile-app.py ├── ollama-app.py ├── app-input.py ├── app-gradio.py ├── llamafile-multiagent-app.py ├── app.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | output.md 3 | *.log 4 | *.llamafile -------------------------------------------------------------------------------- /env-sample: -------------------------------------------------------------------------------- 1 | SERPER_API_KEY=[YOUR_API_KEY] 2 | OPENAI_API_KEY=[YOUR_API_KEY] -------------------------------------------------------------------------------- /images/user-input.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heaversm/crew-llamafile/HEAD/images/user-input.jpg -------------------------------------------------------------------------------- /Llama2ModelFile: -------------------------------------------------------------------------------- 1 | FROM llama2 2 | 3 | # Set parameters 4 | 5 | PARAMETER temperature 0.8 6 | PARAMETER stop Result 7 | 8 | # Sets a custom system message to specify the behavior of the chat assistant 9 | 10 | # Leaving it blank for now. 11 | 12 | SYSTEM """""" -------------------------------------------------------------------------------- /crewai-create-llamafile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/zsh 2 | 3 | # variables 4 | model_name="llama2" 5 | custom_model_name="crewai-llama2" 6 | 7 | #get the base model 8 | ollama pull $model_name 9 | 10 | #create the model file 11 | ollama create $custom_model_name -f ./Llama2ModelFile -------------------------------------------------------------------------------- /llamafile-app.py: -------------------------------------------------------------------------------- 1 | from crewai import Agent, Task, Crew 2 | from langchain_community.llms.llamafile import Llamafile 3 | import os 4 | from dotenv import load_dotenv 5 | load_dotenv() 6 | os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") 7 | 8 | llm = Llamafile() 9 | 10 | general_agent = Agent(role='Absurdist Poet', 11 | goal='Tell a fantastical story about llamas', 12 | backstory="""You are technically savvy poet. 13 | You write traditional poems as allegories for modern day topics in technology and artificial intelligence.""", 14 | verbose=True, 15 | allow_delegation=True) 16 | task = Task (description="""Write a limerick about llamas, weaving in the latest trends in AI""", 17 | agent = general_agent, 18 | expected_output="A limerick poem.") 19 | 20 | crew = Crew( 21 | agents=[general_agent], 22 | tasks=[task], 23 | verbose=2 24 | ) 25 | 26 | result = crew.kickoff() 27 | 28 | print(result) -------------------------------------------------------------------------------- /ollama-app.py: -------------------------------------------------------------------------------- 1 | from crewai import Agent, Task, Crew 2 | from langchain_openai import ChatOpenAI 3 | import os 4 | os.environ["OPENAI_API_KEY"] = "NA" 5 | 6 | llm = ChatOpenAI( 7 | model = "crewai-llama2", 8 | base_url = "http://localhost:11434/v1") 9 | 10 | general_agent = Agent(role = "Math Professor", 11 | goal = """Provide the solution to the students that are asking mathematical questions and give them the answer.""", 12 | backstory = """You are an excellent math professor that likes to solve math questions in a way that everyone can understand your solution""", 13 | allow_delegation = False, 14 | verbose = True, 15 | llm = llm) 16 | task = Task (description="""what is 3 + 5""", 17 | agent = general_agent, 18 | expected_output="A numerical answer.") 19 | 20 | crew = Crew( 21 | agents=[general_agent], 22 | tasks=[task], 23 | verbose=2 24 | ) 25 | 26 | result = crew.kickoff() 27 | 28 | print(result) -------------------------------------------------------------------------------- /app-input.py: -------------------------------------------------------------------------------- 1 | from crewai import Agent, Task, Crew 2 | from langchain_community.llms.llamafile import Llamafile 3 | import os 4 | from dotenv import load_dotenv 5 | 6 | # Load environment variables 7 | load_dotenv() 8 | os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") 9 | 10 | def get_user_input(): 11 | role = input("Agent Job Title: ") 12 | backstory = input("Agent Qualifications: ") 13 | task_description = input("Agent Task: ") 14 | goal = input("Goal of Task: ") 15 | expected_output = input("Expected Output Format: ") 16 | 17 | return role, goal, backstory, task_description, expected_output 18 | 19 | def main(): 20 | # Get user input 21 | role, goal, backstory, task_description, expected_output = get_user_input() 22 | 23 | # Initialize Llamafile 24 | llm = Llamafile() 25 | 26 | # Create agent with user-specified parameters 27 | general_agent = Agent( 28 | role=role, 29 | goal=goal, 30 | backstory=backstory, 31 | verbose=True, 32 | allow_delegation=True 33 | ) 34 | 35 | # Create task with user-specified parameters 36 | task = Task( 37 | description=task_description, 38 | agent=general_agent, 39 | expected_output=expected_output 40 | ) 41 | 42 | # Create crew 43 | crew = Crew( 44 | agents=[general_agent], 45 | tasks=[task], 46 | verbose=2 47 | ) 48 | 49 | # Execute crew kickoff 50 | result = crew.kickoff() 51 | 52 | print(result) 53 | 54 | if __name__ == "__main__": 55 | main() -------------------------------------------------------------------------------- /app-gradio.py: -------------------------------------------------------------------------------- 1 | import gradio as gr 2 | from crewai import Agent, Task, Crew 3 | from langchain_community.llms.llamafile import Llamafile 4 | import os 5 | from dotenv import load_dotenv 6 | 7 | # Load environment variables 8 | load_dotenv() 9 | os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") 10 | 11 | # Initialize Llamafile 12 | llm = Llamafile() 13 | 14 | def run_agent(role, goal, backstory, task_description, expected_output): 15 | # Create agent with user-specified parameters 16 | general_agent = Agent( 17 | role=role, 18 | goal=goal, 19 | backstory=backstory, 20 | verbose=True, 21 | allow_delegation=True 22 | ) 23 | 24 | # Create task with user-specified parameters 25 | task = Task( 26 | description=task_description, 27 | agent=general_agent, 28 | expected_output=expected_output 29 | ) 30 | 31 | # Create crew 32 | crew = Crew( 33 | agents=[general_agent], 34 | tasks=[task], 35 | verbose=2 36 | ) 37 | 38 | # Execute crew kickoff 39 | result = crew.kickoff() 40 | 41 | return result 42 | 43 | # Define Gradio interface 44 | with gr.Blocks() as demo: 45 | gr.Markdown("# Agent and Task Configuration") 46 | 47 | role = gr.Textbox(label="Role of the Agent", placeholder="Enter the role of your agent") 48 | goal = gr.Textbox(label="Goal of the Agent", placeholder="Enter the goal of your agent") 49 | backstory = gr.Textbox(label="Backstory of the Agent", placeholder="Enter the backstory of your agent") 50 | task_description = gr.Textbox(label="Task Description", placeholder="Enter the description of the task") 51 | expected_output = gr.Textbox(label="Expected Output", placeholder="Enter the expected output of the task") 52 | 53 | submit_button = gr.Button("Run Agent") 54 | 55 | result = gr.Textbox(label="Result") 56 | 57 | submit_button.click( 58 | fn=run_agent, 59 | inputs=[role, goal, backstory, task_description, expected_output], 60 | outputs=result 61 | ) 62 | 63 | # Launch the interface 64 | demo.launch() -------------------------------------------------------------------------------- /llamafile-multiagent-app.py: -------------------------------------------------------------------------------- 1 | from crewai import Agent, Task, Crew, Process 2 | from crewai_tools import SerperDevTool 3 | from langchain_community.llms.llamafile import Llamafile 4 | import os 5 | from dotenv import load_dotenv 6 | load_dotenv() 7 | os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") 8 | os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY") 9 | search_tool = SerperDevTool() 10 | llm = Llamafile() 11 | 12 | # Define your agents with roles and goals 13 | researcher = Agent( 14 | role='Senior Research Analyst', 15 | goal='Uncover cutting-edge developments in AI and data science using no more than 3 sources of information', 16 | backstory="""You work at a leading tech think tank. 17 | Your expertise lies in identifying emerging trends. 18 | You have a knack for dissecting complex data and presenting actionable insights.""", 19 | verbose=True, 20 | allow_delegation=False, 21 | tools=[search_tool] 22 | ) 23 | writer = Agent( 24 | role='Tech Content Strategist', 25 | goal='Craft compelling content on tech advancements', 26 | backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles. 27 | You transform complex concepts into compelling narratives.""", 28 | verbose=True, 29 | allow_delegation=True 30 | ) 31 | 32 | # Create tasks for your agents 33 | task1 = Task( 34 | description="""Conduct a comprehensive analysis of the latest advancements in AI in 2024 searching for no more than 3 sources of information. 35 | Identify key trends, breakthrough technologies, and potential industry impacts.""", 36 | expected_output="Full analysis report in bullet points", 37 | agent=researcher 38 | ) 39 | 40 | task2 = Task( 41 | description="""Using the insights provided, develop an engaging blog 42 | post that highlights the most significant AI advancements. 43 | Your post should be informative yet accessible, catering to a tech-savvy audience. 44 | Make it sound cool, avoid complex words so it doesn't sound like AI.""", 45 | expected_output="Full blog post of 5-10 paragraphs", 46 | agent=writer 47 | ) 48 | 49 | # Instantiate your crew with a sequential process 50 | crew = Crew( 51 | agents=[researcher, writer], 52 | tasks=[task1, task2], 53 | verbose=2, # You can set it to 1 or 2 to different logging levels 54 | ) 55 | 56 | # Get your crew to work! 57 | result = crew.kickoff() 58 | 59 | print(result) -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import os 2 | from crewai import Agent, Task, Crew, Process 3 | from crewai_tools import SerperDevTool 4 | from dotenv import load_dotenv 5 | load_dotenv() 6 | 7 | os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") 8 | os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY") 9 | os.environ['OPENAI_MODEL_NAME'] = 'gpt-4o' 10 | 11 | # You can choose to use a local model through Ollama for example. See https://docs.crewai.com/how-to/LLM-Connections/ for more information. 12 | 13 | # os.environ["OPENAI_API_BASE"] = 'http://localhost:11434/v1' 14 | # os.environ["OPENAI_MODEL_NAME"] ='openhermes' # Adjust based on available model 15 | # os.environ["OPENAI_API_KEY"] ='sk-111111111111111111111111111111111111111111111111' 16 | 17 | search_tool = SerperDevTool() 18 | 19 | # Define your agents with roles and goals 20 | researcher = Agent( 21 | role='Senior Research Analyst', 22 | goal='Uncover cutting-edge developments in AI and data science', 23 | backstory="""You work at a leading tech think tank. 24 | Your expertise lies in identifying emerging trends. 25 | You have a knack for dissecting complex data and presenting actionable insights.""", 26 | verbose=True, 27 | allow_delegation=False, 28 | tools=[search_tool] 29 | ) 30 | writer = Agent( 31 | role='Tech Content Strategist', 32 | goal='Craft compelling content on tech advancements', 33 | backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles. 34 | You transform complex concepts into compelling narratives.""", 35 | verbose=True, 36 | allow_delegation=True 37 | ) 38 | 39 | # Create tasks for your agents 40 | task1 = Task( 41 | description="""Conduct a comprehensive analysis of the latest advancements in AI in 2024. 42 | Identify key trends, breakthrough technologies, and potential industry impacts.""", 43 | expected_output="Full analysis report in bullet points", 44 | agent=researcher 45 | ) 46 | 47 | task2 = Task( 48 | description="""Using the insights provided, develop an engaging blog 49 | post that highlights the most significant AI advancements. 50 | Your post should be informative yet accessible, catering to a tech-savvy audience. 51 | Make it sound cool, avoid complex words so it doesn't sound like AI.""", 52 | expected_output="Full blog post of at least 4 paragraphs", 53 | agent=writer 54 | ) 55 | 56 | # Instantiate your crew with a sequential process 57 | crew = Crew( 58 | agents=[researcher, writer], 59 | tasks=[task1, task2], 60 | verbose=2, # You can set it to 1 or 2 to different logging levels 61 | ) 62 | 63 | # Get your crew to work! 64 | result = crew.kickoff() 65 | 66 | 67 | # Write result as a markdown file and download it 68 | # Define the filename 69 | filename = "output.md" 70 | 71 | # Open the file in write mode ('w') and save the result 72 | with open(filename, "w") as file: 73 | file.write(result) 74 | 75 | print("######################") 76 | print(result) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CREW AI Examples 2 | 3 | This is a set of scripts (and accompanying (Youtube Tutorial)[https://youtu.be/OUgb3hKSn9U]) that allow you to create AI agents to accomplish tasks such as conducting research and reporting on findings using the [CrewAI](https://www.crewai.com/) Agent Framework and nearly any large language model (through services like Langchain), including locally using [Mozilla's LLamafile](https://github.com/Mozilla-Ocho/llamafile) or Ollama. 4 | 5 | ## Basic Example 6 | 7 | ### Setup: 8 | 9 | * Get a [serper API key](https://serper.dev) 10 | * Get an [openAI API key](https://platform.openai.com/api-keys) 11 | * Save the `env-sample` file as `.env` and paste in your API keys 12 | 13 | ### Installation: 14 | 15 | In terminal, in root folder, type: 16 | 17 | * `pip install crewai` 18 | * `pip install 'crewai[tools]'` 19 | * `pip install dotenv` 20 | 21 | 22 | ### Configuration 23 | 24 | In `app.py`: 25 | * Set desired model name (`OPENAI_MODEL_NAME='gpt-4o'`) 26 | * Modify agents to do whatever task you'd like to see performed. 27 | 28 | ### Run Workflow 29 | 30 | * In terminal, type: `python app.py` 31 | 32 | ## Llamafile Example 33 | 34 | Mozilla's Llamafile allows you to run LLM models locally, and includes a langchain integration. To use it, in addition to the installation steps for CrewAI, do the following: 35 | 36 | * Get a [llamafile](https://github.com/Mozilla-Ocho/llamafile?tab=readme-ov-file) model. In this case, we'll get TinyLlama: 37 | 38 | `wget https://huggingface.co/jartine/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile` 39 | 40 | * Make sure you can execute the file by changing its permissions: 41 | 42 | `chmod +x TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile` 43 | 44 | * Run the model: 45 | 46 | `./TinyLlama-1.1B-Chat-v1.0.Q5_K_M.llamafile --server --nobrowser` 47 | 48 | This will open an API endpoint at localhost:8080. 49 | 50 | * If you haven't already, install the langchain community module: `pip install langchain_community` 51 | 52 | * Run the crew agent script: 53 | 54 | `python3 llamafile-app.py` 55 | 56 | ### User-prompt created agents with Llamafile 57 | 58 | The `app-input.py` file allows you to create a custom agent and research task via command line prompt: 59 | 60 | --- 61 | ![Providing terminal input to the model to create an agent](images/user-input.jpg) 62 | --- 63 | 64 | ### Llamafile Agent from UI input 65 | 66 | Uses gradio to allow users to specify agent parameters via web-based text fields 67 | 68 | * `pip install gradio` 69 | * `python3 app-gradio.py` 70 | * Open web browser at [http://127.0.0.1:7860/](http://127.0.0.1:7860/) 71 | 72 | 73 | 74 | ### Multi-agent llamafile 75 | 76 | For more complex tasks, you'll need a model with a larger context window. For this, I'll use Llama2: 77 | 78 | `wget https://huggingface.co/Mozilla/llava-v1.5-7b-llamafile/resolve/main/llava-v1.5-7b-q4.llamafile` 79 | * `chmod +x llava-v1.5-7b-q4.llamafile` 80 | * `./llava-v1.5-7b-q4.llamafile --server --nobrowser` 81 | 82 | Once this model is running, run the multiagent app (this will take a bit of time, as it needs to go through a series of steps to research and then write about AI): 83 | 84 | * `python3 llamafile-multiagent-app.py` 85 | 86 | ## Ollama Example 87 | 88 | In addition to the installation steps above, do the following: 89 | 90 | * [Download ollama](https://github.com/ollama/ollama) 91 | * In terminal, run `pip install langchain_openai` 92 | * Get the llama2 model: `ollama pull llama2` 93 | * In the terminal from project root, run `bash crewai-create-llamafile.sh` (llama2 is about 3.8 GB) 94 | * Once this is done, from terminal, run `python ollama-app.py` and view the output in your terminal 95 | --------------------------------------------------------------------------------