-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexplicit_client.py
More file actions
43 lines (33 loc) · 1.15 KB
/
explicit_client.py
File metadata and controls
43 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Explicit provider — use a local OpenAI-compatible server."""
import asyncio
import os
import ai
messages = [ai.user_message("Hello!")]
async def main() -> None:
# Example for local OpenAI-compatible servers like LM Studio.
provider = ai.get_provider(
"openai",
base_url=os.environ.get("LOCAL_OPENAI_BASE_URL", "http://localhost:1234/v1"),
api_key=os.environ.get("LOCAL_OPENAI_API_KEY", "some-key"),
headers={"X-Custom-Header": "example"},
)
model = ai.Model(
os.environ.get("LOCAL_OPENAI_MODEL", "local-model"),
provider=provider,
)
try:
try:
await ai.probe(model)
except ai.ProviderError as exc:
print(f"[SKIP] local OpenAI-compatible server is unavailable: {exc}")
return
async with ai.stream(model, messages) as s:
async for event in s:
if isinstance(event, ai.events.TextDelta):
print(event.chunk, end="", flush=True)
print()
finally:
# Explicit providers need explicit cleanup.
await provider.aclose()
if __name__ == "__main__":
asyncio.run(main())