polyllm-openai 0.1.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.
@@ -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-openai
3
+ Version: 0.1.0
4
+ Summary: OpenAI 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,openai
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.1.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-openai"
7
+ version = "0.1.0"
8
+ description = "OpenAI plugin for PolyLLM."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ dependencies = ["polyllm-core>=0.1.0"]
12
+ license = "MIT"
13
+ license-files = ["LICENSE"]
14
+ authors = [{ name = "PolyLLM contributors" }]
15
+ keywords = ["llm", "polyllm", "openai"]
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_openai = ["py.typed"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,4 @@
1
+ from .plugin import OpenAIPlugin, parse_sse
2
+ from .specs import models
3
+
4
+ __all__ = ["OpenAIPlugin", "models", "parse_sse"]
@@ -0,0 +1,212 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import json
5
+ from typing import Any, AsyncIterator, Callable, Mapping, 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
+ FinishReason,
14
+ LLMPlugin,
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 capabilities, models
28
+
29
+ Transport = Callable[[str, str, dict[str, str], dict[str, Any] | None, int], tuple[int, str]]
30
+
31
+
32
+ class OpenAIPlugin(LLMPlugin):
33
+ provider = "openai"
34
+ models = models
35
+
36
+ def __init__(self, transport: Transport | None = None):
37
+ self._transport = transport or urllib_transport
38
+
39
+ async def chat(
40
+ self,
41
+ request: UnifiedRequest,
42
+ config: ProviderConfig,
43
+ context: ChatContext,
44
+ ) -> UnifiedResponse:
45
+ status, body = await self._request("POST", "/chat/completions", self._build_body(request), config, context)
46
+ data = self._parse_json(status, body)
47
+ choice = data["choices"][0]
48
+ message = choice["message"]
49
+ return UnifiedResponse(
50
+ id=data["id"],
51
+ model=data["model"],
52
+ choices=[
53
+ UnifiedChoice(
54
+ index=choice["index"],
55
+ message=UnifiedMessage(
56
+ role="assistant",
57
+ content=message.get("content") or "",
58
+ tool_calls=tuple(
59
+ UnifiedToolCall(
60
+ id=call["id"],
61
+ type="function",
62
+ function=dict(call["function"]),
63
+ )
64
+ for call in message.get("tool_calls", [])
65
+ ),
66
+ ),
67
+ finish_reason=choice.get("finish_reason", "other"),
68
+ )
69
+ ],
70
+ usage=self._parse_usage(data.get("usage", {})),
71
+ provider_metadata=data.get("metadata"),
72
+ )
73
+
74
+ async def chat_stream(
75
+ self,
76
+ request: UnifiedRequest,
77
+ config: ProviderConfig,
78
+ context: ChatContext,
79
+ ) -> AsyncIterator[UnifiedStreamChunk]:
80
+ body = self._build_body(request)
81
+ body["stream"] = True
82
+ status, text = await self._request("POST", "/chat/completions", body, config, context)
83
+ if status < 200 or status >= 300:
84
+ raise PolyLLMError("PROVIDER_ERROR", f"OpenAI API 错误: {status} {text}", {"status": status})
85
+ for event in parse_sse(text):
86
+ if event.get("object") != "chat.completion.chunk":
87
+ continue
88
+ yield UnifiedStreamChunk(
89
+ id=event["id"],
90
+ model=event["model"],
91
+ delta=dict(event.get("delta", {})),
92
+ finish_reason=event.get("finish_reason"),
93
+ )
94
+
95
+ async def list_models(self, config: ProviderConfig) -> list[ProviderModelInfo]:
96
+ status, body = await self._request("GET", "/models", None, config, ChatContext(model=self.models[0]))
97
+ data = self._parse_json(status, body)
98
+ return [
99
+ ProviderModelInfo(
100
+ id=item["id"],
101
+ display_name=item.get("display_name"),
102
+ owned_by=item.get("owned_by"),
103
+ created_at=item.get("created"),
104
+ metadata=item.get("metadata"),
105
+ )
106
+ for item in data.get("data", [])
107
+ ]
108
+
109
+ def create_model_spec(self, model_id: str) -> ModelSpec:
110
+ return ModelSpec(
111
+ id=model_id,
112
+ provider=self.provider,
113
+ capabilities=capabilities,
114
+ context_window=128000,
115
+ max_output=16384,
116
+ )
117
+
118
+ def _build_body(self, request: UnifiedRequest) -> dict[str, Any]:
119
+ body: dict[str, Any] = {
120
+ "model": request.model,
121
+ "messages": [self._serialize_message(message) for message in request.messages],
122
+ }
123
+ if request.temperature is not None:
124
+ body["temperature"] = request.temperature
125
+ if request.max_tokens is not None:
126
+ body["max_tokens"] = request.max_tokens
127
+ if request.top_p is not None:
128
+ body["top_p"] = request.top_p
129
+ if request.json_mode:
130
+ body["response_format"] = {"type": "json_object"}
131
+ if request.tools:
132
+ body["tools"] = [{"type": "function", "function": dict(tool.function)} for tool in request.tools]
133
+ return body
134
+
135
+ @staticmethod
136
+ def _serialize_message(message: UnifiedMessage) -> dict[str, Any]:
137
+ result: dict[str, Any] = {"role": message.role, "content": message.content}
138
+ if message.name:
139
+ result["name"] = message.name
140
+ if message.tool_call_id:
141
+ result["tool_call_id"] = message.tool_call_id
142
+ if message.tool_calls:
143
+ result["tool_calls"] = [dict(call.__dict__) for call in message.tool_calls]
144
+ return result
145
+
146
+ async def _request(
147
+ self,
148
+ method: str,
149
+ path: str,
150
+ payload: dict[str, Any] | None,
151
+ config: ProviderConfig,
152
+ context: ChatContext | None,
153
+ ) -> tuple[int, str]:
154
+ runtime = resolve_provider_runtime(self.provider, config)
155
+ base_url = (runtime.base_url or "https://api.openai.com/v1").rstrip("/")
156
+ headers = {"Authorization": f"Bearer {runtime.api_key}", **runtime.headers}
157
+ if payload is not None:
158
+ headers["Content-Type"] = "application/json"
159
+ return await asyncio.to_thread(
160
+ self._transport,
161
+ method,
162
+ f"{base_url}{path}",
163
+ headers,
164
+ payload,
165
+ context.timeout_ms if context is not None and context.timeout_ms is not None else 120000,
166
+ )
167
+
168
+ @staticmethod
169
+ def _parse_usage(data: Mapping[str, Any]) -> UnifiedUsage:
170
+ return UnifiedUsage(
171
+ prompt_tokens=data.get("prompt_tokens", 0),
172
+ completion_tokens=data.get("completion_tokens", 0),
173
+ total_tokens=data.get("total_tokens", 0),
174
+ )
175
+
176
+ @staticmethod
177
+ def _parse_json(status: int, body: str) -> dict[str, Any]:
178
+ try:
179
+ data = cast(dict[str, Any], json.loads(body))
180
+ except json.JSONDecodeError as error:
181
+ raise PolyLLMError("PROVIDER_ERROR", f"OpenAI 返回无效 JSON: {body[:200]}", {"status": status}) from error
182
+ if status < 200 or status >= 300:
183
+ raise PolyLLMError("PROVIDER_ERROR", f"OpenAI API 错误: {status} {body}", {"status": status, "body": data})
184
+ return data
185
+
186
+
187
+ def urllib_transport(
188
+ method: str,
189
+ url: str,
190
+ headers: dict[str, str],
191
+ payload: dict[str, Any] | None,
192
+ timeout_ms: int,
193
+ ) -> tuple[int, str]:
194
+ data = json.dumps(payload).encode("utf-8") if payload is not None else None
195
+ request = Request(url, data=data, headers=headers, method=method)
196
+ try:
197
+ with urlopen(request, timeout=timeout_ms / 1000) as response:
198
+ return response.status, response.read().decode("utf-8")
199
+ except HTTPError as error:
200
+ return error.code, error.read().decode("utf-8")
201
+
202
+
203
+ def parse_sse(text: str) -> list[dict[str, Any]]:
204
+ events: list[dict[str, Any]] = []
205
+ for line in text.splitlines():
206
+ if not line.startswith("data:"):
207
+ continue
208
+ value = line.removeprefix("data:").strip()
209
+ if not value or value == "[DONE]":
210
+ continue
211
+ events.append(json.loads(value))
212
+ return events
File without changes
@@ -0,0 +1,39 @@
1
+ from polyllm_core.types import ModelCapabilities, ModelSpec, NumericParamRange
2
+
3
+ capabilities = ModelCapabilities(
4
+ streaming=True,
5
+ vision=True,
6
+ function_calling=True,
7
+ json_mode=True,
8
+ system_prompt=True,
9
+ )
10
+
11
+ models = (
12
+ ModelSpec(
13
+ id="gpt-4o-mini",
14
+ provider="openai",
15
+ aliases=("gpt4o-mini",),
16
+ capabilities=capabilities,
17
+ params={
18
+ "temperature": NumericParamRange(0, 2, 1),
19
+ "max_tokens": NumericParamRange(1, 16384, 4096, field="max_tokens"),
20
+ "top_p": NumericParamRange(0, 1, 1),
21
+ },
22
+ context_window=128000,
23
+ max_output=16384,
24
+ ),
25
+ ModelSpec(
26
+ id="gpt-4o",
27
+ provider="openai",
28
+ aliases=("gpt4o",),
29
+ snapshot="gpt-4o-2024-11-20",
30
+ capabilities=capabilities,
31
+ params={
32
+ "temperature": NumericParamRange(0, 2, 1),
33
+ "max_tokens": NumericParamRange(1, 16384, 4096, field="max_tokens"),
34
+ "top_p": NumericParamRange(0, 1, 1),
35
+ },
36
+ context_window=128000,
37
+ max_output=16384,
38
+ ),
39
+ )
@@ -0,0 +1,35 @@
1
+ Metadata-Version: 2.4
2
+ Name: polyllm-openai
3
+ Version: 0.1.0
4
+ Summary: OpenAI 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,openai
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.1.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_openai/__init__.py
5
+ src/polyllm_openai/plugin.py
6
+ src/polyllm_openai/py.typed
7
+ src/polyllm_openai/specs.py
8
+ src/polyllm_openai.egg-info/PKG-INFO
9
+ src/polyllm_openai.egg-info/SOURCES.txt
10
+ src/polyllm_openai.egg-info/dependency_links.txt
11
+ src/polyllm_openai.egg-info/requires.txt
12
+ src/polyllm_openai.egg-info/top_level.txt
13
+ tests/test_openai.py
@@ -0,0 +1 @@
1
+ polyllm-core>=0.1.0
@@ -0,0 +1 @@
1
+ polyllm_openai
@@ -0,0 +1,77 @@
1
+ import asyncio
2
+ import json
3
+ import unittest
4
+
5
+ from polyllm_core.types import ProviderConfig, UnifiedMessage, UnifiedRequest
6
+ from polyllm_openai import OpenAIPlugin
7
+
8
+
9
+ class OpenAITests(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
+ "id": "resp",
18
+ "model": "gpt-4o-mini",
19
+ "choices": [
20
+ {"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}
21
+ ],
22
+ "usage": {"prompt_tokens": 2, "completion_tokens": 3, "total_tokens": 5},
23
+ }
24
+ )
25
+
26
+ plugin = OpenAIPlugin(transport)
27
+ response = asyncio.run(
28
+ plugin.chat(
29
+ UnifiedRequest(
30
+ model="gpt-4o-mini",
31
+ messages=[UnifiedMessage(role="user", content="hi")],
32
+ temperature=0.2,
33
+ max_tokens=128,
34
+ ),
35
+ ProviderConfig(api_key="k"),
36
+ None,
37
+ )
38
+ )
39
+ self.assertEqual(response.choices[0].message.content, "ok")
40
+ self.assertEqual(calls[0][3]["max_tokens"], 128)
41
+ self.assertTrue(calls[0][2]["Authorization"].startswith("Bearer "))
42
+
43
+ def test_list_models(self):
44
+ def transport(method, url, headers, payload, timeout):
45
+ self.assertEqual(method, "GET")
46
+ self.assertTrue(url.endswith("/models"))
47
+ return 200, json.dumps({"data": [{"id": "gpt-4o-mini", "owned_by": "openai"}]})
48
+
49
+ models = asyncio.run(OpenAIPlugin(transport).list_models(ProviderConfig(api_key="k")))
50
+ self.assertEqual(models[0].id, "gpt-4o-mini")
51
+
52
+ def test_stream_parses_sse(self):
53
+ sse = (
54
+ 'data: {"id":"r","model":"gpt-4o-mini","object":"chat.completion.chunk",'
55
+ '"delta":{"role":"assistant","content":"he"}}\n\n'
56
+ 'data: {"id":"r","model":"gpt-4o-mini","object":"chat.completion.chunk",'
57
+ '"delta":{"content":"llo"},"finish_reason":"stop"}\n\n'
58
+ "data: [DONE]\n\n"
59
+ )
60
+
61
+ async def run():
62
+ chunks = []
63
+ async for chunk in OpenAIPlugin(lambda *args: (200, sse)).chat_stream(
64
+ UnifiedRequest(model="gpt-4o-mini", messages=[UnifiedMessage(role="user", content="hi")], stream=True),
65
+ ProviderConfig(api_key="k"),
66
+ None,
67
+ ):
68
+ chunks.append(chunk)
69
+ return chunks
70
+
71
+ chunks = asyncio.run(run())
72
+ self.assertEqual([chunk.delta.get("content") for chunk in chunks], ["he", "llo"])
73
+ self.assertEqual(chunks[-1].finish_reason, "stop")
74
+
75
+
76
+ if __name__ == "__main__":
77
+ unittest.main()