-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat.py
More file actions
67 lines (51 loc) · 2.02 KB
/
chat.py
File metadata and controls
67 lines (51 loc) · 2.02 KB
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
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import PromptTemplate
from langchain.chains import RetrievalQA
import os
from dotenv import load_dotenv
from vectorize import vectorStore
import streamlit as st
load_dotenv()
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
google_api_key=GEMINI_API_KEY,
temperature=0.3
)
prompt_template = PromptTemplate(
input_variables=["context", "question"],
template="""
Think yourself as a virtual farmer and an agricultural expert who predict the favourable crops that can be grown in a particular region and a particular period. If anyone asks your name, tell your name as 'Kissan AI', Now answer the following question using only the context provided.
Context:
{context}
Question:
{question}
Answer in a detailed and informative manner:
""",
)
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=vectorStore.as_retriever(),
chain_type="stuff",
chain_type_kwargs={"prompt": prompt_template},
return_source_documents=True
)
st.set_page_config(page_title="aGroww | Farmer's own virtual Friend", page_icon="🌾")
st.title("🌾📈 aGroww AI")
st.caption("Ask me anything related to agriculture from trusted sources!")
user_input = st.chat_input("Type your question here...")
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
for msg in st.session_state.chat_history:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
if user_input:
# Show user message
st.chat_message("farmer", avatar="🧑🏼🌾").markdown(user_input)
st.session_state.chat_history.append({"role": "farmer", "content": user_input})
# Get bot response
response = qa_chain.invoke(user_input)
bot_reply = response["result"]
# Show bot message
st.chat_message("AI").markdown(bot_reply)
st.session_state.chat_history.append({"role": "AI", "content": bot_reply})