Эх сурвалжийг харах

本地模式先检查端口是否可用

xiongweixp 8 сар өмнө
parent
commit
991ad76ad9
1 өөрчлөгдсөн 17 нэмэгдсэн , 4 устгасан
  1. 17 4
      erniecli.py

+ 17 - 4
erniecli.py

@@ -6,6 +6,7 @@ import time
 import _thread
 import os
 import subprocess
+import socket
 
 ERNIE_API_KEY = ""
 ERNIE_SECRET_KEY = ""
@@ -208,6 +209,16 @@ def askOpenAI(quest):
     
     return response.text
 
+# 检查端口是否可用
+def check_port_available(port):
+    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+    sock.settimeout(1)
+    result = sock.connect_ex(('localhost', port))
+    if result == 0:
+        return True
+    else:
+        return False
+
 # 查询本地
 def askLocal(quest):
     res = requests.post(
@@ -346,10 +357,12 @@ erniecli version          :    show version")
             #使用绿色字体
         cli = ""
         if USE_LOCAL:
-            # 本地ollama
-            lock[1] ="\033[1;32m终端助理正在本地思考...\033[0m"
-            answer = askLocal(question)
-            cli = parse(answer, True)
+            # 检查11434端口是否可用
+            if check_port_available(LOCAL_PORT):
+                # 本地ollama
+                lock[1] ="\033[1;32m终端助理正在本地思考...\033[0m"
+                answer = askLocal(question)
+                cli = parse(answer, True)
         if cli== "":
             lock[1] ="\033[1;32m终端助理正在云端思考...\033[0m"