Spaces:
Sleeping
Sleeping
from langchain_groq import ChatGroq | |
from crewai import Agent, Task, Crew | |
import os | |
# Initialize the LLM with proper configuration and error handling | |
try: | |
llm = ChatGroq( | |
temperature=0, | |
groq_api_key="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", | |
model_name="llama3-8b-8192" | |
) | |
except Exception as e: | |
print(f"Error initializing LLM: {e}") | |
raise | |
# Define the Teacher agent with appropriate attributes | |
try: | |
Teacher = Agent( | |
role="Teacher", | |
goal="Provide the best quality questions and answers to your students. Provide high-quality summaries to your students.", | |
backstory=( | |
"You are a dedicated high school teacher with over a decade of experience in the education field. " | |
"You hold a Master’s degree in Computer Science and have always had a passion for fostering curiosity and critical thinking in your students." | |
), | |
verbose=True, | |
allow_delegation=False, | |
llm=llm | |
) | |
except Exception as e: | |
print(f"Error initializing Teacher agent: {e}") | |
raise | |
# Define the Text_Generation task with clear instructions | |
try: | |
Text_Generation = Task( | |
description=( | |
"You will be given a paragraph as input for creating questions and a short summary. " | |
"Make sure to use everything you know to provide the best support possible. " | |
"You must strive to provide a complete and accurate response." | |
), | |
expected_output=( | |
"You should give a short summary and a set of questions with their answers in the form of multiple-choice answers from the input you receive. " | |
"Make sure to use everything you know to provide the best support possible. " | |
"You must strive to provide a complete and accurate response and maintain a helpful and friendly tone throughout." | |
), | |
llm=llm, | |
agent=Teacher, | |
) | |
except Exception as e: | |
print(f"Error initializing Text_Generation task: {e}") | |
raise | |
# Initialize the Crew with the defined agents and tasks | |
try: | |
crew = Crew( | |
agents=[Teacher], | |
tasks=[Text_Generation], | |
verbose=True, | |
) | |
except Exception as e: | |
print(f"Error initializing crew: {e}") | |
raise | |
# Define the predict function to handle inputs and invoke the crew | |
def predict(item): | |
try: | |
inputs = {'code': item['code']} | |
result = crew.kickoff(inputs=inputs) | |
return result | |
except Exception as e: | |
print(f"Error in predict function: {e}") | |
raise | |