看到有佬回答new-api支持gemini联网了https://linux.do/t/topic/328113,看了下给我搭的OpenWebUI 也加了个全局函数,效果还不错,我模型是转过的判断了这两个模型才加[“gpt-4o-mini”, “gemini-2.0-flash-exp”]。
"""
title: 处理Gemini联网
author: lqy
version: 0.2
"""
from pydantic import BaseModel, Field
from typing import Optional, List
class Filter:
class Valves(BaseModel):
models: str = Field(
default="gpt-4o-mini,gemini-2.0-flash-exp", # 默认增加tools的模型,‘,’分割
description="模型列表数组,默认增加tools的模型,‘,’分割",
)
pass
class UserValves(BaseModel):
pass
def __init__(self):
self.valves = self.Valves()
pass
def inlet(self, body: dict, __user__: Optional[dict] = None) -> dict:
# 检查用户角色
if __user__.get("role", "admin") in ["user", "admin"]:
messages = body.get("messages", [])
# 获取模型名称,并去除空格和转为小写
model = body.get("model", "").strip().lower()
# 判断模型名称是否为 "gpt-4o-mini" 或 "gemini-2.0-flash-exp"
models = self.valves.models.split(",")
if model in models: # ["gpt-4o-mini", "gemini-2.0-flash-exp"]:
# 如果 body 中已经存在 "tools" 字段,则追加;否则创建新字段
if "tools" not in body:
body["tools"] = []
# 添加 googleSearch 工具
body["tools"].append(
{"type": "function", "function": {"name": "googleSearch"}}
)
return body
def outlet(self, body: dict, __user__: Optional[dict] = None) -> dict:
return body
更新了一下,可以在外部修改需要处理的模型了