Skip to content

Commit 2748f90

Browse files
author
yashksaini-coder
committed
add ask.py for AI investment assistant chat functionality
1 parent 56c469c commit 2748f90

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ask.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import groq
2+
import os
3+
from dotenv import load_dotenv
4+
5+
load_dotenv(dotenv_path=".env")
6+
GROQ_API_KEY = os.getenv("api_key")
7+
8+
groq_client = groq.Client(api_key=GROQ_API_KEY)
9+
10+
def chat(query: str):
11+
"""
12+
API endpoint to handle user investment-related questions and return AI-generated insights.
13+
"""
14+
if not query:
15+
return {"error": "Query parameter is required"}
16+
17+
try:
18+
response = groq_client.chat.completions.create(
19+
model="llama-3.3-70b-versatile",
20+
messages=[{"role": "system", "content": "You are an AI investment assistant."},
21+
{"role": "user", "content": query}]
22+
)
23+
24+
answer = response.choices[0].message.content
25+
return {"question": query, "answer": answer}
26+
27+
except Exception as e:
28+
return {"error": str(e)}

0 commit comments

Comments
 (0)