thanglekdi commited on
Commit
d660115
·
1 Parent(s): a29a6a4
Files changed (2) hide show
  1. app.py +18 -20
  2. requirements.txt +1 -1
app.py CHANGED
@@ -4,31 +4,29 @@ import openai
4
 
5
  # 1️⃣ Thiết lập OpenAI API key (đặt biến môi trường OPENAI_API_KEY trước khi chạy)
6
  # openai.api_key = "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"
7
-
8
  def respond(message, history, system_message, max_tokens, temperature, top_p):
9
- # 2.1 — Gom system + lịch sử chat vào messages list
10
- messages = [{"role": "system", "content": system_message}]
11
- for u, b in history:
12
- messages.append({"role": "user", "content": u})
13
- messages.append({"role": "assistant", "content": b})
14
- messages.append({"role": "user", "content": message})
15
-
16
- # 2.2 — Gọi OpenAI ChatCompletion
17
- resp = openai.ChatCompletion.create(
18
- model="gpt-4.1-nano",
19
- messages=messages,
20
- max_tokens=max_tokens,
21
- temperature=temperature,
22
- top_p=top_p,
23
  )
 
 
24
 
25
- # 2.3 Trích nội dung assistant reply
26
- reply = resp.choices[0].message.content.strip()
27
 
28
- # 2.4 — Cập nhật history và trả về
29
- history.append((message, reply))
30
 
31
- return reply
 
 
 
 
 
 
 
 
 
 
32
 
33
  # 3️⃣ Giao diện Gradio
34
  demo = gr.ChatInterface(
 
4
 
5
  # 1️⃣ Thiết lập OpenAI API key (đặt biến môi trường OPENAI_API_KEY trước khi chạy)
6
  # openai.api_key = "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"
7
+ client = openai.OpenAI()
8
  def respond(message, history, system_message, max_tokens, temperature, top_p):
9
+ resp = client.responses.create(
10
+ model="gpt-4o", # hoặc "gpt-3.5-turbo", "gpt-4" tùy quyền truy cập
11
+ input=message
 
 
 
 
 
 
 
 
 
 
 
12
  )
13
+ # 3️⃣ Trích content
14
+ return resp.choices[0].message.content.strip()
15
 
16
+ # 1️⃣ Khởi tạo OpenAI client (tự động lấy OPENAI_API_KEY từ env)
 
17
 
 
 
18
 
19
+ def generate_response(prompt: str):
20
+ """
21
+ Gọi OpenAI Model Response endpoint để sinh text.
22
+ """
23
+ # 2️⃣ Gọi API model response
24
+ resp = client.responses.create(
25
+ model="gpt-4o", # hoặc "gpt-3.5-turbo", "gpt-4" tùy quyền truy cập
26
+ input=prompt
27
+ )
28
+ # 3️⃣ Trích content
29
+ return resp.choices[0].message.content.strip()
30
 
31
  # 3️⃣ Giao diện Gradio
32
  demo = gr.ChatInterface(
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
  gradio
2
- openai==0.28
 
1
  gradio
2
+ openai