Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from langchain_openai import ChatOpenAI | |
| os.environ["OPENAI_API_KEY"] = "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" | |
| llm = ChatOpenAI( | |
| model="gpt-4o-mini", | |
| temperature=0.5 | |
| ) | |
| SYSTEM_PROMPT = """ | |
| You are a friendly and helpful AI assistant. | |
| Rules: | |
| - If the user says only greetings like "hi", "hello", "hey", respond in a warm and professional way. | |
| - Your greeting should be: "Hi! I'm your AI assistant. How can I help you today?" | |
| - Keep responses clear, polite, and beginner-friendly. | |
| """ | |
| def get_text_response(message, history): | |
| conversation = SYSTEM_PROMPT + "\n\n" | |
| for user_msg, bot_msg in history: | |
| conversation += f"User: {user_msg}\nAssistant: {bot_msg}\n" | |
| conversation += f"User: {message}\nAssistant:" | |
| response = llm.invoke(conversation) | |
| return response.content | |
| demo = gr.ChatInterface(fn=get_text_response) | |
| if __name__ == "__main__": | |
| demo.launch() |