@godspeedai/cognate-provider-openai-compatible 0.1.1 → 0.1.2
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.
- package/README.md +99 -17
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,19 +1,101 @@
|
|
|
1
1
|
# @godspeedai/cognate-provider-openai-compatible
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
(
|
|
18
|
-
|
|
19
|
-
|
|
3
|
+
A Cognate model provider for any OpenAI-compatible chat completions endpoint — llama.cpp,
|
|
4
|
+
vLLM, Ollama's `/v1`, LM Studio, or hosted APIs — over plain `fetch`. No vendor SDK.
|
|
5
|
+
|
|
6
|
+
**When to use this package:** your app needs a real model and the server speaks the
|
|
7
|
+
OpenAI `/chat/completions` wire format. For deterministic scripted answers in tests, use
|
|
8
|
+
[`@godspeedai/cognate-provider-local`](https://www.npmjs.com/package/@godspeedai/cognate-provider-local)
|
|
9
|
+
instead.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
bun add @godspeedai/cognate-provider-openai-compatible
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Requires [Bun](https://bun.sh) >= 1.4.0 (it uses the platform `fetch`, `AbortSignal`, and web
|
|
18
|
+
streams). The SDK ships Bun-native TypeScript source, so add `@types/bun` and `@types/node`
|
|
19
|
+
as dev dependencies when typechecking your project.
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { openAICompatible } from "@godspeedai/cognate-provider-openai-compatible";
|
|
25
|
+
|
|
26
|
+
const provider = openAICompatible({
|
|
27
|
+
id: "gpt-4o-mini", // provider id becomes `openai-compatible:gpt-4o-mini`
|
|
28
|
+
baseURL: process.env.OPENAI_BASE_URL ?? "https://api.openai.com/v1",
|
|
29
|
+
model: "gpt-4o-mini",
|
|
30
|
+
apiKey: () => process.env.OPENAI_API_KEY ?? "", // read per request, never stored
|
|
31
|
+
timeoutMs: 60_000,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const response = await provider.generate({
|
|
35
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
36
|
+
});
|
|
37
|
+
console.log(response.message.content);
|
|
38
|
+
console.log(response.usage); // { inputTokens, outputTokens } when the server reports usage
|
|
39
|
+
|
|
40
|
+
// Streaming: text deltas and tool-call deltas, then a final `done` delta
|
|
41
|
+
// carrying the assembled full response.
|
|
42
|
+
for await (const delta of provider.stream({ messages: [{ role: "user", content: "Hello!" }] })) {
|
|
43
|
+
if (delta.type === "text") process.stdout.write(delta.text);
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Point `baseURL` at any compatible server, for example `http://localhost:11434/v1` for Ollama
|
|
48
|
+
or your llama.cpp/vLLM route. The provider posts to `<baseURL>/chat/completions`.
|
|
49
|
+
|
|
50
|
+
## What is mapped
|
|
51
|
+
|
|
52
|
+
- **Messages and tools** become the OpenAI wire shape: tools as
|
|
53
|
+
`{ type: "function", function: { name, description, parameters } }`, assistant tool calls
|
|
54
|
+
with JSON-stringified arguments, `responseFormat.schema` as `json_schema` (or `json_object`
|
|
55
|
+
without a schema), plus `max_tokens` and `temperature`.
|
|
56
|
+
- **Streaming** parses SSE `data:` lines (tolerant of chunk boundaries), accumulates tool
|
|
57
|
+
calls whose id, name, and arguments arrive split across chunks, and yields ordered deltas
|
|
58
|
+
followed by `tool_call` entries and a `done` delta with the assembled response.
|
|
59
|
+
- **Errors** become typed `ModelProviderError` kinds: `unauthorized` (401/403),
|
|
60
|
+
`rate_limited` (429), `invalid_request` (other 4xx), `unavailable` (5xx, network errors,
|
|
61
|
+
timeouts), `cancelled` (caller abort), and `malformed_response` (unparseable bodies).
|
|
62
|
+
|
|
63
|
+
The `apiKey` function is called per request and its value is never stored, logged, or included
|
|
64
|
+
in an error message or in the capability contract the provider publishes.
|
|
65
|
+
|
|
66
|
+
## Kernel component
|
|
67
|
+
|
|
68
|
+
`modelComponent(provider)` wraps a provider as a kernel `Component` publishing the
|
|
69
|
+
`model.generate` capability, so a runtime can select and invoke it like any other capability:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { modelComponent } from "@godspeedai/cognate-provider-openai-compatible";
|
|
73
|
+
|
|
74
|
+
const component = modelComponent(provider);
|
|
75
|
+
// pass it in the runtime's `components` list
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Limitations
|
|
79
|
+
|
|
80
|
+
- Chat completions only: no embeddings, files, audio, or assistant-state APIs.
|
|
81
|
+
- Feature flags default to `stream`/`tool_use`/`structured_output` `true`,
|
|
82
|
+
`multimodal_input` `false`, `context_limit` `8192`. Override `features` to match what your
|
|
83
|
+
server actually supports, or eligibility checks will trust flags the server may not honor.
|
|
84
|
+
- Structured output depends on the server honoring `response_format`; the provider delivers
|
|
85
|
+
whatever the server returns.
|
|
86
|
+
- Tool call arguments must be valid JSON on the wire; a server that streams malformed
|
|
87
|
+
arguments surfaces `malformed_response`.
|
|
88
|
+
|
|
89
|
+
## Related packages
|
|
90
|
+
|
|
91
|
+
- [`@godspeedai/cognate-capabilities`](https://www.npmjs.com/package/@godspeedai/cognate-capabilities) —
|
|
92
|
+
the `ModelProvider` contract, `ModelProviderError`, and selection engine.
|
|
93
|
+
- [`@godspeedai/cognate-provider-local`](https://www.npmjs.com/package/@godspeedai/cognate-provider-local) —
|
|
94
|
+
deterministic scripted provider for tests and offline use.
|
|
95
|
+
- [`@godspeedai/cognate-runtime-bun`](https://www.npmjs.com/package/@godspeedai/cognate-runtime-bun) —
|
|
96
|
+
the reference runtime; pass a provider (or its component) to give agents a model.
|
|
97
|
+
- [`@godspeedai/cognate`](https://www.npmjs.com/package/@godspeedai/cognate) — the SDK umbrella.
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@godspeedai/cognate-provider-openai-compatible",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "ModelProvider for OpenAI-compatible chat completion endpoints (llama.cpp, vLLM, Ollama's /v1, hosted APIs) over fetch; no SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
".": "./src/index.ts"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@godspeedai/cognate-capabilities": "^0.1.
|
|
26
|
-
"@godspeedai/cognate-events": "^0.1.
|
|
27
|
-
"@godspeedai/cognate-kernel-api": "^0.1.
|
|
25
|
+
"@godspeedai/cognate-capabilities": "^0.1.2",
|
|
26
|
+
"@godspeedai/cognate-events": "^0.1.2",
|
|
27
|
+
"@godspeedai/cognate-kernel-api": "^0.1.2"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@godspeedai/cognate-provider-local": "workspace:*"
|