获取client real ip。
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@
|
|
3 |
import openai
|
4 |
import html
|
5 |
import os
|
|
|
6 |
|
7 |
import gradio as gr
|
8 |
import random
|
@@ -21,6 +22,20 @@ MODELS = [
|
|
21 |
|
22 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def reduce_history(history):
|
25 |
mid = len(history) // 2
|
26 |
logging.info('history reduced')
|
@@ -28,7 +43,7 @@ def reduce_history(history):
|
|
28 |
|
29 |
|
30 |
def chat_with_model(request, role, content, history=[], model=MODELS[0]):
|
31 |
-
|
32 |
if role == 'system':
|
33 |
history = []
|
34 |
history.append({'role': role, 'content': content})
|
@@ -37,10 +52,11 @@ def chat_with_model(request, role, content, history=[], model=MODELS[0]):
|
|
37 |
messages=history
|
38 |
)
|
39 |
|
40 |
-
content=html.escape(response['choices'][0]['message']['content'])
|
41 |
total_tokens = response['usage']['total_tokens']
|
42 |
history.append({'role': 'assistant', 'content': content})
|
43 |
-
logging.info('client={}, history count={}'.format(
|
|
|
44 |
if 4096 - total_tokens < 500:
|
45 |
history = reduce_history(history)
|
46 |
return content, history
|
@@ -67,25 +83,30 @@ def launch():
|
|
67 |
return user_message, chatbot + [[user_message, None]]
|
68 |
|
69 |
def bot_resp(msg, chatbot, history, model, request: gr.Request):
|
70 |
-
bot_message, history = chat_with_model(
|
|
|
71 |
chatbot[-1][1] = bot_message
|
72 |
return '', chatbot, history
|
73 |
|
74 |
def bot_change(selected_bot, history, model, request: gr.Request):
|
75 |
-
|
|
|
|
|
76 |
if BOT_INIT_PROMPTS.get(selected_bot):
|
77 |
content, history = chat_with_model(request,
|
78 |
-
|
79 |
return [[None, content]], '', history
|
80 |
else:
|
81 |
return [], '', []
|
82 |
|
83 |
# Events
|
84 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
85 |
-
bot_resp, [msg, chatbot, history, model_selector], [
|
|
|
86 |
)
|
87 |
submit.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
88 |
-
bot_resp, [msg, chatbot, history, model_selector], [
|
|
|
89 |
)
|
90 |
clear.click(lambda: None, None, chatbot, queue=False).then(
|
91 |
bot_change, [bot_selector, history, model_selector], [chatbot, msg, history])
|
|
|
3 |
import openai
|
4 |
import html
|
5 |
import os
|
6 |
+
import socket
|
7 |
|
8 |
import gradio as gr
|
9 |
import random
|
|
|
22 |
|
23 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
24 |
|
25 |
+
|
26 |
+
def parse_request(request: gr.Request):
|
27 |
+
client_ip = request.client.host
|
28 |
+
local_ip = socket.gethostbyname(socket.gethostbyname(""))
|
29 |
+
headers = request.headers
|
30 |
+
if headers and 'x-forwarded-for' in headers:
|
31 |
+
x_forwarded_for = headers['x-forwarded-for']
|
32 |
+
client_ip = x_forwarded_for.split(' ')[0] if x_forwarded_for else ""
|
33 |
+
|
34 |
+
return {"client_ip": client_ip,
|
35 |
+
"local_ip": local_ip,
|
36 |
+
"headers": headers}
|
37 |
+
|
38 |
+
|
39 |
def reduce_history(history):
|
40 |
mid = len(history) // 2
|
41 |
logging.info('history reduced')
|
|
|
43 |
|
44 |
|
45 |
def chat_with_model(request, role, content, history=[], model=MODELS[0]):
|
46 |
+
client_ip = parse_request(request)["client_ip"]
|
47 |
if role == 'system':
|
48 |
history = []
|
49 |
history.append({'role': role, 'content': content})
|
|
|
52 |
messages=history
|
53 |
)
|
54 |
|
55 |
+
content = html.escape(response['choices'][0]['message']['content'])
|
56 |
total_tokens = response['usage']['total_tokens']
|
57 |
history.append({'role': 'assistant', 'content': content})
|
58 |
+
logging.info('client={}, history count={}'.format(
|
59 |
+
client_ip, str(len(history))))
|
60 |
if 4096 - total_tokens < 500:
|
61 |
history = reduce_history(history)
|
62 |
return content, history
|
|
|
83 |
return user_message, chatbot + [[user_message, None]]
|
84 |
|
85 |
def bot_resp(msg, chatbot, history, model, request: gr.Request):
|
86 |
+
bot_message, history = chat_with_model(
|
87 |
+
request, 'user', msg, history, model)
|
88 |
chatbot[-1][1] = bot_message
|
89 |
return '', chatbot, history
|
90 |
|
91 |
def bot_change(selected_bot, history, model, request: gr.Request):
|
92 |
+
client_ip = parse_request(request)["client_ip"]
|
93 |
+
logging.info(
|
94 |
+
f'client={client_ip} Bot changed={selected_bot}')
|
95 |
if BOT_INIT_PROMPTS.get(selected_bot):
|
96 |
content, history = chat_with_model(request,
|
97 |
+
'system', BOT_INIT_PROMPTS[selected_bot], history, model)
|
98 |
return [[None, content]], '', history
|
99 |
else:
|
100 |
return [], '', []
|
101 |
|
102 |
# Events
|
103 |
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
104 |
+
bot_resp, [msg, chatbot, history, model_selector], [
|
105 |
+
msg, chatbot, history]
|
106 |
)
|
107 |
submit.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
108 |
+
bot_resp, [msg, chatbot, history, model_selector], [
|
109 |
+
msg, chatbot, history]
|
110 |
)
|
111 |
clear.click(lambda: None, None, chatbot, queue=False).then(
|
112 |
bot_change, [bot_selector, history, model_selector], [chatbot, msg, history])
|