""" This application provides an interface for generating text using the CrewAI framework for orchestrating agents, and models powered by Groq. The interface allows users to input a topic and receive a generated research article. """ import gradio as gr from crewai import Agent, Task, Crew, LLM, Process from crewai_tools import tool from langchain_community.tools import DuckDuckGoSearchRun # Use DuckDuckGo Search component @tool('DuckDuckGoSearch') def search(query: str) -> str: """ Perform a web search using DuckDuckGo. Args: query (str): The search query to be executed. Returns: str: The search result from DuckDuckGo. Raises: ValueError: If the query is an empty string. Exception: For any unexpected error during the search execution. """ if not query.strip(): raise ValueError("Query must not be empty.") try: result = DuckDuckGoSearchRun().run(query) except Exception as error: raise Exception(f"Search execution error: {error}") return result def generate_response(topic: str, api_key: str) -> str: """ Generates a research article based on the input topic using a Groq model. Args: topic (str): The text of the topic to be researched. api_key (str): The API key to authenticate the request to Groq. Returns: str: The generated research article or an error message. """ if not topic: return "Error: No topic provided. Please input a valid topic." try: # Configure the large language model LLM llm = LLM( model="groq/llama-3.1-8b-instant", temperature=0.2, top_p=0.5, max_tokens=500, presence_penalty=0.1, frequency_penalty=0.1, api_key=api_key ) except Exception as error: return f"Error configuring LLM: {str(error)}" # Create the 'Researcher' agent researcher = Agent( role="Researcher", goal="Conduct foundational research on {topic}.", backstory="A researcher who identifies trends and patterns.", llm=llm, max_iter=3, max_rpm=15, max_execution_time=30, verbose=True, allow_delegation=False ) # Create the 'Writer' agent writer = Agent( role="Writer", goal="Write a clear and concise research article on {topic}.", backstory="A writer who provides objective and impartial insights.", llm=llm, max_iter=3, max_rpm=15, max_execution_time=30, verbose=True, allow_delegation=False ) # Create the research task research = Task( description="Find current and relevant information on {topic}.", agent=researcher, expected_output="A bullet list outline for a concise research article.", tools=[search] ) # Create the writing task write = Task( description="Write a clear and concise research article on {topic}.", expected_output="An article with an introduction, main points, and a conclusion.", agent=writer, context=[research] ) # Define the crew of agents and tasks crew = Crew( agents=[researcher, writer], tasks=[research, write], process=Process.sequential, verbose=True ) try: # Inputs for the crew process inputs = {"topic": topic} # Execute the crew kickoff process crew_output = crew.kickoff(inputs=inputs) # Access the crew output article = crew_output.raw if crew_output else "Error: No output from crew." # Return the crew output return article except Exception as error: return f"Error during crew execution: {str(e)}" # Custom CSS for the Groq badge and color scheme custom_css = """ .gradio-container { background-color: #f5f5f5; } .gr-button-primary { background-color: #f55036 !important; border-color: #f55036 !important; } .gr-button-secondary { color: #f55036 !important; border-color: #f55036 !important; } #groq-badge { position: fixed; bottom: 20px; right: 20px; z-index: 1000; } """ # Define the Gradio interface with gr.Blocks(theme=gr.themes.Default()) as demo: gr.Markdown("# CrewAI + Groq Research Assistant") # Textbox to input the Groq API key api_key_input = gr.Textbox( type="password", label="Enter Groq API key", placeholder="Enter your API key...", lines=1 ) # Textbox to input the research topic with gr.Row(): topic_input = gr.Textbox( label="Enter Topic", placeholder="Type a topic here...", lines=1 ) # Markdown output to display the generated article with gr.Row(): article_output = gr.Markdown(label="Generated Research Article") # Submit button submit_button = gr.Button("Generate Article", variant="primary") # Add the Groq badge gr.HTML( """
POWERED BY GROQ
""" ) # Connect button click to the generate_response function submit_button.click( generate_response, inputs=[topic_input, api_key_input], outputs=[article_output] ) # Markdown instructions for using the app gr.Markdown( """ ## How to use this app: 1. Enter your [Groq API Key](https://console.groq.com/keys) in the provided field. 2. Enter a topic. 3. Click the "Generate Article" button to generate a research article. """ ) # Launch the Gradio interface demo.launch()