ai: Implement parallel request to the server.
Browse files
jarvis.py
CHANGED
@@ -17,6 +17,7 @@ import pptx
|
|
17 |
import fitz
|
18 |
import io
|
19 |
import uuid
|
|
|
20 |
|
21 |
from openai import OpenAI
|
22 |
|
@@ -116,6 +117,13 @@ def process_ai_response(ai_text):
|
|
116 |
except Exception:
|
117 |
return ai_text
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
def chat_with_model(history, user_input, selected_model_display, sess):
|
120 |
if not LINUX_SERVER_PROVIDER_KEYS or not LINUX_SERVER_HOSTS:
|
121 |
return RESPONSES["RESPONSE_3"]
|
@@ -126,14 +134,15 @@ def chat_with_model(history, user_input, selected_model_display, sess):
|
|
126 |
messages = [{"role": "user", "content": user} for user, _ in history]
|
127 |
messages += [{"role": "assistant", "content": assistant} for _, assistant in history if assistant]
|
128 |
messages.append({"role": "user", "content": user_input})
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
137 |
|
138 |
def respond(multi_input, history, selected_model_display, sess):
|
139 |
message = {"text": multi_input.get("text", "").strip(), "files": multi_input.get("files", [])}
|
|
|
17 |
import fitz
|
18 |
import io
|
19 |
import uuid
|
20 |
+
import concurrent.futures
|
21 |
|
22 |
from openai import OpenAI
|
23 |
|
|
|
117 |
except Exception:
|
118 |
return ai_text
|
119 |
|
120 |
+
def fetch_response(host, provider_key, selected_model, messages, model_config, session_id):
|
121 |
+
client = OpenAI(base_url=host, api_key=provider_key)
|
122 |
+
data = {"model": selected_model, "messages": messages, **model_config}
|
123 |
+
response = client.chat.completions.create(extra_body={"optillm_approach": "rto|re2|cot_reflection|self_consistency|plansearch|leap|z3|bon|moa|mcts|mcp|router|privacy|executecode|json", "session_id": session_id}, **data)
|
124 |
+
ai_text = response.choices[0].message.content if response.choices and response.choices[0].message and response.choices[0].message.content else RESPONSES["RESPONSE_2"]
|
125 |
+
return process_ai_response(ai_text)
|
126 |
+
|
127 |
def chat_with_model(history, user_input, selected_model_display, sess):
|
128 |
if not LINUX_SERVER_PROVIDER_KEYS or not LINUX_SERVER_HOSTS:
|
129 |
return RESPONSES["RESPONSE_3"]
|
|
|
134 |
messages = [{"role": "user", "content": user} for user, _ in history]
|
135 |
messages += [{"role": "assistant", "content": assistant} for _, assistant in history if assistant]
|
136 |
messages.append({"role": "user", "content": user_input})
|
137 |
+
futures = []
|
138 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=len(LINUX_SERVER_HOSTS)) as executor:
|
139 |
+
for host, key in zip(LINUX_SERVER_HOSTS, LINUX_SERVER_PROVIDER_KEYS):
|
140 |
+
futures.append(executor.submit(fetch_response, host, key, selected_model, messages, model_config, sess.session_id))
|
141 |
+
done, not_done = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_COMPLETED)
|
142 |
+
for future in not_done:
|
143 |
+
future.cancel()
|
144 |
+
result = list(done)[0].result() if done else RESPONSES["RESPONSE_2"]
|
145 |
+
return result
|
146 |
|
147 |
def respond(multi_input, history, selected_model_display, sess):
|
148 |
message = {"text": multi_input.get("text", "").strip(), "files": multi_input.get("files", [])}
|