mirror of
https://github.com/linyqh/NarratoAI.git
synced 2025-12-11 02:12:50 +00:00
更新 gemini 模型请求参数设置
This commit is contained in:
parent
ddb4e5b8a6
commit
d0f8027024
@ -152,13 +152,13 @@ class SubtitleAnalyzer:
|
||||
}
|
||||
|
||||
# 构建请求URL
|
||||
url = f"{self.base_url}/models/{self.model}:generateContent?key={self.api_key}"
|
||||
url = f"{self.base_url}/models/{self.model}:generateContent"
|
||||
|
||||
# 发送请求
|
||||
response = requests.post(
|
||||
url,
|
||||
json=payload,
|
||||
headers={"Content-Type": "application/json", "User-Agent": "NarratoAI/1.0"},
|
||||
headers={"Content-Type": "application/json", "x-goog-api-key": self.api_key},
|
||||
timeout=120
|
||||
)
|
||||
|
||||
@ -440,13 +440,13 @@ class SubtitleAnalyzer:
|
||||
}
|
||||
|
||||
# 构建请求URL
|
||||
url = f"{self.base_url}/models/{self.model}:generateContent?key={self.api_key}"
|
||||
url = f"{self.base_url}/models/{self.model}:generateContent"
|
||||
|
||||
# 发送请求
|
||||
response = requests.post(
|
||||
url,
|
||||
json=payload,
|
||||
headers={"Content-Type": "application/json", "User-Agent": "NarratoAI/1.0"},
|
||||
headers={"Content-Type": "application/json", "x-goog-api-key": self.api_key},
|
||||
timeout=120
|
||||
)
|
||||
|
||||
|
||||
@ -140,7 +140,7 @@ class GeminiVisionProvider(VisionModelProvider):
|
||||
"""执行原生Gemini API调用,包含重试机制"""
|
||||
from app.config import config
|
||||
|
||||
url = f"{self.base_url}/models/{self.model_name}:generateContent?key={self.api_key}"
|
||||
url = f"{self.base_url}/models/{self.model_name}:generateContent"
|
||||
|
||||
max_retries = config.app.get('llm_max_retries', 3)
|
||||
base_timeout = config.app.get('llm_vision_timeout', 120)
|
||||
@ -157,7 +157,7 @@ class GeminiVisionProvider(VisionModelProvider):
|
||||
json=payload,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "NarratoAI/1.0"
|
||||
"x-goog-api-key": self.api_key
|
||||
},
|
||||
timeout=timeout
|
||||
)
|
||||
@ -330,7 +330,7 @@ class GeminiTextProvider(TextModelProvider):
|
||||
"""执行原生Gemini API调用,包含重试机制"""
|
||||
from app.config import config
|
||||
|
||||
url = f"{self.base_url}/models/{self.model_name}:generateContent?key={self.api_key}"
|
||||
url = f"{self.base_url}/models/{self.model_name}:generateContent"
|
||||
|
||||
max_retries = config.app.get('llm_max_retries', 3)
|
||||
base_timeout = config.app.get('llm_text_timeout', 180) # 文本生成任务使用更长的基础超时时间
|
||||
@ -347,7 +347,7 @@ class GeminiTextProvider(TextModelProvider):
|
||||
json=payload,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "NarratoAI/1.0"
|
||||
"x-goog-api-key": self.api_key
|
||||
},
|
||||
timeout=timeout
|
||||
)
|
||||
|
||||
@ -107,7 +107,7 @@ class VisionAnalyzer:
|
||||
}
|
||||
|
||||
# 构建请求URL
|
||||
url = f"{self.base_url}/models/{self.model_name}:generateContent?key={self.api_key}"
|
||||
url = f"{self.base_url}/models/{self.model_name}:generateContent"
|
||||
|
||||
# 发送请求
|
||||
response = await asyncio.to_thread(
|
||||
@ -116,7 +116,7 @@ class VisionAnalyzer:
|
||||
json=request_data,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "NarratoAI/1.0"
|
||||
"x-goog-api-key": self.api_key
|
||||
},
|
||||
timeout=120 # 增加超时时间
|
||||
)
|
||||
|
||||
@ -230,7 +230,7 @@ class GeminiOpenAIGenerator(BaseGenerator):
|
||||
}
|
||||
|
||||
# 构建请求URL
|
||||
url = f"{self.base_url}/models/{self.model_name}:generateContent?key={self.api_key}"
|
||||
url = f"{self.base_url}/models/{self.model_name}:generateContent"
|
||||
|
||||
# 发送请求
|
||||
response = requests.post(
|
||||
@ -238,7 +238,7 @@ class GeminiOpenAIGenerator(BaseGenerator):
|
||||
json=request_data,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "NarratoAI/1.0"
|
||||
"x-goog-api-key": self.api_key
|
||||
},
|
||||
timeout=120
|
||||
)
|
||||
|
||||
@ -138,6 +138,7 @@ def test_vision_model_connection(api_key, base_url, model_name, provider, tr):
|
||||
str: 测试结果消息
|
||||
"""
|
||||
import requests
|
||||
logger.debug(f"大模型连通性测试: {base_url} 模型: {model_name} apikey: {api_key}")
|
||||
if provider.lower() == 'gemini':
|
||||
# 原生Gemini API测试
|
||||
try:
|
||||
@ -145,43 +146,21 @@ def test_vision_model_connection(api_key, base_url, model_name, provider, tr):
|
||||
request_data = {
|
||||
"contents": [{
|
||||
"parts": [{"text": "直接回复我文本'当前网络可用'"}]
|
||||
}],
|
||||
"generationConfig": {
|
||||
"temperature": 1.0,
|
||||
"topK": 40,
|
||||
"topP": 0.95,
|
||||
"maxOutputTokens": 100,
|
||||
},
|
||||
"safetySettings": [
|
||||
{
|
||||
"category": "HARM_CATEGORY_HARASSMENT",
|
||||
"threshold": "BLOCK_NONE"
|
||||
},
|
||||
{
|
||||
"category": "HARM_CATEGORY_HATE_SPEECH",
|
||||
"threshold": "BLOCK_NONE"
|
||||
},
|
||||
{
|
||||
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
||||
"threshold": "BLOCK_NONE"
|
||||
},
|
||||
{
|
||||
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
||||
"threshold": "BLOCK_NONE"
|
||||
}
|
||||
]
|
||||
}]
|
||||
}
|
||||
|
||||
# 构建请求URL
|
||||
api_base_url = base_url or "https://generativelanguage.googleapis.com/v1beta"
|
||||
url = f"{api_base_url}/models/{model_name}:generateContent?key={api_key}"
|
||||
|
||||
api_base_url = base_url
|
||||
url = f"{api_base_url}/models/{model_name}:generateContent"
|
||||
# 发送请求
|
||||
response = requests.post(
|
||||
url,
|
||||
json=request_data,
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=30
|
||||
headers={
|
||||
"x-goog-api-key": api_key,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
@ -190,7 +169,6 @@ def test_vision_model_connection(api_key, base_url, model_name, provider, tr):
|
||||
return False, f"{tr('原生Gemini模型连接失败')}: HTTP {response.status_code}"
|
||||
except Exception as e:
|
||||
return False, f"{tr('原生Gemini模型连接失败')}: {str(e)}"
|
||||
|
||||
elif provider.lower() == 'gemini(openai)':
|
||||
# OpenAI兼容的Gemini代理测试
|
||||
try:
|
||||
@ -215,23 +193,6 @@ def test_vision_model_connection(api_key, base_url, model_name, provider, tr):
|
||||
return False, f"{tr('OpenAI兼容Gemini代理连接失败')}: HTTP {response.status_code}"
|
||||
except Exception as e:
|
||||
return False, f"{tr('OpenAI兼容Gemini代理连接失败')}: {str(e)}"
|
||||
elif provider.lower() == 'narratoapi':
|
||||
try:
|
||||
# 构建测试请求
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}"
|
||||
}
|
||||
|
||||
test_url = f"{base_url.rstrip('/')}/health"
|
||||
response = requests.get(test_url, headers=headers, timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
return True, tr("NarratoAPI is available")
|
||||
else:
|
||||
return False, f"{tr('NarratoAPI is not available')}: HTTP {response.status_code}"
|
||||
except Exception as e:
|
||||
return False, f"{tr('NarratoAPI is not available')}: {str(e)}"
|
||||
|
||||
else:
|
||||
from openai import OpenAI
|
||||
try:
|
||||
@ -441,7 +402,8 @@ def test_text_model_connection(api_key, base_url, model_name, provider, tr):
|
||||
str: 测试结果消息
|
||||
"""
|
||||
import requests
|
||||
|
||||
logger.debug(f"大模型连通性测试: {base_url} 模型: {model_name} apikey: {api_key}")
|
||||
|
||||
try:
|
||||
# 构建统一的测试请求(遵循OpenAI格式)
|
||||
headers = {
|
||||
@ -457,43 +419,22 @@ def test_text_model_connection(api_key, base_url, model_name, provider, tr):
|
||||
request_data = {
|
||||
"contents": [{
|
||||
"parts": [{"text": "直接回复我文本'当前网络可用'"}]
|
||||
}],
|
||||
"generationConfig": {
|
||||
"temperature": 1.0,
|
||||
"topK": 40,
|
||||
"topP": 0.95,
|
||||
"maxOutputTokens": 100,
|
||||
},
|
||||
"safetySettings": [
|
||||
{
|
||||
"category": "HARM_CATEGORY_HARASSMENT",
|
||||
"threshold": "BLOCK_NONE"
|
||||
},
|
||||
{
|
||||
"category": "HARM_CATEGORY_HATE_SPEECH",
|
||||
"threshold": "BLOCK_NONE"
|
||||
},
|
||||
{
|
||||
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
||||
"threshold": "BLOCK_NONE"
|
||||
},
|
||||
{
|
||||
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
||||
"threshold": "BLOCK_NONE"
|
||||
}
|
||||
]
|
||||
}]
|
||||
}
|
||||
|
||||
# 构建请求URL
|
||||
api_base_url = base_url or "https://generativelanguage.googleapis.com/v1beta"
|
||||
url = f"{api_base_url}/models/{model_name}:generateContent?key={api_key}"
|
||||
api_base_url = base_url
|
||||
url = f"{api_base_url}/models/{model_name}:generateContent"
|
||||
|
||||
# 发送请求
|
||||
response = requests.post(
|
||||
url,
|
||||
json=request_data,
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=30
|
||||
headers={
|
||||
"x-goog-api-key": api_key,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user