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