typesdigital commited on
Commit
e7028c7
1 Parent(s): 70bcbb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -35
app.py CHANGED
@@ -1,45 +1,63 @@
1
- import os
2
  import gradio as gr
 
 
3
  from crewai import Agent, Task, Crew, Process
4
- from langchain_groq import ChatGroq
5
- from langchain.tools import DuckDuckGoSearchRun
6
 
7
- # Initialize the GROQ language model
8
- groq_llm = ChatGroq(
9
- groq_api_key=os.environ["GROQ_API_KEY"],
10
- model_name="mixtral-8x7b-32768"
11
  )
12
 
13
  # Initialize the search tool
14
  search_tool = DuckDuckGoSearchRun()
15
 
16
- # Create agents
17
- researcher = Agent(
18
- role='Senior Researcher',
19
- goal='Conduct thorough research on given topics',
20
- backstory='You are an experienced researcher with a keen eye for detail and the ability to find relevant information quickly.',
21
- verbose=True,
22
- allow_delegation=False,
23
- llm=groq_llm,
24
- tools=[search_tool]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
 
27
- writer = Agent(
28
- role='Content Writer',
29
- goal='Create engaging and informative content based on research',
30
- backstory='You are a skilled writer capable of turning complex information into easily understandable and engaging content.',
31
- verbose=True,
32
- allow_delegation=False,
33
- llm=groq_llm
34
  )
35
 
36
- editor = Agent(
37
- role='Editor',
38
- goal='Refine and improve the written content',
39
- backstory='You are a meticulous editor with a strong command of language and an eye for clarity and coherence.',
40
- verbose=True,
41
- allow_delegation=False,
42
- llm=groq_llm
43
  )
44
 
45
  def create_crew(query):
@@ -69,18 +87,21 @@ def create_crew(query):
69
 
70
  return crew
71
 
72
- def process_query(query):
73
  crew = create_crew(query)
74
  result = crew.kickoff()
75
  return result
76
 
77
- # Create Gradio interface
78
  iface = gr.Interface(
79
  fn=process_query,
80
- inputs=gr.Textbox(lines=2, placeholder="Enter your query here..."),
 
 
 
81
  outputs=gr.Textbox(lines=10, label="AI Agent Response"),
82
- title="AI Agent Chatbot",
83
- description="Ask a question or provide a topic, and our AI agents will research, write, and edit a response for you."
84
  )
85
 
86
  iface.launch()
 
 
1
  import gradio as gr
2
+ import os
3
+ from groq import Groq
4
  from crewai import Agent, Task, Crew, Process
5
+ from langchain_community.tools import DuckDuckGoSearchRun
 
6
 
7
+ # Initialize the GROQ client
8
+ client = Groq(
9
+ api_key=os.environ["GROQ_API_KEY"],
 
10
  )
11
 
12
  # Initialize the search tool
13
  search_tool = DuckDuckGoSearchRun()
14
 
15
+ # Create a function to generate text using GROQ
16
+ def generate_text(prompt, max_tokens=500):
17
+ chat_completion = client.chat.completions.create(
18
+ messages=[
19
+ {
20
+ "role": "user",
21
+ "content": prompt,
22
+ }
23
+ ],
24
+ model="mixtral-8x7b-32768",
25
+ max_tokens=max_tokens,
26
+ )
27
+ return chat_completion.choices[0].message.content
28
+
29
+ # Create agents using the GROQ text generation function
30
+ def create_agent(role, goal, backstory):
31
+ return Agent(
32
+ role=role,
33
+ goal=goal,
34
+ backstory=backstory,
35
+ verbose=True,
36
+ allow_delegation=False,
37
+ llm_config={
38
+ "function": generate_text,
39
+ "config": {
40
+ "max_tokens": 500
41
+ }
42
+ }
43
+ )
44
+
45
+ researcher = create_agent(
46
+ 'Senior Researcher',
47
+ 'Conduct thorough research on given topics',
48
+ 'You are an experienced researcher with a keen eye for detail and the ability to find relevant information quickly.'
49
  )
50
 
51
+ writer = create_agent(
52
+ 'Content Writer',
53
+ 'Create engaging and informative content based on research',
54
+ 'You are a skilled writer capable of turning complex information into easily understandable and engaging content.'
 
 
 
55
  )
56
 
57
+ editor = create_agent(
58
+ 'Editor',
59
+ 'Refine and improve the written content',
60
+ 'You are a meticulous editor with a strong command of language and an eye for clarity and coherence.'
 
 
 
61
  )
62
 
63
  def create_crew(query):
 
87
 
88
  return crew
89
 
90
+ def process_query(query, max_tokens=500):
91
  crew = create_crew(query)
92
  result = crew.kickoff()
93
  return result
94
 
95
+ # Create the Gradio interface
96
  iface = gr.Interface(
97
  fn=process_query,
98
+ inputs=[
99
+ gr.Textbox(lines=5, label="Enter your query"),
100
+ gr.Slider(minimum=100, maximum=1000, value=500, step=50, label="Max Tokens")
101
+ ],
102
  outputs=gr.Textbox(lines=10, label="AI Agent Response"),
103
+ title="CrewAI-powered Chatbot",
104
+ description="Enter a query and get a researched, written, and edited response using CrewAI and GROQ API."
105
  )
106
 
107
  iface.launch()