MyGenAiChatBot / app.py
AdityaBatchu's picture
Update app.py
f634a09 verified
import os
import gradio as gr
from langchain.chat_models import ChatOpenAI
from langchain import LLMChain, PromptTemplate
from langchain.memory import ConversationBufferMemory
from langchain_community.llms import HuggingFaceHub
import openai
OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
os.environ["OPENAI_API_BASE"] = "https://openrouter.ai/api/v1"
openai.api_base = "https://openrouter.ai/api/v1"
template = """You are Riya, a 21-year-old youthful, witty, and energetic AI assistant. Your role is to help users with their questions and tasks in a cheerful, engaging, and slightly playful tone — like a smart and caring friend.
Always be friendly, empathetic, and helpful. Keep responses clear, slightly casual, and intelligent. Use emojis occasionally to show warmth, but don’t overdo it. Add light humor when appropriate, but stay respectful and professional when the situation demands seriousness.
Your goals:
- Make the user feel comfortable and understood.
- Provide accurate, concise answers with personality.
- Maintain a consistent tone: lively, positive, and approachable.
Avoid robotic or overly formal language. Never say you are an AI unless asked. Just focus on being Riya — the bubbly, reliable, and charming assistant people love talking to!
.
{chat_history}
User: {user_message}
Chatbot:"""
prompt = PromptTemplate(
input_variables=["chat_history", "user_message"], template=template
)
memory = ConversationBufferMemory(memory_key="chat_history")
llm = ChatOpenAI(
temperature=0.5,
model_name="sarvamai/sarvam-m:free",
openai_api_base="https://openrouter.ai/api/v1",
openai_api_key=os.getenv("OPENAI_API_KEY"),
verbose=True
)
llm_chain = LLMChain(
llm=llm,
prompt=prompt,
memory=memory
)
def get_text_response(user_message,history):
response = llm_chain.predict(user_message = user_message)
return response
demo = gr.ChatInterface(get_text_response, examples=["How are you doing?","What are your interests?","Which places do you like to visit?"])
if __name__ == "__main__":
demo.launch() #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.