File size: 1,055 Bytes
e2b2f54
 
53a3000
e2b2f54
caaa69e
 
 
 
4586819
caaa69e
 
dd29d8d
 
 
 
 
 
 
 
 
4586819
dd29d8d
4586819
 
 
 
 
e2b2f54
4586819
 
e2b2f54
4586819
e2b2f54
 
4586819
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
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()