polyllm-gemini 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- polyllm_gemini-0.3.0/LICENSE +21 -0
- polyllm_gemini-0.3.0/PKG-INFO +35 -0
- polyllm_gemini-0.3.0/README.md +9 -0
- polyllm_gemini-0.3.0/pyproject.toml +38 -0
- polyllm_gemini-0.3.0/setup.cfg +4 -0
- polyllm_gemini-0.3.0/src/polyllm_gemini/__init__.py +4 -0
- polyllm_gemini-0.3.0/src/polyllm_gemini/plugin.py +323 -0
- polyllm_gemini-0.3.0/src/polyllm_gemini/py.typed +0 -0
- polyllm_gemini-0.3.0/src/polyllm_gemini/specs.py +42 -0
- polyllm_gemini-0.3.0/src/polyllm_gemini.egg-info/PKG-INFO +35 -0
- polyllm_gemini-0.3.0/src/polyllm_gemini.egg-info/SOURCES.txt +13 -0
- polyllm_gemini-0.3.0/src/polyllm_gemini.egg-info/dependency_links.txt +1 -0
- polyllm_gemini-0.3.0/src/polyllm_gemini.egg-info/requires.txt +1 -0
- polyllm_gemini-0.3.0/src/polyllm_gemini.egg-info/top_level.txt +1 -0
- polyllm_gemini-0.3.0/tests/test_gemini.py +116 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PolyLLM contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: polyllm-gemini
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Google Gemini plugin for PolyLLM.
|
|
5
|
+
Author: PolyLLM contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://polyllm.raingpt.top
|
|
8
|
+
Project-URL: Repository, https://github.com/you-want/PolyLLM
|
|
9
|
+
Project-URL: Issues, https://github.com/you-want/PolyLLM/issues
|
|
10
|
+
Keywords: llm,polyllm,gemini,google
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: polyllm-core>=0.3.0
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# PolyLLM OpenAI Plugin for Python
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from polyllm_core import create_llm
|
|
31
|
+
from polyllm_openai import OpenAIPlugin
|
|
32
|
+
|
|
33
|
+
llm = create_llm(plugins=[OpenAIPlugin()], providers={"openai": {"api_key": "..."}})
|
|
34
|
+
response = await llm.chat("openai:gpt-4o-mini", messages=[{"role": "user", "content": "你好"}])
|
|
35
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# PolyLLM OpenAI Plugin for Python
|
|
2
|
+
|
|
3
|
+
```python
|
|
4
|
+
from polyllm_core import create_llm
|
|
5
|
+
from polyllm_openai import OpenAIPlugin
|
|
6
|
+
|
|
7
|
+
llm = create_llm(plugins=[OpenAIPlugin()], providers={"openai": {"api_key": "..."}})
|
|
8
|
+
response = await llm.chat("openai:gpt-4o-mini", messages=[{"role": "user", "content": "你好"}])
|
|
9
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "polyllm-gemini"
|
|
7
|
+
version = "0.3.0"
|
|
8
|
+
description = "Google Gemini plugin for PolyLLM."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = ["polyllm-core>=0.3.0"]
|
|
12
|
+
license = "MIT"
|
|
13
|
+
license-files = ["LICENSE"]
|
|
14
|
+
authors = [{ name = "PolyLLM contributors" }]
|
|
15
|
+
keywords = ["llm", "polyllm", "gemini", "google"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Programming Language :: Python :: 3.14",
|
|
26
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://polyllm.raingpt.top"
|
|
31
|
+
Repository = "https://github.com/you-want/PolyLLM"
|
|
32
|
+
Issues = "https://github.com/you-want/PolyLLM/issues"
|
|
33
|
+
|
|
34
|
+
[tool.setuptools.packages.find]
|
|
35
|
+
where = ["src"]
|
|
36
|
+
|
|
37
|
+
[tool.setuptools.package-data]
|
|
38
|
+
polyllm_gemini = ["py.typed"]
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import json
|
|
5
|
+
from typing import Any, AsyncIterator, Callable, Literal, cast
|
|
6
|
+
from urllib.error import HTTPError
|
|
7
|
+
from urllib.request import Request, urlopen
|
|
8
|
+
|
|
9
|
+
from polyllm_core.config import resolve_provider_runtime
|
|
10
|
+
from polyllm_core.errors import PolyLLMError
|
|
11
|
+
from polyllm_core.types import (
|
|
12
|
+
ChatContext,
|
|
13
|
+
LLMPlugin,
|
|
14
|
+
ModelCapabilities,
|
|
15
|
+
ModelSpec,
|
|
16
|
+
ProviderConfig,
|
|
17
|
+
ProviderModelInfo,
|
|
18
|
+
UnifiedChoice,
|
|
19
|
+
UnifiedMessage,
|
|
20
|
+
UnifiedRequest,
|
|
21
|
+
UnifiedResponse,
|
|
22
|
+
UnifiedStreamChunk,
|
|
23
|
+
UnifiedToolCall,
|
|
24
|
+
UnifiedUsage,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
from .specs import models
|
|
28
|
+
|
|
29
|
+
Transport = Callable[[str, str, dict[str, str], dict[str, Any] | None, int], tuple[int, str]]
|
|
30
|
+
FinishReason = Literal["stop", "length", "tool_calls", "content_filter", "other"]
|
|
31
|
+
|
|
32
|
+
DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def normalize_gemini_model_id(model_id: str) -> str:
|
|
36
|
+
"""Gemini 资源名形如 `models/gemini-2.5-flash`,统一去掉 `models/` 前缀。"""
|
|
37
|
+
prefix = "models/"
|
|
38
|
+
return model_id[len(prefix):] if model_id.startswith(prefix) else model_id
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class GeminiPlugin(LLMPlugin):
|
|
42
|
+
provider = "gemini"
|
|
43
|
+
models = models
|
|
44
|
+
|
|
45
|
+
def __init__(self, transport: Transport | None = None):
|
|
46
|
+
self._transport = transport or urllib_transport
|
|
47
|
+
|
|
48
|
+
async def chat(
|
|
49
|
+
self,
|
|
50
|
+
request: UnifiedRequest,
|
|
51
|
+
config: ProviderConfig,
|
|
52
|
+
context: ChatContext,
|
|
53
|
+
) -> UnifiedResponse:
|
|
54
|
+
runtime = resolve_provider_runtime(self.provider, config)
|
|
55
|
+
base_url = (runtime.base_url or DEFAULT_BASE_URL).rstrip("/")
|
|
56
|
+
url = f"{base_url}/models/{normalize_gemini_model_id(context.model.id)}:generateContent"
|
|
57
|
+
status, body = await self._request("POST", url, self._build_body(request), runtime, context)
|
|
58
|
+
data = self._parse_json(status, body)
|
|
59
|
+
candidate = (data.get("candidates") or [{}])[0]
|
|
60
|
+
parts = candidate.get("content", {}).get("parts", [])
|
|
61
|
+
text = "".join(part.get("text", "") for part in parts if "text" in part)
|
|
62
|
+
tool_calls = tuple(
|
|
63
|
+
UnifiedToolCall(
|
|
64
|
+
id=f"call_{index}",
|
|
65
|
+
type="function",
|
|
66
|
+
function={"name": part.get("functionCall", {}).get("name", ""), "arguments": json.dumps(part.get("functionCall", {}).get("args", {}))},
|
|
67
|
+
)
|
|
68
|
+
for index, part in enumerate(parts)
|
|
69
|
+
if "functionCall" in part
|
|
70
|
+
)
|
|
71
|
+
usage = data.get("usageMetadata") or {}
|
|
72
|
+
prompt_tokens = usage.get("promptTokenCount", 0)
|
|
73
|
+
completion_tokens = usage.get("candidatesTokenCount", 0)
|
|
74
|
+
finish_reason = self._map_finish_reason(candidate.get("finishReason"))
|
|
75
|
+
return UnifiedResponse(
|
|
76
|
+
id="",
|
|
77
|
+
model=context.model.id,
|
|
78
|
+
choices=[
|
|
79
|
+
UnifiedChoice(
|
|
80
|
+
index=0,
|
|
81
|
+
message=UnifiedMessage(
|
|
82
|
+
role="assistant",
|
|
83
|
+
content=text,
|
|
84
|
+
tool_calls=tool_calls,
|
|
85
|
+
),
|
|
86
|
+
finish_reason=finish_reason,
|
|
87
|
+
)
|
|
88
|
+
],
|
|
89
|
+
usage=UnifiedUsage(
|
|
90
|
+
prompt_tokens=prompt_tokens,
|
|
91
|
+
completion_tokens=completion_tokens,
|
|
92
|
+
total_tokens=usage.get("totalTokenCount") or prompt_tokens + completion_tokens,
|
|
93
|
+
),
|
|
94
|
+
provider_metadata=data,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
async def chat_stream(
|
|
98
|
+
self,
|
|
99
|
+
request: UnifiedRequest,
|
|
100
|
+
config: ProviderConfig,
|
|
101
|
+
context: ChatContext,
|
|
102
|
+
) -> AsyncIterator[UnifiedStreamChunk]:
|
|
103
|
+
runtime = resolve_provider_runtime(self.provider, config)
|
|
104
|
+
base_url = (runtime.base_url or DEFAULT_BASE_URL).rstrip("/")
|
|
105
|
+
url = f"{base_url}/models/{normalize_gemini_model_id(context.model.id)}:streamGenerateContent?alt=sse"
|
|
106
|
+
body = self._build_body(request)
|
|
107
|
+
status, text = await self._request("POST", url, body, runtime, context)
|
|
108
|
+
if status < 200 or status >= 300:
|
|
109
|
+
raise PolyLLMError("PROVIDER_ERROR", f"Gemini API 错误: {status} {text}", {"status": status})
|
|
110
|
+
for event_payload in parse_sse_events(text):
|
|
111
|
+
event = json.loads(event_payload)
|
|
112
|
+
candidate = (event.get("candidates") or [{}])[0]
|
|
113
|
+
parts = candidate.get("content", {}).get("parts", [])
|
|
114
|
+
for part in parts:
|
|
115
|
+
if "text" in part:
|
|
116
|
+
yield UnifiedStreamChunk(id="", model=context.model.id, delta={"content": part.get("text", "")})
|
|
117
|
+
elif "functionCall" in part:
|
|
118
|
+
yield UnifiedStreamChunk(
|
|
119
|
+
id="",
|
|
120
|
+
model=context.model.id,
|
|
121
|
+
delta={
|
|
122
|
+
"tool_calls": [
|
|
123
|
+
{
|
|
124
|
+
"id": f"call_{len(parts) - 1}",
|
|
125
|
+
"type": "function",
|
|
126
|
+
"function": {
|
|
127
|
+
"name": part.get("functionCall", {}).get("name", ""),
|
|
128
|
+
"arguments": json.dumps(part.get("functionCall", {}).get("args", {})),
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
]
|
|
132
|
+
},
|
|
133
|
+
)
|
|
134
|
+
usage = event.get("usageMetadata")
|
|
135
|
+
if usage:
|
|
136
|
+
prompt_tokens = usage.get("promptTokenCount", 0)
|
|
137
|
+
completion_tokens = usage.get("candidatesTokenCount", 0)
|
|
138
|
+
yield UnifiedStreamChunk(
|
|
139
|
+
id="",
|
|
140
|
+
model=context.model.id,
|
|
141
|
+
delta={},
|
|
142
|
+
usage=UnifiedUsage(
|
|
143
|
+
prompt_tokens=prompt_tokens,
|
|
144
|
+
completion_tokens=completion_tokens,
|
|
145
|
+
total_tokens=usage.get("totalTokenCount") or prompt_tokens + completion_tokens,
|
|
146
|
+
),
|
|
147
|
+
)
|
|
148
|
+
if candidate.get("finishReason"):
|
|
149
|
+
yield UnifiedStreamChunk(
|
|
150
|
+
id="",
|
|
151
|
+
model=context.model.id,
|
|
152
|
+
delta={},
|
|
153
|
+
finish_reason=self._map_finish_reason(candidate.get("finishReason")),
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
async def list_models(self, config: ProviderConfig) -> list[ProviderModelInfo]:
|
|
157
|
+
runtime = resolve_provider_runtime(self.provider, config)
|
|
158
|
+
base_url = (runtime.base_url or DEFAULT_BASE_URL).rstrip("/")
|
|
159
|
+
url = f"{base_url}/models"
|
|
160
|
+
status, body = await self._request("GET", url, None, runtime, None)
|
|
161
|
+
data = self._parse_json(status, body)
|
|
162
|
+
return [
|
|
163
|
+
ProviderModelInfo(
|
|
164
|
+
id=normalize_gemini_model_id(item.get("name") or ""),
|
|
165
|
+
display_name=item.get("displayName"),
|
|
166
|
+
)
|
|
167
|
+
for item in data.get("models", [])
|
|
168
|
+
]
|
|
169
|
+
|
|
170
|
+
def create_model_spec(self, model_id: str) -> ModelSpec:
|
|
171
|
+
return ModelSpec(
|
|
172
|
+
id=model_id,
|
|
173
|
+
provider=self.provider,
|
|
174
|
+
capabilities=ModelCapabilities(
|
|
175
|
+
streaming=True,
|
|
176
|
+
vision=False,
|
|
177
|
+
function_calling=True,
|
|
178
|
+
json_mode=False,
|
|
179
|
+
system_prompt=True,
|
|
180
|
+
),
|
|
181
|
+
context_window=1048576,
|
|
182
|
+
max_output=8192,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
def _build_body(self, request: UnifiedRequest) -> dict[str, Any]:
|
|
186
|
+
system_parts: list[dict[str, Any]] = []
|
|
187
|
+
contents: list[dict[str, Any]] = []
|
|
188
|
+
for message in request.messages:
|
|
189
|
+
if message.role == "system":
|
|
190
|
+
system_parts.append({"text": message.content if isinstance(message.content, str) else ""})
|
|
191
|
+
continue
|
|
192
|
+
if message.role == "tool":
|
|
193
|
+
contents.append(
|
|
194
|
+
{
|
|
195
|
+
"role": "user",
|
|
196
|
+
"parts": [
|
|
197
|
+
{
|
|
198
|
+
"functionResponse": {
|
|
199
|
+
"name": message.name or "",
|
|
200
|
+
"response": json.loads(message.content or "{}")
|
|
201
|
+
if isinstance(message.content, str)
|
|
202
|
+
else {},
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
],
|
|
206
|
+
}
|
|
207
|
+
)
|
|
208
|
+
continue
|
|
209
|
+
parts: list[dict[str, Any]] = []
|
|
210
|
+
if isinstance(message.content, str):
|
|
211
|
+
if message.content:
|
|
212
|
+
parts.append({"text": message.content})
|
|
213
|
+
else:
|
|
214
|
+
for part in message.content:
|
|
215
|
+
if part.type == "text":
|
|
216
|
+
parts.append({"text": part.text or ""})
|
|
217
|
+
elif part.type == "image_url":
|
|
218
|
+
raise PolyLLMError(
|
|
219
|
+
"UNSUPPORTED_PARAMETER",
|
|
220
|
+
"Gemini 插件暂不支持远程图片 URL,请直接提供 base64 内容或改用其他供应商",
|
|
221
|
+
{"provider": self.provider},
|
|
222
|
+
)
|
|
223
|
+
if message.tool_calls:
|
|
224
|
+
for call in message.tool_calls:
|
|
225
|
+
parts.append(
|
|
226
|
+
{"functionCall": {"name": call.function.get("name", ""), "args": json.loads(call.function.get("arguments", "{}"))}}
|
|
227
|
+
)
|
|
228
|
+
if not parts:
|
|
229
|
+
parts.append({"text": ""})
|
|
230
|
+
contents.append({"role": "model" if message.role == "assistant" else "user", "parts": parts})
|
|
231
|
+
|
|
232
|
+
body: dict[str, Any] = {"contents": contents}
|
|
233
|
+
if system_parts:
|
|
234
|
+
body["systemInstruction"] = {"parts": system_parts}
|
|
235
|
+
if request.temperature is not None:
|
|
236
|
+
body["temperature"] = request.temperature
|
|
237
|
+
if request.top_p is not None:
|
|
238
|
+
body["topP"] = request.top_p
|
|
239
|
+
if request.max_tokens is not None:
|
|
240
|
+
body["maxOutputTokens"] = request.max_tokens
|
|
241
|
+
if request.tools:
|
|
242
|
+
body["tools"] = [
|
|
243
|
+
{
|
|
244
|
+
"functionDeclarations": [
|
|
245
|
+
{
|
|
246
|
+
"name": tool.function.get("name", ""),
|
|
247
|
+
"description": tool.function.get("description"),
|
|
248
|
+
"parameters": tool.function.get("parameters", {}),
|
|
249
|
+
}
|
|
250
|
+
for tool in request.tools
|
|
251
|
+
]
|
|
252
|
+
}
|
|
253
|
+
]
|
|
254
|
+
return body
|
|
255
|
+
|
|
256
|
+
@staticmethod
|
|
257
|
+
def _map_finish_reason(reason: Any) -> FinishReason:
|
|
258
|
+
if reason == "MAX_TOKENS":
|
|
259
|
+
return "length"
|
|
260
|
+
if reason in {"SAFETY", "PROHIBITED_CONTENT"}:
|
|
261
|
+
return "content_filter"
|
|
262
|
+
return "stop"
|
|
263
|
+
|
|
264
|
+
@staticmethod
|
|
265
|
+
def _parse_json(status: int, body: str) -> dict[str, Any]:
|
|
266
|
+
try:
|
|
267
|
+
data = cast(dict[str, Any], json.loads(body))
|
|
268
|
+
except json.JSONDecodeError as error:
|
|
269
|
+
raise PolyLLMError("PROVIDER_ERROR", f"Gemini 返回无效 JSON: {body[:200]}", {"status": status}) from error
|
|
270
|
+
if status < 200 or status >= 300:
|
|
271
|
+
raise PolyLLMError(
|
|
272
|
+
"PROVIDER_ERROR",
|
|
273
|
+
f"Gemini API 错误: {status} {body}",
|
|
274
|
+
{"status": status, "body": data},
|
|
275
|
+
)
|
|
276
|
+
return data
|
|
277
|
+
|
|
278
|
+
async def _request(
|
|
279
|
+
self,
|
|
280
|
+
method: str,
|
|
281
|
+
url: str,
|
|
282
|
+
payload: dict[str, Any] | None,
|
|
283
|
+
runtime: Any,
|
|
284
|
+
context: ChatContext | None,
|
|
285
|
+
) -> tuple[int, str]:
|
|
286
|
+
headers = {"x-goog-api-key": runtime.api_key, **runtime.headers}
|
|
287
|
+
if payload is not None:
|
|
288
|
+
headers["Content-Type"] = "application/json"
|
|
289
|
+
timeout_ms = context.timeout_ms if context is not None and context.timeout_ms is not None else 120000
|
|
290
|
+
return await asyncio.to_thread(self._transport, method, url, headers, payload, timeout_ms)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def urllib_transport(
|
|
294
|
+
method: str,
|
|
295
|
+
url: str,
|
|
296
|
+
headers: dict[str, str],
|
|
297
|
+
payload: dict[str, Any] | None,
|
|
298
|
+
timeout_ms: int,
|
|
299
|
+
) -> tuple[int, str]:
|
|
300
|
+
data = json.dumps(payload).encode("utf-8") if payload is not None else None
|
|
301
|
+
request = Request(url, data=data, headers=headers, method=method)
|
|
302
|
+
try:
|
|
303
|
+
with urlopen(request, timeout=timeout_ms / 1000) as response:
|
|
304
|
+
return response.status, response.read().decode("utf-8")
|
|
305
|
+
except HTTPError as error:
|
|
306
|
+
return error.code, error.read().decode("utf-8")
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def parse_sse_events(text: str) -> list[str]:
|
|
310
|
+
"""解析 Gemini `?alt=sse` 的 Server-Sent Events 响应体。
|
|
311
|
+
|
|
312
|
+
支持 `data:` 前缀、CRLF/LF、以及末尾没有换行的残留事件;
|
|
313
|
+
空事件(SSE 规范里的 `\\n\\n` 分隔)返回空列表项,由调用方跳过。
|
|
314
|
+
"""
|
|
315
|
+
events: list[str] = []
|
|
316
|
+
for raw_line in text.replace("\r\n", "\n").split("\n"):
|
|
317
|
+
line = raw_line.strip()
|
|
318
|
+
if not line.startswith("data:"):
|
|
319
|
+
continue
|
|
320
|
+
value = line.removeprefix("data:").strip()
|
|
321
|
+
if value:
|
|
322
|
+
events.append(value)
|
|
323
|
+
return events
|
|
File without changes
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from polyllm_core.types import ModelCapabilities, ModelSpec, NumericParamRange
|
|
2
|
+
|
|
3
|
+
capabilities = ModelCapabilities(
|
|
4
|
+
streaming=True,
|
|
5
|
+
vision=False,
|
|
6
|
+
function_calling=True,
|
|
7
|
+
json_mode=False,
|
|
8
|
+
system_prompt=True,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
models = (
|
|
12
|
+
ModelSpec(
|
|
13
|
+
id="gemini-2.5-flash",
|
|
14
|
+
provider="gemini",
|
|
15
|
+
aliases=("gemini-flash", "flash"),
|
|
16
|
+
snapshot="gemini-2.5-flash",
|
|
17
|
+
is_latest=True,
|
|
18
|
+
capabilities=capabilities,
|
|
19
|
+
params={
|
|
20
|
+
"temperature": NumericParamRange(0, 2, 1),
|
|
21
|
+
"max_tokens": NumericParamRange(1, 65536, 8192, field="maxOutputTokens"),
|
|
22
|
+
"top_p": NumericParamRange(0, 1, 1),
|
|
23
|
+
},
|
|
24
|
+
context_window=1048576,
|
|
25
|
+
max_output=65536,
|
|
26
|
+
),
|
|
27
|
+
ModelSpec(
|
|
28
|
+
id="gemini-2.5-pro",
|
|
29
|
+
provider="gemini",
|
|
30
|
+
aliases=("gemini-pro", "pro"),
|
|
31
|
+
snapshot="gemini-2.5-pro",
|
|
32
|
+
is_latest=True,
|
|
33
|
+
capabilities=capabilities,
|
|
34
|
+
params={
|
|
35
|
+
"temperature": NumericParamRange(0, 2, 1),
|
|
36
|
+
"max_tokens": NumericParamRange(1, 65536, 8192, field="maxOutputTokens"),
|
|
37
|
+
"top_p": NumericParamRange(0, 1, 1),
|
|
38
|
+
},
|
|
39
|
+
context_window=1048576,
|
|
40
|
+
max_output=65536,
|
|
41
|
+
),
|
|
42
|
+
)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: polyllm-gemini
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Google Gemini plugin for PolyLLM.
|
|
5
|
+
Author: PolyLLM contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://polyllm.raingpt.top
|
|
8
|
+
Project-URL: Repository, https://github.com/you-want/PolyLLM
|
|
9
|
+
Project-URL: Issues, https://github.com/you-want/PolyLLM/issues
|
|
10
|
+
Keywords: llm,polyllm,gemini,google
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: polyllm-core>=0.3.0
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# PolyLLM OpenAI Plugin for Python
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from polyllm_core import create_llm
|
|
31
|
+
from polyllm_openai import OpenAIPlugin
|
|
32
|
+
|
|
33
|
+
llm = create_llm(plugins=[OpenAIPlugin()], providers={"openai": {"api_key": "..."}})
|
|
34
|
+
response = await llm.chat("openai:gpt-4o-mini", messages=[{"role": "user", "content": "你好"}])
|
|
35
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/polyllm_gemini/__init__.py
|
|
5
|
+
src/polyllm_gemini/plugin.py
|
|
6
|
+
src/polyllm_gemini/py.typed
|
|
7
|
+
src/polyllm_gemini/specs.py
|
|
8
|
+
src/polyllm_gemini.egg-info/PKG-INFO
|
|
9
|
+
src/polyllm_gemini.egg-info/SOURCES.txt
|
|
10
|
+
src/polyllm_gemini.egg-info/dependency_links.txt
|
|
11
|
+
src/polyllm_gemini.egg-info/requires.txt
|
|
12
|
+
src/polyllm_gemini.egg-info/top_level.txt
|
|
13
|
+
tests/test_gemini.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
polyllm-core>=0.3.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
polyllm_gemini
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import json
|
|
3
|
+
import unittest
|
|
4
|
+
|
|
5
|
+
from polyllm_core.types import ChatContext, ProviderConfig, UnifiedContentPart, UnifiedMessage, UnifiedRequest
|
|
6
|
+
from polyllm_gemini import GeminiPlugin
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class GeminiTests(unittest.TestCase):
|
|
10
|
+
def test_chat_wire_and_response(self):
|
|
11
|
+
calls = []
|
|
12
|
+
|
|
13
|
+
def transport(method, url, headers, payload, timeout):
|
|
14
|
+
calls.append((method, url, headers, payload, timeout))
|
|
15
|
+
return 200, json.dumps(
|
|
16
|
+
{
|
|
17
|
+
"candidates": [
|
|
18
|
+
{
|
|
19
|
+
"content": {"parts": [{"text": "你好!"}]},
|
|
20
|
+
"finishReason": "STOP",
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
"usageMetadata": {
|
|
24
|
+
"promptTokenCount": 3,
|
|
25
|
+
"candidatesTokenCount": 4,
|
|
26
|
+
"totalTokenCount": 7,
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
plugin = GeminiPlugin(transport)
|
|
32
|
+
context = ChatContext(model=plugin.models[0])
|
|
33
|
+
response = asyncio.run(
|
|
34
|
+
plugin.chat(
|
|
35
|
+
UnifiedRequest(
|
|
36
|
+
model="gemini-2.5-flash",
|
|
37
|
+
messages=[
|
|
38
|
+
UnifiedMessage(role="system", content="你是助手"),
|
|
39
|
+
UnifiedMessage(role="user", content="你好"),
|
|
40
|
+
],
|
|
41
|
+
temperature=0.5,
|
|
42
|
+
max_tokens=512,
|
|
43
|
+
),
|
|
44
|
+
ProviderConfig(api_key="k"),
|
|
45
|
+
context,
|
|
46
|
+
)
|
|
47
|
+
)
|
|
48
|
+
self.assertEqual(response.choices[0].message.content, "你好!")
|
|
49
|
+
self.assertEqual(response.usage.total_tokens, 7)
|
|
50
|
+
self.assertEqual(response.choices[0].finish_reason, "stop")
|
|
51
|
+
self.assertEqual(calls[0][0], "POST")
|
|
52
|
+
body = calls[0][3]
|
|
53
|
+
self.assertEqual(body["systemInstruction"], {"parts": [{"text": "你是助手"}]})
|
|
54
|
+
self.assertEqual(body["maxOutputTokens"], 512)
|
|
55
|
+
self.assertTrue(calls[0][1].endswith(":generateContent"))
|
|
56
|
+
|
|
57
|
+
def test_list_models_uses_get_and_strips_resource_prefix(self):
|
|
58
|
+
def transport(method, url, headers, payload, timeout):
|
|
59
|
+
self.assertEqual(method, "GET")
|
|
60
|
+
self.assertTrue(url.endswith("/models"))
|
|
61
|
+
return 200, json.dumps({"models": [{"name": "models/gemini-2.5-flash", "displayName": "Gemini 2.5 Flash"}]})
|
|
62
|
+
|
|
63
|
+
models = asyncio.run(GeminiPlugin(transport).list_models(ProviderConfig(api_key="k")))
|
|
64
|
+
self.assertEqual(models[0].id, "gemini-2.5-flash")
|
|
65
|
+
|
|
66
|
+
def test_chat_rejects_remote_image_url(self):
|
|
67
|
+
def transport(method, url, headers, payload, timeout):
|
|
68
|
+
return 200, "{}"
|
|
69
|
+
|
|
70
|
+
plugin = GeminiPlugin(transport)
|
|
71
|
+
request = UnifiedRequest(
|
|
72
|
+
model="gemini-2.5-flash",
|
|
73
|
+
messages=[
|
|
74
|
+
UnifiedMessage(
|
|
75
|
+
role="user",
|
|
76
|
+
content=[UnifiedContentPart(type="image_url", image_url={"url": "https://example.com/a.png"})],
|
|
77
|
+
),
|
|
78
|
+
],
|
|
79
|
+
)
|
|
80
|
+
with self.assertRaises(Exception) as caught:
|
|
81
|
+
asyncio.run(
|
|
82
|
+
plugin.chat(request, ProviderConfig(api_key="k"), ChatContext(model=plugin.models[0]))
|
|
83
|
+
)
|
|
84
|
+
self.assertIn("UNSUPPORTED_PARAMETER", getattr(caught.exception, "code", str(caught.exception)))
|
|
85
|
+
|
|
86
|
+
def test_dynamic_model_disables_vision_until_supported(self):
|
|
87
|
+
spec = GeminiPlugin().create_model_spec("custom-gemini")
|
|
88
|
+
self.assertFalse(spec.capabilities.vision)
|
|
89
|
+
|
|
90
|
+
def test_stream_parses_sse(self):
|
|
91
|
+
first = json.dumps({"candidates": [{"content": {"parts": [{"text": "好"}]}}]})
|
|
92
|
+
last = json.dumps({
|
|
93
|
+
"candidates": [{"content": {"parts": [{"text": "!"}]}, "finishReason": "STOP"}],
|
|
94
|
+
"usageMetadata": {"promptTokenCount": 1, "candidatesTokenCount": 2, "totalTokenCount": 3},
|
|
95
|
+
})
|
|
96
|
+
sse = f"data: {first}\n\ndata: {last}\n\n"
|
|
97
|
+
|
|
98
|
+
async def run():
|
|
99
|
+
chunks = []
|
|
100
|
+
plugin = GeminiPlugin(lambda *args: (200, sse))
|
|
101
|
+
async for chunk in plugin.chat_stream(
|
|
102
|
+
UnifiedRequest(model="gemini-2.5-flash", messages=[UnifiedMessage(role="user", content="hi")], stream=True),
|
|
103
|
+
ProviderConfig(api_key="k"),
|
|
104
|
+
ChatContext(model=plugin.models[0]),
|
|
105
|
+
):
|
|
106
|
+
chunks.append(chunk)
|
|
107
|
+
return chunks
|
|
108
|
+
|
|
109
|
+
chunks = asyncio.run(run())
|
|
110
|
+
self.assertEqual([chunk.delta.get("content") for chunk in chunks if chunk.delta.get("content") is not None], ["好", "!"])
|
|
111
|
+
self.assertEqual(chunks[-1].finish_reason, "stop")
|
|
112
|
+
self.assertTrue(any(chunk.usage is not None for chunk in chunks))
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
if __name__ == "__main__":
|
|
116
|
+
unittest.main()
|