rajrakeshdr commited on
Commit
f1dcea0
·
verified ·
1 Parent(s): aeb35b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -8
app.py CHANGED
@@ -3,6 +3,7 @@ from pydantic import BaseModel
3
  from langchain_groq import ChatGroq
4
  from langchain.chains import LLMChain
5
  from langchain.prompts import PromptTemplate
 
6
  import os
7
 
8
  # Initialize FastAPI app
@@ -19,25 +20,63 @@ llm = ChatGroq(
19
  groq_api_key="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" # Replace with your actual Groq API key
20
  )
21
 
22
- prompt_template = PromptTemplate(
23
- input_variables=["query"],
24
- template="Please provide a detailed response to the following query: {query}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
- chain = LLMChain(llm=llm, prompt=prompt_template)
27
 
28
  @app.post("/search")
29
  async def process_search(search_query: SearchQuery):
30
  try:
31
- # Process the query using LangChain
32
- response = chain.run(query=search_query.query)
 
 
 
 
33
 
34
  return {
35
  "status": "success",
36
- "response": response
 
37
  }
38
  except Exception as e:
39
  raise HTTPException(status_code=500, detail=str(e))
40
 
41
  @app.get("/")
42
  async def root():
43
- return {"message": "Search API is running"}
 
3
  from langchain_groq import ChatGroq
4
  from langchain.chains import LLMChain
5
  from langchain.prompts import PromptTemplate
6
+ from crewai import Agent, Task, Crew
7
  import os
8
 
9
  # Initialize FastAPI app
 
20
  groq_api_key="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" # Replace with your actual Groq API key
21
  )
22
 
23
+ # Define the classifier agent
24
+ classifier_agent = Agent(
25
+ role='Classifier',
26
+ goal='Understand the context of the user query and generate up to 5 suggestions.',
27
+ backstory='You are an AI that specializes in understanding user queries and providing relevant suggestions.',
28
+ llm=llm,
29
+ verbose=True
30
+ )
31
+
32
+ # Define the task for the classifier agent
33
+ classifier_task = Task(
34
+ description='Analyze the user query and generate up to 5 suggestions based on the context.',
35
+ agent=classifier_agent,
36
+ expected_output='A list of up to 5 suggestions related to the user query.'
37
+ )
38
+
39
+ # Define the main agent for processing the query
40
+ main_agent = Agent(
41
+ role='Main Agent',
42
+ goal='Provide a detailed response to the user query.',
43
+ backstory='You are an AI that specializes in providing detailed and accurate responses to user queries.',
44
+ llm=llm,
45
+ verbose=True
46
+ )
47
+
48
+ # Define the task for the main agent
49
+ main_task = Task(
50
+ description='Provide a detailed response to the user query.',
51
+ agent=main_agent,
52
+ expected_output='A detailed and accurate response to the user query.'
53
+ )
54
+
55
+ # Create the crew
56
+ crew = Crew(
57
+ agents=[classifier_agent, main_agent],
58
+ tasks=[classifier_task, main_task],
59
+ verbose=2
60
  )
 
61
 
62
  @app.post("/search")
63
  async def process_search(search_query: SearchQuery):
64
  try:
65
+ # Process the query using CrewAI
66
+ result = crew.kickoff(inputs={'query': search_query.query})
67
+
68
+ # Extract the response and suggestions from the result
69
+ response = result['outputs']['main_agent']
70
+ suggestions = result['outputs']['classifier_agent']
71
 
72
  return {
73
  "status": "success",
74
+ "response": response,
75
+ "suggestions": suggestions
76
  }
77
  except Exception as e:
78
  raise HTTPException(status_code=500, detail=str(e))
79
 
80
  @app.get("/")
81
  async def root():
82
+ return {"message": "Search API is running"}