Spaces:
Sleeping
Sleeping
File size: 2,414 Bytes
d17c60a f1dcea0 d17c60a 1476c30 d17c60a 1476c30 d17c60a febf236 5161994 d17c60a f1dcea0 d17c60a f1dcea0 d17c60a f1dcea0 d17c60a 1476c30 d17c60a f1dcea0 |
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 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from langchain_groq import ChatGroq
from crewai import Agent, Task, Crew
import os
# Initialize FastAPI app
app = FastAPI()
# Create a request model
class SearchQuery(BaseModel):
query: str
# Initialize LangChain with Groq
llm = ChatGroq(
temperature=0.7,
model_name="mixtral-8x7b-32768",
groq_api_key="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" # Replace with your actual Groq API key
)
# Define the classifier agent
classifier_agent = Agent(
role='Classifier',
goal='Understand the context of the user query and generate up to 5 suggestions.',
backstory='You are an AI that specializes in understanding user queries and providing relevant suggestions.',
llm=llm,
verbose=True
)
# Define the task for the classifier agent
classifier_task = Task(
description='Analyze the user query and generate up to 5 suggestions based on the context.',
agent=classifier_agent,
expected_output='A list of up to 5 suggestions related to the user query.'
)
# Define the main agent for processing the query
main_agent = Agent(
role='Main Agent',
goal='Provide a detailed response to the user query.',
backstory='You are an AI that specializes in providing detailed and accurate responses to user queries.',
llm=llm,
verbose=True
)
# Define the task for the main agent
main_task = Task(
description='Provide a detailed response to the user query.',
agent=main_agent,
expected_output='A detailed and accurate response to the user query.'
)
# Create the crew
crew = Crew(
agents=[classifier_agent, main_agent],
tasks=[classifier_task, main_task],
verbose=2
)
@app.post("/search")
async def process_search(search_query: SearchQuery):
try:
# Process the query using CrewAI
result = crew.kickoff(inputs={'query': search_query.query})
# Extract the response and suggestions from the result
response = result['outputs']['main_agent']
suggestions = result['outputs']['classifier_agent']
return {
"status": "success",
"response": response,
"suggestions": suggestions
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def root():
return {"message": "Search API is running"} |