完全移除內網連接,專注外網API

變更項目:
- 刪除所有內網相關程式檔案
- 移除內網IP參考 (192.168.x.x)
- 精簡檔案結構,只保留核心程式
- 重命名主程式為 llama_chat.py
- 更新所有文檔移除內網內容
- 專注於外網 API 連接和多端點支援

保留檔案:
- llama_chat.py (主程式)
- llama_full_api.py (完整版)
- quick_test.py (快速測試)
- test_all_models.py (模型測試)
- README.md, 操作指南.md (文檔)
This commit is contained in:
2025-09-19 22:07:01 +08:00
parent e71495ece4
commit 3c0fba5fc8
12 changed files with 181 additions and 1043 deletions

View File

@@ -1,34 +1,57 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Llama 內網 API 對話程式
支援多個端點和模型選擇
Llama API 外網連接程式
使用外網端點進行 AI 對話
"""
from openai import OpenAI
import requests
import sys
import re
from datetime import datetime
# API 配置
# API 金鑰
API_KEY = "paVrIT+XU1NhwCAOb0X4aYi75QKogK5YNMGvQF1dCyo="
# 可用端點 (前 3 個已測試可用)
# 外網 API 端點配置
ENDPOINTS = [
"http://192.168.0.6:21180/v1",
"http://192.168.0.6:21181/v1",
"http://192.168.0.6:21182/v1",
"http://192.168.0.6:21183/v1"
{
"name": "Llama 通用端點",
"url": "https://llama.theaken.com/v1",
"models": ["gpt-oss-120b", "deepseek-r1-671b", "qwen3-embedding-8b"]
},
{
"name": "GPT-OSS 專用端點",
"url": "https://llama.theaken.com/v1/gpt-oss-120b",
"models": ["gpt-oss-120b"]
},
{
"name": "DeepSeek 專用端點",
"url": "https://llama.theaken.com/v1/deepseek-r1-671b",
"models": ["deepseek-r1-671b"]
}
]
# 模型列表
MODELS = [
"gpt-oss-120b",
"deepseek-r1-671b",
"qwen3-embedding-8b"
# 備用外網端點(如果主要端點無法使用)
BACKUP_ENDPOINTS = [
{
"name": "備用端點 1",
"url": "https://api.llama.theaken.com/v1",
"models": ["gpt-oss-120b", "deepseek-r1-671b", "qwen3-embedding-8b"]
},
{
"name": "備用端點 2",
"url": "https://llama-api.theaken.com/v1",
"models": ["gpt-oss-120b", "deepseek-r1-671b", "qwen3-embedding-8b"]
}
]
def clean_response(text):
"""清理 AI 回應中的特殊標記"""
if not text:
return text
# 移除思考標記
if "<think>" in text:
text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
@@ -47,34 +70,102 @@ def clean_response(text):
return text
def test_endpoint(endpoint):
def test_endpoint(endpoint_info, timeout=10):
"""測試端點是否可用"""
url = endpoint_info["url"]
model = endpoint_info["models"][0] if endpoint_info["models"] else "gpt-oss-120b"
print(f" 測試 {endpoint_info['name']}...", end="", flush=True)
try:
client = OpenAI(api_key=API_KEY, base_url=endpoint)
response = client.chat.completions.create(
model="gpt-oss-120b",
messages=[{"role": "user", "content": "Hi"}],
max_tokens=10,
timeout=5
# 處理特殊的模型端點 URL
if url.endswith("/gpt-oss-120b") or url.endswith("/deepseek-r1-671b"):
base_url = url.rsplit("/", 1)[0]
else:
base_url = url
client = OpenAI(
api_key=API_KEY,
base_url=base_url,
timeout=timeout
)
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "test"}],
max_tokens=5
)
print(" ✓ 可用")
return True
except:
except Exception as e:
error_msg = str(e)
if "502" in error_msg:
print(" ✗ 伺服器暫時無法使用 (502)")
elif "timeout" in error_msg.lower():
print(" ✗ 連接超時")
elif "connection" in error_msg.lower():
print(" ✗ 無法連接")
else:
print(f" ✗ 錯誤")
return False
def chat_session(endpoint, model):
def find_working_endpoint():
"""尋找可用的端點"""
print("\n正在測試外網端點...")
print("-" * 50)
# 先測試主要端點
print("主要端點:")
for endpoint in ENDPOINTS:
if test_endpoint(endpoint):
return endpoint
# 如果主要端點都不可用,測試備用端點
print("\n備用端點:")
for endpoint in BACKUP_ENDPOINTS:
if test_endpoint(endpoint):
return endpoint
return None
def chat_session(endpoint_info):
"""對話主程式"""
print("\n" + "="*60)
print("Llama AI 對話系統")
print("="*60)
print(f"端點: {endpoint}")
print(f"模型: {model}")
print(f"使用端點: {endpoint_info['name']}")
print(f"URL: {endpoint_info['url']}")
print(f"可用模型: {', '.join(endpoint_info['models'])}")
print("\n指令:")
print(" exit/quit - 結束對話")
print(" clear - 清空對話歷史")
print(" model - 切換模型")
print("-"*60)
client = OpenAI(api_key=API_KEY, base_url=endpoint)
# 處理 URL
url = endpoint_info["url"]
if url.endswith("/gpt-oss-120b") or url.endswith("/deepseek-r1-671b"):
base_url = url.rsplit("/", 1)[0]
else:
base_url = url
client = OpenAI(api_key=API_KEY, base_url=base_url)
# 選擇模型
if len(endpoint_info['models']) == 1:
current_model = endpoint_info['models'][0]
else:
print("\n選擇模型:")
for i, model in enumerate(endpoint_info['models'], 1):
print(f" {i}. {model}")
choice = input("選擇 (預設: 1): ").strip()
if choice.isdigit() and 1 <= int(choice) <= len(endpoint_info['models']):
current_model = endpoint_info['models'][int(choice)-1]
else:
current_model = endpoint_info['models'][0]
print(f"\n使用模型: {current_model}")
messages = []
while True:
@@ -94,13 +185,16 @@ def chat_session(endpoint, model):
continue
if user_input.lower() == 'model':
print("\n可用模型:")
for i, m in enumerate(MODELS, 1):
print(f" {i}. {m}")
choice = input("選擇 (1-3): ").strip()
if choice in ['1', '2', '3']:
model = MODELS[int(choice)-1]
print(f"[系統] 已切換到 {model}")
if len(endpoint_info['models']) == 1:
print(f"[系統] 此端點只支援 {endpoint_info['models'][0]}")
else:
print("\n可用模型:")
for i, m in enumerate(endpoint_info['models'], 1):
print(f" {i}. {m}")
choice = input("選擇: ").strip()
if choice.isdigit() and 1 <= int(choice) <= len(endpoint_info['models']):
current_model = endpoint_info['models'][int(choice)-1]
print(f"[系統] 已切換到 {current_model}")
continue
messages.append({"role": "user", "content": user_input})
@@ -109,7 +203,7 @@ def chat_session(endpoint, model):
try:
response = client.chat.completions.create(
model=model,
model=current_model,
messages=messages,
temperature=0.7,
max_tokens=1000
@@ -118,17 +212,14 @@ def chat_session(endpoint, model):
ai_response = response.choices[0].message.content
ai_response = clean_response(ai_response)
print("\r" + " "*20 + "\r", end="") # 清除 "思考中..."
print("\r" + " "*20 + "\r", end="")
print(f"AI: {ai_response}")
messages.append({"role": "assistant", "content": ai_response})
except UnicodeEncodeError:
print("\r[錯誤] 編碼問題,請使用英文對話")
messages.pop() # 移除最後的用戶訊息
except Exception as e:
print(f"\r[錯誤] {str(e)[:100]}")
messages.pop() # 移除最後的用戶訊息
messages.pop()
except KeyboardInterrupt:
print("\n\n[中斷] 使用 exit 命令正常退出")
@@ -139,52 +230,33 @@ def chat_session(endpoint, model):
def main():
print("="*60)
print("Llama 內網 API 對話程式")
print("Llama AI 外網對話程式")
print(f"時間: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("="*60)
# 測試端點
print("\n正在檢查可用端點...")
available = []
for i, endpoint in enumerate(ENDPOINTS[:3], 1): # 只測試前3個
print(f" 測試 {endpoint}...", end="", flush=True)
if test_endpoint(endpoint):
print(" [OK]")
available.append(endpoint)
else:
print(" [失敗]")
# 尋找可用端點
working_endpoint = find_working_endpoint()
if not available:
print("\n[錯誤] 沒有可用的端點")
if not working_endpoint:
print("\n" + "="*60)
print("錯誤:無法連接到任何外網端點")
print("="*60)
print("\n可能的原因:")
print("1. 外網 API 伺服器暫時離線")
print("2. 網路連接問題")
print("3. 防火牆或代理設定")
print("\n建議:")
print("1. 稍後再試10-30分鐘後")
print("2. 檢查網路連接")
print("3. 聯繫 API 管理員")
sys.exit(1)
# 選擇端點
if len(available) == 1:
selected_endpoint = available[0]
print(f"\n使用端點: {selected_endpoint}")
else:
print(f"\n找到 {len(available)} 個可用端點:")
for i, ep in enumerate(available, 1):
print(f" {i}. {ep}")
print("\n選擇端點 (預設: 1): ", end="")
choice = input().strip()
if choice and choice.isdigit() and 1 <= int(choice) <= len(available):
selected_endpoint = available[int(choice)-1]
else:
selected_endpoint = available[0]
# 選擇模型
print("\n可用模型:")
for i, model in enumerate(MODELS, 1):
print(f" {i}. {model}")
print("\n選擇模型 (預設: 1): ", end="")
choice = input().strip()
if choice in ['1', '2', '3']:
selected_model = MODELS[int(choice)-1]
else:
selected_model = MODELS[0]
print("\n" + "="*60)
print(f"成功連接到: {working_endpoint['name']}")
print("="*60)
# 開始對話
chat_session(selected_endpoint, selected_model)
chat_session(working_endpoint)
if __name__ == "__main__":
try:
@@ -193,4 +265,6 @@ if __name__ == "__main__":
print("\n\n程式已退出")
except Exception as e:
print(f"\n[錯誤] {e}")
import traceback
traceback.print_exc()
sys.exit(1)