File size: 3,010 Bytes
763e39c
e7028c7
 
763e39c
e7028c7
763e39c
e7028c7
 
 
763e39c
 
 
 
 
e7028c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
763e39c
 
e7028c7
 
 
 
763e39c
 
e7028c7
 
 
 
763e39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7028c7
763e39c
 
 
 
e7028c7
763e39c
 
e7028c7
 
 
 
763e39c
e7028c7
 
763e39c
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr
import os
from groq import Groq
from crewai import Agent, Task, Crew, Process
from langchain_community.tools import DuckDuckGoSearchRun

# Initialize the GROQ client
client = Groq(
    api_key=os.environ["GROQ_API_KEY"],
)

# Initialize the search tool
search_tool = DuckDuckGoSearchRun()

# Create a function to generate text using GROQ
def generate_text(prompt, max_tokens=500):
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        model="mixtral-8x7b-32768",
        max_tokens=max_tokens,
    )
    return chat_completion.choices[0].message.content

# Create agents using the GROQ text generation function
def create_agent(role, goal, backstory):
    return Agent(
        role=role,
        goal=goal,
        backstory=backstory,
        verbose=True,
        allow_delegation=False,
        llm_config={
            "function": generate_text,
            "config": {
                "max_tokens": 500
            }
        }
    )

researcher = create_agent(
    'Senior Researcher',
    'Conduct thorough research on given topics',
    'You are an experienced researcher with a keen eye for detail and the ability to find relevant information quickly.'
)

writer = create_agent(
    'Content Writer',
    'Create engaging and informative content based on research',
    'You are a skilled writer capable of turning complex information into easily understandable and engaging content.'
)

editor = create_agent(
    'Editor',
    'Refine and improve the written content',
    'You are a meticulous editor with a strong command of language and an eye for clarity and coherence.'
)

def create_crew(query):
    # Create tasks
    research_task = Task(
        description=f"Research the following topic thoroughly: {query}",
        agent=researcher
    )

    writing_task = Task(
        description="Write an informative article based on the research conducted",
        agent=writer
    )

    editing_task = Task(
        description="Review and refine the written article, ensuring clarity, coherence, and engagement",
        agent=editor
    )

    # Create the crew
    crew = Crew(
        agents=[researcher, writer, editor],
        tasks=[research_task, writing_task, editing_task],
        verbose=2,
        process=Process.sequential
    )

    return crew

def process_query(query, max_tokens=500):
    crew = create_crew(query)
    result = crew.kickoff()
    return result

# Create the Gradio interface
iface = gr.Interface(
    fn=process_query,
    inputs=[
        gr.Textbox(lines=5, label="Enter your query"),
        gr.Slider(minimum=100, maximum=1000, value=500, step=50, label="Max Tokens")
    ],
    outputs=gr.Textbox(lines=10, label="AI Agent Response"),
    title="CrewAI-powered Chatbot",
    description="Enter a query and get a researched, written, and edited response using CrewAI and GROQ API."
)

iface.launch()