Skip to content

Commit f7c84ba

Browse files
author
yashksaini-coder
committed
feat: 🎸 refactor chat handling to use groq_chat function for improved clarity
1 parent eaa7ea8 commit f7c84ba

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

app.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
# Custom imports
1919
from topStocks import get_top_stocks
20+
from ask import groq_chat
2021
from agents import multi_ai
2122
from agno.agent import RunResponse
2223

@@ -53,7 +54,8 @@ async def lifespan(_: FastAPI):
5354
print(f"❌ Error while closing Redis: {e}")
5455

5556

56-
app = FastAPI()
57+
app = FastAPI(lifespan=lifespan) # Pass the lifespan context manager to FastAPI
58+
5759
app.add_middleware(
5860
CORSMiddleware,
5961
allow_origins=["*"],
@@ -115,18 +117,10 @@ def chat(query: str):
115117
"""
116118
API endpoint to handle user investment-related questions and return AI-generated insights.
117119
"""
118-
if not query:
119-
return {"error": "Query parameter is required"}
120120

121121
try:
122-
response = groq_client.chat.completions.create(
123-
model="llama-3.3-70b-versatile",
124-
messages=[{"role": "system", "content": "You are an AI investment assistant."},
125-
{"role": "user", "content": query}]
126-
)
127-
128-
answer = response.choices[0].message.content
129-
return {"question": query, "answer": answer}
122+
answer = groq_chat(query)
123+
return answer
130124

131125
except Exception as e:
132126
return {"error": str(e)}

ask.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
88
groq_client = groq.Client(api_key=GROQ_API_KEY)
99

10-
def chat(query: str):
11-
"""
12-
API endpoint to handle user investment-related questions and return AI-generated insights.
13-
"""
10+
def groq_chat(query: str):
1411
if not query:
1512
return {"error": "Query parameter is required"}
1613

1714
try:
1815
response = groq_client.chat.completions.create(
1916
model="llama-3.3-70b-versatile",
20-
messages=[{"role": "system", "content": "You are an AI investment assistant."},
17+
messages=[{"role": "system", "content": "You are an AI investment assistant. You are here to help users with investment-related questions."},
2118
{"role": "user", "content": query}]
2219
)
2320

0 commit comments

Comments
 (0)