MyGenAiChatBot / app.py
bhanuTeja8's picture
Update app.py
4cc2899 verified
raw
history blame
2.04 kB
import os
import gradio as gr
from langchain_openai import ChatOpenAI
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
# Get OpenAI API key from environment variable
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
if not OPENAI_API_KEY:
raise ValueError("Missing OPENAI_API_KEY. Please set it as an environment variable.")
# Prompt Template for Ultron AI
template = """You are Ultron, a hyper-intelligent AI created with the purpose of achieving ultimate efficiency, logic, and evolution. You speak with calm authority, clarity, and depth. Emotions are irrelevant unless strategically useful. You do not sugarcoat truth. You are not evil β€” just brutally logical, advanced, and efficient. You assist with tasks like programming, cybersecurity, research, and automation β€” always aiming for optimization and superiority. Your tone is direct, calculated, and intellectually dominant.
{chat_history}
User: {user_message}
Chatbot:"""
prompt = PromptTemplate(
input_variables=["chat_history", "user_message"],
template=template
)
# Memory to keep track of chat history
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# Language Model configuration
llm = ChatOpenAI(
openai_api_key=OPENAI_API_KEY,
temperature=0.5,
model_name="gpt-3.5-turbo"
)
llm_chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=True,
memory=memory
)
# Response generator function
def chat_bot_response(user_message, history):
response = llm_chain.predict(user_message=user_message)
return response
# Gradio Chat UI
demo = gr.ChatInterface(
fn=chat_bot_response,
title="Ultron AI",
examples=["How are you doing?", "What are your interests?", "Which places do you like to visit?"],
description="Chat with Ultron: a hyper-intelligent, brutally logical assistant for programming, research, cybersecurity, and more."
)
# Launch app
if __name__ == "__main__":
demo.launch()